- Flask app which displays Twitter hashtag link list in browser.
- Python 3, Flask, Jinja
- Linux 5.3.0
- Ubuntu 19.10 (Eoan Ermine)
- Python 3.7.5
- GNOME Shell 3.34.3
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 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$
From the app's root directory, enter the following args in a Unix-like terminal emulator:
$ . flask_run.sh
$ . flask_run.sh
is the same as $ source flask_run.sh
.
Then open http://127.0.0.1:5000/popular
or http://127.0.0.1:5000/crypto
in a web browser.
Permissions may need to be adjusted.
export FLASK_ENV=development
export FLASK_APP=memelinks.py
flask run
export FLASK_ENV=development
tells Flask to run the app in development mode which includes a reloader and a debugger.export FLASK_APP=memelinks
tells Flask which app to run.flask run
starts the app.
#! /bin/bash
When a text file with a shebang is used as if it is an executable in a Unix-like operating system, the program loader mechanism parses the rest of the file's initial line as an interpreter directive. The loader executes the specified interpreter program, passing to it as an argument the path that was initially used when attempting to run the script, so that the program may use the file as input data.
In other words, after the shebang (#!
), the program loader parses the interpreter directive (/bin/python3
) as a Unix argument.
from flask import Flask
from flask import render_template
Flask is a lightweight WSGI web application framework. It is designed to make getting started quick and easy, with the ability to scale up to complex applications. It began as a simple wrapper around Werkzeug and Jinja and has become one of the most popular Python web application frameworks.
[render_template] Renders a template from the template folder with the given [
hashtags
andklinks
].
APP = Flask(__name__)
POP_HASHTAGS = [
'Medicaid', 'Ethereum', 'Crypto', 'Crowdfunding', 'Giveaway', 'TBT',
'Contest', 'BlackHistoryMonth', 'WomensHistoryMonth', 'Cryptocurrency',
'WomensDay', 'HappyBirthday', 'Authentication', 'Win', 'Medicare',
'InternationalWomensDay', 'InfluencerMarketing', 'Opioid',
'HealthInsurance', 'QA', 'Funny', 'WomenInSTEM', 'IWD2019',
'Photography', 'MondayMotivation', 'OOTD', 'Vegan', 'TravelTuesday']
POP_DICT = {}
APP = Flask(__name__)
initializes the Flask instance.POP_HASHTAGS
contains a list of Twitter hashtag strings, which will be passed to the Jinja template.POP_DICT
is a dict populated by the looping structure.
@APP.route('/')
def memelinks():
'''Displays list of Twitter hashtag links in browser.'''
for hashtag in HASHTAGS:
KLINKS[hashtag] = f'https://twitter.com/search?q=%23{hashtag}&f=live'
return render_template('template.htm', hashtags=HASHTAGS, klinks=KLINKS)
memelinks()
is the memelinks module's method which displays a list of Twitter hashtag links in the browser.@APP.route('/')
is a decorator which modifiesmemelinks()
, setting its route as the root directory.for
loop iterates through the iterableHASHTAGS
list, as hashtag strings and URL addresses are appended to theKLINKS
dictionary as key/value pairs.- finally,
render_template()
renderstemplate.htm
, passing ithashtags
andklinks
as data.
Flask searches for Jinja templates in the /templates
directory.
$ ls templates/
crypto.htm pop.htm
Modify hashtag lists to suit specific engagements campaigns.
Two looping structures generate the crypto links; one for each prefix involved: $
and #
.