Python-Flask/C2/Installation-of-Python-Flask/English

From Script | Spoken-Tutorial
Revision as of 02:02, 26 December 2018 by SiddharthaSarkar (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Title of script: Installation of Python Flask

Author: Siddhartha Sarkar

Keywords: ​Video tutorial, Python Flask, Installation, Virtual Environment.


Visual Cue Narration
Slide 1: Installation of Python Flask Welcome to the spoken tutorial on Installation of Python Flask.
Slide 2: Learning Objectives In this tutorial we will learn to
  • Create a Virtual Environment.
  • Activate and deactivate the Virtual Environment.
  • Install packages inside the Virtual Environment.
  • And launch a Flask application.


Slides 3: System Requirements To record this tutorial, I’m using
  • Ubuntu Linux 16.04 OS
  • Python 3.5.2
  • Atom text editor 1.22.1
  • Firefox web browser and
  • A working Internet connection.


You can use any text editor and web browser of your choice.

Slide 5:

Pre-requisites


To follow this tutorial, you should have working knowledge on
  • Linux commands and
  • Basic Python programming


If not, then please go through the corresponding tutorials on this website.

Slide 6: Virtual Environment
  • A virtual environment is a self-contained directory tree.
  • It contains a Python installation for a particular version of Python.
  • Additional packages can be installed inside the virtual environment.


Slide 6: Virtual Environment
(pronounce as v-e-n-v)
  • Venv is the short form for virtual environment.
  • Venv is used to avoid conflict between different Python installations.


Slide 7: Virtual Environment
  • For every project, we can have a separate Venv.
  • We will install Venv via a package called pip.


Slide 8: Pip
  • Pip is a package manager program.
  • It is used to install, upgrade, and remove Python packages.
  • Pip3 is the Python3 version of pip


Slide: Pip3 installation
  • To check if pip3 is already installed, type the following command in the terminal:


pip3 <space> hyphen hyphen version and press Enter.*
If it gives a version number, then you don’t need to install it again.
  • Else, install pip3 using the following command in the terminal:


sudo <space> apt <space> install <space> python3-pip

Open the terminal Open the terminal by pressing Ctrl, Alt and T keys together.
Type pip3 --version [Enter] Let us check the version of pip.


Type pip3 <space> hyphen hyphen version and press Enter.

Point to pip version

You should be able to see some valid pip3 version number.


In my case it is pip 8.1.1 for python 3.5.

(pronounce as virtual-e-n-v). Next let us install Virtualenv.
Type

$ sudo pip3 install virtualenv [Enter]

Type sudo <space> pip3 <space> install <space> virtualenv


And press Enter.

Only narration Next, I will create a folder named project_flask.


This folder will help me to organize my files in a single place.

[Terminal]


Type mkdir project_flask [Enter]

To do so, type mkdir <space> project underscore flask and press Enter.
[Terminal]


Type cd project_flask [Enter]

Then, type cd <space> project underscore flask and press Enter.
Only narration Now let us create a new virtual environment inside our newly created directory.
[Terminal]

Typing

$ virtualenv -p python3 flask_venv

So type

virtualenv <space> <hyphen> p <space> python3 <space> flask_venv


This will create a virtual environment named flask_venv.


You may choose any other name for the virtual environment.


The hyphen p flag is used to specify which python interpreter is to be used.

[Enter] Now press Enter.
[Terminal]

Typing


$ virtualenv -h [Enter]

For help, type the following command in the terminal

virtualenv -h and press Enter.


Here, we can see the list of available commands and options.

Highlight -p and its description. Observe that -p is used to specify the version of the python interpreter to be used.
[Terminal]

Type $ ls [Enter]

Type ls and press Enter.


Note that we have a folder named flask_venv in our current directory.

[Terminal]

Type


$ ls flask_venv [Enter]

Now, let us see the contents of flask_venv directory.


Type ls <space> flask underscore venv


Press Enter.

[Terminal]

Point to the list of directories in the terminal

Observe that there are a number of files and directories inside flask_venv.
[Terminal] Point to bin bin​ contains the executable files.
[Terminal] Point to lib lib​ contains supporting library files.
[Terminal] Point to site-packages Packages ​installed in this environment will appear in

site-packages.

[Terminal] Point to

lib/ python3.5 directory

site-packages can be found ​inside lib slash python3.5 directory.
Next, we have to activate this virtual environment.


Activate means, from now on all the python commands will run inside this venv.

[Terminal]

Typing

$ . flask_venv/bin/activate


To do that, we have to run the activate executable from the bin directory.


Type dot <space> flask <underscore> venv slash bin slash activate


Press Enter.

The shell now uses an instance of Python from our virtual environment.
[Terminal]

Highlight

(flask_env)

Once activated, notice the change in the terminal.


We can see the name of the environment flask_venv inside parentheses, in the terminal.


This indicates that we are inside an activated virtual environment called flask_venv.

[Terminal]

Type

$ python3 -V

We should now make sure that we are using Python 3 inside this Virtual Environment.


For that, type python3 <space> hyphen capital V and press Enter.


In my case it shows Python 3.5.2.

[Terminal]

Typing

$ pip3 install flask

Next let us install flask inside venv.


Type pip3<space> install <space> flask and press Enter.


This will install the Flask package only for this venv and not system wide.

[Terminal]

Highlight

“...installed Flask-1.0.2 Jinja2-2.10 MarkupSafe-1.0 Werkzeug-0.14.1...”

Some associated packages like Jinja2, MarkupSafe and Werkzeug will also get installed.
[Terminal]


Type


$pip list

To confirm, whether Flask has been installed or not, type

pip <space> list


And press Enter.

[Terminal]


Highlight flask, jinja2, werkzeug.

Observe that we have flask, jinja2, werkzeug etc.


That means our Flask installation is successful.

[Terminal] Let us see the working of Flask by writing a simple ‘hello world’ app.
Only narration Let’s create a python file at the same level as flask_venv.


This is so that the flask_venv is logically separated from the hello world app.

Only narration I will be using the atom text editor.


However, you can use any text editor of your choice.

[Terminal]


$ atom hello_flask.py

Type the command

atom <space> hello underscore flask dot py

and press Enter.

A text editor called Atom will open.


This will open the file in the text editor window.

[Text Editor]


Close the default tabs.

If you open the Atom editor, for the first time you may see two default tabs.


Welcome” and “Telemetry Consent”.


Click on the close buttons next to these tabs to close them.

[Text Editor]


from flask import Flask

app = Flask(__name__)


@app.route('/')

def hello_world():

(Tab)return 'Hello, World!'

Type the code as shown here.


Which will launch a very simple built-in server.


[Text Editor] We will discuss this program and its elements in detail in later tutorials.
Save the file by pressing Ctrl+S. For now, let’s save the file by pressing Ctrl and S keys together.
Click close button. Then close the editor by clicking on the close button at the top left corner of the window.
[Terminal]

Type


$ export FLASK_APP=hello_flask.py

Come back to the terminal, and type

export <space> FLASK_APP <equal to> hello underscore flask dot py


And press Enter.

Only narration. This command sets an environment variable named FLASK_APP with the python file-name.


The FLASK_APP variable specifies the module to import while running our app.


This tells the terminal which flask app to work with.

[Terminal]


Type

$ echo $FLASK_APP

It is possible to verify that we have indeed set FLASK_APP variable by the filename.


To do that type

echo <space> <dollar> FLASK underscore APP

And press Enter.

Point to the output Observe that we got the filename hello underscore flask dot py back.
[Terminal]

Type


$ python3 -m flask run








[Enter]

Now type

python3 <space> hyphen m <space> flask <space> run


This command runs the flask application named hello underscore flask.


The file to execute is the one given by the FLASK_APP environment variable.


We have already set that in the previous step.


Press Enter.

Point to the message If all goes well, you should see a message

“Running on http://127.0.0.1:5000/


This implies that the server is running locally.

[Firefox]

Type

http://127.0.0.1:5000/

And press Enter.

Now, open a web browser.


In the address bar, type http://127.0.0.1:5000/ and press Enter.

Highlight ‘Hello, World!’ We can see the message ‘Hello, World!’ written in the browser window.


Which means that Flask is working fine in the Virtual Environment.

Switch to Terminal Let us go back to the terminal.
Press Ctrl+c simultaneously To stop the server, press Ctrl and C keys simultaneously.
[Terminal]

Type deactivate and press Enter

When the Virtual Environment is not in use, we can deactivate it.


To do so, type deactivate and press Enter.

[Terminal]

Point the shell prompt

We can see that the flask_venv name disappears from the command prompt.
With this, we come to the end of this tutorial.


Let us summarize.

Slide 9: Summary In this tutorial we learnt to
  • Create a Virtual Environment.
  • Activate and deactivate the Virtual Environment.
  • Install Flask and
  • Launch “Hello world” in Flask.


Slide: About Spoken Tutorial project The video at the following link summarises the Spoken Tutorial project.


Please download and watch it.

Slide:

Spoken Tutorial workshops

The Spoken Tutorial Project team conducts workshops and gives certificates.


For more details, please write to us.

Slide: Forum for specific questions:
  • Do you have questions in this Spoken Tutorial?
  • Please visit this site
  • Choose the minute and second where you have the question.
  • Explain your question briefly.
  • Someone from our team will answer them.


Slide: Forum for specific questions:
  • The Spoken Tutorial forum is for specific questions on this tutorial.
  • Please do not post unrelated and general questions on them.
  • This will help reduce the clutter.
  • With less clutter, we can use these discussion as instructional material.


Slide: Acknowledgement Spoken Tutorial Project is funded by NMEICT, MHRD, Government of India.


More information on this mission is available at this link.

This is Siddhartha Sarkar signing off. Thanks for watching.

Contributors and Content Editors

Pravin1389, SiddharthaSarkar