top of page

Flask Project Help | Creating Flask App Using Virtual Environment

First, open the command prompt and go to the directory in which you want to create a flask app.

Here we go to our project directory "C:\Users\Admin\FlaskProjects", and then apply below command to create a virtual environment.

Before start first you sure that python 3.X in installed or not if not then installed.

Create a directory where you want to project live:

C:\Users\Admin\FlaskProjects\mkdir site1

C:\Users\Admin\FlaskProjects\cd site1

Now creating the virtual environment

C:\Users\Admin\FlaskProjects\site1\python3 -m venv venv

you can create a virtual environment with the following command:

C:\Users\Admin\FlaskProjects\site1\virtualenv venv

Now you can go to the venv directory and inside the script, you can activate the virtual environment(C:\Users\Admin\FlaskProjects\site1\venv\Scripts)


Press Enter now your virtual environment activated, see below

Virtual environment activate

(venv) C:\Users\Admin\FlaskProjects\site1\venv\Scripts>

After this install the flask

(venv) C:\Users\Admin\FlaskProjects\site1\venv\Scripts>pip install flask

Now creating a package which name is "app", it uses to execute your project

Now go to the app directory and here u create two python files: and

Write below code in this file



from flask import Flask

app = Flask(__name__)

from app import routes


After this open the “” file and write below code inside it.



from app importapp



def index():

return "Hello, World!"


After this create another python file “” where app folder exitst.

It used to store application instance with single line code


from app importapp


Here Screenshots of file locations:

Now come into the Script directory and type below command to run the app:

Now go to the Script dir and run below command:

(venv) C:\Users\Admin\FlaskProjects\site1\venv\Scripts> set

This is used to set the flask app

Now run it using below command:

(venv) C:\Users\Admin\FlaskProjects\site1\venv\Scripts>flask run

*Serving Flask app ""

* Environment: production

WARNING: This is a development server. Do not use it in a production deployment.

Use a production WSGI server instead.

* Debug mode: off

* Running on (Press CTRL+C to quit)

Good your app is ready and run. Copy url and paste it into our browser.

See below result on the browser:

Adding "Templates" files(html, css, etc)

Now create “templates” dir where and files

Back to the app dir and ceate folder templates

(venv) C:\Users\Admin\FlaskProjects\site1\venv\Scripts\app\mkdir templates

Creating the index.html file and inside it write below code:



                 <title>{{ title }} – site1</title>

      <h1>Hello, {{ user.username }}!</h1>


Now edit file and add below code:


from flask importrender_template

from app import app



def index():

user = {'username': 'naveen'}

return render_template('index.html',title='Home', user=user)


To render the template I had to import a function that comes with the Flask framework called render_template(). This function takes a template filename and a variable list of template arguments and returns the same template, but with all the placeholders in it replaced with actual values.

The render_template() function invokes the Jinja2 template engine that comes bundled with the Flask framework. Jinja2 substitutes {{ ... }} blocks with the corresponding values, given by the arguments provided in the render_template() call.

Template Inheritance

In this we use the concept of inheritance, use one template file content in another by using "extend", keyword as per given below:

app/templates/base.html: Base template with navigation bar


             {% if title %}
             <title>{{ title }} – site1</title>
             {% else %}
             <title>Welcome to site1</title>
             {% endif %}
               <div>site1: <a href="/index">Home</a></div>
               {% block content %}{% endblock %}


Now using base.html content in index.html using below:

app/templates/index.html: Inherit from base template


{% extends "base.html" %}

{% block content %}

<h1>Hi, {{ user.username }}!</h1>

{% for post in posts %}

<div><p>{{ }} says: <b>{{post.body }}</b></p></div>

{% endfor %}

{% endblock %}


Thanks for reading our flask blog, I hope it may help for creating flask help. If you need any help related to flask projects then contact us.

4 views0 comments

Recent Posts

See All


bottom of page