Very basic Python functions to generate HTML in a compasable way.
* Usage
Very much inspired from React.
A component is any function that returns the result of a ~create_element~ call. This function that the element name,
Let us first define a Text component. We will create a text element without any attributes:
#+begin_src python :session
from chp import create_element, create_prop, render_element
def Text(t):
return create_element("span", [], t)
t = Text("YourLabs")
render_element(t)
# outputs => "<span >YourLabs</span>"
#+end_src
#+RESULTS:
: <span >YourLabs</span>
Let's now use this component to display text inside of a link:
#+begin_src python :session
def Link(href, children):
href = create_prop("href", href)
return create_element("a", [href], children)
l = Link("yourlabs.org", [Text("YourLabs")])
render_element(l)
# outputs => "<a href="yourlabs.org"><span >YourLabs</span></a>"
#+end_src
#+RESULTS:
: <a href="yourlabs.org"><span >YourLabs</span></a>
Let us now define a Menu component that will create links inside of a nav element based on some input. Let's also rename out ~create_element~ and ~create_prop~ functions to ~ce~ and ~cp~ respectively
#+begin_src python :session
ce = create_element
cp = create_prop
def Menu(links=[]):
c = [] # menu children links array
for l in links:
el = Link(l["href"], [Text(l["text"])])
c.append(el)
return ce("nav", [cp("class", "menu")], c)
links = [
{
"href": "yourlabs.org",
"text": "YourLabs",
},
{
"href": "novamedia.nyc",
"text": "NovaMedia",
},
]
m = Menu(links)
render_element(m)
# outputs => <nav class="menu"><a href="yourlabs.org"><span >YourLabs</span></a><a href="novamedia.nyc"><span >NovaMedia</span></a></nav>
#+end_src
#+RESULTS:
Pretty printed, this final output is:
#+begin_src html
<nav class="menu">
<a href="yourlabs.org">
<span>YourLabs</span>
</a>
<a href="novamedia.nyc">
<span>NovaMedia</span>
</a>
</nav>
#+end_src
Feel free to check out the app.py and components.py files to see how a full page can be built easily using this method. The app.py file writes the html output to another file called output.html.
* Trying it
#+begin_src bash
git clone git@github.com:tbinetruy/CHIP.git
cd CHIP
python app.py # write html to output.html file
firefox output.html
#+end_src
* Examples
To run the example project:
- pip install --user --editable path/to/chp[dev]
- yarn install; yarn start
- chp-django runserver
- py.test path/to/chp
#+begin_src python
import chp
def FormSchema(is_checked):
return chp.Form([
chp.Row([
chp.Input('username'),
chp.CheckboxField(is_checked),
])
])
class PostForm(forms.ModelForm):
def render(self):
is_checked = 'checked' # self.checked
return mark_safe(FormSchema(is_checked).render_element(Form))
#+end_src
~phtml~ becomes a string containing the following html code:
#+begin_src html
<form class="mdc-layout-grid__cell">
<div class="mdc-layout-grid__inner">
<input class="mdc-input__native-control" type="text" id="{{ id }}" value="{{ value }}" name="username"></input>
<div class="mdc-form-field">
<div class="mdc-checkbox">
<input class="mdc-checkbox__native-control" type="checkbox" id="{{ id }}" name="password"></input>
<div class="mdc-checkbox-background"></div>
<label for="{{ id }}">{{ label }}</label>
</div>
</div>
</div>
<div >
{% for error in form.non_field_errors %}
{{ error }}
{% endfor %}
</div>
</form>
#+end_src
* TODOS
- Testing of the pyreact.py file. Some of the high level results were copy pasted into tests.org
Metadata
Release files for chp 0.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| chp-0.0.1.tar.gz | 6.6 kB | Without provenance | Details for chp-0.0.1.tar.gz |
Release files / chp-0.0.1.tar.gz
| Download URL | chp-0.0.1.tar.gz |
|---|---|
| Size | 6.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
451a2136c1dfcf26c724faf7e81bf5e537fb79bc1d89a6725cfb244ca4b88a2f
|
|
BLAKE2b-256 checksum How to use checksums |
21ab1d58cafab2cd019fb95fe79185c9d1d612552f77870aa7aa643d0a48cfcf
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
Python-urllib/3.6
|
Security for chp 0.0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.