Creating a new flask project

Here's a step-by-step guide to creating a new Flask project:

Step 1: Install Flask

If you haven't already, install Flask using pip:

pip install flask

Step 2: Create a new directory for your project

Create a new directory for your project and navigate into it:

mkdir myflaskproject
cd myflaskproject

Step 3: Create a new file for your Flask app

Create a new file called app.py (or any other name you prefer) and add the following code:

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello_world():
    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!".

Step 4: Run your Flask app

Run your Flask app using the following command:

python app.py

This will start the Flask development server, which you can access by navigating to http://localhost:5000 in your web browser.

Step 5: Create a new file for your templates

Create a new directory called templates and add a new file called index.html with the following code:

<!DOCTYPE html>
<html>
  <head>
    <title>My Flask App</title>
  </head>
  <body>
    <h1>Hello, World!</h1>
  </body>
</html>

This code defines a simple HTML page with a heading that says "Hello, World!".

Step 6: Update your Flask app to use templates

Update your app.py file to use the render_template function to render the index.html template:

from flask import Flask, render_template

app = Flask(__name__)

@app.route("/")
def hello_world():
    return render_template("index.html")

if __name__ == "__main__":
    app.run(debug=True)

This code tells Flask to render the index.html template when the root URL is accessed.

Step 7: Run your Flask app again

Run your Flask app again using the following command:

python app.py

This will start the Flask development server again, and you can access your app by navigating to http://localhost:5000 in your web browser. You should see the HTML page with the heading "Hello, World!".

That's it! You've created a new Flask project and run it successfully. From here, you can start building your app by adding more routes, templates, and functionality.