DEV Community

Cover image for Quick start! getting started with flask, python3.
chrisMurimi
chrisMurimi

Posted on

Quick start! getting started with flask, python3.

Python is a cool and beautiful programing language. It is beginner friendly, easy to learn and its syntax is very clear and concise. Python is yet powerful enough to be used by global tech giants in their products and applications. One area where Python shines is web development. Python offers many frameworks from which to choose from including bottle.py, Flask, Fast API, CherryPy, Pyramid, Django and web2py. This frameworks have been used is some of the most world applications.

What is flask.

Flask is a micro web framework written in Python. It is classified as a microframework because it does not require particular tools or libraries. It has no database abstraction layer, form validation, or any other components where pre-existing third-party libraries provide common functions. However, Flask supports extensions that can add application features as if they were implemented in Flask itself. Extensions exist for object-relational mappers, form validation, upload handling, various open authentication technologies and several common framework related tools.

Setting up Flask.

Python Virtual environment.

A virtual environment is a Python environment such that the Python interpreter, libraries and scripts installed into it are isolated from those installed in other virtual environments, and (by default) any libraries installed in a “system” Python, i.e., one which is installed as part of your operating system.
virtual environment is used to manage Python packages for different projects. Using virtual environment allows you to avoid installing Python packages globally which could break system tools or other projects.

You can install virtual environment using pip. Use this code in windows (I recommend using git bash than the windows CMD):

pip install virtualenv
Enter fullscreen mode Exit fullscreen mode

Create the virtual environment.

virtualenv my_env
Enter fullscreen mode Exit fullscreen mode

my_env is the name of the virtual environment we have created.

Activate the virtual environment.

source my_env\Scripts\activate
Enter fullscreen mode Exit fullscreen mode

Now we have a virtual environment let install flask.

Installing flask.

Flask is easy to set up. Use pip install flask to install both Flask and all of its dependencies including the Jinja2 templating system.

pip install flask
Enter fullscreen mode Exit fullscreen mode

A basic Flask app.

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello_world(self):
    return "Hello, world"
Enter fullscreen mode Exit fullscreen mode

This app doesn’t do much — it just creates a website with a single route that displays “Hello, world” in the browser.

Routes in flask.

Routes in a Flask app can be created by defining a view function and associating a URL with it using the route() decorator. Routes specify how the Flask app handles requests it receives, such as what to display on the webpage at a certain URL.

@app.route("/")
def hello_world(self):
    return "Hello, world"
Enter fullscreen mode Exit fullscreen mode

Flask App Object.

The Python flask module contains all the classes and functions needed for building a Flask app. The Flask class can be imported to create the main application object. It takes the name of the app as an argument.

from flask import Flask

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

Running Flask App.

A Flask app can be run by exporting the FLASK_APP environment variable and running flask run in the terminal.

export FLASK_APP=app.py
flask run 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)