"Flask Web Development: Create Your Own Website Example"

Building a Simple Website with Flask: A Step-by-Step Example

Flask, a popular Python web framework, is renowned for its simplicity and flexibility. It's an excellent choice for both beginners and experienced developers looking to create small to medium-sized web applications. In this guide, we'll create a simple example website using Flask, covering installation, setup, and essential features.

Setting Up Your Flask Environment

Before we dive into creating our example website, ensure you have Python (3.6 or later) and pip installed on your system. Then, install Flask using pip:

pip install flask

Creating a New Flask Project

Navigate to the directory where you want to create your project and run the following command to create a new Flask application:

What Can You Do With Flask? (Applications & Examples)
What Can You Do With Flask? (Applications & Examples)

flask new my_project

Replace 'my_project' with the name you want for your project. This command will create a new folder with the basic structure of a Flask application.

Understanding the Project Structure

Flask projects follow a simple structure. Here's a breakdown of the generated folders and files:

  • my_project/: The main project folder.
  • my_project/__init__.py: The main application file where you'll configure your Flask app.
  • my_project/templates/: Folder for HTML templates.
  • my_project/static/: Folder for static files like CSS and JavaScript.
  • my_project/config.py: Configuration file for your application.

Running the Flask Development Server

To run your Flask application, navigate to the project directory and execute the following command:

an image of a website page with bottles and oranges on the table in front of it
an image of a website page with bottles and oranges on the table in front of it

export FLASK_APP=my_project
flask run

Your application should now be running on http://127.0.0.1:5000/.

Creating Routes and Views

In Flask, routes map URLs to Python functions. Let's create a simple route in my_project/__init__.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)

We've created a route for the home page that renders an HTML template named 'home.html'. Create this template in the my_project/templates/ folder:

Professional Website Design | Sandhill Studio
Professional Website Design | Sandhill Studio

<!DOCTYPE html>
<html>
<head>
    <title>My Flask Website</title>
</head>
<body>
    <h1>Welcome to my Flask website!</h1>
</body>
</html>

Now, when you navigate to http://127.0.0.1:5000/, you should see your new website with the heading "Welcome to my Flask website!".

Adding More Pages and Navigation

Let's add an 'About' page and update our navigation. First, create a new template named 'about.html' in the my_project/templates/ folder:

<!DOCTYPE html>
<html>
<head>
    <title>About</title>
</head>
<body>
    <h1>About this website</h1>
    <p>This is a simple website built with Flask.</p>
</body>
</html>

Next, update the 'home.html' template to include a navigation bar:

<!DOCTYPE html>
<html>
<head>
    <title>Home</title>
</head>
<body>
    <nav>
        <a href="/">Home</a>
        <a href="/about">About</a>
    </nav>
    <h1>Welcome to my Flask website!</h1>
</body>
</html>

Finally, add a new route in my_project/__init__.py for the 'About' page:

@app.route('/about')
def about():
    return render_template('about.html')

Now, you should have a simple website with a home page and an 'About' page, accessible via the navigation bar.

Conclusion

In this guide, we've created a simple example website using Flask, covering essential features like setting up the development environment, creating routes and views, and adding multiple pages with navigation. Flask's simplicity and flexibility make it an excellent choice for both beginners and experienced developers looking to build web applications quickly and efficiently.

an image of a website page with different items on the front and back pages, including beer bottles
an image of a website page with different items on the front and back pages, including beer bottles
Flask Project Structure: Build a Scalable Web App – Real Python
Flask Project Structure: Build a Scalable Web App – Real Python
GitHub - pallets/flask: The Python micro framework for building web applications.
GitHub - pallets/flask: The Python micro framework for building web applications.
Python Tutorials – Real Python
Python Tutorials – Real Python
Water Bottle Branding Concept | Product Marketing Example for Fitness Brand
Water Bottle Branding Concept | Product Marketing Example for Fitness Brand
Sip Elixirs | Web Design Inspiration | Siteinspire
Sip Elixirs | Web Design Inspiration | Siteinspire
Drink Monday | eCommerce Website Design Gallery & Tech Inspiration
Drink Monday | eCommerce Website Design Gallery & Tech Inspiration
CrazyCap Bottle - website concept
CrazyCap Bottle - website concept
Beauty & Wellness Website Detail Product Page
Beauty & Wellness Website Detail Product Page
BrewDistrict24 - Landing Page Design | Lapa Ninja
BrewDistrict24 - Landing Page Design | Lapa Ninja
Python Flask Tutorial: Full-Featured Web App Part 1 - Getting Started
Python Flask Tutorial: Full-Featured Web App Part 1 - Getting Started
Vikre Distillery - SMAKK Studios Site
Vikre Distillery - SMAKK Studios Site
three different types of water bottles with the words ice's time capsuleed by coffees everywhere
three different types of water bottles with the words ice's time capsuleed by coffees everywhere
Modern UI/UX Website Layout
Modern UI/UX Website Layout
RIVR | eCommerce Website Design Gallery & Tech Inspiration
RIVR | eCommerce Website Design Gallery & Tech Inspiration
Drink Shopify store website design example
Drink Shopify store website design example
35cl Cocktail Co. | eCommerce Website Design Gallery & Tech Inspiration
35cl Cocktail Co. | eCommerce Website Design Gallery & Tech Inspiration
Beer website
Beer website
WEBSITE DESIGN FOR PIMS DRINKS SPACE - Christine Antilevskaya
WEBSITE DESIGN FOR PIMS DRINKS SPACE - Christine Antilevskaya
Flask by Example (Learning Path) – Real Python
Flask by Example (Learning Path) – Real Python
100+ Bakery Website Templates
100+ Bakery Website Templates
Margarita Cocktail Event Landing Page UI/UX Design by Khai Luu
Margarita Cocktail Event Landing Page UI/UX Design by Khai Luu
Measured | The drinks worth talking about on Landbook - get inspired by blog design and more
Measured | The drinks worth talking about on Landbook - get inspired by blog design and more