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.
3 Answers
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...
3 Comments
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.
2 Comments
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/