How to create website using Flask ?
17 December, 2022
3
3
0
Contributors
What is Flask?
Flask is a lightweight Python web framework that makes it easy to build and deploy web applications quickly. It's a microframework, meaning it doesn't come with all the bells and whistles of a full-stack framework like Django, but it's simple and easy to use.
To get started with Flask, you'll need to have Python installed on your machine. If you don't have it already, you can download the latest version from the Python website (https://www.python.org/).
How to Start?
Once you have Python installed, you can use pip, the Python package manager, to install Flask. Open up a terminal and enter the following command:
This will install Flask and all of its dependencies.
Next, we'll create a new Flask project. In your terminal, navigate to the directory where you want to create your project, and create a file called [ app.py ]
In the app.py
file, we'll create a Flask app and define a route. A route is a URL path that maps to a Python function. Add the following code to the app.py
file:
This creates a Flask app and defines a route for the root URL (/
) that returns the string "Hello, World!".
To run the app, enter the following command in the terminal:
This will start the Flask development server, and you should be able to see your app by visiting http://localhost:5000
in a web browser.
Now, let's add some more routes and dynamic content to our app. In the app.py
file, add the following code:
This creates a new route that takes a dynamic parameter called name
and returns a personalized greeting. You can test this route by visiting http://localhost:5000/hello/<name>
, where <name>
is a placeholder for the actual name you want to use.
How to use HTML, CSS and JavaScript with Flask.
Next, we'll add a template to our app. Flask uses templates to generate HTML dynamically.
In the templates
directory of your project, create a file called index.html
and add the following code:
This creates a basic HTML template with a few links that you can follow.
This creates a basic HTML template with a few links that you can follow.
Finally, we'll modify our hello
route to use the template. Add the following code to the app.py
file:
python
flask
django
frontend
backend