Flask Course - Python Web Application Development
Getting Started with Flask
Flask is designed to be simple and easy to use, making it an excellent choice for beginners and experienced developers alike. To start using Flask, you'll need to have Python installed on your computer. Flask can be installed using pip, Python's package manager.
1. Installing Flask
To install Flask, open your terminal or command prompt and run the following command:
bashpip install Flask
This command downloads and installs Flask and its dependencies. Once Flask is installed, you're ready to start building your web application.
2. Creating a Basic Flask Application
Start by creating a new directory for your project. Inside this directory, create a file named app.py
. This file will contain the main code for your Flask application. Here's a simple example of a Flask application:
pythonfrom flask import Flask app = Flask(__name__) @app.route('/') def home(): return 'Hello, Flask!' if __name__ == '__main__': app.run(debug=True)
In this example, we import the Flask
class, create an instance of it, and define a route (/
) that returns the string 'Hello, Flask!'
when accessed. Running this script starts a development server that you can access in your web browser at http://127.0.0.1:5000
.
3. Understanding Flask Routing
Routing in Flask is the process of mapping URLs to functions in your application. Each route is associated with a specific function that returns the content to be displayed on the web page. You can define multiple routes in your Flask application to handle different pages.
Here's an example of how to define multiple routes:
python@app.route('/about') def about(): return 'This is the About page.' @app.route('/contact') def contact(): return 'This is the Contact page.'
With these routes defined, navigating to /about
or /contact
will display the corresponding content.
4. Using Flask Templates
Templates allow you to separate the presentation layer from the logic in your Flask application. Flask uses the Jinja2 templating engine to render HTML templates. To use templates, create a directory named templates
in your project folder. Inside this directory, create a file named index.html
:
htmlhtml> <html lang="en"> <head> <meta charset="utf-8"> <title>Flask Apptitle> head> <body> <h1>{{ title }}h1> <p>{{ message }}p> body> html>
In your app.py
file, you can render this template as follows:
pythonfrom flask import render_template @app.route('/') def home(): return render_template('index.html', title='Welcome to Flask', message='This is a simple Flask app.')
5. Handling Forms in Flask
Flask provides tools to handle user input through forms. To use forms, you'll need to install Flask-WTF, an extension that simplifies form handling. Install it using pip:
bashpip install Flask-WTF
Create a form class in a file named forms.py
:
pythonfrom flask_wtf import FlaskForm from wtforms import StringField, SubmitField from wtforms.validators import DataRequired class ContactForm(FlaskForm): name = StringField('Name', validators=[DataRequired()]) message = StringField('Message', validators=[DataRequired()]) submit = SubmitField('Send')
In your app.py
file, import and use this form:
pythonfrom forms import ContactForm from flask import Flask, render_template, redirect, url_for @app.route('/contact', methods=['GET', 'POST']) def contact(): form = ContactForm() if form.validate_on_submit(): # Process the form data (e.g., save to database, send email) return redirect(url_for('home')) return render_template('contact.html', form=form)
Create a contact.html
template:
htmlhtml> <html lang="en"> <head> <meta charset="utf-8"> <title>Contact Ustitle> head> <body> <h1>Contact Ush1> <form method="POST"> {{ form.hidden_tag() }} <p> {{ form.name.label }}<br> {{ form.name(size=32) }} p> <p> {{ form.message.label }}<br> {{ form.message(size=64) }} p> <p> {{ form.submit() }} p> form> body> html>
6. Deploying a Flask Application
Once your Flask application is ready, you may want to deploy it to a production server. One popular option is to use Gunicorn, a Python WSGI HTTP Server. Install it using pip:
bashpip install gunicorn
To run your Flask application with Gunicorn, use the following command:
bashgunicorn -w 4 app:app
This command starts Gunicorn with 4 worker processes, which can handle multiple requests simultaneously.
Conclusion
Flask is a versatile and easy-to-learn framework for developing Python web applications. By following the steps outlined above, you can create a basic Flask application, understand routing, use templates, handle forms, and deploy your app. Whether you're building a small project or a complex application, Flask provides the tools and flexibility you need to succeed in web development.
Popular Comments
No Comments Yet