Sitemap
Patrick Fuller

Patrick Fuller

A Simple Flask App

Fun With Flask, Brython, and Jinja

4 min readOct 11, 2019

--

Context

This blog follows code used to make a simple app using python, flask and html. The code can be found here, and my current versions are the following

flask --versionPython 3.6.9Flask 1.1.1Werkzeug 0.15.5

*This project was heavily inspired by Miles Erickson’s work here.

Static/, Templates/ And App.py

The structure of the this app has three components. The Static/ directory contains the Brython javascript files, brython.js and brython_stdlib.js. This implementation could run without the stdlib but if you wanted to do fancier stuff you’ll want it.

The Templates/ directory houses the HTML, simply named index.html. This file directly contains the brython code. This is where the fun part comes in. You can write python code to tell the browser what to do. Below is a rough outline of how it is included into HTML structure.

<body onload="brython(1)">
<script type="text/python3">
*write python here*
</script>
...(divs and such)
</body>

Alternatively the python script can be stored in a separate file and read in as such, <script type=”text/python” src=”test.py”></script>.

The Brython being onloaded also allows the Jinja to be interpereted in the body outside of the <script></script> section. Jinja allows python like syntax directly in the HTML like the following:

{% for element in var_from_server %}
{% if element > 2 %}
<div>{{element}}</div>
{% endif %}
{% endfor %}

In this case var_from_server is a test_dummy list of integers that gets sent to the client browser from the server in the app.py file.

Get Patrick Fuller’s stories in your inbox

Join Medium for free to get updates from this writer.

The app.py file contains all python. It imports the Flask module and uses @ decorators to tell flask how to find its functionality.

The the app.py code is run on the server while Brython code runs client-side in the browser.

The app.py (on the server side) does a few things:

  1. Tells flask where to find the brython files and any other code that would be kept in the static directory.
  2. Tells flask where to find the html file and maybe passes other data to the browser.
  3. Receives (‘GET’) data from client browser.
  4. Defines what the server does with the data.
  5. Sends (‘POST’) data to the client browser.

The brython in the HTML (on the client side):

  1. What action triggers the data getting sent to the server.
  2. Where on the server to send the data.
  3. What to do with the new data that gets send back from the server.

To get it fired it up let flask know where to find the app.py file. In this case, export FLASK_APP=app.py. Then the command flask run will launch the app. It will tell you where to find your app (circled below) and if you follow it in your browser you find it. If you open the Javascript console monitor you will be able to see where the Brython gets run. It may come in handy for debugging and can be toggled with Command + Option + J (Mac) or Control + Shift + J (Windows, Linux, Chrome OS).

Below is a side by side visualization of these two Brython and app.py components. The print statements are included to demonstrate what runs where and how one might debug.

Note the print statements to see which code is runs where.

To be clear adding 1 to a number is something that could be done just in browser. This is a simple example to illustrate what runs on the server and what runs in the browser, and hopefully inspire you to clone the repo and try it out and build your own creations!

To conclude, I do not claim to be an authority on these matters but to give some context here are a few explanations.

Flask

As per https://pymbook.readthedocs.io/en/latest/flask.html:

“Flask is a web framework. This means flask provides you with tools, libraries and technologies that allow you to build a web application.”

Official documentation here: http://flask.palletsprojects.com/en/1.1.x/

Werkzeug

From the documentation,

“Werkzeug is a comprehensive WSGI web application library. It began as a simple collection of various utilities for WSGI applications and has become one of the most advanced WSGI utility libraries.

Flask wraps Werkzeug, using it to handle the details of WSGI while providing more structure and patterns for defining powerful applications.”

Brython

In Brython — Python In The Browser, Alex Armstrong writes

“Brython is a Python to JavaScript compiler, but it does the whole job in the browser and so makes Python appear to be a client scripting language.”

Documentation here: http://brython.info/

Jinja

From the documentation,

“Jinja is a modern and designer-friendly templating language for Python, modelled after Django’s templates.”

I certainly find it friendly!

--

--