How It Works (I Think)
I wrote the Dark Castle text game in order to learn some Python. I assumed it would be trivial to host my app on the web. I was wrong.
As it turns out, I've now spent nearly as much time learning about hosting a web app as I did about Python itself. One of the challenges is simply to understand how all the moving pieces work together. This changes over time so what I show here may not be true in a few years. And honestly, parts of it may be wrong right now - I'm learning as I go - so please let me know if you spot errors. With those caveats, here's how I think it works:

Dev Flow
- Files live in Working Copy iPad Git client
- Pythonista IDE and Textastic text editor update documents in Working Copy
- Working Copy updates are committed locally
- Working Copy updates are pushed to GitHub origin via Git protocol (port 9418; similar to ssh but no auth)
- From bash console, pull GitHub updates to pythonanywhere.com via Git protocol
Web Flow
- Based on the URL entered, the Client Browser hits the Web Server hosted on pythonanywhere.com. At present, NGiNX is the most popular dedicated Web Server but I have no knowledge of what Web Server pythonanywhere.com uses.
- Web Server communicates with WSGI (Web Server Gateway Interface). The WSGI is middleware that allows any WSGI-compliant Web Server to interface with any WSGI-compliant app framework. gUnicorn (Green Unicorn) is presently the most popluar dedicated WSGI but I have no knowledge of what WSGI pythonanaywhere uses.
- WSGI interfaces with Flask Micro-Framework
- Flask pulls (or creates) sessision variables stored in client side cookies; Usually just User ID but in my code all persistent variables. Also, based on the url address requested, Flask routes to the correct html generation sequence.
- Flask Micro-Framework calls Python Function. Each instance of the Python Function runs in its own memory space.
- Typically the Python Function would use a DB key stored in session variables to read and write session-specific data from a Database (e.g. SQLAlchemy) but in my code it's all in cookies
- The Python function returns varables to Flask
- Flask updates persistent session variables and stores them in the browser cookie
- Flask passes the data returned by thge Python Function to the Jinja2 Template Engine
- Jinja2 merges the Python Function data with the HTML Templates in /templates (e.g. base.html, index.html) to produce the Custom HTML Web Page (Note: the /templates location is defined by Flask which is tightly integrated with Jinja2)
- Jinja2 returns the Custom HTML Web Page to Flask
- The Custom HTML Web Page is sent by Flask to the WSGI
- The WSGI passes the Custom HTML Web Page to the Web Server
- The Web Server applies static content (e.g. CSS styling and images) to the Custom HTML Web Page (Note: the /static location is defined by Flask but I believe it is applied to the Custome HTML Page by the Web Server)
- The Web Server returns the Fully Formatted Web Page to the Client Browser
Diagram source found here
- Tom