= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Ben Shaw:
An Introduction to Helio
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
@ Kiwi PyCon 2013 - Saturday, 07 Sep 2013 - Track 1
http://nz.pycon.org/
**Audience level**
Intermediate
**Description**
At Yellow, we love building web-applications that leverage the best parts of client and server side languages, so we created the open-source Helio. Its modular design lets us compose Python, Javascript, HTML and CSS in harmony, allowing each to perform in the area it excels. This talk is about Helio's creation, and how it integrates with your favourite Python web framework(s) in your next project.
**Abstract**
Background
When building a dynamic “AJAX” website with Python, there are many Javascript based MVC libraries to provide assistance, however, they move much of the templating and logic into the HTML and Javascript, leaving the Python backend as little more than a JSON or XML generator. Helio was created to move the web content generation tasks back to the Python web framework, while still allowing dynamic pages that are interactive and can update portions of themselves without a complete refresh. Helio lets the Python web framework assume its traditional role of data retrieval and template rendering, while Javascript is left to marshall browser events. This talk details the features of Helio, gives an overview into how it works, and outlines how it can be integrated with a Python web framework.
Components
Helio is built on the concept of self-contained, reusable and extendable Components, that encapsulate a Python view controller, and (optionally) Javascript, CSS and HTML files.
View Controller
A view controller class, written in Python, responds to events received from the browser, fetches data, and renders HTML which the browser retrieves. It is also responsible for defining what Javascript and CSS should apply to the component. View controllers can inherit from other controllers, and can overwrite as much or as little of its parents functionality as necessary. For example, a child view controller could choose to just implement its own CSS, while retaining the actions provided by the parent’s Javascript. The view controller can persist its data via the web framework’s session storage.
Javascript
A component’s Javascript file is where Javascript events (e.g. onclick) for the component are attached in the browser. The Javascript file is dynamically retrieved and its events attached when the first instance of the component is loaded by the browser. Any Javascript library, like jQuery, can be integrated.
CSS
The component’s CSS file is dynamically retrieved when the first instance of the component is loaded by the browser, and then unloaded when no instances remain. Any CSS framework or library (such as Foundation, Twitter Bootstrap or various grid libraries) can be integrated.
Included Components
Helio includes a number of components that can be used as the base for custom components and extended as necessary. Some example are sortable tables with pagination and master detail views.
View Controller Tree
An instantiated component is stored server side in a view controller tree. Each component instance has a unique path identifying its location in the tree, based on its parent’s location and an identifier assigned by the parent. For example, the component at path page.document.0 is child 0 of page.document, which in turn is child document of page (the root component). The path allows any component in the tree to be addressed, both server side and in the browser’s DOM.
Rendered View Tree
The markup for the view controller tree that is displayed by the browser can be generated and requested in full or in part, depending on how much of the page the browser needs to refresh. For example, on the initial load, the browser will request the HTML for page, which would render the tree from page (the root component) down; this includes page.document and page.sidebar. If the browser only needs to refresh the sidebar, it can request just page.sidebar, which will render the view controller tree from page.sidebar down.
The path for each component is encapsulated in its HTML as its id attribute.
Server Side Events
Events can be sent between components on the server side via the NotificationCentre; events can either be sent directly to a component via its address, or components can choose to subscribe to events of a certain type (an implementation of the observer pattern).
Javascript Events
A view controller can generate an event, in Python, that is returned to the browser for Javascript to interpret; likewise the browser can generate an event in Javascript for the python view controller to interpret. An example of this might be a controller that supports pagination. The browser will generate and send a change-page event to the controller at tree-address page.results-view (including a payload indicating what page is to be selected). The controller will change the pagination, generate new HTML, then return a load event to the browser, letting the browser know to update that component with the new HTML.
Development and use at Yellow
Helio was conceived as the base of Axle, Yellow’s new order, asset and content management system. Axle is built upon Django, which allows the use of Django’s URL routing, templating, middleware and other popular niceties, supplemented by the dynamic interaction that Helio provides. The nature of Helio components meant each Axle developer could work on their own small part of the system without worrying that they would affect someone else’s development, and yet know that their component would work correctly when integrated with the rest. Component inheritance in Helio also meant that as base components were enhanced, the components inheriting from them were also improved, for example, after adding table sorting to the base table view, all table components were then sortable. The development of Helio was not without challenges, particularly in tracking down errors in Javascript events and classes as they are dynamically loaded. Extensive use of automated unit and regression testing has been used to make sure that Helio performs as designed.
Web Framework Integration
Helio is customisable and supports multiple Python web frameworks. With simple configuration changes it can be integrated with Django, Flask and Pyramid, and will probably work with any web framework provided that framework can render templates, load static files and store session data.
Summary
Helio allows the Python framework to perform the data manipulation and template rendering that it is designed for, and Javascript is only used to send events to update/retrieve data, not for templating or data manipulation. However, if a developer chooses, they can add extra Javascript libraries that they are familiar with if they want more templating or data manipulation in the browser.
**YouTube**