Damir Shaykxutd
Damir Shaykxutd

Reputation: 29

How to run add absolute path to custom python module/library at Pyscript on web server

I need to run my code from custom library and show it at clientside/browser I have problem with run code Pyscript from custom library when i`m include path (by recommendation https://github.com/pyscript/pyscript/issues/519) at my hosting web side for client

https://test.com/lib/python/example/index.html with code 👇
<html>
<head>
<script defer src="https://pyscript.net/releases/2022.12.1/pyscript.js"></script>
<link rel="stylesheet" href="https://pyscript.net/releases/2022.12.1/pyscript.css"/>
</head>
<body>    

<py-config>
    [[fetch]] 
paths = ["https://test.com/lib/python/example/custom/example.py"] #custom py-module
or 
[[fetch]]
url = "https://test.com/lib/python/example/"
folder = "custom/"
files = ["example.py"]
or
[[fetch]]
path = "lib/python/example/custom/example.py"
uri = "https://test.com/lib/python/example/custom/example.py"

# example.py
/* code file
def hello():
    print("Pyscript")
*/

</py-config>

<py-script>
    import example
    example.hello()
</py-script>
    

</body>
</html>

Paths on server:

with path
|---https://test.com/lib/python/example/custom/
|---------------------------------------------/index.html
|---------------------------------------------/example.py

or
|---https://test.com/lib/python/example/custom/
|---------------------------------------------/index.html
|---------------------------------------------/py/example.py

I have error - (PY1000): Couldn't determine the filename from the path , please supply 'to_file' parameter. 😪

How can i resolve it and run my custom module code by absolutely path to Pyscript on web hosting and show it 😞 ?

Upvotes: 0

Views: 286

Answers (2)

Damir Shaykxutd
Damir Shaykxutd

Reputation: 29

<py-config>
  [[fetch]]
  files = ['example.py'] #name of your py-file
  from = '../lib/python/example/custom/py/' #folder where placed your py-file
</py-config>
<py-script>
  import example
  example.hello()
</py-script>

Upvotes: -1

Jeff Glass
Jeff Glass

Reputation: 983

You're using the wrong attributes for <py-config>. The attribute you want is files, which takes a list of (relative or absolute URLs:

<py-config>
  [[fetch]]
  files = ['example.py']
</py-config>
<py-script>
  import example
  example.hello()
</py-script>

By default, all loaded files are placed in the same directory that Python code is executed from. If you want to place it in a different folder in the emscripten virtual file system, use the to_folder attribute. To specify a specific file name to store the content at (e.g. if you're loading content from a URL that doesn't end in [filename].py), use to_file.

Upvotes: 3

Related Questions