DEV Community

Cover image for Flask Beginnings
Collins
Collins

Posted on

Flask Beginnings

flask-image

Flask is a micro web-framework, micro in the sense that it doesn't come with out-of-the-box functionalities, desired functionalities are rather added by extensions. Flask also does not rely on pre-existing third-party libraries or frameworks, Flask's main dependencies were all authored by the creator of Flask himself.


Why you should learn Flask?

  • It is easier to learn if you're new to web development, but not Python.
  • It has a large and supportive community
  • It is faster to develop once you're grounded in your knowledge of Python. You can use it to easily complete your next side project
  • It is in demand, being one of the popular web frameworks, it is used a lot in production.
  • It can be used to build fast websites that can handle traffic, flask is Pinterest's core technology, with 322 million active users.
  • Finally, the documentation is a treasure, as it is very comprehensive and beginner-friendly.

Requirements for learning flask

  • Knowledge of basic Python concepts (generators, decorators, e.t.c)
  • A computer with python, a web browser, and a text/code editor installed.
  • A brain

Installing Flask

If you're familiar with the Interactive Python console, you know about pip.

PIP (Preferred Installer Program) is the package installer for Python and it can install any package from PyPi - Python Package Index.

pip comes pre-installed with Python since version 2.7+ and 3.4+, so you must have python installed, flask is also a package in PyPI.

To check if you have pip installed open your command-line interface, and run the command:

pip --version
Enter fullscreen mode Exit fullscreen mode

If your output looks like below, it means you have it installed

pip X.Y.Z ...\site-packages\pip (python X.Y)
Enter fullscreen mode Exit fullscreen mode

Else if it looks in anyway like this:

'pip' is not recognized as an internal or external command,
operable program or batch file.
Enter fullscreen mode Exit fullscreen mode

Then you should download and install pip before continuing.

Once that is settled, flask can now be installed using pip, enter into the command-line:

pip install flask
Enter fullscreen mode Exit fullscreen mode

First Flask Web Application.

Once you're done with the flask installation, you're ready to write your first flask web application.

code-image

from flask import Flask

app = Flask(__name__)

@app.route("/")
def index():
    return "<h1> Hello world </h1>"
Enter fullscreen mode Exit fullscreen mode

That is all the code you need to spin your first flask web app, don't worry if you don't understand anything yet, I'll explain the different components as you read on.

The first line:

from flask import Flask
Enter fullscreen mode Exit fullscreen mode

If you've used Python, you're familiar with import statements, here you're importing the Flask class from the flask module.

The next line we've got:

app = Flask(__name__)
Enter fullscreen mode Exit fullscreen mode

The app object is an instance of the Flask class, it is created by passing the __name__ argument to the Flask constructor, the __name__ argument contains information about the python script you're currently writing your web application in.

Flask uses the __name__ argument to determine the file to run.

The app object above handles all requests made from the client to the server.

The next block:

@app.route("/")
def index():
    return "<h1> Hello world </h1>"
Enter fullscreen mode Exit fullscreen mode

When the webserver receives requests, it passes them to the Flask application instance, but the flask application instance does not know what to do for each URL request, that's where the decorator and function above comes in.

The decorator @app.route("/") contains a route method that takes a string (actually a URL) as an argument, and the function defined below it is a view function, whatever is returned by the function is the response.

When a request comes in, the route method needs to know what code to run for each request, therefore it maps the URL to the function defined below the decorator - view function, where all the processing occurs before a response is rendered.


Flask Development Web Server

Now to view your response in the web browser, you'll have to start the Flask server, flask comes with a development web server not suitable for production (for production you'd have to put it behind a WSGI (Web Server Gateway Interface) server like Gunicorn).

To start the flask server, you'd first have to make the webserver know what file to run, to do that you'll set the FLASK_APP environment variable to our app.py web app file.

To do that run the command:

set FLASK_APP=app.py
Enter fullscreen mode Exit fullscreen mode

For Unix users replace set with export

Now you can run your app on the server:

flask run

 * Serving Flask app "app.py"
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Enter fullscreen mode Exit fullscreen mode

The server is started, now copy the address http://127.0.0.1:5000/ to your browser.

flask-run

The response returned by the view function can get more complex and the need to return a response based on the context of the request would rather make you return Templates rather than just a string containing HTML elements (a bunch of these inside the python code would make the code unmaintainable).


Flask resources:

Top comments (11)

Collapse
 
lightsource profile image
Israel

Good one there! You made it seem like a good frame work to start with. Kudos ๐Ÿ‘๐Ÿฝ๐Ÿ‘๐Ÿฝ

Collapse
 
0th profile image
Collins

Thank you.๐Ÿคฒ
It really is a good framework to start with.

Collapse
 
iceorfiresite profile image
Ice or Fire

I'm learning flask now after having previously used django. Both are good and have their place depending on what your needs are.

Collapse
 
ironcladdev profile image
Conner Ow

I use flask a lot. I LOVE IT!

Collapse
 
0th profile image
Collins

Yh... A good tool is always usedโ˜บ๏ธ.

Collapse
 
neheystev profile image
Nehemiah Stephen

This is detailed enough for a beginner just like I am. Thank You for the motivation to learn...

Collapse
 
0th profile image
Collins

I'm glad my article could arouse your interest in flask, you should learn and give it a shot sometime.๐Ÿ˜

Collapse
 
joelbuenrostro profile image
Joel Buenrostro

Well explained

Collapse
 
0th profile image
Collins

Thanks, bro!

Collapse
 
dark_phoenix profile image
Phoenix

Straight to the point. Nice job ๐Ÿ‘Œ
Congrats on your first post.

Collapse
 
0th profile image
Collins

Thanks a lot๐Ÿฅฐ.