Easily Build Your First Web App With Python’s Flask
Installation to execution in 10 minutes
Moving from simple command-line scripts to web app development is a big leap in our Python journey. There are a variety of framework options with; however, even the concept can be overwhelming if you’ve never worked with a web framework.
If you’ve found this article then you’re probably researching different Python frameworks and trying to decide which to invest in. Instead of suffering from analysis paralysis, take a leap of faith and jump into Flask with this guide.
The first step is the hardest to take, but the easiest to make .We’ll introduce Flask, explain why to start here, and build our first app…all in 10 minutes.
What is Flask? Why Should I Try It?
Flask is a Python micro web framework. What does that mean? Flask is intended for web applications and is “micro” because it omits native tools and libraries for a “install what you need” approach. This philosophy is captured in its clever slogan — web development, one drop at a time.
As a result, Flask is extremely lightweight. While finding the appropriate libraries can take some time later on, the small base footprint of Flask makes it perfect for building our first web app.
There are a plethora of articles debating is Flask right for project X? We’re not worried about that here, we just want to get our feet wet and there is no framework faster than Flask for this purpose.
Installing Flask
We’re going to install Flask into a Python virtual environment. It won’t hurt anything if you aren’t familiar with virtual environments, but follow along for now and read A Simple Introduction to Python Virtual Environments afterwards.
First, we’ll create a virtual environment called my_app and activate the environment.>>> python3 -m venv my_app
>>> cd my_app
>>> source bin/activate
At this point, you should see (my_app) at the beginning of your command prompt. This informs us that our virtual environment is active. Next, it’s time to install Flask.pip3 install flask
The Flask installation is quick; there’s very little to download. Next, open your favorite editor — we recommend Visual Studio Code — and create a file named app.py. In this file, we’ll do four things: (1) import Flask, (2) create a Flask object, (3) define a route, and (4) define the web response.from flask import Flaskapp = Flask(__name__)@app.route("/")
def home():
return "Welcome to my app!"
After saving, we’ll define the entry point for our virtual environment’s flask installation. Open up your terminal and enter the following command.export FLASK_APP=app.py
Now, all we have to do is start up the app using the command flask run. You should see something like this.* 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)
Open your web browser and navigate to the above URL or localhost:5000and see the results. That’s all there is to it!
A Brief Explanation
We sped through the process of build our first web app, so let’s circle back and fill in the gaps.
When we create our Flask object, we pass __name__ as the single argument. This sets the current file as the app’s anchor, letting Flask know how to import and build when necessary.
To create a route, we use our Flask object with the method .route() as a decorator. The single forward slash is the actual path we’re defining for the URL. If we had designate the route as ("/flask"), then we would need to enter localhost:5000/flask to access the subsequent function.
Speaking of, our function home() will be executed when the route is accessed. The function can be more robust, but the important takeaway here is that we return a value which is then printed to the screen.
Conclusion
Our app doesn’t really do anything right now, but we installed Flask, created a route, and bound it to a function. Believe it or not, this is enough to get you dangerous.
At this point, think of a simple app that you want to build in Flask, or decide Flask isn’t for you and clean up your virtual environment. Regardless, being introduced to installing a framework, defining routes, and testing in the browser sets you up for accelerated success… all in 10 minutes.
Please share your experiences, questions, and feedback below. Follow Code 85 for more plain language programming guides. Thanks for reading!