This program tries to create a hand cricket simulation using Python, HTML, Jinja and Flask. I created this program to learn flask.
Jinja is a template engine. A Jinja template is simply a text file. Jinja can generate any text-based format (HTML, XML, CSV, LaTeX, etc). A Jinja template doesn’t need to have a specific extension. A template contains variables and/or expressions, which get replaced with values when a template is rendered; and tags, which control the logic of the template. The template syntax is heavily inspired by Django and Python.
There are a few kinds of delimiters. The default Jinja delimiters are configured as follows:
- {% ... %} for Statements
- {{ ... }} for Expressions to print to the template output
- {# ... #} for Comments not included in the template output
{% if kenny.sick %}
Kenny is sick.
{% elif kenny.dead %}
You killed Kenny! You bastard!!!
{% else %}
Kenny looks okay --- so far
{% endif %}
<h1>Members</h1>
<ul>
{% for user in users %}
<li>{{ user.username|e }}</li>
{% endfor %}
</ul>
To launch the application with debugger, run the following command:
flask run --debug
This launches a very simple builtin local server.
We use the route()
decorator to tell Flask what URL should trigger our function.
Web applications use different HTTP request methods when accessing URLs. By default, a route only answers to GET
requests. You can use the methods
argument of the route()
decorator to handle different HTTP methods like POST
.
To render a template you can use the render_template()
method. All you have to do is provide the name of the template and the variables you want to pass to the template engine as keyword arguments.
To redirect a user to another endpoint, use the redirect()
function; to abort a request early with an error code, use the abort()
function.