[Edu-sig] rounding out a Python course...
kirby urner
kirby.urner at gmail.com
Wed Aug 3 13:13:26 EDT 2016
Having explored Jupyter Notebooks, our next task
was to dissect a simple Flask application, with an
API published here:
http://thekirbster.pythonanywhere.com
The goal with my campers is to get them used to
self-hosting, serving solo, with a server process on
localhost, then deploy, most straightforwardly from
Anaconda Spyder to Pythonanywhere in my experience,
though I also enjoy Eclipse, Pycharm and other IDEs.
https://flic.kr/p/EebhmS
When working with Python beginners, I don't insist on
bringing in Github or any version control right away.
Unittesting with Pyunit, yes, we got to that (in a Jupyter
Notebook):
http://nbviewer.jupyter.org/github/4dsolutions/Python5/blob/master/Atoms%20in%20Python.ipynb
Shortened: http://goo.gl/nek3Uf
But actively using Github, in a 40 hour course on Python,
is not required.
Pythonanywhere lets us copy files from localhost to the
server with a capable GUI all its own.
My Flask application carries on with the Periodic Table
of Elements, fleshed out as lists, tuples, namedTuples,
then as a class (with __getitem__ to mirror namedTuple
behavior).
We study I/O in terms of reading/writing JSON files, with
a quick look at pickle (just for contrast, of what it's like to
archive full Python objects).
I emphasize the security aspects of JSON: in being
so low level, it's not about sneaking in runnable code.
The advantage of stretching to Flask, is that whereas
all the running code the students look at is pure Python,
I show them where the JavaScript would go, in
/static/js, to be imported by the html5 / jinja2 templates
(as well as the css).
PythonAnywhere is already set up to host Flask
applications. They only take minutes to set up. One
of my students had an application going within the
course of the 3-lab session.
I told the students it was OK to mess up the periodic
table with bogus data and indeed one of the elements
has 1009 protons I think it is.
We were able to POST to the SQLite database with
code this simple (you're welcome to try, just for fun (the
data may go away when I swap in a new copy, or I may
change the "secret security word" -- there's no login).
It won't accept elements already posted.
Simple client (programmaticly POST, no actual browser
form used).
# -*- coding: utf-8 -*-
"""
Created on Fri Jul 29 17:35:58 2016
@author: Kirby Urner
Post a chemical element to the Periodic Table.
Goes into sqlite DB Periodic_table.Elements
API:
HTML views
/elements
/elements/H
/elements/Si
...
/elements/all
JSON output
/api/elements?elem=H
/api/elements?elem=O
...
/api/elements?elem=all
Requires:
flask_app.py <--- uses flask (conda install flask)
connector.py
Databases (SQLite)
glossary.db <--- these may need to go in home dir
periodic_table.db
Jinja2 templates
/templates/
home.html
elements.html
elem_page.html
all_elems.html
glossary.html
"""
import requests
data = {}
data["protons"]=81
data["symbol"]="Tl"
data["long_name"]="Thallium"
data["mass"]=204.3833
data["series"]="Post-transition metal"
data["secret"]="DADA" # <--- primitive authentication
# the_url = 'http://localhost:5000/api/elements'
the_url = 'http://thekirbster.pythonanywhere.com/api/elements'
r = requests.post(the_url, data=data)
print(r.status_code)
print(r.content)
PS: here's from my review of decorators from last
night, written live while I chatted:
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 2 20:58:29 2016
@author: kurner
"""
def UFO(arg):
def eater(f):
f.note = arg
return f
return eater
@UFO("I've been abucted")
def innocent_bystander():
print("Hello world")
print(innocent_bystander())
print(innocent_bystander.note)
====
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 2, 2016
Course: PYT-PR (Saisoft.net)
Session 09
Kirby Urner
Instructor:
kirby.urner at gmail.com
@thekirbster
Audio Check (6:15 PM PDT)
Introduction:
Python in the Ecosystem (continued)
PythonAnywhere + Github + Flask
Jupyter Notebooks
Batteries Included: SQLite (sqlite3)
Web Dev: Dissecting a Flask application
LAB 1: get a Flask application up & running on localhost
Reviewing Decorators
LAB 2: use Flask client to add to Periodic_table.db
Another look at Pythons "scaffolding" (__rib__ cage):
The Permutation class ("word scrambles")
(more concepts through Cryptography & Group Theory)
LAB 3: doodle period: encode and decode various phrases
Play with web app more. Pythonanywhere anyone?
Summary of Session 09
"""
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/edu-sig/attachments/20160803/e2374e69/attachment.html>
More information about the Edu-sig
mailing list