Codecademy Team

Deploying a Simple Python Script With Flask

Learn how to deploy a program in Python using 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 up and ready it for a production state. This process is called deployment. This allows out application to successfully execute in an environment other than our development and test environments.

There are several methods for deploying applications, but in this article, we will dive into 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 like 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 will show how to install Flask on a Windows or macOS environment.

To install Flask, we will use pip, the package manager for Python that allows us to install and manage Python packages that are not a 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, following the steps in pip’s documentation.

Open a command line terminal in your environment where Python3 is already installed and set up. First, let’s make sure we are running the latest, upgraded version of pip by running the following command:

pip install --upgrade pip

If a newer version of pip is available, it will download and install as shown in the screenshots below:

This image shows pip being upgraded in the command line.

Now that we have the latest version of pip installed, let’s install Flask. Run the following command:

pip install flask

Flask will download and install as shown in the below screenshot:

This image shows Flask being installed in the command line.

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.

app = Flask(__name__)
@app.route('/')
def welcome():
return "Welcome to the Codecademy Calculator!"

Next, we create a Flask instance and assign it to the variable named app. We can create several routes using the @app.route decorator.

These routes correspond to the URL web request 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 numbers
num1 = 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 following code:

  • num1 and num2 allow 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 /division route. This function divides the values num1 and num2.
  • @app.route('/multiplication') specifies the web request with /multiplication.
  • multiplication() is a function that executes in the /multiplication route. This function multiplies the values num1 and num2.

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 the web browser.

Running the code

Now, it’s time to run our program and see Flask come to life! You can either copy and paste the code above into a Python file or download the code by clicking here.

Once you have the code ready on your computer, run it (e.g., flask --app app run). You should see the following:

Enter your first number:

After you enter two numbers, you should see 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 the localhost http://127.0.0.1:5000/ on your browser, and the following page should appear:

This is a minimal webpage with the following text: "Welcome to the Codecademy Calculator!"

To go to the /division or /multiplication route, you can add division or multiplication after the final /. If you wanted to see the division() function executed, go to http://127.0.0.1:5000/division, and you should see something like the following:

This is a minimal webpage that shows the division function.

Conclusion

Deploying allows us to quickly package up our application to be easily configured and installed into environments other than where we developed the application. 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!