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