# pyxl **Repository Path**: mirrors_dropbox/pyxl ## Basic Information - **Project Name**: pyxl - **Description**: A Python extension for writing structured and reusable inline HTML. - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-09-24 - **Last Updated**: 2026-02-14 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README Pyxl is an open source package that extends Python to support inline HTML. It converts HTML fragments into valid Python expressions, and is meant as a replacement for traditional python templating systems like [Mako](http://www.makotemplates.org/) or [Cheetah](http://www.cheetahtemplate.org/). It automatically escapes data, enforces correct markup and makes it easier to write reusable and well structured UI code. Pyxl was inspired by the [XHP](https://github.com/facebook/xhp/wiki) project at Facebook. This project only supports Python 2. However, a [Python 3 fork](https://github.com/gvanrossum/pyxl3) is available. ## Motivation At Cove, where Pyxl was developed, we found that using templates was getting in the way of quickly building new features. There were the usual issues of remembering to escape data to prevent XSS holes, avoiding invalid markup and deciphering cryptic stack traces. More importantly, our templates were getting hard to manage and understand which made iterating on our product more work than should be necessary. Existing templating systems do support things like logic and reusable modules - but they are essentially like having a different programming language for writing UI which falls well short of python itself. The primary reason templating systems exist is because creating HTML in languages like python means writing crazy string manipulation code, or losing the niceness of writing actual HTML by doing something like this: ```py import html print ( html.head().appendChild( html.body().appendChild( html.text("Hello World!")))) ``` To get around these limitations, we developed Pyxl which allowed us to treat HTML as a part of the python language itself. So, writing the above example with Pyxl would look like: ```py # coding: pyxl print Hello World! ``` This meant no longer dealing with a separate "templating" language, and a lot more control over how we wrote our front-end code. Also, since Pyxl maps HTML to structured python objects and expressions instead of arbitrary blobs of strings, adding support for things like automatically escaping data was trivial. Switching to Pyxl led to much cleaner and modularized UI code, and allowed us to write new features and pages a lot quicker. ## Installation Clone the repo and run the following commands from the directory you cloned to. ```sh python setup.py build sudo python setup.py install sudo python finish_install.py ``` To confirm that Pyxl was correctly installed, run the following command from the same directory: ```sh python pyxl/examples/hello_world.py ``` You should see the string `Hello World!` printed out. Thats it! You're ready to use Pyxl. ## Running the tests After installing pyxl: ```sh easy_install unittest2 python pyxl_tests.py ``` ## How it works Pyxl converts HTML tags into python objects before the file is run through the interpreter, so the code that actually runs is regular python. For example, the `Hello World` example above is converted into: ```py print x_head().append_children(x_body().append_children("Hello World!")) ``` Pyxl's usefulness comes from being able to write HTML rather than unwieldy object instantiations and function calls. Note that Pyxl automatically adds objects for all HTML tags to Python builtins, so there is no need to import `x_head` or `x_body` in the example above. The conversion to Python is relatively straightforward: Opening tags are converted into object instantiations for the respective tag, nested tags are passed in as arguments to the `append_children` method, and closing tags close the bracket to the `append_children` call. As a result, a big advantage of this is that stack traces on errors map directly to what you've written. To learn more about how Pyxl does this, see the **Implementation Details** section below. ## Documentation All python files with inline HTML must have the following first line: ```py # coding: pyxl ``` With that, you can start using HTML in your python file. ### Inline Python Expressions Anything wrapped with {}'s is evaluated as a python expression. Please note that attribute values must be wrapped inside quotes, regardless of whether it contains a python expression or not. When used in attribute values, the python expression must evaluate to something that can be cast to unicode. When used inside a tag, the expression can evaluate to anything that can be cast to unicode, an HTML tag, or a list containing those two types. This is demonstrated in the example below: ```py image_name = "bolton.png" image = text = "Michael Bolton" block =
{image}{text}
element_list = [image, text] block2 =
{element_list}
``` ### Dynamic Elements Pyxl converts tags into python objects in the background, which inherit from a class called [`x_base`](https://github.com/dropbox/pyxl/blob/master/pyxl/pyxl/base.py). This means that tags have certain methods you can call on them. Here is an example snippet that uses the `append` function to dynamically create an unordered list. ```py items = ['Puppies', 'Dragons'] nav =