Can I use python to create flash like browser games? - python

is it possible to use python to create flash like browser games? (Actually I want to use it for an economic simulation, but it amounts to the same as a browser game)
Davoud

The answer would be yes, assuming you consider this a good example of what you want to do:
http://pyjs.org/examples/Space.html
This browser-based version of Asteroids was created using Pyjamas, which enables you to write the code in python in one place, and have it run either on the browser, or on the desktop:
http://pyjs.org/
Having recently found Pyjamas, and also preferring to consolidate my code in one language (Python!) and location (instead of having some code server-side, and some browser/client-side, in different languages), it's definitely an exciting technology. Its authors have ported the Google Web Toolkit to Python, a really impressive feat, retaining the expressive power of Python (something like 80,000 lines of Java was shrunk to 8,000 lines of Python). More Pythonistas should know about it. :)

You could use Python to do client side scripting using Silverlight + IronPython. Of course, this requires all your users install Silverlight.
I think you're talking about using Python on the back end, in which case running something on the server side with Python (in which case this Django vs other Python web frameworks SO question is a good general list and may have what you're looking for.

You need to use something that the current browsers support, this means you're stuck with Flash, Java applets or Javascript+HTML if you want your game displayed in a browser.
You can use python on the backend and display pure HTML, if that is enough for your needs.

Yes, but there a a number of ways to get there.
Flash is the client side rendering. You could use Python to generate Flash, or you could use Python to generate some dynamic HTML with Javascript, etc. that was interactive in a similar way.
But the Python will be running on the server. The Flash, ActionScript, HTML, JavaScript, etc. will all be running on the client.
So while the answer to the question is yes, I am going to suggest you might need to do more research and ask a better question.

You could have Python CGI code as a backend and send input in to it through AJAX. Its probably better just use something on the client side for this, though.

Related

Using python with HTML

I have written a python3 implementation of connect4. It features an AI that will play against you. It currently runs using the module pygame, which allows you to draw certain objects on a window.
I'd like to have this game work on a HTML canvas with JavaScript for drawing. Something akin to p5.js.
I'd rather not write the whole thing in JavaScript, since the python file runs perfectly (and I don't want to run all the code client-side for security reasons). Python also offers better functionality for this.
Is there an easy way to do this?
Stack Overflow really isn't designed for general "how do I do this" type questions. It's for more specific "I tried X, expected Y, but got Z instead" type question. But I'll try to help in a general sense.
Python can't run in a web browser. Web browsers can only run HTML and JavaScript. So if you want your program to work in a web browser, you have to use HTML and JavaScript.
You might be able to run your game on a server and then use something like a REST API to trigger the logic from the client, but honestly that's probably overkil. You're probably better off just rewriting your logic in JavaScript.
There are JavaScript implementation that take Python code and convert it into JavaScript commands. Googling "run python in web browser" returns a ton of results. In my experience, those are going to be pretty hit and miss though, and again, your best bet is to rewrite your game in JavaScript.
See also:
is it possible to run pygame or pyglet in a browser?
Any way to embed Pygame in webpages?
Please try to ask more specific questions in the fiture. If you're asking stuff like "How do I do that, and what are it's implications?" then that's a question for Google, not for Stack Overflow. Good luck.

Python in a webapp (client side)

This is part of some preliminary research and I am having a difficult time figuring out what options might be available or if this is even a situation where a solution even exists.
Essentially we have an existing python based simulation that we would like to make available to people via the web. It can be pretty processor intensive, so while we could just run the sim server side and write a client that connects to it, this would not be ideal.
Writing a UI in Flash/Flex or HTML5, not a problem. However, is there any way to keep the core simulation logic in python without having it live server side? Is there any existing way to embed python modules in either of these technologies?
Thanks all.
Pyjamas: Python->Javascript, set of widgets for use in a browser or a desktop
Skulpt: Python written in Javascript
Emscripten: C/C++ -> LLVM -> Javascript
Empythoned: Based on emscripten and cpython, working on a stdlib? There are bugs to file

Is it possible to import a module in python without using "import" or "eval"? [duplicate]

