Running Python Script in HTML - python

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/

Related

How to work with python as client side scripting language

I have a fully functional gaze tracking code Python project which has been tested directly through the terminal. My guide wants me to implement this over the web. He wants all the processing to be done on the client-side because our servers are not powerful enough to handle multiple requests at the same time. My code involves the use of libraries like OpenCV, DLIB and mediapipe. I wanted to know whether is it possible to implement the code on the client-side by using python itself.
And project contains import statements as
import cv2
import dlib
from enum import Enum
from threading import Timer
import pyautogui
Even if I switch to JavaScript for client-side scripting will it result in performance losses, is there any way to avoid them? Is there any other way to implement the code on the client-side by keeping everything in python?
Not usually a done thing, JS is the only really supported browser-client-side language, although you can now also use WASM.
There are a few JS tools to allow Python running in the browser, most popularly Brython and Skulpt.
However, they may not work with all your imported packages, and will be much slower than writing your code in JS.
It's worth giving those two a go, but unfortunately, I think you may have to move to JS, or to wrapping your code in a web app framework (eg django) to allow the processing to happen on the server, where it has access to those libraries.

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)

Can I pre-compile a python script?

I have a python script. Lets say http://domain.com/hello.py, which only prints "Hello, World!".
Is it possible to precompile this Python file?
I get around 300 requests per second and the overhead of compiling is way to high. In Java the server can handle this easily but for calculations Python works much easier.
the problem is not that you need to "precompile" python, the problem is that you are trying to execute python scripts using normal cgi script stuff...
the real answer is to use a better web backend than simple cgi to run your python
I would suggest the following in order of appearance
1. nginx + gnunicorn
2. apache2 + mod-wsgi
3. something else
4. anything else
...
n-1. fcgi
n. cgi
I know this isnt really an answer and is entirely opinion based
Python code is compiled automatically the first time it is run, by the CPython (standard Python) interpreter. You can pre-compile it if you want to optimize the first request, but that usually isn't necessary. Aside from that, you'd need to convert your Python code into a Python C / cython Module. There are some tools to help you convert Python code into a Python Module if that's the route you want to go.
There's also a Python module called SciPy that's commonly used for Scientific Computing and Data Science applications, which provides a tool called Weave which allows you to inline C/C++ code into your Python code allowing certain performance-critical portions of the code to run using compiled C/C++ code.
via the python interface where your python source file is abc.py:
import py_compile
py_compile.compile('abc.py')

Execute a python script from firefox, on a very specific app, just to let firefox provide the graphical IDE

So, I've been searching the topic for quite a while, but I am unable to find out whether it is possible or not. I suspect not, but I need to be sure.
What I'm doing:
I am building a fancy web app that uses xml, xsl and javascript. However, I need to do some transformation to a text file to convert it into a proper xml file. To do this, I use a python script.
What I need:
I am working to make as easy as possible the use of the app, but this gets kind of destroyed when the user has to execute a terminal script before anything. I need a graphical IDE for python, but I just don't have time to learn how to make one, and I would love that the entire app would work within the same web environment, much more intuitive for the user. Then, a really easy way to do that would be to use Firefox as the IDE, for example choosing the file to be transformed through it, and launching from there the python script.
What I require:
I need compatibility only with Firefox. But of course any system capable to install Python and Firefox should work as well. The user would happily install both programs, as long as they have interest in the app.
What I don't know:
Is that possible, to execute an external python script? If not, for security reasons I imagine, is there any way for the user to allow it? Maybe through the installation of a plugin?
maybe http://mozex.mozdev.org/ is what I am looking for?
Thanks!
Gerard
Yes, It is possible to execute an external python script. You can use a simpleServer (example) that receives a (Ajax) request from your page and calls a subprocess or os.startfile to launch the script. In fact, you can call the function itself from within the server and return the result (e.g. as json) to the page and use your javascript to handle it.
If you're after actually using python in firefox, rather than having the server side written in python (although the webserver can of course be running locally), I would guess pythonext (used to be pyxpcomext) is what you need.

Execute python code inside browser without Jython

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

Categories

Resources