Supercharge your development with unmatched features:
Access a full terminal environment, run Linux commands, and manage your project’s dependencies directly within the IDE.
Browse and interact with websites directly within the IDE. Supports real-time interaction with web content without leaving the workspace.
Manage your project files and directories effortlessly within the IDE. Create, edit, rename, move, and delete files—all in one place.
Experience seamless code editing with real-time syntax highlighting, tab support, and intelligent code suggestions for a smoother development workflow.
Flask is a lightweight web framework for Python. It provides the essentials to build web applications, such as routing, templates, and handling requests, while being minimalistic and flexible for developers to build their own features.
To set up Flask, install it via pip. Make sure you have Python installed on your system.
pip install flask
Verify the installation by running the following command:
python -m flask --version
Start by creating a simple Flask application that returns a "Hello, World!" message on the root route.
# app.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return "Hello, World!"
if __name__ == "__main__":
app.run(debug=True)
Run the app using:
python app.py
Flask uses routes to map URLs to functions, called view functions, which generate the response for the client. The @app.route
decorator is used to define these routes.
# Example:
@app.route('/hello')
def hello_page():
return "Hello from Flask!"
Flask uses Jinja2 for rendering templates. Templates allow you to generate HTML dynamically based on the data passed to them from the server.
# Example:
from flask import render_template
@app.route('/greet')
def greet():
return render_template('greet.html', name="Alice")
Create the greet.html
file inside the templates folder with content like:
Greeting
Hello, {{ name }}!
Flask allows you to handle form submissions and access form data using the request
object.
# Example:
from flask import request
@app.route('/submit', methods=['POST'])
def submit():
name = request.form['name']
return f"Hello, {name}!"
HTML form example:
Flask can serve static files (such as images, CSS, and JavaScript) from a special static
folder. Simply place static files inside this folder, and access them using the /static/
URL prefix.
# Example of linking a static CSS file:
Flask provides utilities for redirecting users to different URLs and handling errors.
# Example:
from flask import redirect, url_for
@app.route('/old-page')
def old_page():
return redirect(url_for('hello'))
@app.errorhandler(404)
def page_not_found(error):
return "Page not found", 404
Flask can integrate with various databases using extensions such as Flask-SQLAlchemy. This allows you to interact with relational databases through models and queries.
# Example using SQLAlchemy:
from flask_sqlalchemy import SQLAlchemy
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db'
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80))
@app.route('/add_user')
def add_user():
new_user = User(name='Alice')
db.session.add(new_user)
db.session.commit()
return 'User added!'
Flask has many extensions that provide additional functionality. Common extensions include Flask-WTF (forms), Flask-Login (authentication), Flask-Mail (email), and Flask-RESTful (building APIs).
# Example with Flask-WTF:
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField
class MyForm(FlaskForm):
name = StringField('Name')
submit = SubmitField('Submit')