A Project for creating Python web project using the Flask
web framework in Visual Studio 2019
Introduction
In this step by step tutorial, I will demonstrate how to create a Python web project using the Flask
web framework in Visual Studio 2019.
Step 1. Open Visual Studio 2019
Click on “Create a new project” option on the right side of the window.
Figure 1
Step 2. In the top middle search box,
Search for “Python” and it will be listing all the frameworks which support
Python coding.
Now select the Flask
Web Project and click on Next button
Figure 2
Enter the Project name
and click on the “Create” button at the button.
Figure 3
Step 3. Now you can see your Project has
been created successfully.
But there will be a
warning message displayed on the top of the Visual Studio window, so now you have
to click on “Create virtual environment”,
Figure 4
Basically, it will ask
you to select the Base interpreter for your project, select accordingly and
Click on the “Create” button.
Figure 5
And it will create the
Virtual environment for your project.
Figure 6
Now you have successfully created your Python web project
using Flask Framework.
In the Solution Explorer, it will create 3 Python Code files along with
4 Html pages in the template folder automatically. You no need to create manually
for routing your application.
Views.py - Routes and views for the flask
application.
from DateTime import datetime
from flask import
render_template
from FlaskWebProject1 import app
@app.route('/')
@app.route('/home')
def home():
"""Renders
the home page."""
return render_template(
'index.html',
title='Home
Page',
year=datetime.now().year,
)
@app.route('/contact')
def contact():
"""Renders
the contact page."""
return render_template(
'contact.html',
title='Contact',
year=datetime.now().year,
message='Your
contact page.'
)
@app.route('/about')
def about():
"""Renders
the about page."""
return render_template(
'about.html',
title='About',
year=datetime.now().year,
message='Your
application description page.'
)
runserver.py - This script runs the
FlaskWebProject1 application using a development server.
from os import environ
from FlaskWebProject1 import app
if __name__ == '__main__':
HOST = environ.get('SERVER_HOST', 'localhost')
try:
PORT = int(environ.get('SERVER_PORT', '5555'))
except ValueError:
PORT = 5555
app.run(HOST, PORT)
__init__.py - The flask application package.
from flask import Flask
app = Flask(__name__)
import FlaskWebProject1.views
Example for Html pages
About.html
{%
extends "layout.html" %}
{% block
content %}
<h2>{{ title }}.</h2>
<h3>{{ message }}</h3>
<p>Use this
area to provide additional information.</p>
{%
endblock %}
Step 4. Now click on the Web Server Run
button. (Press F5)
You will be able to see the below web page on your browser.
Figure 7
Summary
In this article, I discussed how to create Python web
project using the Flask web framework in Visual Studio 2019. We also saw how we
can add a Virtual environment and we saw auto generated Python and Html code.
I hope this post will help you. Please put your feedback using comments which will help me to improve myself for the next post. If you have any doubts or queries, please ask in the comments section and if you like this post, please share it with your friends. Thanks!
0 Comments