Deploying a Simple Python Script With Flask
Introduction
In this article, we will cover how to locally deploy a Python application using Flask, including:
- the importance of deployment
- how to install Flask using pip
What Is Deployment?
After we have designed, built, and thoroughly tested our application, it is time to package it and prepare it for production. This process is called deployment. This allows our application to successfully run in environments other than our development and test environments.
There are several methods for deploying applications, but in this article, we will explore deploying a simple web application to a local server using the Python web framework Flask.
Flask is a lightweight framework for developing web applications. It provides an efficient and easy way to deploy web-based projects. Flask offers a built-in development server that we can use to test and deploy web application code locally (which we will cover in this article). It also includes useful features, such as templating, that allow us to render HTML templates and provide the functional back-end code for template components.
Installing Flask
To use the Flask library for deployment, we must first install it in our environment. The following steps show how to install Flask on Windows or macOS.
To install Flask, we will use pip, the Python package manager that allows us to install and manage Python packages that are not part of the Python standard library. If you have Python installed on your computer, you most likely have pip automatically installed. However, if you need to install it, follow the steps in pip’s documentation.
Open a command-line terminal in your environment where Python 3 is already installed and set up. First, let’s make sure we are running the latest upgraded version of pip by executing the following command:
pip install --upgrade pip
If a newer version of pip is available, it will be downloaded and installed as shown in the following screenshot:
Now that we have the latest version of pip installed, let’s install Flask. Run the following command:
pip install flask
Flask will be downloaded and installed as shown in the following screenshot:
Understanding the Code
Take a look at the following calculator application code. We must first import the Flask library into our application in order to use it later on.
from flask import Flaskapp = Flask(__name__)@app.route('/')def welcome():return "Welcome to the Codecademy Calculator!"
Next, we create a Flask instance and assign it to a variable named app. We can create several routes using the @app.route() decorator.
These routes correspond to the URL web requests made to access different pages of the web application. The function that follows the decorator executes upon the web request. The main URL of our application is denoted by /. If we want to add additional URL routes for /division or /multiplication, we can define routes in our Flask application for each.
# allows user to input two numbersnum1 = int(input('Enter your first number: '))num2 = int(input('Enter your second number: '))@app.route('/division')def division():return "Now dividing " + str(num1) + " and " + str(num2) + "! The result is: {result}".format(result=str(num1/num2))@app.route('/multiplication')def multiplication():return "Now multiplying " + str(num1) + " and " + str(num2) + "! The result is: {result}".format(result=str(num1*num2))
Let’s review the preceding code:
num1andnum2allow users to input two numbers to multiply or divide.@app.route('/division')specifies the web request with/division.division()is a function that executes in the/divisionroute. This function divides the valuesnum1andnum2.@app.route('/multiplication')specifies the web request with/multiplication.multiplication()is a function that executes in the/multiplicationroute. This function multiplies the valuesnum1andnum2.
Finally, we need to add the last line to our program:
app.run()
Calling app.run() will start the application server, allowing the application to load in a web browser.
Running the Code
Now, it’s time to run our program and see Flask running. You can either copy and paste the example code provided earlier into a Python file or download the code. Name your Python file app.py.
Once you have the code ready on your computer, run it using flask --app app run. You should see the following:
Enter your first number:
After you enter two numbers, you should see something like this:
* Serving Flask app 'app' (lazy loading)
* 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)
Visit http://127.0.0.1:5000/ in your browser, and the following page should appear:

To go to the /division or /multiplication route, you can add division or multiplication after the final /. If you want to see the division() function executed, go to http://127.0.0.1:5000/division, and you should see something like the following:
Conclusion
Deploying allows us to quickly package our application for easy configuration and installation in environments other than where we developed it. This was a brief introduction to deployment with Flask. If you want to learn more, you can dive deeper into creating web applications in our Learn Flask course.
Happy coding!
'The Codecademy Team, composed of experienced educators and tech experts, is dedicated to making tech skills accessible to all. We empower learners worldwide with expert-reviewed content that develops and enhances the technical skills needed to advance and succeed in their careers.'
Meet the full teamRelated articles
- Article
FastAPI vs Flask: Key Differences, Performance, and Use Cases
Learn the features, advantages, and disadvantages of FastAPI and Flask along with their differences and use cases. - Article
Deploying a Flask App
Learn how to deploy your own Flask application with Heroku. - Article
What is Python?
What is Python, and what can it do?
Learn more on Codecademy
- Learn how to create fully-featured, interactive web applications with Flask, the Python framework.
- With Certificate
- Intermediate.12 hours
- Build your first Flask app, a web framework that allows you to build fully-featured web applications using Python.
- Intermediate.2 hours
- Learn how to code in Python, design and access databases, create interactive web applications, and share your apps with the world.
- Includes 8 Courses
- With Certificate
- Intermediate.29 hours