'Import a local module on <py-script> (Python for browsers)

I found a very useful application, that works like html tags for Python applications. Supposedly I could use all the features of Python between tags within a Browserlike php, for example:

<py-script>
.......My program here....
</py-script>

Apparently the program works in a remote place, so when you use the libraries it uses the site libraries. But the libraries I need, (pyautocad) are locally in my computer, so when I try to load then the program searches in the site and fails. Below I add a snipett of the program

 <html>
  <head>
  <!-- <link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />-->
    <!-- <script defer src="https://pyscript.net/alpha/pyscript.js"></script> -->
    
    <link rel="stylesheet" href="pyscript.css" />
    <script defer src="pyscript.js"></script>
    <py-env> 
        paths:
        api.py
    </py-env>
  </head>
  <body>
        <py-script>
import sys 
import os
from pyautocad import Autocad()
from api import *
#watchout()
        </py-script>
  </body>
</html>

I tried it via href, py-env and other methods with no luck. Does someone have any suggestions? Regards and thanks in advance.



Solution 1:[1]

When running Pyscript in the web browser loaded from a web server, your HTML/JavaScript/Python cannot specify local files. That is a security restriction enforced by the web browser. Accessing local files from a script is prohibited.

It is possible to create a form input element that the user can click to read a local file. Then you can instantiate an object that contains Python code and pass that object to Pyodide for execution. You can also store that object in the virtual file system (IDBFS - IndexDB) for loading from your script in the future without requiring the local file upload.

The simplest solution is to put the Python wheels on your web server. If that is not possible then you will need to write code that interfaces with Pyodide.

Note: you will need to verify that pyautocad is supported by Pyscript/Pyodide. That package probably contains C code which means that package must be ported to be supported by Pyodide.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1