Flask Beginnings

 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.
  • 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

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

pip X.Y.Z ...\site-packages\pip (python X.Y)

Else if it looks in anyway like this:

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

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

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>"

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

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__)

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>"

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

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)

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).


lask resources:

 

Comments

Popular posts from this blog

Easy Text-to-Speech with Python

Flutter for Single-Page Scrollable Websites with Navigator 2.0

Better File Storage in Oracle Cloud