Develop and Deploy an Python Application in a Jiffy By Using a Flask and
It is
"IE=edge">"width=device-width, initial-scale=1.0"> HomePage
Flask Demo Application
For e, enter %
"My name is [1] 1[2]} -|2|2.} learning about Application Deployment!
% endfor % end for %
Execute the application, then return to MySQL Workbench to check the results. It should look like these:

When you access the table, the two entries generated by the app are returned. Your app will now display the database-derived information to the user's browser.

How Do You Install Your Python Flask App
How to Prepare your Python Project for Deployment
's Web Application Hosting platform deploys your application's code to your preferred Git hosting service. The next step is to make your application's environment accommodate this path and permit to deploy your application together with the required dependencies.
Create a new directory inside your flask_demo project's root. Let's call it myapp. Then move the templates directory and your demo.py file into myapp..
Inside within the myapp directory, you can create an wsgi.py file with the following content:
from myapp.demo import app as application
if __name__ == "__main__":
application.run(debug=True)
The build process at uses pip to create your application. It is possible to send a list of the application's dependencies to pip on the production side with the requirements.txt file in the directory root of your project.
While still working in the Venv virtual environment, and in Flask's flask_demo root directory, you are able to create the requirements.txt file that is specific to your project with the following command:
pip freeze > requirements.txt
The text file's contents text file will look something similar to this:
blinker==1.7.0
click==8.1.7
Flask==3.0.0
Flask-MySQLdb==2.0.0
itsdangerous==2.1.2
Jinja2==3.1.2
MarkupSafe==2.1.3
mysql-connector-python==8.2.0
mysqlclient==2.2.1
protobuf==4.21.12
python-dotenv==1.0.0
Werkzeug==3.0.1
The .env file with details about your database for production to the server. As you will not require the Python-dotenv
library to read .env in production it is possible to delete its reference in requirements.txt and remove (or remove) the following lines from demo.py:
Dotenv import load_dotenvload_dotenv()
Add a Python WSGI HTTP Server into the Project
You can incorporate an Gunicorn requirements in your program by incorporating it within your virtual environment like this:
pip installation of gunicorn
After Gunicorn is installed, use Pip to create requirements.txt again.
A different method to install Gunicorn locally would be to edit requirements.txt and simply include an entry such as this:
gunicorn==21.2.0
To wrap up the groundwork to build building the WSGI server, make an archive file within the project's root directory named Procfile and then add the following lines:
web: gunicorn myapp.wsgi
This is the base of the start command for your application in production.
Getting Your Project Prepared for Git
The updated directory structure is now ready for deployment at , but it isn't ideal to have all these files being used in production. Make an .gitignore file in the project root that contains content such as:
/venv
.env
It will prevent the files within the the venv directory, as well as local database secrets contained in .env from uploading to your Git hosting host.
You can now initiate your local Git environment and push the source code to your Git host by using the appropriate tools.
Implementing Your Python Flask Application to
In the process of configuring your building environment, you must select Buildpacks for setting an image for the container and leave all other settings at their defaults. (You don't have to specify a start command as it's already included in the Procfile.)

After reviewing the billing details (you are able to start at no cost! ), click the "Build now" button to view your progress on the log viewer.

Making a Database available for your Python Flask App
Once you have created the database, select it from the list of accessible DB servers, and then scroll to the Internal connections and the Allowed applications section of the Overview tab. If you hit the Connect connection button, your Flask application service located in the same data center will be accessible to choose from:

Simply click the add environment variable to application checkbox prior to making the connection. This displays the environment variables that will carry information about your database- all handled securely without the need for to create the .env file.

At the bottom of the above dialog The settings available during the running time as well as Available during build process will be enabled in default -- which is exactly what you're looking for.
Once you have clicked the Add Connection button, all the variables that are required to access databases are added to your project Settings within My's Applications dashboard:

Even if you rebuilt your application following new enhancements, your details of the database connection will remain.

Congratulations! You've created the Python Flask application and deployed it on the platform.
Summary
Flask's flexible framework makes the creation of a Python application a lot easier. The ability to deploy an Flask application makes things even more simple, speeding creation and deployment.
Steve Bonisteel
Steve Bonisteel is a Technical Editor who started his writing career as a print journalist who chased ambulances and fire trucks. He's been writing about technological developments on the Internet since the 1990s.