I did some digging into how serving files directly from github works. Hope this info helps others.
This follows up on another recent thread, but is distinct enough for its own post.
Problem
Let's say we want to render / run a file on github. It could be html, or javascript, or another format. How can we do that?
As an example, take www/tests/test_dict.py in the brython repo. There are two urls to access it:
The view url is for viewing the file source in your browser. It converts the file contents to an html table with line numbers and syntax highlighting, surrounded by the github interface. This is not suitable for serving js or brython code for execution: your browser sees an ordinary html page, not the code itself.
The raw url serves what you need to execute: just the contents of the file itself. However your browser won't render or execute it, because github serves the file as Content-type: text/plain. Your browser will only display the file text contents, not interpret them as html or run them as js. This is billed as a "security" feature, though it's actual security value is questionable. (Not sure if brython follows the same rule as browsers and refuses to run a python script served as text/plain - my guess is it will but haven't tested it).
So your browser won't render or run any content served from a github raw url. The contents are suitable, but the HTTP Content-type header is not.
Solution
There are several ways around this: configure your browser to ignore Content-type, run an http proxy to change the content type header, use a browser extension to do the same, write a script to ajax the raw contents and dynamically parse them, etc. But most of these solutions only work for a single user.
It turns out there are several websites that solve this problem. They simply proxy content from github and change the content-type header to the correct value (text/html, text/javascript, application/javascript, etc). One such site is
githack.com.
If your github files use relative paths that match your github directory structure, you can simply access the files through
githack.com instead of
github.com and they will run correctly. I've tested this and it works.
Voila, the page renders and the code runs. Brython modules in /www/tests/ are imported correctly.
Limitations
Now there are some things that don't work, because the paths aren't setup correctly. Importing the foobar module fails, because it lives in a path (www/src/Lib/site-packages/) that isn't known to the brython interpreter. This could be fixed by passing it in the
pythonpath argument to the brython() interpreter when it's invoked in index.html.
I'm not suggesting you should run brython code this way. Setting up your source files with all relative paths may be inconvenient. Absolute paths like /css/header.css will be different in your repo than on your server, due to the extra project and branch name dirs that githack needs, not to mention your web server root probably being different from your git tree root.
Lessons
My takeaway points are:
- hosting from github directly can work, if you really want it to and set things up properly
- understanding how to make it work requires a deeper understanding of how web applications work, which is helpful for any deployment whether using github or not.