[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:


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.


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


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

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


HTML views

JSON output

flask_app.py  <--- uses flask (conda install flask)

Databases (SQLite)
glossary.db   <--- these may need to go in home dir

Jinja2 templates

import requests

data = {}
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)

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")



# -*- coding: utf-8 -*-
Created on Tue Aug 2, 2016

Course: PYT-PR (Saisoft.net)
Session 09
Kirby Urner

kirby.urner at gmail.com

Audio Check (6:15 PM PDT)

  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