Starting a Flask App: A Step-by-Step Guide
Flask is a lightweight and flexible Python web framework that allows developers to build web applications quickly and efficiently. With its simplicity and ease of use, Flask has become a popular choice for building web applications, from small prototypes to complex enterprise-level applications. In this article, we will guide you through the process of starting a new Flask app.
Step 1: Install Flask
Before you can start building your Flask app, you need to install Flask. You can install Flask using pip, the Python package manager. Here’s how to install Flask:
- Open your terminal or command prompt.
- Type the following command to install Flask:
pip install flask - Press Enter to run the command.
Step 2: Create a New Flask App
Once Flask is installed, you can create a new Flask app. Here’s how to create a new app:
- Open your terminal or command prompt.
- Type the following command to create a new app:
flask new myapp - Press Enter to run the command.
- This will create a new directory called
myappwith the basic structure for a Flask app.
Step 3: Configure the App
Before you can start building your app, you need to configure it. Here’s how to configure the app:
- Open the
app.pyfile in yourmyappdirectory. - Add the following code to the file:
from flask import Flask
app = Flask(name)
@app.route("/")
def index():
return "Hello, World!"
if name == "main":
app.run(debug=True)
* This code creates a new Flask app and defines a single route for the root URL (`"/"`) that returns the string "Hello, World!".
* The `if __name__ == "__main__":` block is used to run the app in debug mode.
**Step 4: Run the App**
Once you've configured your app, you can run it. Here's how to run the app:
* Open your terminal or command prompt.
* Type the following command to run the app: `python app.py`
* Press Enter to run the command.
**Step 5: Test the App**
Once the app is running, you can test it. Here's how to test the app:
* Open a web browser and navigate to `http://localhost:5000`.
* You should see the string "Hello, World!" displayed on the page.
**Step 6: Add Routes and Views**
Once you've tested the app, you can start adding routes and views. Here's how to add a new route and view:
* Open the `app.py` file in your `myapp` directory.
* Add the following code to the file:
```python
from flask import Flask, render_template
app = Flask(__name__)
@app.route("/")
def index():
return render_template("index.html")
@app.route("/about")
def about():
return "This is the about page"
if __name__ == "__main__":
app.run(debug=True)
- This code adds a new route for the root URL (
"/") that renders an HTML template calledindex.html. - The
about()function returns a string that describes the about page.
Step 7: Add Models and Database
Once you’ve added routes and views, you need to add models and a database. Here’s how to add a model and database:
- Open the
models.pyfile in yourmyappdirectory. - Add the following code to the file:
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), nullable=False)
email = db.Column(db.String(100), nullable=False, unique=True)
def __repr__(self):
return f"User('{self.name}', '{self.email}')"
* This code defines a new model called `User` that has three columns: `id`, `name`, and `email`.
* The `db` object is used to interact with the database.
**Step 8: Add a Database**
Once you've added a model and database, you need to add a database. Here's how to add a database:
* Open the `config.py` file in your `myapp` directory.
* Add the following code to the file:
```python
SQLALCHEMY_DATABASE_URI = "sqlite:///myapp.db"
SQLALCHEMY_TRACK_MODIFICATIONS = False
- This code defines a new database called
myapp.dbthat uses SQLite as the database engine.
Step 9: Run the App
Once you’ve added a database, you can run the app. Here’s how to run the app:
- Open your terminal or command prompt.
- Type the following command to run the app:
python app.py - Press Enter to run the command.
Conclusion
Starting a Flask app is a straightforward process that requires just a few steps. By following these steps, you can create a new Flask app and start building your web application. Remember to always test your app thoroughly and add routes and views as needed. With Flask, you can build complex web applications quickly and efficiently.
Table of Contents
- Step 1: Install Flask
- Step 2: Create a New Flask App
- Step 3: Configure the App
- Step 4: Run the App
- Step 5: Test the App
- Step 6: Add Routes and Views
- Step 7: Add Models and Database
- Step 8: Run the App
- Conclusion
