Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run python code directly on a webpage [closed]

Tags:

python

My problem is as follows: I have written a python code, and I need to run it on a web page.Basically I need that whatever is on the console should be displayed as it is.

I have no experience in web development and similar libraries, and I need to get this done in a short time. Kindly tell how should I proceed?

Note: I might be plotting some graphs also. It would be great if they could be displayed all at once(sequentially) on the website

like image 306
the_he_man Avatar asked Jun 28 '26 10:06

the_he_man


2 Answers

  • https://brython.info/
  • https://skulpt.org/
  • https://pyodide.org/en/stable/

There are multiple python implementations in the browser: some are WebAssembly (WASM) and some are JavaScript.

Is it a good idea to run python on browser as a replacement for JavaScript in 2022? No it is not; learn JavaScript. No in-browser python implementation can match JavaScript and its performance as of today and most probably ever.

like image 85
Işık Kaplan Avatar answered Jun 29 '26 23:06

Işık Kaplan


PyScript (https://pyscript.net/) allows you to run Python in a web browser, using modules like Matplotlib or Numpy.

Here is a basic Hello-World example that outputs the current time with Python. The only difference with normal Python is that you use display which is imported from pyscript rather than print.

<head>
  <script type="module" src="https://pyscript.net/releases/2024.1.1/core.js"></script>
</head>

<body>
  <script type="py">
    from pyscript import display
    from datetime import datetime
    now = datetime.now()
    display(now.strftime("%m/%d/%Y, %H:%M:%S"))
  </script>
</body>

Click run code snippet for it to work. It takes two or so seconds for it to start up but after that, the python runs smoothly.

In the documentation (https://docs.pyscript.net/2024.9.1/) there are instructions for Matplotlib and other libraries.

like image 25
Sam Avatar answered Jun 30 '26 00:06

Sam