Create Python Website- Get Started with Flask

Python is being widely used to develop impressive web applications. Making a website with python can be easily done with Flask.  Flask is a micro-framework.  It provides with a very minimal structure for creating a web application. Such an application can be extended by importing required packages or modules as required. In this tutorial you will learn to Create Python Website using Flask. It’s a first step towards build website with python.

Prerequisite for this tutorial is basic knowledge of HTML, CSS and Python.

Step-1 Create a Virtual Environment for a Python Website

In the first step you will prepare the virtual development environment for your web application. It will be constituted of a group of packages/ libraries that are required by the current application. This eliminates interference of shared libraries among multiple projects. Check this out for details and requirements of your OS/system.

  1. Create the project folder  and open that folder in command window
f:\flask\myproject\ 
  • Run this command to create the virtual environment.  After the command is executed, Check that env folder will appear in your application folder
py –m venv env
  • Execute this command to activate the environment
env\scripts\activate. 

With this the command prompt of will change to (env) f:\flask\myproject\

Step-2 Install Flask

 Run pip command to install Flask (you have to be connected with internet while doing this).

On the command prompt available after 3rd sub-step of the first step, execute this command (for windows)

Pip install flask

After this step you will be able to see the following dependencies in your application folder under F:\Flask\myproject\env\Lib\site-packages

  • Werkzeug – WSGI – Python interface between applications and servers.
  • Jinja – template language for rendering pages requested by users.
  • MarkupSafe to protect your application against SQL injection threats.
  • ItsDangerous –for  integrity and protecting session cookie.
  • Click- framework for coding command line applications. It helps in executing the flask commands on command line

Note-do not delete anything from these

Now you  are ready to write your first Flask-Python code

Step-3 Create a basic Flask application

Create a file named application.py in your application folder(for this example f:\flask\myproject) and copy the following code

# import the flask class
from flask import Flask 
# create an instance of the flask class 
app=Flask(__name__)
# define the route() decorator to link with a valid URL in the application
@app.route('/') 
# define a function that is triggered when this URL appears in the browser address bar
def index(): 
  # return something when this function is called 
    return "Welcome to your Python Flask Website" 

Step-4 Set FLASK_APP Environment Variable

Set the FLASK_APP Environment Variable with the name of the python file you have just created as your application file. This will define entry point of your web application

(env) f:\flask\myproject\Set FLASK_APP= application.py

Step-5 Run Flask

Execute this command to start the application.

(env) f:\flask\myproject\flask run

Your Flask application will start. You will get a URL as seen in the last line in below image. Copy it and paste in your browser.

Output in Browser

Create Python Website- output in browser

Note: Each time you make changes in your Web Application you need to stop it and start again to see the result. Press CTRL+C for quitting flask application. Execute flask run command again. The command window will remain open when you are running the application.

Be First to Comment

    Leave a Reply

    Your email address will not be published. Required fields are marked *