In the world of web development, Python is one of the most popular programming languages. It is known for its simplicity, ease of use, and ability to integrate well with other technologies. Flask is a web framework that uses Python to build web applications. In this tutorial, we will show you how to create a web application using Python and Flask.
Introduction
Before we start, make sure you have Python and Flask installed on your system. If you haven’t already installed them, you can do so by following the instructions on their respective websites.
Flask is a lightweight web framework that allows you to easily build web applications. It provides a simple way to handle HTTP requests and responses and allows you to create web applications quickly and efficiently. Flask is also extensible, which means you can add functionality to your web application as needed.
Let’s get started with building a web application using Python and Flask.
Creating a Flask Application
To start, create a new directory for your application. In this directory, create a new file called app.py. This file will contain the code for your Flask application. Open the file in your text editor and add the following code:
from flask import Flask app = Flask(__name__) @app.route('/') def index(): return 'Hello, World!'
In this code, we import the Flask module and create a new Flask application instance called app. We then define a route for the index page using the @app.route decorator. This decorator tells Flask that the index function should be called when someone visits the root URL of the web application.
In our index function, we simply return the string “Hello, World!”. This will be the content that is displayed when someone visits the root URL of our web application.
Running the Flask Application
To run the Flask application, save the app.py file and open a command prompt or terminal window. Navigate to the directory where your app.py file is located and type the following command:
python app.py
This will start the Flask development server and your application will be available at http://localhost:5000/. Open your web browser and navigate to this URL to see your web application in action.
Adding HTML Templates
Our application is currently returning a simple string as a response. To make our application more dynamic, we can use HTML templates to generate our web pages. Flask uses the Jinja2 template engine to render templates.
To use a template, create a new directory called templates in your application directory. In this directory, create a new file called index.html:
Hello, World! {{ message }}
In this template, we define an HTML page with a title and a body. We also include a placeholder for our message using Jinja2 syntax. The message variable will be passed to the template by our Python code.
To use this template in our application, we need to modify our index function in app.py:
from flask import Flask, render_template app = Flask(__name__) @app.route('/') def index(): message = "Hello, World!" return render_template('index.html', message=message)
In this modified code, we import the render_template function from Flask and add a new variable called message to our index function. We then use the render_template function to render our index.html template and pass in the message variable.
Now when we visit our web application in our browser, we will see the message “Hello, World!” rendered using our HTML template.
In this tutorial, we showed you how to create a web application using Python and Flask. We covered the basics of Flask application development, including creating a Flask application, defining routes, returning responses, and using HTML templates. We hope this tutorial has given you a good foundation to start building web applications using Python and Flask.
Want to learn more about Python, checkout the Python Official Documentation for detail.