This is a project to learn and implement basic web development in python using flask and run on localhost. This page was followed to build this tutorial: https://www.tutorialspoint.com/flask/
In this tutorial, we will learn how to run helloflask.py app written in python on a local web browser.
pip3 install flask
git clone https://github.com/nausheenfatma/basic_flask_tutorial.git
Goto root directory:
$ cd basic_flask_tutorial
$ python3 helloflask.py
If the command runs successfully, it should look like this:
-
http://127.0.0.1:5000 prints welcome message
-
http://127.0.0.1:5000/hello prints hello message
-
http://127.0.0.1:5000/hello/nausheen prints hello message with URL variable.
-
Till now the messages were rendering from python return statements http://127.0.0.1:5000/hellopage renders an html pagefound in templates/hellopage.html instead of strings returned from python
-
http://127.0.0.1:5000/userinfo is a form to take user input and display after submit to next page
-
File uploads example: http://127.0.0.1:5000/upload page to upload a file.
Voila! Congratulations on learning how to run flask locally!
Wait, we are not done yet! If you notice that running the flask app gives a warning: WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
Check this reference docs : https://flask.palletsprojects.com/en/2.2.x/deploying/
The docs clearly mentions: After developing your application, you’ll want to make it available publicly to other users. When you’re developing locally, you’re probably using the built-in development server, debugger, and reloader. These should not be used in production. Instead, you should use a dedicated WSGI server or hosting platform.
Now there two ways:
- Self hosting options
- Using Hosting platforms
I chose Waitress which is a pure Python WSGI server. You may try others.
Steps :
pip3 install waitress
def create_app():
return app
waitress-serve --port=8080 --call helloflask:create_app
Now check URL http://localhost:8080/hello on localhost