Dark Castle

Enter the castle! Get the gold! Save the land! (But DON'T eat the biscuits)

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

  1. Files live in Working Copy iPad Git client
  2. Pythonista IDE and Textastic text editor update documents in Working Copy
  3. Working Copy updates are committed locally
  4. Working Copy updates are pushed to GitHub origin via Git protocol (port 9418; similar to ssh but no auth)
  5. From bash console, pull GitHub updates to pythonanywhere.com via Git protocol


Web Flow

  1. 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.
  2. 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.
  3. WSGI interfaces with Flask Micro-Framework
  4. 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.
  5. Flask Micro-Framework calls Python Function. Each instance of the Python Function runs in its own memory space.
  6. 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
  7. The Python function returns varables to Flask
  8. Flask updates persistent session variables and stores them in the browser cookie
  9. Flask passes the data returned by thge Python Function to the Jinja2 Template Engine
  10. 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)
  11. Jinja2 returns the Custom HTML Web Page to Flask
  12. The Custom HTML Web Page is sent by Flask to the WSGI
  13. The WSGI passes the Custom HTML Web Page to the Web Server
  14. 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)
  15. The Web Server returns the Fully Formatted Web Page to the Client Browser

Diagram source found here

- Tom