26

I want to run a python file in the web I have in a GitHub repository. Is it possible to do this?

And by running in the web, I mean putting #!/usr/bin/python and print 'Content-type:text/html\n in the first two lines.

Xantium
  • 11,201
  • 10
  • 62
  • 89
brandbest1
  • 416
  • 1
  • 5
  • 11
  • 2
    this is a little unclear -- are you looking for something like [Github Pages](https://pages.github.com/)? or are you trying to use a python script to generate output that will be viewable on a web page? for the latter, you'll need to use a web server like bottle or flask – jjwon Jun 19 '14 at 00:12
  • 1
    https://github.com/iodide-project/pyodide – Asclepius Feb 01 '19 at 00:14

2 Answers2

31

In general this is not possible, Github (pages) serves only static content (ex: HTML, CSS, JS). If you want python to run (ex generate dynamic content) you need a web server capable of running python (your browser were the contents of GitHub Pages get downloaded and run can't do it).

That said there are experimental ways of running subsets of python in the browser. Take a look for a example at this question.

Community
  • 1
  • 1
elyase
  • 39,479
  • 12
  • 112
  • 119
  • And what about angular material pages? – Nabin Oct 31 '16 at 07:38
  • @NabinKhadka yes, that would be possible because angular is a frontend technology. The static code (HTML, CSS, JS) gets downloaded from GitHub to the browser and runs there. – elyase Oct 31 '16 at 14:09
  • Thank you elyase. Will dig into it. +1 – Nabin Nov 01 '16 at 02:53
  • Thank you elyase. Does this mean that in the case of GitHub pages serving static content, the browser is the HTTP server also in addition to being the client ? – tauseef_CuriousGuy Jul 09 '20 at 19:16
-2

If you truly want to generate a complete HTTP response via standard output, then start by reading about CGI and Python's standard cgi module. You'll also need to have access to a CGI-compatible web server, perhaps running on a virtual host.

However, CGI is quite obsolescent as a way to produce dynamic output for the web. @jjwon's suggestion to look at Python-based web application frameworks like Flask is a good one.

Dan Lenski
  • 76,929
  • 13
  • 76
  • 124
  • Oh yeah, I have worked with cgi before, do you know how to get it working with github? – brandbest1 Jun 19 '14 at 00:34
  • What does "get it working with github" mean? Are you trying to use GitHub's servers as your personal web server? They are unlikely to be cool with that. :-P – Dan Lenski Jun 19 '14 at 00:39
  • "CGI is quite obsolescent", sure it is, but it is fine since it follows the KISS principle. – user877329 Sep 16 '15 at 12:32
  • @user877329, I don't understand. I recommended it because it's a literal solution to the original question, but I suspect it's not **really** what the OP is looking for :-P – Dan Lenski Sep 17 '15 at 03:38