Table of Contents

flask Code Samples

Go to the code ⟶

Bare Flask App

This project folder contains the minimal code needed to run a Flask app.

https://www.youtube.com/embed/OKavgGmuDPo

What is Flask?

Flask is a Python framework for making web applications.

What is Flask used for?

Flask is used for making servers and fullstack web applications.

While primarily thought of for making servers, Flask comes with built-in software for making frontends, too.

Flask is a great option for web applications that incorporate machine learning, artificial intelligence, data science, the Internet of Things, and hardware like the Raspberry Pi.

Flask is great for those technologies because the Python ecosystem has lots of packages, documentation, and tutorials.

Does Flask require any special software?

Flask does not require any specific tools or libraries in order to use it.

For instance, it does not have a database abstraction layer. So you can easily connect your Flask application to MongoDB, SQL, and virtually any other storage solution.

Flask also does not come with form validation. You can implement your own form validation solutions or something from a third-party.

You will, however, need Python installed on your computer, a code editor like Visual Studio code, and the Flask package installed in your project.

Other web frameworks, like Django, come with more pre-built requirements and therefore are not as flexible.

How do you run a Flask app?

You can run a Flask app on your computer. You can have one up and running in 2 minutes.

Want to do it now?

The accompanying .py file has code for making a basic Flask server.

Here is how to run the sample code in the accompanying folder:

  • clone or download this folder
  • create a virtual environment
  • install Flask (pip install Flask)
  • run the app.py file (python3 app.py)

Flask server example

Code example with explanations are in the app.py file located in this folder and below:

# import Flask from flask import Flask # Create an instance of a Flask server # Flask has the basic functionality we want for the server # __name__ is a variable representing the module (folder) that you # call Flask inside of app = Flask(__name__) # The following is from: # https://blog.miguelgrinberg.com/post/why-do-we-pass-name-to-the-flask-class # Python sets the __name__ variable to the module name, so the # value of this variable depends on the Python source # file in which you use it. # For example, in a module named test.py located in the # top-level directory of the application, the value of __name__ is # test. # If the test.py module is located inside a Python package called # my_package, then the value of __name__ is my_package.test. # Exceptions regarding the value of __name__: ## 1. Inside __init__.py package constructor module, the value ## of __name__ is the package name, without __init__. ## For example, in my_package/__init__.py, the value of __name__ ## is just my_package. ## 2. In the main module of the application (the file you run the ## Python interpreter on) the value of __name__ has the special ## value of __main__. # Create a route # This route corresponds to the host on which you run the server # You see below the server will run on port 2000, so this route # is http://localhost:2000/ @app.route("/") def home(): return "Hello World" # Create annother route # This route corresponds to the host + "/stocks" # Below you see the server will run on port 2000, so this route is # http://localhost:2000/stocks @app.route("/stocks") def stocks(): return "Hello Stocks" # The next lines of code are what run the server # The if statment means the server only runs from the main (root) # folder in your project because it checks if __name__ == "__main__" # It runs on localhost:2000, so you can open that in your browser if __name__ == "__main__": app.run(host="localhost", port=2000, debug=True, use_reloader=True) # To run the file, open a terminal session in your root folder # Type python3 app.py and hit enter # After running the server: # Open the browser and go to http://localhost:2000/ # The browser should say "Hello World" # Then open the browser and go to http://localhost:2000/stocks # The browser should say "Hello Stocks"
Update README.md