Creating Websites with Python using Flask Tutorial #1

Posted by


Flask is a lightweight web application framework for Python. It is designed to be simple and easy to use, making it an ideal choice for beginners who want to create websites with Python. In this tutorial, we will walk you through the process of creating a simple website using Flask.

Step 1: Install Flask
Before you can start building your website with Flask, you will need to install the Flask library. You can do this using pip, the Python package installer. Open up your terminal and run the following command:

pip install Flask

This will download and install the Flask library on your system.

Step 2: Create a Flask App
Now that you have Flask installed, you can start building your website. Create a new Python file in your preferred text editor and import the Flask library. In this file, you will define your Flask application.

from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
    return "Hello, World!"

if __name__ == '__main__':
    app.run()

In this code snippet, we create a new Flask application and define a route for the home page ("/"). When a user visits the home page, the home function will be called and will return the message "Hello, World!".

Step 3: Run Your Flask App
Save the Python file you created in the previous step and run it using the following command in your terminal:

python filename.py

This will start a development server for your Flask application. You can now open your web browser and navigate to http://127.0.0.1:5000/ to see your website in action. You should see the message "Hello, World!" displayed on the page.

Step 4: Create Additional Routes
To create additional pages on your website, you can define more routes in your Flask application. For example, you can add a route for a contact page:

@app.route('/contact')
def contact():
    return "Contact Us"

You can then navigate to http://127.0.0.1:5000/contact in your web browser to see the "Contact Us" message displayed.

Step 5: Create Templates
While returning simple messages from your routes works fine for basic websites, you will likely want to create more complex HTML pages for your website. Flask allows you to use templates to dynamically generate HTML content.

Create a new folder in your project directory called templates and create a new HTML file inside it. In this HTML file, you can define the structure of your webpage using HTML and Jinja2 templating language.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{{ title }}</title>
</head>
<body>
    <h1>{{ message }}</h1>
</body>
</html>

In your Flask application, you can render this template by making use of the render_template function:

from flask import render_template

@app.route('/about')
def about():
    title = "About Us"
    message = "Learn more about our company"
    return render_template('about.html', title=title, message=message)

Now when you navigate to http://127.0.0.1:5000/about, you will see the "About Us" page with the message "Learn more about our company" displayed.

Step 6: Conclusion
Congratulations! You have successfully created a simple website using Flask. In this tutorial, you have learned how to install Flask, create a Flask application, define routes, use templates to render HTML content, and run your website locally. Flask is a versatile framework that you can use to build more complex web applications in Python. Feel free to explore the Flask documentation and experiment with different features to enhance your website further. Happy coding!

0 0 votes
Article Rating
49 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@TechWithTim
2 months ago

Start a high paying tech career making $60k+/year with NO DEBT: https://coursecareers.com/a/techwithtim

@SavorSupremeKitchen
2 months ago

2:10

@tanjinaislam3306
2 months ago

I don't understand what he did? Is there any course for beginners who can learn from scratch?

@lorenzasaettone8466
2 months ago

Can you add a tutorial about how to incorporate a python function that uses open ai, a gpt assistant that answers to input things? I want that what happens in my vscode console is visible in my Web app. I don’t know if I was clear

@FreAcker
2 months ago

@app.errorhandler(404)

def page_not_found(e):

return "You got an error using some kind a exploits", 404

prevent open redirect vuln.

@origamicaptain5664
2 months ago

This is really helpful. Thank you.

It is a little confusing that you use home as the example for the second page route because there is already a function called home for the root route.

I figured out what was going on, but it would have been clearer if a different route name was used.

@EverydayNewExperiences-lb9ui
2 months ago

bit struggling running it in command prompt, but finally i run my webpage. thanks to you time

@johnlloyd5047
2 months ago

Hello can anyone help me on how can I make my python code into flask, then connect it to my laravel project

@user-tg4hs1sd9r
2 months ago

what is the extension that you use which colors the whitespaces/tabspace in vscode?

@rachitsingh4913
2 months ago

Loved this tutorial

@SumanthShivashankar
2 months ago

Nice tutorial .. step by step clear explanation.Thanks for sharing the knowledge

@gooo1762
2 months ago

thanks a lot

@convexfinance6324
2 months ago

not a beginner friendly, still complicated

@cr.nagaraj
2 months ago

2:10

@cr.nagaraj
2 months ago

2:10

@prajapatiniraj3263
2 months ago

This video deserves to go viral, it's that good

@zandy286
2 months ago

IDK why, since i start try this i has to change a lot of stuff, cause l just bump on few errors, but well, now l am on this:

– l using VS code, due to since ever have coding there
– l oppened my file with the py extension, and copy the code
– tried launch on my windowns powershell and like "sorry, module ABC is an unvalid syntax"
– try change and change that, however nothing

Still trying by searchs and random ideas…

Could someones help?

@user-te8rb7ki8z
2 months ago

You may want to start with the concept of a virtual environment. Coming from javascript land this is very new

@JohnDoe-rp8xn
2 months ago

you forgot to put a corner thing at 1:40 for pip.

@user-vj8uj5mb8t
2 months ago

cool video)