Package: ryzom
Ryzom: Replace HTML Templates with Python ComponentsWhy?Because while frameworks like Django claim that "templates include a restricted
language to avoid for the HTML coder to shoot themself in the foot", the GoF on
the other hand states that Decorator is the pattern that is most efficient for
designing GUIs, which is actually a big part of the success encountered by
frameworks such as React.What?Ryzom basically offers Python Components, with extra sauce of bleeding edge
features such as "compiling Python code to JS", and "data binding" (DOM
refreshes itself when data changes in the DB) if you enable websockets.StateCurrently in Beta stage, we are brushing up for a production release in an Open
Source project for an NGO defending democracy, with an online voting platform
secured with homomorphic encryption, basically a Django project built on top of
microsoft/electionguard-python.DemoWhile Django is not a requirement for Ryzom, we currently only have a demo app
in Django:git clone https://yourlabs.io/oss/ryzom.git
sudo -u postgres createdb -O $UTF -E UTF8 ryzom_django_example
cd ryzom
pip install -e .[project]
./manage.py migrate
./manage.py runserver
# open localhost:8000 for a basic form
# open localhost:8000/reactive for databinding with django channels

# to run tests:
py.testUsageHTMLContentComponents are Python classes in charge of rendering an HTML tag. As such, they
may have content (children):fromryzom.htmlimport*yourdiv=Div('some',P('content'))yourdiv.render()=='<div>some <p>content</p></div>'Most components should instanciate with*contentas first argument, and you
can pass as many children as needed there. These goes intoself.contentwhich
you can also change after instanciation.You may also pass component as keyword arguments, in which case they will be
have a "slot" attribute and be assigned to self:yourdiv=Div(main=P('content'))yourdiv.main==P('content',slot='main')Special contentAny content that does not define ato_htmlmethod will be casted to
str and wrapped inside aText()component.Any content that isNonewill beremoved fromself.content.AttributesHTML tags also have attributes which we have a Pythonic API for:Div('hi',cls='x',data_y='z').render()=='<div class="x" data-y="z">hi</div>'If you don't like to have the attrs after the content of the element, then keep
in mind you can also pass content components as keyword arguments.Declarative and inheritance are supported too:classSomething(Div):attrs=dict(cls='something',data_something='foo')classSomethingNew(Something):attrs=dict(addcls='new')# how to add a class without re-definingyourdiv=SomethingNew('hi')yourdiv.render()=='<div class="something new" data-something="foo">hi</div>'StylesStyles may be declared within attrs or on their own too.classFoo(Div):style=dict(margin_top='1px')# is the same as:classFoo(Div):style='margin-top: 1px'# is the same as:classFoo(Div):attrs=dict(style='margin-top: 1px')Class style attributes will be extracted into a CSS bundle.Instance style attributes will be rendered inline.Every component that has a style will also render a class attribute.SASS also works, but won't be interpreted by Ryzom and just be rendered by
libsass as-is:classFormContainer(Container):sass='''.FormContainermax-width: 580px.mdc-text-field, .mdc-form-field, .mdc-select, formwidth: 100%'''JavaScriptThis repository provides a py2js fork that you may use to write JavaScript in
Python. There are three ways you can write js in Python: the "HTML way",
"jQuery way" and the "WebComponent" way.You must however understand that our purpose is to write JS in Python, rather
than supporting Python in JS like the Transcrypt project. In our case, we will
restrict ourselves to a subset of both the JS and Python language, so things
like Python__mro__or even multiple inheritance won't be supported at all.However, you can still write JS in Python and generate a JS bundle.HTML Wayonclick,onsubmit,onchangeand so on may be defined in Python. They will
receive the target element as first argument:classYourComponent(A):defonclick(element):alert(self.injected_dependency(element))definjected_dependency(element):returnelement.attributes.href.valueThe above will bundle aYourComponent_onclickfunction, theYourComponent_dependencyfunction, and recursively.AndYourComponentwill render withonclick="YourComponent_onclick(this)".WebComponent: HTMLElementThe following defines a custom HTMLElement with a JS HTMLElement class, it will
generate a basic web component.classDeleteButton(Component):classHTMLElement:defconnectedCallback(self):this.addEventListener('click',this.delete.bind(this))asyncdefdelete(self,event):csrf=document.querySelector('[name="csrfmiddlewaretoken"]')awaitfetch(this.attributes['delete-url'].value,{method:'delete',headers:{'X-CSRFTOKEN':csrf.value},redirect:'manual',}).then(lambdaresponse:print(response))This will generate the following JS, which will let the browser responsible for
the components lifecycle, check window.customElement.define documentation for
details.classDeleteButtonextendsHTMLElement{connectedCallback(){this.addEventListener('click',this.delete.bind(this));}asyncdelete(){varcsrf=document.querySelector('[name="csrfmiddlewaretoken"]');awaitfetch(this.attributes['delete-url'].value,{method:'delete',headers:{'X-CSRFTOKEN':csrf.value},redirect:'manual'}).then((response)=>{returnconsole.log(response)});}}window.customElements.define("delete-button",DeleteButton);And that's pretty rock'n'roll if you ask me.BUTthere is a catch: currently, youmustset the first argument toselflike in Python, so that the transpiler knows that this function is a
class method and that it shouldn't render with thefunctionprefix that
doesn't work in ES6 classes.The jQuery wayYou can do it "the jQuery way" by defining a py2js method in your
component:classYourComponent(Div):defnested_injection():alert('submit!')defon_form_submit():self.nested_injection()defpy2js(self):getElementByUuid(self.id).addEventListener('submit',self.on_form_submit)This will make your component also render the addEventListener statement in a
script tag, and the bundle will package the on_form_submit function.BundlesThe component will depend on their CSS and JS bundles. Without Django, you can
do it manually as such:fromryzomimportbundleyour_components_modules=['ryzom_mdc.html','your.html',]css_bundle=bundle.css(*your_components_modules)js_bundle=bundle.js(*your_components_modules)DjangoINSTALLED_APPSAdd tosettings.INSTALLED_APPS:'ryzom',# add py-builtins.js static file'ryzom_django',# enable autodiscover of app_name.html'ryzom_django_mdc',# enable MDC form renderingTEMPLATESWhile ryzom offers to register components to template names,ryzom_djangooffers the template backend to make any use of that with Django, add the
template backend as such insettings.py:TEMPLATES=[{'BACKEND':'ryzom_django.template_backend.Ryzom',},{'BACKEND':'django.template.backends.django.DjangoTemplates','DIRS':[],'APP_DIRS':True,'OPTIONS':{'context_processors':['django.template.context_processors.debug','django.template.context_processors.request','django.contrib.auth.context_processors.auth','django.contrib.messages.context_processors.messages',],},},]This template backend will allow two usages:overriding html template names with components,using components import path in dotted-style fortemplate_name,
ie.template_name = 'yourapp.html.SomeThing'Register templates for viewsCurrently,ryzom_djangowill auto-discover (import) any app'shtml.pyfile. As such, this is where you can define all your view templates
replacements withryzom.html.template. For example, to set the default
template for adjango.views.generic.ListViewwith modelYourModel:fromryzom_mdcimport*classBaseTemplate(Html):title='Your site title'@template('yourapp/yourmodel_list.html',BaseTemplate)classYourModelList(Ul)def__init__(self,**context):super().__init(*[Li(f'{obj}')forobjincontext['object_list'])])Import thehtmlmodule from ryzom_mdc or from ryzom, depending on the flavor
you want. You can nest components on the fly as you register a template, which
replaces{% extends %}.You may chain as many parents as you would like, for example you could have a
"card" layout that sets a small content width:classCardLayout(Div):style='max-width: 20em; margin: auto'@html.template('yourapp/yourmodel_form.html',BaseTemplate,CardLayout)classYourModelForm(Form):def__init__(self,**context):super().__init__(CSRFInput(context['view'].request),context['form'],method="post",)Bundlesryzom_djangoapp provides 3 commands:ryzom_css: output the CSS bundleryzom_js: output the JS bundleryzom_bundle: writebundle.jsandbundle.cssinryzom_bundle/staticAs well as 2 views,JSBundleViewandCSSBundleViewthat you can use in
development, include them in yoururls.pyas such:fromdjango.confimportsettingsifsettings.DEBUG:urlpatterns.append(path('bundles/',include('ryzom_django.bundle')),)For production, you may write the bundles before running collectstatic as such:./manage.pyryzom_bundle
./manage.pycollectstaticThen, make sure you use theHtmlcomponent fromryzom_djangoor anyryzom_django_*app, which will include them automatically.FormsAPIryzom_django.forms patches django.forms.BaseForm with 2 new methods:BaseForm.to_html(): render the HTML, makes the BaseForm objects "quack"
like a component, also useable in non-ryzom templates to get the rendering
with{{ form.to_html }}BaseForm.to_component(): called by to_html(), this is where the default
layout is generated, which you can override to customize the form object
rendering. It will return a CList (tagless component list) of the
to_component() result of every boundfield.ryzom_django.forms patches django.forms.BoundField with 2 new methods:BoundField.to_component(): this will return the Component template
registered for the field widget template name if any, in which case it will
use thefrom_boundfield(boundfield)of that template.BoundField.to_html(): render the HTML, makes the BoundField objects "quack"
like components.As such, you can configure how a form object renders by overriding theto_component()method, and use BoundField objects like components too:defto_component(self):returnDiv(H3('Example form!'),self['some_field'],# BoundField quacks like a Component!Div(Input(type='submit'),))DemoAn example Django project is available insrc/ryzom_django_example/, example
code is in theurls.pyfile.Supported APILow-levels documented in this section are subject to unfriendly change prior to
v1 as we are still researching use cases, please use responsibly.We are trying to secure the following Component methods that you will like to
override when refactoring code:Component.context(*content, **context): alter the context before rendering to bubble
up new context data from inner components to parent components, aiming to
solve the same problem we have blocks and extends in jinja templates.Component.content_html(*content, **context): render inner HTMLComponent.to_html(*content, **context): renders the outer and inner HTMLNot thread safeCurrently, components are not thread safe because much of its rendering code
alters self in a way that will change how it would render again. Some core
component code altersself.contentduring rendering, example in "Special
content": "None" case.Thread safety is an active discussion topic whenever some thread-unsafe code is
proposed for merge, but we are not yet certain that this is an issue because of
all the better ways Python offers to organize code. For example:Wrap declarations in lambdas:classYourView:to_button=lambda:YourButton()Instead of:classYourView:to_button=YourButton()We are careful with thread safety in new developments, but it seems must
convenient to just being able to alterselfon the way.UNIX was not designed to stop its users from doing stupid things, as that would also stop them from doing clever things.
— Doug Gwyn