how to create a flask app in python

To create a Flask app in Python, you can follow these steps:

  1. Install Flask
main.py
pip install flask
18 chars
2 lines
  1. Create a file called app.py and import Flask
main.py
from flask import Flask

app = Flask(__name__)
47 chars
4 lines
  1. Define the routes using the @app.route decorator
main.py
@app.route("/")
def home():
    return "Hello, World!"

@app.route("/about")
def about():
    return "This is an about page"
125 chars
8 lines
  1. Run the app using app.run()
main.py
if __name__ == "__main__":
    app.run(debug=True)
51 chars
3 lines
  1. In the terminal, navigate to the directory where app.py is located and run the following command to start the server
main.py
python app.py
14 chars
2 lines
  1. Open a web browser and navigate to http://localhost:5000 to view the app.

gistlibby LogSnag