Skip to content

Latest commit

 

History

History
58 lines (47 loc) · 2.56 KB

README.md

File metadata and controls

58 lines (47 loc) · 2.56 KB

Hand Cricket Simulation

This program tries to create a hand cricket simulation using Python, HTML, Jinja and Flask. I created this program to learn flask.

Learing References

Jinja Notes

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

Important Topics

If Conditions

{% if kenny.sick %}
    Kenny is sick.
{% elif kenny.dead %}
    You killed Kenny!  You bastard!!!
{% else %}
    Kenny looks okay --- so far
{% endif %}

For Loop

<h1>Members</h1>
<ul>
{% for user in users %}
  <li>{{ user.username|e }}</li>
{% endfor %}
</ul>

Flask Notes

To launch the application with debugger, run the following command:

flask run --debug

This launches a very simple builtin local server.

Basic Functions

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.