top of page

Flask Project Help | Creating Flask App Using Virtual Environment

Updated: Mar 23, 2021



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)


C:\Users\Admin\FlaskProjects\site1\venv\Scripts\activate


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:


__init__.py and routes.py


__init.py

Write below code in this file


app/__init__.py:

#-----------------------

from flask import Flask

app = Flask(__name__)

from app import routes

#-----------------------


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

#------------------------------------------

app/routes.py:

from app importapp

@app.route('/')

@app.route('/index')

def index():

return "Hello, World!"

#-------------------

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

It used to store application instance with single line code



site1.py

#--------------

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 FLASK_APP=site1.py


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 "site1.py"

* 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 http://127.0.0.1:5000/ (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 __init__.py and routes.py 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:


app/templates/index.html:


#------------------------


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

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

#-------------------------


Now edit routes.py file and add below code:


#-------------------------

from flask importrender_template

from app import app

@app.route('/')

@app.route('/index')

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


#--------------------------


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

#---------------------------------


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>{{post.author.username }} 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.

Comments


bottom of page