Last active
August 9, 2019 17:38
-
-
Save jviide/2ab2108b041fffc0dcdd1449395e86d0 to your computer and use it in GitHub Desktop.
htm.py ❤️ hyperpython
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from htm import htm | |
from hyperpython import h | |
from inspect import signature, Parameter | |
@htm | |
def html(tag, props, children): | |
if callable(tag): | |
return relaxed_call(tag, children=children, **props) | |
return h(tag, props, children) | |
def relaxed_call(callable_, **kwargs): | |
# Hackety-hack-hack warning! | |
# Call callable_ with the given keyword arguments, but | |
# only those that callable_ expects - ignore others. | |
sig = signature(callable_) | |
parameters = sig.parameters | |
if not any(p.kind == p.VAR_KEYWORD for p in parameters.values()): | |
extra_key = "_" | |
while extra_key in parameters: | |
extra_key += "_" | |
sig = sig.replace(parameters=[*parameters.values(), Parameter(extra_key, Parameter.VAR_KEYWORD)]) | |
kwargs = dict(sig.bind(**kwargs).arguments) | |
kwargs.pop(extra_key, None) | |
return callable_(**kwargs) | |
class HyperpythonComponent: | |
def __hyperpython__(self): | |
return html("""<h2>Hyperpython components!</h2>""") | |
def functional_component(children, header="Functional components!"): | |
return html(""" | |
<h2>{header}</h2> | |
{children} | |
""") | |
print( | |
html(""" | |
<div> | |
<h1>Hello Python</h1> | |
<p>Now you can write HTML in Python!</p> | |
<!-- also with components --> | |
<{HyperpythonComponent} /> | |
<!-- and functional components --> | |
<{functional_component}> | |
<li>foo</li> | |
<li>bar</li> | |
<//> | |
</div> | |
""") | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment