Rocket Stove YT Iframes: Python 3, Flask app, Jinja 2 template, csv.reader()
A Python virtual environment helps prevent changes to system Python which would render an OS unstable. For example, if a Python module requires a previous version of a dependency, which system Python also uses, and then changes such an existing dependency to another version, system instability can result. So, a Python virtual environment can help contain a Python development within its own sandbox to help prevent it from knocking the swing set over or tilting the merry-go-round. For developers, virtual environments can become a system security measure of sorts.
Below, is an example of virtual environment creation:
user_foo@foo_host:~/Desktop$ python3 -m venv foo
user_foo@foo_host:~/Desktop$ cd foo
user_foo@foo_host:~/Desktop/foo$ ls -l
total 20
drwxrwxr-x 2 user_foo user_foo 4096 Jul 1 10:11 bin
drwxrwxr-x 2 user_foo user_foo 4096 Jul 1 09:30 include
drwxrwxr-x 3 user_foo user_foo 4096 Jul 1 09:30 lib
lrwxrwxrwx 1 user_foo user_foo 3 Jul 1 09:30 lib64 -> lib
-rw-rw-r-- 1 user_foo user_foo 69 Jul 1 10:10 pyvenv.cfg
drwxrwxr-x 3 user_foo user_foo 4096 Jul 1 10:10 share
$ source bin/activate
(foo) user_foo@foo_host:~/Desktop/foo$
Navigate to the root directory of rocket_stove
and run the app:
. .run.sh
#! /bin/dash
export FLASK_ENV=development
export FLASK_APP=rocket_stove
flask run
dash
is basically the same thing asbash
, as they are both Unix shells, but dash is smaller.export FLASK_ENV=development
environmental variable sets the Flask app todevelopment
mode, which includes a debugger.export FLASK_APP=rocket_stove
environmental variable tells Flask which app to run.flask run
starts the Flask app
#! /bin/python3
First of all, the Unix shebang line is ignored by the Python executable file because that line starts with a #
character which leads a comment string.
The shebang is actually a human-readable instance of a magic number in the executable file, the magic byte string being 0x23 0x21, the two-character encoding in ASCII of
#!
. This magic number is detected by the exec family of functions, which determine whether a file is a script or an executable binary. The presence of the shebang will result in the execution of the specified executable, usually an interpreter for the script's language.1
In this case, the interpreter of the script's language is /bin/python3
.
'''`rocket_stove` module contains `rocket_stove()` method which renders \
a template of YouTube links.'''
Python's interactive shell can be used to access that __doc__
string.
>>> __doc__
'`rocket_stove` module contains `rocket_stove()` method which renders a template of YouTube links.'
This documentation is used to describe the module.
The line break character \
can be added to restrict a newline character from appearing in the doc string.
from csv import reader
from flask import Flask
from flask import render_template
- csv.reader() returns a reader object assigned to
_reader
which iterates over records incsv/rocket_stove.csv
. - A Flask app is an instance of the Flask class. Everything about the application, such as configuration and URLs, will be registered with Flask.
flask.render_template()
is used to render a Jinja 2 template.
APP = Flask(__name__) # initialize Flask instance
VIDEO_TITLE = [] # list of video titles
URL_PROF_MAT = {} # dict values: URL, profile name, stove material
APP = Flask(__name__)
initializes the Flask instance.VIDEO_TITLE = []
initializes the list where video titles will be appended.URL_PROF_MAT = {}
initializes dictionary where video title keys and URL values are assigned.
rocket_stove
module can be imported into the Python interactive shell, which enables access to the method's documentation, for example:
>>> import rocket_stove
>>> dir(rocket_stove)
['APP', 'Flask', 'URL_PROF_MAT', 'VIDEO_TITLE', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'reader', 'render_template', 'rocket_stove']
>>> rocket_stove.rocket_stove.__doc__
'`rocket_stove()` method renders list of YouTube links to a template.'
- the open() built-in function opens
csv/rocket_stove.csv
for reading in text mode with'UTF-8'
encoding, as demonstrated below:
>>> open('csv/rocket_stove.csv')
<_io.TextIOWrapper name='csv/rocket_stove.csv' mode='r' encoding='UTF-8'>
- csv.reader() returns a reader object which iterates over records in
csv/rocket_stove.csv
.
>>> _file = open('csv/rocket_stove.csv')
>>> rocket_stove.reader(_file)
<_csv.reader object at 0x7fea494d72e0>
next(_reader)
is used to skip the first row of the CSV file which is a title row, and which advances to the first record of the database in the second line. The first row of that database containsTitle,URL
.
A for statement is used to iterate through the CSV reader object:
>>> _reader = rocket_stove.reader(_file)
>>> for i in _reader:
... print(i)
...
['Title', 'URL']
['How To Make Rocket Stove', 'SYYSuHulf-4']
["World's Best Rocket Stove!! - How to Build a Simple Camp Stove", 'GMcDekiRKs8']
['5 Minute Rocket Stove', 'dwZitmJl7zY']
['How to design and make a Rocket Stove Griddle', 's_aKIs8sYPk']
['The Classic Rocket Stove Build', 'X8OOZuJcTFE']
['Hot Water Rocket Stove II - BUILD-', 'PBI4LaeKxDE']
['Hot Water Rocket Stove K Type Part 2', '7wRszNLpuc0']
['Vortex Rocket Stove Pt 1', 'UBIBW1z2gks']
['Rocket stove (Pellet; wood).3 in 1. 거꾸로 타는 난로 만들기', 'uQ2MP6ryYCw']
['Como hacer estufa Rocket. Paso a paso / Handmade rocket stove.', 'JQ8axWRoEtA']
['Homemade wood burning Rocket stove', '9-r4VLHQRlM']
['The Best Homemade Rocket Stove', 'B-9qo6P4NQU']
['로켓스토브 고기불판 (Rocket stove)', 'jjsObiA3Heg']
['立佳設計工坊 - 火箭爐 Rocket stove', 'ug7jmoHf-Bg']
['Brick Rocket Stove Build And Water Boil Test vs Stove Top', 'Z_Rozs12174']
['DIY Brick Rocket Stove - Cooking Without Electrical Power', 'onMbSLuC7Oc']
['How to build a better brick rocket stove for $10', 'r66jjYdBmg8']
['Build Your Own Brick Rocket Stove', 'GRZM8pdsjyc']
['J shaped gravity fed brick rocket stove', 'v26b3jrylEc']
['Rocket stove en briques', 'TqW5Kse3ft0']
['How To Make A Concrete And Perlite Rocket Stove', 'TYr-Uj4HqDs']
VIDEO_TITLE
is a list of all of the video titles which will also be used as keys to select the associated URLs.URL
contains key/value pairs of video titles/URLs, e.g.'How To Make Rocket Stove': ['SYYSuHulf-4']
>>> VIDEO_TITLE = []
>>> URL = {}
>>> import rocket_stove
>>> _file = open('csv/rocket_stove.csv')
>>> _reader = rocket_stove.reader(_file)
>>> for record in _reader:
... VIDEO_TITLE.append(record[0])
... URL_PROF_MAT[record[0]] = [record[1]]
...
>>> VIDEO_TITLE
['Title', 'How To Make Rocket Stove', "World's Best Rocket Stove!! - How to Build a Simple Camp Stove", '5 Minute Rocket Stove', 'How to design and make a Rocket Stove Griddle', 'The Classic Rocket Stove Build', 'Hot Water Rocket Stove II - BUILD-', 'Hot Water Rocket Stove K Type Part 2', 'Vortex Rocket Stove Pt 1', 'Rocket stove (Pellet; wood).3 in 1. 거꾸로 타는 난로 만들기', 'Como hacer estufa Rocket. Paso a paso / Handmade rocket stove.', 'Homemade wood burning Rocket stove', 'The Best Homemade Rocket Stove', '로켓스토브 고기불판 (Rocket stove)', '立佳設計工坊 - 火箭爐 Rocket stove', 'Brick Rocket Stove Build And Water Boil Test vs Stove Top', 'DIY Brick Rocket Stove - Cooking Without Electrical Power', 'How to build a better brick rocket stove for $10', 'Build Your Own Brick Rocket Stove', 'J shaped gravity fed brick rocket stove', 'Rocket stove en briques', 'How To Make A Concrete And Perlite Rocket Stove']
>>> URL_PROF_MAT
{'Title': ['URL'], 'How To Make Rocket Stove': ['SYYSuHulf-4'], "World's Best Rocket Stove!! - How to Build a Simple Camp Stove": ['GMcDekiRKs8'], '5 Minute Rocket Stove': ['dwZitmJl7zY'], 'How to design and make a Rocket Stove Griddle': ['s_aKIs8sYPk'], 'The Classic Rocket Stove Build': ['X8OOZuJcTFE'], 'Hot Water Rocket Stove II - BUILD-': ['PBI4LaeKxDE'], 'Hot Water Rocket Stove K Type Part 2': ['7wRszNLpuc0'], 'Vortex Rocket Stove Pt 1': ['UBIBW1z2gks'], 'Rocket stove (Pellet; wood).3 in 1. 거꾸로 타는 난로 만들기': ['uQ2MP6ryYCw'], 'Como hacer estufa Rocket. Paso a paso / Handmade rocket stove.': ['JQ8axWRoEtA'], 'Homemade wood burning Rocket stove': ['9-r4VLHQRlM'], 'The Best Homemade Rocket Stove': ['B-9qo6P4NQU'], '로켓스토브 고기불판 (Rocket stove)': ['jjsObiA3Heg'], '立佳設計工坊 - 火箭爐 Rocket stove': ['ug7jmoHf-Bg'], 'Brick Rocket Stove Build And Water Boil Test vs Stove Top': ['Z_Rozs12174'], 'DIY Brick Rocket Stove - Cooking Without Electrical Power': ['onMbSLuC7Oc'], 'How to build a better brick rocket stove for $10': ['r66jjYdBmg8'], 'Build Your Own Brick Rocket Stove': ['GRZM8pdsjyc'], 'J shaped gravity fed brick rocket stove': ['v26b3jrylEc'], 'Rocket stove en briques': ['TqW5Kse3ft0'], 'How To Make A Concrete And Perlite Rocket Stove': ['TYr-Uj4HqDs']}
render_template('template.htm', video_title=VIDEO_TITLE, url=URL)
renders the Jinja2 template.