Flask Tutorials: Understanding `flask run` 🎯

beginner
19 min

Flask Tutorials: Understanding flask run 🎯

Welcome to this comprehensive guide on using the flask run command in Flask, a powerful Python web framework! In this tutorial, we'll take a deep dive into the flask run command, exploring its purpose, usage, and practical applications. By the end, you'll have a solid understanding of how to use this essential tool for running your Flask applications.

Getting Started 📝

Before we delve into the details, let's ensure you have Flask installed. If you haven't already, install Flask using pip, the Python package installer:

bash
pip install flask

Creating a Flask Application 💡

First, let's create a simple Flask application. Open your favorite text editor and create a new Python file, say app.py.

python
from flask import Flask app = Flask(__name__) @app.route('/') def home(): return "Hello, World!" if __name__ == '__main__': app.run()

Save the file and open a terminal or command prompt. Navigate to the directory where app.py is located, and run the application using the following command:

bash
python app.py

The flask run Command 📝

The flask run command is a built-in Flask utility that helps us run our applications. It performs several tasks, including:

  1. Starting the development server
  2. Setting up the necessary environment
  3. Providing a URL to access our application
  4. Rebuilding the application if changes are detected

Let's run our application using flask run:

bash
flask run

You should see output similar to this:

bash
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) * Restarting with stat

Now, open a web browser and navigate to http://127.0.0.1:5000/. You should see "Hello, World!" displayed, indicating that your Flask application is running!

Command Line Options 💡

The flask run command offers various options to fine-tune your application's behavior. Some common options include:

  • --debug: Enables debug mode, which provides more detailed error messages.
  • --port=PORT: Specifies the port to run the server on (default is 5000).
  • --host=HOST: Sets the host to run the server on (default is 127.0.0.1 or localhost).

Here's an example of using the --debug option:

bash
flask run --debug

Building a Real-World Example 💡

Let's build a simple web application that allows users to add and retrieve to-do items. Create a new file called todo.py and paste the following code:

python
from flask import Flask, request, jsonify import json app = Flask(__name__) todos = {} @app.route('/todo', methods=['POST']) def add_todo(): todo = request.get_json() todo_id = len(todos) + 1 todos[todo_id] = todo return jsonify({"status": "success", "todo_id": todo_id}), 201 @app.route('/todo/<int:todo_id>', methods=['GET']) def get_todo(todo_id): if todo_id in todos: return jsonify(todos[todo_id]) else: return jsonify({"status": "error", "message": "Todo not found"}), 404 if __name__ == '__main__': app.run()

Save the file and run the application using flask run. You can now send a POST request to http://127.0.0.1:5000/todo with a JSON payload containing a to-do item to add it to the application. To retrieve a to-do item, send a GET request to http://127.0.0.1:5000/todo/<todo_id>.

Quiz 💡

Quick Quiz
Question 1 of 1

What command do we use to run a Flask application?

With this in-depth guide, you now have a strong foundation for using the flask run command in your Flask applications. As you continue to explore Flask, remember to practice, experiment, and have fun! Happy coding! 🤖💻🚀