How To Create A flask app in Windows

Mohit Malaya Nandy
2 min readSep 5, 2024

--

flask & python logo

A. Set Up Your Environment:

  1. Install Python: — Ensure that Python is installed on your machine. You can download it from python.org

2. Create a Project Directory: Create a new directory for your project.

mkdir filename
cd filename

3. Set up a Vertual Environment(Optional but Recomended):

  • Create a virtual environment:
py -m venv .venv
  • Activate the virtual environment:
.venv\Scripts\activate
  • Install Flask within this virtual environment:
pip install Flask

B. Create a flask app:

1. Create the Flask App File: Create a file named `app.py` in your project directory. This will be the main file of your Flask app:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
return "Hello, Flask!"

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

2. Run the Flask App: In your terminal, while in the project directory, run:

flask --app app run

Flask will start a local development server, and you should see output indicating that the server is running, typically on `http://127.0.0.1:5000/`

3. View in Browser: Open your web browser and go to `http://127.0.0.1:5000/`. You should see “Hello, Flask!” displayed on the page.

C. Developing Your Flask App:

1. Routing: — You can create additional routes in your Flask app by adding more route functions:

@app.route('/about')
def about():
return "This is the about page!"

2. Templates: — Flask allows you to use templates to render HTML pages. First, create a `templates` directory in your project and add an HTML file, e.g., `index.html`

html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Home</title>
</head>
<body>
<h1>{{ title }}</h1>
<p>Welcome to Flask!</p>
</body>
</html>

In your `app.py`, render the template:

from flask import render_template

@app.route('/')
def home():
return render_template('index.html', title="Home")

3. Static Files: — To serve static files like CSS and JavaScript, create a `static` directory in your project and place your files there. You can then link them in your templates:

<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">

4. Deploying Your Flask App: When you’re ready to deploy your Flask app, you can use services like Heroku, AWS Elastic Beanstalk, or Google Cloud Platform.

Your project structure might look like this:

my_flask_app/

├── app.py
├── venv/
├── templates/
│ └── index.html
└── static/
└── style.css

This is a basic introduction to getting started with Flask. From here, you can explore more advanced features like working with databases, handling forms, authentication, and more.

--

--

Mohit Malaya Nandy
Mohit Malaya Nandy

Written by Mohit Malaya Nandy

Hello ! It's me mohit. I just call myself a Techie, I have slightly higher than average general knowledge of a lot of techs. My interest is learning new things

No responses yet