how to set and run flask app on terminal

To set up and run a Flask app on the terminal, follow these steps:

  1. Create a virtual environment (optional):
  2. Open the terminal.
  3. Create a new directory for your Flask app (if needed).
  4. Navigate to the directory using the cd command.
  5. Create a virtual environment by running the command: python3 -m venv myenv
  6. Activate the virtual environment by running the command: source myenv/bin/activate (for Unix/Linux) or myenv\Scripts\activate (for Windows).

  7. Install Flask:

  8. With the virtual environment activated, run the command: pip install flask

  9. Create a Flask app:

  10. Create a new Python file (e.g., app.py) in your project directory.
  11. Import the necessary Flask modules by adding the following lines at the beginning of the file: python from flask import Flask app = Flask(__name__)
  12. Define a route and a function to handle the route. For example: python @app.route('/') def hello(): return 'Hello, Flask!'

  13. Run the Flask app:

  14. In the terminal, navigate to the directory where your Flask app file (app.py) is located.
  15. Run the command: flask run
  16. The Flask development server will start, and the app will be accessible at the specified URL (usually http://127.0.0.1:5000/).

Note: If you make changes to your Flask app while the development server is running, the server will automatically detect the changes and reload the app.

That's it! You have successfully set up and run a Flask app on the terminal. Let me know if you have any further questions.