adding a favorite icon to the top level Jinja2 template, in vscode

Python in a Flask

Kirby Urner
6 min readDec 26, 2018

--

Actually the “Flask” is in Python (a language), and the metaphor relates to the tiny swigs one takes from a flask, getting the most from the least.

For those just joining the Python subculture, the story is: because the language is so friendly, many new web frameworks grew up, including the famous Zope and Plone lineage, not SQL based.

Ruby on Rails was creaming Python at the box office back in those days, as the latter as yet had no flagship… until Django came along. A newspaper in Lawrence, Kansas decided to open source its solution, which became an overnight sensation.

Other Python frameworks were not driven out of business, as what does that even mean in the world of open source? Some might have been more sidelined, but then if they work as advertised at some archeological layer, it’s not like they’ll rust, as long as there’s an underlying Python 2.7 interpreter available (the classic golden oldie).

I use thekirbster.pythonanywhere.com to warehouse a simple teaching website built with Flask, and based on looking up stuff using SQL against flat file databases. It currently shares about chemical elements (a periodic table), geek terms, and a few shapes (polyhedrons).

Shapes could easily go more relational and in other demonstrations of mine, it has, meaning a set of tables make it possible to extract the XYZ coordinates, and perhaps the quadray coordinates, of all the vertexes of a particular shape.

The Polyhedrons table has Tetrahedron as a single row, which points to a tuple of face tuples, which in turn consist of vertexes in a cycle. The faces and vertexes each get their own table.

Starting with a Tetrahedron, one SQL SELECT statement might retrieve all the information necessary for a ray-tracer such as POV-Ray to actually draw the shape on screen.

Orientation preserving rotations may be expressed as permutations, also studied in Group Theory. My Oregon Curriculum Network integrates all of these topics under the heading of Digital Mathematics.

labeled vertexes connecting edges into faces into polyhedrons

I have a similar demo web application in Django, on c9.io, which uses Amazon web services. This was a direction I was suggesting for one of the schools I’ve previously worked for, with aspirations for global reach.

In any case, my Cloud9 Django application, similar to this one in Flask, is not “always up” the way thekirbster is. I tend to boot it just before class, me the teacher.

Once you get to A RESTful Flask Application, you’ll see it links through to source code at the actual Github site I pull code from when I’m ready to put my changes into production.

For example, today on Christmas Day itself, I was doing some recreational ornamentation, decorating my application with a small flask, as we see in chemistry, both an allusion to Flask and to the periodic table.

I got the PNG and ICO files from a public website, and then discovered the ICO file (Windows icon) was 32x32, not regulation 16x16. On my browser, a black rectangle appeared, in place of a flask. That changed when I switched attention to the PNG.

the icon, blown up to 512x512

The application source code under the hood is not the most sophisticated. I continue hacking on it, when not sharing it in teaching. The shapes database more overlaps the other work I’m doing in Python, using Jupyter Notebook technology.

My advice is to consider doing something similar as your “business card” to the source code readers: establish a free open account on Github, push to it from your development platform, pull from it to your production website, which need not be complicated. You’re simply sharing some angle of interest.

If you don’t wish to share source code, of course that’s up to you.

Then leverage your Github presence with Jupyter Notebook work.

Github has a decent native Notebook rendering capability. Embedded Youtubes won’t be streamable. I’ve used notebook-embedded links to an nbviewer view of the same URL as a workaround in some cases, if the Youtubes are that important.

Think of my advice as coaching someone wishing to be “well rounded” as to how their Python is applied. A web developer likely uses an IDE, knows some JavaScript, HTML, CSS, and has Python sitting on a back end somewhere, poised to handle the incoming HTTP requests.

However, the Jupyter Notebook creator is a different type of professional. The Notebook is already a web page, with a live connection to a kernel.

You might be an economist, a stock market forecaster, a data scientist.

You may still use an IDE but in the Notebook you just import the source, and showcase the API only. A Jupyter Notebook is more likely to be a scientific paper with interactive exhibits.

So why not leverage your Python to explore both forks of the river, both branches of the tree? Here’s an old sketch from my teaching notes, giving the flavor.

Forks in the Tree

If you already know Python, then what you might consider somewhat pathological about my Flask application, is my use of the context manager construct in the imported connectors2 module, ancillary to the top level flask_app.

Since opening files using the keyword “with” is already considered the most proper, it makes oodles of sense to use “with” when opening databases also, and the end of the indented block, a cue to close it again (as is done with ordinary files).

However, in my eagerness to teach higher Python, I forgo using a simple __enter__ and __exit__ pattern in my connector classes (what the keyword “with” and indented block completion ordinarily trigger). I instead decorate a generator with @contextmanager and carry out entering and exiting duties before and after a single yield statement.

The Standard Library includes the contextlib module, buried in which is this way to transform what we call a generator function (uses yield) into the kind of thing the keyword with is meant to trigger. I mark this coming together of concepts as a kind of summit in my forty hours course. Expect an epiphany regarding what is meant by “pythonic” perhaps.

I invite you to eyeball the source awhile to figure out what that means exactly. Check out the website for real maybe?

connecting to SQLite from Python 3

I’ll feed an instance of elemsDB to the Connector generator, initializing an elemsDB instance with a path to the corresponding SQLite database. The resulting object, dbx, is meanwhile endowed with both connection and cursor objects, both needed by the DB API to accomplish CRUD operations. We use Python’s way of talking SQL to curs.execute( ) the various Create Retrieve Update Delete operations our website users require (and are authorized to commit).

That’s a big mouthful of jargon, which might make no sense at the moment. The point is to see how a “deep dive” into the world of website development might be accomplished through a Pythonic window.

The Python programming language provides a pathway into the inner workings of contemporary technologies. Once you know your way around, you’ll have an easier time transitioning to other languages and technologies. Python was a skeleton key that opened many doors.

--

--