Skip to main content

Small intro in flask

· 4 min read
Strider

Hi, after some time I decided to share my experience with Flask. Flask is a Python framework for web development.

You can find the framework on the page https://palletsprojects.com/p/flask/.

dia.png

It is quite simple, and mostly based on Web Server Gateway Interface (WSGI). The whole thing is OpenSource, and can also be found on GibHub.

The whole project is divided into 6 libraries. I have simply pasted this in.

  • Click — Beautiful, composable command line interface creation kit.
  • Flask — a flexible and popular web development framework
  • ItsDangerous — Safely pass trusted data to untrusted environments and back.
  • Jinja — a full featured template engine for Python
  • MarkupSafe — Safely add untrusted strings to HTML/XML markup.
  • Werkzeug — The comprehensive WSGI web application library.
$ #To install Flask in Python:
$ pip install -U Flask

After the installation, you can start right away and build a small web application. On the question of where and how to host the application later, here you can be reassured, because Flask brings its own web server.

A basic structure of a Flask application looks something like this:

from flask import Flask

app = Flask(__name__)


@app.route('/')
def startpage():
return "Hello World"

if __name__ == '__main__':
app.run(threaded=True, host='0.0.0.0', port=8080)

We import Flask into our project and specify where Flask should mount. The constructor, Flask(name) , specifies that Flask should hook into this script. This means that if you import this script, Flask will run.

To tell Flask which address and port to run on, we specify the necessary parameters in the run call. Here in the case on the default address and the port 8080. In addition, we specify that the server should run with threads to handle later multiple clients.

To set the routes / URI paths, you create a method e.g. startpage and mark that, with @app.route('route') route in the brackets is our route. This way Flask knows which routes exist.

For testing we just return a string. Let's run this for a while.

dia2.png

dia3.png

Ok can you also include HTML and CSS there? Yes that is possible 😄

dia4.png

Is a little blunt purely hammered but it works

dia5.png

You can also create templates, that is, outsource the HTML and CSS code and render it later on the calls.

To do this, we create a new structure in the project:

/
├── App.py
└── templates/
└── start/
└── page.html

Our code now needs to be customized a bit. First we remove the HTML and CSS code from the program and store it in the file page.html. Then we import render_template from Flask and can use it later to render the templates. We also give the Flask constructor the path where the templates are contained.

The code looks like this:

dia6.png

The page.html looks like this:

dia7.png

Why did I take the texts out of the template? Quite simply we see in the curly brackets our placeholders, which are replaced when called, by real data. So we can also create dynamic content 😄

With the line render_template, we give to the template still further parameters with. These are called the same as our placeholders. There we can render our data into the template.

You can also include there For-Loops, If-Else ... in the templates to get more overview and dynamics.

dia8.png

The list is passed under the name data, to the render_template method. As a result we see our list.

dia9.png

Flask can do much more, but I'll show that in the next post.

I hope I could give a little insight 😄