Execute python code inside browser without Jython - python

Is there a way to execute python code in a browser, other than using Jython and an applet?
The execution does not have to deal with anything related to graphics. For example, just sum all the digits of a binary 1Gb file (chosen by the browser user) and then return the result to the server.
I am aware that python can be executed remotely outside a browser, but my requirement is to be done inside a browser.
For sure, I take for granted the user will keep the right to execute or not, and will be asked to do so, and all this security stuff... but that is not my question.

The Pyjamas project has a compiler called pyjs which turns Python code into Javascript.

nosklo's answer is wrong: pyxpcomext for firefox adds language="python" support to script tags. yes it's a whopping 10mb plugin, but that's life. i think it's best if you refer to http://wiki.python.org/moin/WebBrowserProgramming because that is where all known documented links between python and web browser technology are recorded: you can take your pick, there.

I put together a table comparing many Python-In-Browser technologies not long ago:
http://stromberg.dnsalias.org/~strombrg/pybrowser/python-browser.html

On my travels, I came across Skulpt, a project which seems to offer Python directly in the browser without plugins. It's licensed under MIT.
Skulpt Homepage
Skulpt # Github

No, you can't.
Modern browsers only run javascript or plugins. You can develop your own python plugin and convince people to download and run it, but I guess that falls to the "not inside the browser" category.

You mean client-side?
Sure you can! But you need to have python installed on the client first.
The linked book describes that in order to use client-side Active Scripting, you can test it with the a simple html file.
<html><body>
<script language='Python'>alert("Hello, Python!")</script>
</body></html>
In the old version refered in that book (Python programming on Win32
By Mark Hammond, Andy Robinson)
it says that you need to install the Python Win32 extensions, and it will automatically register Python Active Scripting. Should you do it manually, you have to run the script python\win32comext\axscript\client\pyscript.py.

http://repl.it/ - Python interpreter in JavaScript running on client side. There are many other languages too. Source is available under MIT license, which is awesome.

You can now (2016) also use:
http://www.transcrypt.org
It compiles Python 3.6 (incl. multiple inheritance, operator overloading, all types of comprehensions, generators & iterators) to lean and fast JS, supports source level debugging with sourcemaps and optional static typechecking using mypy.
Disclaimer: I am the initiator of the project.

By accident I was listening to Hanselminutes where he mentioned about Gestalt project. This is a solution to integrate a languages as IronRuby and IronPython in browser via Silverlight.
So I think the answer is no if you don't have any special plugins.

Brython - "A Python 3 implementation for client-side web programming"
http://www.brython.info/
https://bitbucket.org/olemis/brython/src

Related

Using HTML to implement Python User Interface

I've just started learning a bit of Python and I'm currently trying to implement a Python UI through HTML. Is there anything in vanilla Python that would allow for this, similar to how you can create UI's with Java and XML with JFX or will I have to use a framework such as Django?
I'm reluctant to use Django as there are many features that I do not need
Thanks,
Michael
In vanilla python wsgiref is very helpful for building the server-side of web-applications (possibly with str.format or string.Template and/or json) but if you want it more direct communication I would suggest XML-RPC (there are good js-clients out there).
It is also possible to execute Python Scripts right in your Website with Brython or (with strong constraints) Ironpython
For Windows you can build HTML Applications with Ironpython (have not tried but in theory it should work) or Brython (if you dont want to require the user to have ironpython installed)
You can also use Pyjs to build applications but while it uses html and javascript, i think it you dont see much of it.
There are HTML things in some ui-libraries like in wxpython (I am quite sure you will find them in many libraries)

Running Python Script in HTML

I'm working on a School Project. I've done a lot of Python Script before and I was wondering if I could like import python in html like javascript? How should I do it? Example is importing time. I want to show a Time clock in my webpage from python script.
There are python implementation written in JS. However you cannot run "natively" python in browser, you can actually run python code via javascript or compile python code to js (llvm emscripten)
You are looking for:
skulpt - http://www.skulpt.org/
brython - http://www.brython.info/
pyjs - http://pyjs.org/
pyjamas
And IMHO the most worth to take a look pypy.js:
PyPy.js is an experiment in building a fast and compliant python
environment for the web.
It uses the PyPy python interpreter, compiled for the web via
emscripten, with a custom JIT backend that emits asm.js code at
runtime.
Drawbacks of python in browser
performance (I found that only pypy.js provides benchmarks) is N-time worse
js interpreter libs (needed to load/run python code), have significant size couple MB
These two are the case, why that projects, probably shouldn't be used in the production, at that stage if ever. There are more cons like compatibility, implementation completion...
I is not possible to import Python code in html as you import JavaScript code. JavaScript is executed by the browser of the client and the browsers don't have an included Python interpreter. You have to do it with JavaScript if you want to do it on the Client side.
Here's (I think) the closest you're going to get to doing that. You can use Django and some of its functionalities:
https://docs.djangoproject.com/en/dev/howto/custom-template-tags/

Would it be possible to integrate Python or Perl with Ruby?

