flask notes
flask notes
@app.route('/')
def hello_world():
return 'Hello World’
if __name__ == '__main__':
app.run(debug=True)
Importing flask module in the project is mandatory. An object of Flask class is our WSGI
application.
Flask constructor takes the name of current module (__name__) as argument.
The route() function of the Flask class is a decorator, which tells the application which URL
should call the associated function.
Step 3. Create and activate new virtual environment (Creating Virtual Environment)
python app.py
Your Flask app should now be running, and you can access it in your web browser at
http://127.0.0.1:5000/ or http://localhost:5000/.
Remember to stop the Flask app by pressing Ctrl+C in the terminal when you're done testing
or developing.
FLASK_APP=app.py
flask run
The commands set FLASK_APP=app.py and flask run are used to set the Flask application
entry point and run the development server, respectively. Here's what each command does:
1. Set FLASK_APP:
set FLASK_APP=app.py is a command used in the Windows command
prompt to set the FLASK_APP environment variable to the value app.py.
This environment variable tells Flask which Python file contains the Flask
application. In this case, it indicates that the Flask application is in the file
named app.py.
On Unix-based systems (Linux, macOS), the equivalent command is export
FLASK_APP=app.py.
2. Run Flask Application:
After setting the FLASK_APP environment variable, you use the command
flask run to start the development server and run your Flask application.
The flask run command looks for the FLASK_APP environment variable to
determine which Python file contains the Flask application. It then runs the
Flask development server, allowing you to access your application locally.
By default, the development server will be accessible at http://127.0.0.1:5000/
or http://localhost:5000/.
Press Ctrl+C in the terminal to stop the Flask development server when you're
done testing or developing.