- Python for Web Development
-
Overview
- Introduction to Flask and Django
- Setting Up a Flask Application
- Django Models and Migrations
- Routing and URL Handling in Django and Flask
- Forms and User Authentication in Django and Flask
- REST API Development with Flask & Django
- Working with Databases (SQLite, PostgreSQL, MySQL)
- Template Engines (Jinja2 for Flask, Django Templates)
- Deployment of Flask & Django Applications on AWS, GCP, and Heroku
- Security Best Practices for Web Apps
Setting Up a Flask Application
Add to BookmarkFlask is a lightweight and flexible web framework for Python that makes building web applications simple and efficient. In this tutorial, we will go through the step-by-step process of setting up a Flask application from scratch.
1. Installing Flask
Before starting, ensure that Python is installed on your system. You can check this by running:
python --versionor
python3 --versionNow, install Flask using pip:
pip install Flask2. Creating a Basic Flask App
Once Flask is installed, create a new directory for your project and navigate into it:
mkdir flask_app
cd flask_appInside this directory, create a Python file (e.g., app.py) and add the following code:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return "Welcome to Your First Flask App!"
if __name__ == '__main__':
app.run(debug=True)Explanation:
- We import the
Flaskclass. - We create an instance of the Flask application.
- The
@app.route('/')decorator defines a route for the homepage. - The
home()function returns a simple message. app.run(debug=True)runs the application in debug mode.
3. Running the Flask Application
To start your Flask app, run:
python app.pyor
python3 app.pyYou will see output similar to:
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)Now, open your browser and visit http://127.0.0.1:5000/. You should see "Welcome to Your First Flask App!" displayed on the page.
4. Project Structure
As your Flask project grows, organizing your files becomes crucial. A common structure is:
flask_app/
│── app.py
│── templates/
│ ├── index.html
│── static/
│ ├── style.css
│── requirements.txttemplates/→ Stores HTML files.static/→ Stores CSS, JS, images, etc.requirements.txt→ Lists dependencies (pip freeze > requirements.txt).
5. Creating a Simple HTML Template
Flask supports Jinja2 templating. Let's create a templates folder and an index.html file inside it:
<!DOCTYPE html>
<html>
<head>
<title>My Flask App</title>
</head>
<body>
<h1>Welcome to Flask!</h1>
</body>
</html>Modify app.py to render this HTML:
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def home():
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True)Now, reload http://127.0.0.1:5000/, and you’ll see your HTML page!
6. Running Flask with a Virtual Environment (Optional)
To keep your project dependencies organized, you can use a virtual environment:
python -m venv venv
source venv/bin/activate # On macOS/Linux
venv\Scripts\activate # On Windows
pip install FlaskThis ensures Flask is installed only for this project.
Conclusion
You have successfully set up a basic Flask application! In the next tutorials, we will explore routing, handling user inputs, working with databases, and more. Stay tuned!
Prepare for Interview
- JavaScript Interview Questions for 5+ Years Experience
- JavaScript Interview Questions for 2–5 Years Experience
- JavaScript Interview Questions for 1–2 Years Experience
- JavaScript Interview Questions for 0–1 Year Experience
- JavaScript Interview Questions For Fresher
- SQL Interview Questions for 5+ Years Experience
- SQL Interview Questions for 2–5 Years Experience
- SQL Interview Questions for 1–2 Years Experience
- SQL Interview Questions for 0–1 Year Experience
- SQL Interview Questions for Freshers
- Design Patterns in Python
- Dynamic Programming and Recursion in Python
- Trees and Graphs in Python
- Linked Lists, Stacks, and Queues in Python
- Sorting and Searching in Python
Random Blogs
- Role of Digital Marketing Services to Uplift Online business of Company and Beat Its Competitors
- Understanding LLMs (Large Language Models): The Ultimate Guide for 2025
- Python Challenging Programming Exercises Part 3
- Extract RGB Color From a Image Using CV2
- OLTP vs. OLAP Databases: Advanced Insights and Query Optimization Techniques
- Deep Learning (DL): The Core of Modern AI
- Datasets for Exploratory Data Analysis for Beginners
- How to Start Your Career as a DevOps Engineer
- Create Virtual Host for Nginx on Ubuntu (For Yii2 Basic & Advanced Templates)
- Understanding AI, ML, Data Science, and More: A Beginner's Guide to Choosing Your Career Path
- String Operations in Python
- Understanding HTAP Databases: Bridging Transactions and Analytics
- Datasets for analyze in Tableau
- Store Data Into CSV File Using Python Tkinter GUI Library
- How Multimodal Generative AI Will Change Content Creation Forever
Datasets for Machine Learning
- Awesome-ChatGPT-Prompts
- Amazon Product Reviews Dataset
- Ozone Level Detection Dataset
- Bank Transaction Fraud Detection
- YouTube Trending Video Dataset (updated daily)
- Covid-19 Case Surveillance Public Use Dataset
- US Election 2020
- Forest Fires Dataset
- Mobile Robots Dataset
- Safety Helmet Detection
- All Space Missions from 1957
- OSIC Pulmonary Fibrosis Progression Dataset
- Wine Quality Dataset
- Google Audio Dataset
- Iris flower dataset