I understand that letting any anonymous user upload any sort of file in general can be dangerous, especially if it's code. However, I have an idea to let users upload custom AI scripts to my website. I would provide the template so that the user could compete with other AI's in an online web game I wrote in Python. I either need a solution to ensure a user couldn't compromise any other files or inject malicious code via their uploaded script or a solution for client-side execution of the game. Any suggestions? (I'm looking for a solution that will work with my Python scripts)
I am in no way associated with this site and I'm only linking it because it tries to achieve what you are getting after: jailing of python. The site is code pad.
According to the about page it is ran under geordi and traps all sys calls with ptrace. In addition to be chroot'ed they are on a virtual machine with firewalls in place to disallow outbound connections.
Consider it a starting point but I do have to chime in on the whole danger thing. Gotta CYA myself. :)
Using PyPy you can create a python sandbox. The sandbox is a separate and supposedly secure python environment where you can execute their scripts. More info here
http://codespeak.net/pypy/dist/pypy/doc/sandbox.html
"In theory it's impossible to do anything bad or read a random file on the machine from this prompt."
"This is safe to do even if script.py comes from some random untrusted source, e.g. if it is done by an HTTP server."
Along with other safeguards, you can also incorporate human review of the code. Assuming part of the experience is reviewing other members' solutions, and everyone is a python developer, don't allow new code to be activated until a certain number of members vote for it. Your users aren't going to approve malicious code.
Yes.
Allow them to script their client, not your server.
PyPy is probably a decent bet on the server side as suggested, but I'd look into having your python backend provide well defined APIs and data formats and have the users implement the AI and logic in Javascript so it can run in their browser. So the interaction would look like: For each match/turn/etc, pass data to the browser in a well defined format, provide a javascript template that receives the data and can implement logic, and provide web APIs that can be invoked by the client (browser) to take the desired actions. That way you don't have to worry about security or server power.
Have an extensive API for the users and strip all other calls upon upload (such as import statements). Also, strip everything that has anything to do with file i/o.
(You might want to do multiple passes to ensure that you didn't miss anything.)

Rookie Python-questions

Is it possible to make python run on your homepage? I know, this is a really stupid question but please don't pick on me for my stupidity :)
If it is possible, how? Do you have to upload/install the executing part of Python to you website using FTP? or...?
Edit: Just found out my provider does not support python and that shell access is completely restricted. Problem solved :)
Everything depends on the hosting provider you use for your homepage -- do they offer Python among their services, and, if so, what version, and how do you write server-side scripts to use it (is it CGI-only, or...?) -- if not, or the version / deployment options disappoint, what do they allow in terms of giving you shell access and running long-time processes?
It's impossible for us to judge any of these aspects, because every single one of them depends on your hosting provider, and absolutely none of them depends on Python itself!-)
Yes, you can. I don't know exactly how but I know it is possible. Mabye look into this website:
https://trinket.io/
This website lets you do this. I sent them a message to see how they do it so I will update this to let you know after they respond.
Python is a scripting language, though it is used gracefully for building back end web applications.

Using other languages with ruby

Is it possible to use multiple languages along side with ruby. For example, I have my application code in Ruby on Rails. I would like to calculate the recommendations and I would like to use python for that. So essentially, python code would get the data and calculate all the stuff and probably get the data from DB, calculate and update the tables.Is it possible and what do you guys think about its adv/disadv
Thanks
If you are offloading work to an exterior process, you may want to make this a webservice (ajax, perhaps) of some sort so that you have some sort of consistent interface.
Otherwise, you could always execute the python script in a subshell through ruby, using stdin/stdout/argv, but this can get ugly quick.
Depending on your exact needs, you can either call out to an external process (using popen, system, etc) or you can setup another mini-web-server or something along those lines and have the rails server communicate with it over HTTP with a REST-style API (or whatever best suits your needs).
In your example, you have a ruby frontend website and then a number-crunching python backend service that builds up recommendation data for the ruby site. A fairly nice solution is to have the ruby site send a HTTP request to the python service when it needs data updating (with a payload of information to identify what it needs doing to what or some such) and then the python backend service can crunch away and update the table which presumably your ruby frontend will automatically pick up the changes of during the next request and display.
I would use the system command
as such
system("python myscript.py")
An easy, quick 'n' dirty solution in case you have python scripts and you want to execute them from inside rails, is this:
%x[shell commands or python path/of/pythonscript.py #{ruby variables to pass on the script}]
or
``shell commands or python path/of/pythonscript.py #{ruby variables to pass on the script}\ (with ` symbol in the beginning and the end).
Put the above inside a controller and it will execute.
For some reason, inside ruby on rails, system and exec commands didn't work for me (exec crashed my application and system doesn't do anything).

Categories

Resources