Would it be possible to integrate Python (and/or Perl) and Ruby? I've looked at http://www.goto.info.waseda.ac.jp/~fukusima/ruby/python/doc/ and http://code.google.com/p/ruby-perl/ , but they both seem rather outdated.
Has someone generated a Ruby interface for Python's C API?
Edit: Python can be integrated with many other languages according to http://wiki.python.org/moin/IntegratingPythonWithOtherLanguages . However, that list doesn't include Ruby.
My school (Georgia Tech), along with Bryn Mawr and Microsoft Research, are doing a project right now called Pyjama. Basically, it uses the Microsoft DLR to allow you to freely mix Python and Ruby. I haven't tried it, but it sounds pretty cool.
Here's an example from the website. You enter the class in "Python mode". Then it gets compiled, and you run the command in "Ruby mode".
class PythonClass:
def hello(self, value):
print "Python says hello to", value
pc = python_class().new
pc.hello "Ruby"
Which produces "Python says hello to Ruby".
Integrating dynamic languages is one of the goals of the Parrot project. It's a virtual machine that dynamic language compilers target. Once compiled to the same virtual machine, you should be able to used the "object" form in any of the languages no matter the object's source.
The issue at the moment, however, is stabilizing the virtual machine and finishing off the mostly done compilers. However, that's been the state for a long time. :)
You can write extensions for Ruby in C.
So, if Python has a C API, you can write a C extension for Ruby which uses this API.
I know nothing about the Python API or how large of a piece you want to integrate with, but if it is not too big, this could (possibly) give you a way to run Python code from Ruby.
For a research project I wanted to use the fabulous matplotlib that's available for Python. I also found that library that you referred to. However, it doesn't look like something popular and well tested. So I decided to write the script that generated graphs using pure Python and called it from Ruby via popen. That worked very well for me.
It might by possible, but not very practical. It would be significantly easier to port whatever modules you need from one to the other than it would be to embed one of the interpreters within the other.
If you absolutely have to use both languages in a project, your best option would probably the combination of Jython and Jruby, or IronPython and IronRuby. I'm not sure if you could get them to talk to each other, but at the very least you could host them on the same virtual machine.
Another strategy, as used by Facebook, is to expose APIs via Thrift. You define lightweight service APIs and the RPCs are inter-process.

Python learning environment

I'm looking to get up to speed on Python:
Is it worth working locally via the ActivePython interface, then progressing to a website that supports one of the standard frameworks (Django or Pylons) OR utilize the Google Apps environment?
I want to stay as interactive as possible - making feedback/learning easier.
Go with the Python interpreter. Take one of the tutorials that many people on SO recommend and you're on your way. Once you're comfortable with the language, have a look at a framework like Django, but not sooner.
Not sure what you mean.
For development
First choice: idle -- you already have it.
Second choice: Komodo Edit -- very easy to use, but not as directly interactive as idle.
For deploying applications, that depends on your application. If you're building desktop applications or web applications, you still use similar tools. I prefer using Komodo Edit for big things (either desktop or web) because it's a nice IDE.
What are you asking about? Development tools or final deployment of a finished product?
I learned using the docs and IDLE (with shell). Go to Django well after you fully understand Python.
I would just start locally. Django and Pylons add another layer of complexity to the edit/feedback loop.
Unless your primary focus is to make python websites, just stick with an editor and the console.
ipython and your favorite text editor. spend an hour with these screencasts and you'll be comfy with it in no time.
http://showmedo.com/videotutorials/series?name=CnluURUTV
PyCharm from JetBrains has a nice all in one IDE and it welcomes you with a very good tutorial regarding the usage of the IDE (code completion, IntelliSense etc) based on simple Python examples when you installed it and run it for the first time.
It is interactive, because it explains each step for each topic and lets you try it. Then it checks if you have done it right and gives hints if not.
There is a Community Edition (free) available for download or you can buy the Professional version or have it via subscription.
Visual Studio from Microsoft has also Python integration.
You can choose between a Community Edition (free), or you buy Professional or Enterprise. You can also have an MSDN subscription, then you can always download the latest versions with no extra costs.
There are some template projects available (web projects, Django etc), but starting with it is not so easy as it is with PyCharm, since in Visual Studio there is no interactive tutorial for first use available.
The standard IDE coming with Python is of course IDLE, its latest version can be downloaded here.
However, if you just need an editor supporting Python Syntax, then look for NotePad++ or Visual Studio Code. All of them are free. For quick experiments with Python, there is also pythonfiddle.com or pythontutor.com available.
With pythontutor you can even visualize the execution graphically.
All of these apps / tools mentioned are free.
Then for the Python language and Django you can visit the following websites to start:
in English:
www.python.org/about/gettingstarted - for beginners
www.learnpython.org - beginners & advanced
www.w3schools.com/python - beginners & advanced
docs.djangoproject.com - Django tutorials & documentation
Writing your first Django App - Django step by step
RealPython.com - Various useful topics
Writing a wrapper for C# - call C# methods from Python
in German (use "translate to english" in your browser's context menu):
www.python-lernen.de - beginners & advanced
I am sure there is a lot more out there, but the learning sites mentioned above are free.

What's a good embedded browser for a pygtk application?

I'm planning on using an embedded browser in my pygtk application and I'm debating between gtkmozembed and pywebkitgtk. Is there any compelling difference between the two? Are there any third options that I don't know about?
It should be noted that I won't be using this to access content on the web. I'm mainly using it for UI purposes.
My priorities are:
It needs to be stable.
It needs to be cross-platform.
It should be easy to use.
It should be actively maintained.
It should be extensible.
It should be fast.
gtkmozembed is not available on Windows, although you can use the gecko embedding interface directly. This would require you to write some C++ code.
As far as I know, the gtk webkit port is not available on Windows yet, and still appears to be undergoing a lot of change.
For an example of a cross-platform gecko embedding solution, check out Miro.
Miro is python, and they've written just a couple of C++ classes to embed gecko on Windows, while using gtkmozembed on linux.
if you judge by the web pages then definitely pywebkitgtk
pygtkmoz from this page
"Note: this project is no longer maintained. Please use gnome-python-extras (http://www.pygtk.org) instead. I apologize for any trouble this might cause, but this is better in the long run. Python bindings for GtkEmbedMozilla."
and pywebkitgtk looks like active project changes

Categories

Resources