2

I'm pretty new to programming and I've been learning python in my spare time. As a challenge to myself, I created a simple text adventure for a class project. This is not for a programming class, so the professor won't know how to compile a raw Python script, let alone have a Python interpreter on their Mac.

That being said, is it possible to run python from a browser? I'm imagining some HTML file that my professor, or anyone, can click that launches a browser and they can play my game from there.

I've learned about something called Django from my research on this subject. However, I have no idea what it is, nor how to implement it. Again, I'm pretty new to programming, so if you could "explain like I'm five", that would be great.

EDIT: I found this other thread where the OP asks a similar question, but I don't fully understand the approved answer:

execution python application from browser

6
  • 2
    Macs have python preinstalled... Commented May 10, 2014 at 21:56
  • Why not build your game into a Mac app using py2app? Commented May 10, 2014 at 21:59
  • 1
    Take a look at pyjs. Commented May 11, 2014 at 2:59
  • @joemar.ct Is py2app like py2exe but for macs? I've successfully made an exe of my game already using py2exe, but then I remembered that my professor uses a Mac. Commented May 11, 2014 at 3:11
  • 1
    PythonAnywhere dev here. You could definitely run your script in a browser-based console on our site -- just upload your code, hit "save and run", and then you can even share it with your professor using a share button we have on consoles... dev here. You could definitely run your script in a browser-based console on our site -- just upload your code, hit "save and run", and then you can even share it with your professor using a share button we have on consoles... See Gary Walker's answer below. Commented May 13, 2014 at 10:37

3 Answers 3

4

Well, not really. Your basic browser generally supports 1 programming language, javascript.

However, you could use pythonanywhere" which is a hosted python environment.

You could also try skulpt which is a javacript implementation of python. I have never tried this myself.

Sign up to request clarification or add additional context in comments.

Comments

3

You can host a website on an internal network and run the program from there. Read more about the Python CGI programming here to make a form that will execute your script and print the result as a html page

For example you could have a form that will ask for input in textboxes: Name: _, Value: __, SUBMIT

After they press the button, the browser will then send a request to the python program, execute it, and display the result back to the client as a html webpage.

In addition, you do not need to install any other third-party modules if you are using a school computer. However, ask you teacher before hosting the website on the school network.

The problem is that your program is a "text adventure" which requires a lot more input/output management for a CGI program.

You can use this answer for other projects.

Anyway, here are the steps to setup the server:

1) Create a folder for your website and add a "index.html" file (it can be anything)

2) Add a favicon.ico file in the folder (this will speed up the connection) You can download this one

3) Put this python program in the folder (it will be used to host the website)

import BaseHTTPServer
import CGIHTTPServer
import cgitb; cgitb.enable()
from socket import gethostbyname, gethostname


def server(port):
    server = BaseHTTPServer.HTTPServer
    handler = CGIHTTPServer.CGIHTTPRequestHandler
    server_address = ("", port)
    httpd = server(server_address, handler)
    print "Server %s:%s started" % (gethostbyname(gethostname()), str(port))
    httpd.serve_forever()

server(4) #You can change this. It is a port number

4) Create a cgi-bin folder

5) To make the website available, execute the program created in the step 3. To stop hosting it, just close the python console.

6) While the program is running, you can go into the browser and type the IP adress : port as the URL. You will see your index.html page and favicon.ico icon. Anyone who is connected to the same network can get to the website. You and only you can also get to the website in a browser by entering http:/localhost:port with "port" being the port you've set

7) The rest you need to manage yourself. I cannot create the full script because I do not know what is in your program. Read the link provided in the beginning and modify your program to make it work in the browser.

FYI: It is possible to host more than one website or an instance of the same website at once using different ports. And, you can set and read cookies using Python CGI

Please comment if something doesn't work because of an error in my answer. I will try to fix it.

4 Comments

@aj8uppal, Thanks. I've been working with CGI programming for a while and was happy to answer the question. And all started from a simple program that I wanted to run in a browser...
I haven't tried this solution yet, but from what I can parse: this will create a webpage that I'm going to have to host myself somewhere else? Its not exactly what I'm looking for, bu I do have a domain name that I can throw this up on. I need to start hosting my portfolio online, anyway. I'll report back after I give this the old college try!
@Boateye, this is just to host a simple http server on an internal network without a domain name. For example, if your IPv6 (internal IP) is 192.168.1.3 and you are running it on a port 8080, anyone on your network will be able to see your website by going into the browre and typing 192.168.1.3:8080. However, the second part of my answer was talking about the Python CGI in general. If you have your own domain name, you can simply execute your scripts by enabling CGI in your server and placing all of your files in the designed folder.
@Boateye, Have you ever seen something like "?a=123&b=321" at the end of the urls? This is used to pass data from an html form to a common gateway interface script (almost any programming language) using GET or POST method. The program then uses these variables (a = 123, b = 321) to execute the program and everything you have that "print"s the answer is displayed as html for the user. I recently learned that Google search uses Python CGI. (This info may not be completely correct, but according to many sources, it is)
0

All of the answers so far assume that your game is something that can be presented as a web app. You can only do that if you write or covert your program (or part of it) into Javascript, which may not actually work because the existing compilers (e.g. pyjs) are quite limited.

If you made your program in a GUI (using 'Tkinter', 'Pygame', wxPython, PyQt, etc.), your best option is to package your program into a Mac app using py2app or pyinstaller.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.