Web Hosting using Python
By: Ayush Kalla
Tags: Flask, Python, HTML

Python is a general purpose, high level programming language with increasing audience and most popular in the field of Data Science due to its packages available for Machine Learning and Deep Learning, however Python has some packages that are very useful in the field of web development. Flask is one such Python package used to host websites.

What can Flask do???

Host a/group of html templates on a local server. Integrate your website with CSS/JS and Databases.

What Flask can’t do???

Write the Website for you.

While it is true that you can basically do anything using Python, the most common way to develop websites is still HTML/CSS and JS. Flask can only render the HTML code in the Python application, it can not enable you to create a website using Python (traditionally at least).

After having a brief idea about Flask, lets start using Flask by creating a Hello World page and hosting it using Flask.

Install the dependencies

First things first, we will install the dependencies that will enable us to run flask on our local computer.

You need to have Python and Pip on your system. To install Python go to your terminal and write:

2. After you have Python installed, you need to get pip. Pip is a package manager that would enable us to install Flask. To install pip, go to your terminal and type.

3. After having Python and Pip installed on our system, we need to get Flask. To do this, go to the terminal and execute the command

Let’s start Coding!!!!

Once we have all the dependencies set up, we are ready to make our first flask application. We will start by making a really basic application that will greet us with ‘Hello World’ on the homepage.

This 10 lines of code is enough to get you started with your first Hello World Flask application. We will have a detailed look at the code later in this article. For now let’s run the application.

Running the Flask Application

To run the application, go to the folder containing the Python file via terminal and execute:

Note: Here, the name of my file is 1.py. Your file can have a different name than mine.

2. Once you execute the command, your terminal should look something like this:

3. This tells us that the flask application is running successfully. The default port for flask is 5000. So to check the website you need to go to

localhost:5000. In your browser tab type localhost:5000 and you will see your application running.

Congrats, you have successfully created your first flask application.

Let’s take a detailed look at the code.

1.

This is used to import the Flask module that we downloaded earlier in our Python file.

2.

This line defines app as the flask application. The __name__ argument in the Flask is used to help determine Flask the route of the file.

3.

@app.route is used to determine the structure of the web application i.e how different links will the structured in the web application. Here (‘/’) is used to determine the homepage of our web application. A web application having multiple pages will have multiple different routes.

Line 5 is the function that gets executed once the user reaches the page mentioned in @app.route (the homepage in our case).

Line 6 is the content of the index function. It is the body of our web page and is reflected by the return statement.

4.

Line 9 is like dependencies in Flask and are always included in every Flask application, if the application is being run on your server locally. This line of code ensures that the server runs only if this application is being run directly and not using any import statements.

Line 10 actually runs the application once it is created, hence these 2 lines always go to the bottom of the page. debug is an argument for app.run. This enables us to make changes to the HTML/CSS or the Flask application without restarting the server to see the results. This is usually used in development phase to make things easier for developers.

Thanks for Reading

In the coming publications we will cover how to add CSS/HTML/JS to your flask application and how to connect our application to a SQL Database.

Please comment your views about the publication in the comment box.