15
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.
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.
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
virtualenv my_env
my_env is the name of the virtual environment we have created.
source my_env\Scripts\activate
Now we have a virtual environment let install 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
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello_world(self):
return "Hello, world"
This app doesn’t do much — it just creates a website with a single route that displays “Hello, world” in the browser.
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"
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__)
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
15