5

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.

1

3 Answers 3

5

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:

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

  1. performance (I found that only pypy.js provides benchmarks) is N-time worse
  2. 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...

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

3 Comments

You can see a performance comparison of several options here.
@kikocorreoso I thought about comparison to JS. Thanks for sharing.
Depending what you are doing it could be pretty similar or quite slow as under the hood it is doing a lot of things of the python machinery...
0

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

Well, there is a JavaScript implementation of Python called Skulpt... skulpt.org
Sounds cool. Never heard about it and I an still not sure how it works. Is the python interpreter completely build in JavaScript? It probably can only use a couple of standard packages.
0

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/

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.