This tutorial demonstrates building a simple two-page website using Flask, a lightweight Python web framework. It focuses on static content to establish a foundational workflow, easily expandable for more complex applications.
Flask Installation
Before starting, install Flask. If you encounter issues, consult online resources or leave a comment detailing the problem.
Virtualenv Setup
We'll use virtualenv to create an isolated Python environment for this project. This prevents conflicts with other system libraries.
Check if virtualenv is already installed:
$ virtualenv --version
If not, install it:
$ pip install virtualenv
Create and activate a virtual environment:
$ virtualenv flaskapp $ cd flaskapp $ . bin/activate
Now install Flask:
pip install Flask
Project Structure
Organize your project as follows within the flaskapp
directory:
<code>flaskapp/ ├── app/ │ ├── static/ │ │ ├── css/ │ │ ├── img/ │ │ └── js/ │ ├── templates/ │ ├── routes.py │ └── README.md └── ...</code>
The diagram below illustrates the application flow:
- A user request (e.g.,
/
) reaches theroutes.py
file. -
routes.py
locates the corresponding template in thetemplates
folder. - The template accesses static assets (images, CSS, JavaScript) from the
static
folder. - The rendered HTML is returned to the browser via
routes.py
.
Creating the Home Page
To avoid repetitive HTML boilerplate, we'll use web templates. Flask utilizes the Jinja2 template engine.
First, create a base layout template:
app/templates/layout.html
<!DOCTYPE html> <html> <head> <title>Flask App</title> <link href="{{ url_for('static', filename='css/main.css') }}" rel="stylesheet"> </head> <body> <div class="container"> <h1 id="Flask-App">Flask App</h1> </div> <div class="container"> {% block content %}{% endblock %} </div> </body> </html>
Next, create the home page template:
app/templates/home.html
{% extends "layout.html" %} {% block content %} <div class="jumbo"> <h2 id="Welcome">Welcome!</h2> <h3 id="This-is-the-home-page">This is the home page.</h3> </div> {% endblock %}
Now, map the URL to the template in routes.py
:
app/routes.py
from flask import Flask, render_template app = Flask(__name__) @app.route('/') def home(): return render_template('home.html') if __name__ == '__main__': app.run(debug=True)
Add CSS styling to static/css/main.css
: (Content of main.css remains the same)
Running the app and visiting http://localhost:5000/
will display the home page.
Adding an About Page and Navigation
Let's create an "About" page and add navigation links.
Create the "About" template:
app/templates/about.html
{% extends "layout.html" %} {% block content %} <h2 id="About">About</h2> <p>This is the About page.</p> {% endblock %}
Update routes.py
to include the about page route:
app/routes.py
from flask import Flask, render_template app = Flask(__name__) @app.route('/') def home(): return render_template('home.html') @app.route('/about') def about(): return render_template('about.html') if __name__ == '__main__': app.run(debug=True)
Add navigation links to layout.html
: (Content remains the same)
Add navigation styles to main.css
: (Content remains the same)
Now, you can access the about page at http://localhost:5000/about
.
Conclusion
This tutorial demonstrates a basic Flask application, illustrating a scalable workflow for building more complex web applications. Flask's simplicity and power make it an excellent choice for various web development projects.
The above is the detailed content of An Introduction to Python's Flask Framework. For more information, please follow other related articles on the PHP Chinese website!

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

You can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version
SublimeText3 Linux latest version

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.