0

This is a python inside html question.

I have been looking for some time and have come across this code that embeds python inside a webpage (html).

I have tested it and it works. Here is the code:

The code script is saved as *.html and opens (and runs) in the browser.

<!DOCTYPE html>
<head>
<script type="module" src="https://cdn.jsdelivr.net/gh/vanillawc/wc-code@1.0.3/src/wc-code.js"></script>
</head>
<body>
  <wc-code mode="python">
    <script type="wc-content">
       a = 1
       b = 1
       print(a+b)
    </script>
  </wc-code>                                                                                         
</body>

The <script> tag in the code above contains some basic python code.

However, i cannot seem to import modules with a standard import module call and was wondering if there was a way of doing this ?

Also, similarly, if there is a way of calling or importing a custom python script like my_code.py ?

Thanks

D.L
  • 4,339
  • 5
  • 22
  • 45

3 Answers3

0

I think you should learn flask or Django \

the solution here FLASK can be helpful

  • both `flask` and `django` are web frameworks. So inherently different as cannot run the cde directly from the browser (which is sometimes more convenient). They are launched from the command line. – D.L Mar 27 '22 at 14:08
0

Pyodide is a solution for embedding python inside a webpage.

Binoy Pilakkat
  • 150
  • 1
  • 6
  • The snipped of code in the question is from one of the links from Pyodid. That is where i got this code from. But can one import other modules from within the browser or is it just native python without modules ? – D.L Mar 27 '22 at 14:10
  • do you have an example of importing modules (assuming that this feature exists) ? – D.L Mar 27 '22 at 14:12
  • It seems pyodide doesn't ship all modules. Pl see https://pyodide.org/en/stable/usage/packages-in-pyodide.html for the list of standard packages. – Binoy Pilakkat Mar 29 '22 at 03:56
  • Pyodide documentation says that additional packages can be imported using micropip. Pl go through https://pyodide.org/en/stable/usage/api/micropip-api.html. – Binoy Pilakkat Mar 29 '22 at 03:58
0

Now it can be achievable by using PyScript. You can use the import statements inside the <py-script> tag.

Live Demo :

<link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" />
<script defer src="https://pyscript.net/latest/pyscript.js"></script>

<py-script>
  from datetime import datetime
  datetime.now().strftime("%m/%d/%Y, %H:%M:%S")
</py-script>

Note (As per the current official documentation) :

PyScript is very alpha and under heavy development. There are many known issues, from usability to loading times, and you should expect things to change often. We encourage people to play and explore with PyScript, but at this time we do not recommend using it for production.

Debug Diva
  • 26,058
  • 13
  • 70
  • 123