Guild icon
PyScript ๐Ÿ’ป
๐Ÿ’ฌ | General / โ•ฐโ•ธpyscript-help
Support and technical questions specifically about PyScript. For general Python help see #python-help. Anything else is probably off topic or more appropriate in #chat.
Avatar
breadhead don't care 15/05/2022 05:11
@Leonard Wolf
Avatar
Leonard Wolf 15/05/2022 05:11
Aye go ahead
Avatar
breadhead don't care 15/05/2022 05:12
I'm really new to programming so it might just be something easy to fix.
05:13
So are you suggesting that I try to read from the table rather than write to it?
Avatar
Leonard Wolf 15/05/2022 05:13
Umm yea so like maybe you can first try to create another table and try fetching data from it so we know that is this error from pyscript or sqlite
Avatar
Avatar
breadhead don't care
So are you suggesting that I try to read from the table rather than write to it?
Leonard Wolf 15/05/2022 05:14
Umm I mean to say create one more table and try reading from it and writing to it
Avatar
breadhead don't care 15/05/2022 05:15
Cool. That might tell me if it's an issue with the db or if it's some kind of connection issue that's not allowing it to find the table.
Avatar
Avatar
breadhead don't care
Cool. That might tell me if it's an issue with the db or if it's some kind of connection issue that's not allowing it to find the table.
Leonard Wolf 15/05/2022 05:15
Umm yea try that if its an issue with sqlite I might be not of much help but still ill try my best!
Avatar
breadhead don't care 15/05/2022 05:26
Use ".open FILENAME" to reopen on a persistent database. sqlite> .open gailen.db sqlite> .schema CREATE TABLE sqlite_sequence(name,seq); CREATE TABLE msgs ( "timestamp" TEXT, name TEXT NOT NULL, email TEXT NOT NULL, phone TEXT, message TEXT NOT NULL, msgid INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT ); sqlite> SELECT * FROM msgs; 1234|GAILEN|email@email.com|1234|foo bar baz|1 sqlite>
05:27
Here's what I got using sqlite from CMD (edited)
05:27
So that was all manually entered. (edited)
Avatar
Avatar
Leonard Wolf
Umm yea try that if its an issue with sqlite I might be not of much help but still ill try my best!
breadhead don't care 15/05/2022 05:30
Have you been able to write to a db using sqlite?
05:30
using pyscript i mean...
Avatar
Avatar
breadhead don't care
Have you been able to write to a db using sqlite?
Leonard Wolf 15/05/2022 05:33
Umm I dont use sqlite but use mysql
Avatar
Avatar
breadhead don't care
using pyscript i mean...
Leonard Wolf 15/05/2022 05:33
yes am trying to do it currently gimme few mins to test it
Avatar
breadhead don't care 15/05/2022 05:35
Nice. I'm kind of wondering if I just need to find a different way to do it. I haven't used mysql but if it works better I'll have give that a try.
Avatar
breadhead don't care 15/05/2022 05:51
Thanks for taking a look at that. I need to get to sleep so I'll check in tomorrow.
Avatar
Avatar
breadhead don't care
Nice. I'm kind of wondering if I just need to find a different way to do it. I haven't used mysql but if it works better I'll have give that a try.
Leonard Wolf 15/05/2022 05:51
Am still testing it with my own website on localhost
Avatar
Avatar
breadhead don't care
Thanks for taking a look at that. I need to get to sleep so I'll check in tomorrow.
Leonard Wolf 15/05/2022 05:52
Umm yea am looking into it if it works ill let you know
Avatar
Leonard Wolf 15/05/2022 06:04
Ok so I have tried every possible way to use mysql with pyscript but was unsuccessfully....always outputting some error....ill report this to the official developers and hope they can help!
Avatar
Leonard Wolf 15/05/2022 06:20
@JJ ig you should look into this matter too!
Avatar
If one wants to use db within browser, try duckdb wasm
06:29
Pyodide provides "Python with the scientific stack, compiled to WebAssembly" - it's an incredible project which lets you run a full working Jupyter notebook, complete with complex packages such as numpy and pandas, entirely in your browser without any server-side Python component running at all. It turns ou...
06:30
Basically, at this time, when PyScript is at its infancy, just forget PyScript and search for the solutions in Pyodide.
Avatar
Leonard Wolf 15/05/2022 06:37
@breadhead don't care Ig you will get answer from what @JJ suggested Ik this server is for pyscript but we do support pyodide as pyscript uses it!
06:38
@JJ thanks for you knowledgeful insight!
๐Ÿ‘๐Ÿฝ 1
Avatar
Avatar
Leonard Wolf
@JJ thanks for you knowledgeful insight!
You are welcome. I just did a little bit search ๐Ÿ’ฉ
Avatar
Avatar
JJ
You are welcome. I just did a little bit search ๐Ÿ’ฉ
Leonard Wolf 15/05/2022 06:41
Aye well thanks for that!
Avatar
breadhead don't care 15/05/2022 22:22
Thanks again for the help. I'm trying to knock out a few things and dive back into this. Hopefully I can spend some time on it this evening.
Avatar
Avatar
JJ
breadhead don't care 16/05/2022 00:45
Lol. So this looks promising. What's funny is that I did come across this briefly but moved on because it wasn't strictly pyscript. (edited)
Avatar
Avatar
breadhead don't care
Lol. So this looks promising. What's funny is that I did come across this briefly but moved on because it wasn't strictly pyscript. (edited)
Who needs PyScript when you have full access to Pyodide API ๐Ÿ˜‹
Avatar
breadhead don't care 16/05/2022 00:47
What are the key differences between the two?
Avatar
Avatar
breadhead don't care
What are the key differences between the two?
I think Pyodide is like API, while PyScript is like a specific application. PyScript can be useful in future, but unfortunately it's still an infant
00:54
If one has no idea of frontend development or JavaScript, then PyScript can save him/her ๐Ÿ˜‹
00:55
For me, I just use Pyodide for certain computation in Python that's not available in Javascript
Avatar
breadhead don't care 16/05/2022 00:59
That's definitely part of the appeal to PyScript for me. I'm about to start learning js a little better soon but for now I'm mostly comfortable in the flask framework where I have had to use very little js so far.
Avatar
Avatar
breadhead don't care
That's definitely part of the appeal to PyScript for me. I'm about to start learning js a little better soon but for now I'm mostly comfortable in the flask framework where I have had to use very little js so far.
๐Ÿค Flask was my choice. Now I switched to FastAPI
Avatar
breadhead don't care 16/05/2022 01:02
Might have to dabble in that if only just to replace the weird pepper looking logo on my portfolio. (edited)
Avatar
Avatar
breadhead don't care
Might have to dabble in that if only just to replace the weird pepper looking logo on my portfolio. (edited)
๐Ÿ˜…
01:05
I knew nothing about JavaScript when I used Flask. It turned out modern JavaScript is quite easy to learn, though I used CoffeeScript as transition ๐Ÿ’ฉ
Avatar
breadhead don't care 16/05/2022 01:06
CoffeeScript?
Avatar
Avatar
breadhead don't care
CoffeeScript?
JavaScript with Python syntax
Avatar
breadhead don't care 16/05/2022 01:07
That's sexy.
01:07
Just looked that up. I'm bookmarking it.
01:08
That's going to help me out a lot in the near future.
01:09
Or really mess me up. One of the two.
Avatar
CoffeeScript is quite solid. I wrote a big application out of it. But eventually you will move on to pure JavaScript
Avatar
how can I put text into a <py-inputbox> (this will be a default value, so does not need to be interactive)?
Avatar
Avatar
Thomas
how can I put text into a <py-inputbox> (this will be a default value, so does not need to be interactive)?
Leonard Wolf 16/05/2022 11:29
Umm have you tried specifying value after specifying id?
Avatar
<py-inputbox id="n">10</py-inputbox> like this?
11:31
that doesn't seem to work
Avatar
Leonard Wolf 16/05/2022 11:32
Umm naah not that way ig there aint a way to add a value tag but gimme some time.....
Avatar
Leonard Wolf 16/05/2022 11:45
@Thomas I went through the examples but there aint a way to do so or maybe there is but I was not able to get it So here I have a way for you using vanilla js! <script> window.addEventListener('load', function() { let input_box = document.querySelector("#new-task-content") input_box.value = "hello" }) </script> You can use this just change new-task-content to whatever id your py-inputbox has and hello to value you want it to be
Avatar
that worked -- thanks! (btw. why do I have to use #newtask vs newtask?
Avatar
Leonard Wolf 16/05/2022 11:48
Bcs its a query selector have you ever used vanilla js or better know as normal js
Avatar
not a lot
Avatar
Leonard Wolf 16/05/2022 11:48
Well # here stands for id and if you wanna select some class then you use .
11:49
If we would have had class then we would have written .newtask
Avatar
ohh, I see that makes sense
โœ… 1
Avatar
Leonard Wolf 16/05/2022 11:49
Yupo!
Avatar
@Leonard Wolf are you familiar with bokeh?
11:50
I'm creating a plot into a div, but when I re-execute it just appends, while I want to remove the previous plot
Avatar
Avatar
Thomas
@Leonard Wolf are you familiar with bokeh?
Leonard Wolf 16/05/2022 11:50
Umm naah am not but am sure you can find a tutorial if you want from YouTube
11:51
Or maybe you can wait for @JJ to be online he has used many stuff!
Avatar
I have some warnings that my code outputs that I can't suppress. Those show up in the html, is there a way to suppress the <py-script> output?
Avatar
Avatar
Thomas
I have some warnings that my code outputs that I can't suppress. Those show up in the html, is there a way to suppress the <py-script> output?
Leonard Wolf 16/05/2022 12:32
Umm not that am aware of but yea there's a way to suppress python warnings!
Avatar
unfortunately import warnings warnings.filterwarnings("ignore") doesn't do it
Avatar
Leonard Wolf 16/05/2022 12:33
Is it outputting a python warning?
12:33
try this import warnings def fxn(): warnings.warn("deprecated", DeprecationWarning) with warnings.catch_warnings(): warnings.simplefilter("ignore") fxn()
Avatar
unfortunately that also doesn't work. not sure how these un-ignorable warnings are being generated
Avatar
Leonard Wolf 16/05/2022 13:00
Do you know the exact line which is giving the error?
Avatar
yes.. I suppressed stderr now
Avatar
Avatar
Thomas
yes.. I suppressed stderr now
Leonard Wolf 16/05/2022 15:53
Ohh that gr8! (edited)
Avatar
DennisChou 16/05/2022 18:12
@Thomas could you post how you supressed stderr? just so ppl can find it in case they face a similar problem
Avatar
import os, sys sys.stderr = open(os.devnull, "w")
๐Ÿ‘ 2
Avatar
Hello world ! Iโ€™m new to PyScript and I think itโ€™s great! Currently accustomed to PHP, I find that Python can be a good alternative. I would like to create a form, but is there a post method for PyScript like $name = $_POST[โ€˜nameโ€™]; in PHP? Thanks in advance for your response.
Avatar
DennisChou 16/05/2022 19:02
i havent done it myself but this may be helpful @Victorien (edited)
19:02
Building 7 Foundational WEB GUIs in Python, running directly in the browser via PyScript
19:03
this post has more implementation details. I think Jeff solves your question in one of his examples? (not sure, havent had a chance to fully read it myself) https://jeff.glass/post/7-guis-pyscript/
Let's build 'The 7 GUIs' with Python in the Browser using PyScript
Avatar
Jeff Glass 16/05/2022 19:59
@Victorien if I'm understanding what $_POST does (I'm not super familiar with PHP), it seems like using FormData would be useful, but...
19:59
The FormData interface provides a way to easily construct a set of key/value pairs representing form fields and their values, which can then be easily sent using the fetch() or XMLHttpRequest.send() method. It uses the same format a form would use if the encoding type were set to "multipart/form-data".
19:59
I can't quite figure out how to use the 'new' constructor for a js object inside python/pyscript
19:59
(To create a new FormData object)
20:00
The more manual way would be to do something like:
20:00
from js import document, console from pyodide import create_proxy def _log_to_console(e): form = document.getElementById("my_form") for elem in form.elements: console.log(elem.name + ":" + elem.value) #Stop page from submitting/refreshing e.preventDefault() e.stopPropagation() return False logToConsole = create_proxy(_log_to_console) document.getElementById("my_form").addEventListener("submit", logToConsole)
Avatar
Thank you @DennisChou and @Jeff Glass for your answers, I don't understand everything but I will experiment in more detail with the features.
Avatar
Avatar
Thomas
import os, sys sys.stderr = open(os.devnull, "w")
Leonard Wolf 17/05/2022 06:42
Hey mind if you can send that in #โ•ฐโ•ธqueries-you-faced so if any other people face any such issues they are able solve it by reading queries from there
๐Ÿ‘ 1
Avatar
Hi, I am not good at English Can I create a web dashboard for the discord bot in pyscript? I want to send a request to the discord api in pyscript and I want to use load_dotenv and request or aiohttp. I do not want to use flask, fastapi, etc. (edited)
Avatar
DennisChou 17/05/2022 18:30
from what i understand, pyscript cannot use the python requests library - so you would not be able to make api calls from python directly. There are probably ways around this.
18:36
Requests doesn&#39;t work for upstream reasons (see #91 or pyodide/pyodide#398 ) but, at the same time, is extremely popular and large part of PyScript users come with the expectations that it ...
Avatar
Jeff Glass 17/05/2022 18:55
One option is to use pyodide.http.pyfetch, which is a thin wrapper around JavaScriptโ€™s fetch() method
18:56
See, for example, the code in this open pull request, which builds up a โ€œrequestโ€ method around pyfetch and makes GET/POST/PUT/DELETE requests using it https://github.com/pyscript/pyscript/pull/151/files
Summary Add a basic asynchronus demo in pure python for using pyodide.http.pyfetch for making HTTP requests. Description pyfetch is the pyodide alternative for python&#39;s widely used requests...
Avatar
Ah~ I, see, thank you
Avatar
Avatar
DennisChou
from what i understand, pyscript cannot use the python requests library - so you would not be able to make api calls from python directly. There are probably ways around this.
I try sample code but Hello world request example! Here is the output of your request: JsException(PythonError: Traceback (most recent call last): File "/lib/python3.10/asyncio/futures.py", line 201, in result raise self._exception File "/lib/python3.10/asyncio/tasks.py", line 232, in __step result = coro.send(None) File "/lib/python3.10/site-packages/_pyodide/_base.py", line 500, in eval_code_async await CodeRunner( File "/lib/python3.10/site-packages/_pyodide/_base.py", line 353, in run_async await coroutine File "", line 3, in ModuleNotFoundError: No module named 'request' ) You can also use other methods. See fetch documentation: https://developer.mozilla.org/en-US/docs/Web/API/fetch#parameters See pyodide documentation for what to do with a FetchResponse object: https://pyodide.org/en/stable/usage/api/python-api.html#pyodide.http.FetchResponse i use pythonanywhere
Avatar
Avatar
furimu
I try sample code but Hello world request example! Here is the output of your request: JsException(PythonError: Traceback (most recent call last): File "/lib/python3.10/asyncio/futures.py", line 201, in result raise self._exception File "/lib/python3.10/asyncio/tasks.py", line 232, in __step result = coro.send(None) File "/lib/python3.10/site-packages/_pyodide/_base.py", line 500, in eval_code_async await CodeRunner( File "/lib/python3.10/site-packages/_pyodide/_base.py", line 353, in run_async await coroutine File "", line 3, in ModuleNotFoundError: No module named 'request' ) You can also use other methods. See fetch documentation: https://developer.mozilla.org/en-US/docs/Web/API/fetch#parameters See pyodide documentation for what to do with a FetchResponse object: https://pyodide.org/en/stable/usage/api/python-api.html#pyodide.http.FetchResponse i use pythonanywhere
Leonard Wolf 19/05/2022 06:28
Have you specified the module in pyenv?
Avatar
test.html <html> <head> <link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" /> <script defer src="https://pyscript.net/alpha/pyscript.js"></script> <py-env> - paths: - request.py </py-env> </head> <body><p> Hello world request example! <br> Here is the output of your request: </p> <py-script> import asyncio # important!! import json from request import request baseurl = "https://jsonplaceholder.typicode.com/" # GET headers = {"Content-type": "application/json"} response = await request(baseurl+"posts/2", method="GET", headers=headers) print(f"GET request=> status:{response.status}, json:{await response.json()}") # POST body = json.dumps({"title":"test_title", "body":"test body", "userId":1}) new_post = await request(baseurl+"posts", body=body, method="POST", headers=headers) print(f"POST request=> status:{new_post.status}, json:{await new_post.json()}") # PUT body = json.dumps({"id":1, "title":"test_title", "body":"test body", "userId":2}) new_post = await request(baseurl+"posts/1", body=body, method="PUT", headers=headers) print(f"PUT request=> status:{new_post.status}, json:{await new_post.json()}") # DELETE new_post = await request(baseurl+"posts/1", method="DELETE", headers=headers) print(f"DELETE request=> status:{new_post.status}, json:{await new_post.json()}") </py-script> <div> <p> You can also use other methods. See fetch documentation: <br> https://developer.mozilla.org/en-US/docs/Web/API/fetch#parameters </p> </div> <div> <p> See pyodide documentation for what to do with a FetchResponse object: <br> https://pyodide.org/en/stable/usage/api/python-api.html#pyodide.http.FetchResponse </p> </div> </body> </html> </html> (edited)
Avatar
Leonard Wolf 19/05/2022 06:30
Remove .py from request.py and change request to requests
Avatar
Avatar
Leonard Wolf
Remove .py from request.py and change request to requests
sorry Do I change the file name? Or do change the html? (edited)
Avatar
Avatar
furimu
sorry Do I change the file name? Or do change the html? (edited)
Leonard Wolf 19/05/2022 06:57
<html> <head> <link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" /> <script defer src="https://pyscript.net/alpha/pyscript.js"></script> <py-env> - paths: - requests </py-env> </head> <body><p> Hello world request example! <br> Here is the output of your request: </p> <py-script> import asyncio # important!! import json from request import request baseurl = "https://jsonplaceholder.typicode.com/" # GET headers = {"Content-type": "application/json"} response = await request(baseurl+"posts/2", method="GET", headers=headers) print(f"GET request=> status:{response.status}, json:{await response.json()}") # POST body = json.dumps({"title":"test_title", "body":"test body", "userId":1}) new_post = await request(baseurl+"posts", body=body, method="POST", headers=headers) print(f"POST request=> status:{new_post.status}, json:{await new_post.json()}") # PUT body = json.dumps({"id":1, "title":"test_title", "body":"test body", "userId":2}) new_post = await request(baseurl+"posts/1", body=body, method="PUT", headers=headers) print(f"PUT request=> status:{new_post.status}, json:{await new_post.json()}") # DELETE new_post = await request(baseurl+"posts/1", method="DELETE", headers=headers) print(f"DELETE request=> status:{new_post.status}, json:{await new_post.json()}") </py-script> <div> <p> You can also use other methods. See fetch documentation: <br> https://developer.mozilla.org/en-US/docs/Web/API/fetch#parameters </p> </div> <div> <p> See pyodide documentation for what to do with a FetchResponse object: <br> https://pyodide.org/en/stable/usage/api/python-api.html#pyodide.http.FetchResponse </p> </div> </body> </html> </html>
Avatar
thank you!!
Avatar
Leonard Wolf 19/05/2022 06:59
Happy to help!
Avatar
Avatar
Leonard Wolf
Happy to help!
sorry <html> <head> <link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" /> <script defer src="https://pyscript.net/alpha/pyscript.js"></script> <py-env> - paths: - requests </py-env> </head> <body><p> Hello world request example! <br> Here is the output of your request: </p> <py-script> import asyncio # important!! import json from request import request baseurl = "https://jsonplaceholder.typicode.com/" # GET headers = {"Content-type": "application/json"} response = await request(baseurl+"posts/2", method="GET", headers=headers) print(f"GET request=> status:{response.status}, json:{await response.json()}") # POST body = json.dumps({"title":"test_title", "body":"test body", "userId":1}) new_post = await request(baseurl+"posts", body=body, method="POST", headers=headers) print(f"POST request=> status:{new_post.status}, json:{await new_post.json()}") # PUT body = json.dumps({"id":1, "title":"test_title", "body":"test body", "userId":2}) new_post = await request(baseurl+"posts/1", body=body, method="PUT", headers=headers) print(f"PUT request=> status:{new_post.status}, json:{await new_post.json()}") # DELETE new_post = await request(baseurl+"posts/1", method="DELETE", headers=headers) print(f"DELETE request=> status:{new_post.status}, json:{await new_post.json()}") </py-script> <div> <p> You can also use other methods. See fetch documentation: <br> https://developer.mozilla.org/en-US/docs/Web/API/fetch#parameters </p> </div> <div> <p> See pyodide documentation for what to do with a FetchResponse object: <br> https://pyodide.org/en/stable/usage/api/python-api.html#pyodide.http.FetchResponse </p> </div> </body> </html> </html> PyScript: Loading from file requests failed with error 404 (File not Found). Are your filename and path are correct? Hello world request example! Here is the output of your request: JsException(PythonError: Traceback (most recent call last): File "/lib/python3.10/asyncio/futures.py", line 201, in result raise self._exception File "/lib/python3.10/asyncio/tasks.py", line 232, in __step result = coro.send(None) File "/lib/python3.10/site-packages/_pyodide/_base.py", line 500, in eval_code_async await CodeRunner( File "/lib/python3.10/site-packages/_pyodide/_base.py", line 353, in run_async await coroutine File "", line 3, in ModuleNotFoundError: No module named 'request' ) You can also use other methods. See fetch documentation: https://developer.mozilla.org/en-US/docs/Web/API/fetch#parameters See pyodide documentation for what to do with a FetchResponse object: https://pyodide.org/en/stable/usage/api/python-api.html#pyodide.http.FetchResponse
Avatar
Leonard Wolf 19/05/2022 09:47
index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" /> <script defer src="https://pyscript.net/alpha/pyscript.js"></script> <py-env> - requests </py-env> </head> <body> <py-script> import requests r = requests.get("https://google.com") </py-script> </body> </html>
09:49
Error I got : blah blah blah..... (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) ) So yea as stated by @DennisChou it wont work as SSL is required + there was same issue with mysql connectivity too! So ig we have to wait for PyScript team to add something like async http.pyfetch as in pyodide
Avatar
Avatar
furimu
sorry <html> <head> <link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" /> <script defer src="https://pyscript.net/alpha/pyscript.js"></script> <py-env> - paths: - requests </py-env> </head> <body><p> Hello world request example! <br> Here is the output of your request: </p> <py-script> import asyncio # important!! import json from request import request baseurl = "https://jsonplaceholder.typicode.com/" # GET headers = {"Content-type": "application/json"} response = await request(baseurl+"posts/2", method="GET", headers=headers) print(f"GET request=> status:{response.status}, json:{await response.json()}") # POST body = json.dumps({"title":"test_title", "body":"test body", "userId":1}) new_post = await request(baseurl+"posts", body=body, method="POST", headers=headers) print(f"POST request=> status:{new_post.status}, json:{await new_post.json()}") # PUT body = json.dumps({"id":1, "title":"test_title", "body":"test body", "userId":2}) new_post = await request(baseurl+"posts/1", body=body, method="PUT", headers=headers) print(f"PUT request=> status:{new_post.status}, json:{await new_post.json()}") # DELETE new_post = await request(baseurl+"posts/1", method="DELETE", headers=headers) print(f"DELETE request=> status:{new_post.status}, json:{await new_post.json()}") </py-script> <div> <p> You can also use other methods. See fetch documentation: <br> https://developer.mozilla.org/en-US/docs/Web/API/fetch#parameters </p> </div> <div> <p> See pyodide documentation for what to do with a FetchResponse object: <br> https://pyodide.org/en/stable/usage/api/python-api.html#pyodide.http.FetchResponse </p> </div> </body> </html> </html> PyScript: Loading from file requests failed with error 404 (File not Found). Are your filename and path are correct? Hello world request example! Here is the output of your request: JsException(PythonError: Traceback (most recent call last): File "/lib/python3.10/asyncio/futures.py", line 201, in result raise self._exception File "/lib/python3.10/asyncio/tasks.py", line 232, in __step result = coro.send(None) File "/lib/python3.10/site-packages/_pyodide/_base.py", line 500, in eval_code_async await CodeRunner( File "/lib/python3.10/site-packages/_pyodide/_base.py", line 353, in run_async await coroutine File "", line 3, in ModuleNotFoundError: No module named 'request' ) You can also use other methods. See fetch documentation: https://developer.mozilla.org/en-US/docs/Web/API/fetch#parameters See pyodide documentation for what to do with a FetchResponse object: https://pyodide.org/en/stable/usage/api/python-api.html#pyodide.http.FetchResponse
Leonard Wolf 19/05/2022 09:49
.
๐Ÿ™ 1
Avatar
Is there any possibility to use pyscript in combination with react or another js framework?
Avatar
DennisChou 19/05/2022 20:04
could you clarify by what you mean by in combination?
Avatar
Avatar
Ick3
Is there any possibility to use pyscript in combination with react or another js framework?
I was going to ask the same question. I'm working on a solution for that
Avatar
Avatar
Armaaar
I was going to ask the same question. I'm working on a solution for that
Leonard Wolf 23/05/2022 13:11
Umm pyscript is still in its infant state hopefully the team will be soon rolling out options to combine its use with other frameworks
๐Ÿ‘ 1
Avatar
I used react Helmet to bei able to add pyscript. The Main Problem is that you need for Line Breaks in py-script {"/n"}. But its not necessary if you load a Python Script directly. @Armaaar @Leonard Wolf
15:13
The py-env settings i directly applied to the Index.html
Avatar
Leonard Wolf 23/05/2022 15:15
Aha so that means it works for you!
Avatar
I'm trying to dynamically load the scripts and execute them. Also looking if I can use react parameters inside the python script. It's not working well.
Avatar
hi guys! anyone successfully used ipywidgets in pyscript to update, let's say, values in a matplotlib plot?
Avatar
ChaoticWombat 24/05/2022 10:39
Hey guys I just tried pyscript yesterday
10:40
I'm getting the error of JsException(PythonError: Traceback (most recent call last): File "/lib/python3.10/site-packages/_pyodide/_base.py", line 421, in eval_code CodeRunner( File "/lib/python3.10/site-packages/_pyodide/_base.py", line 237, in __init__ self.ast = next(self._gen) File "/lib/python3.10/site-packages/_pyodide/_base.py", line 141, in _parse_and_compile_gen mod = compile(source, filename, mode, flags | ast.PyCF_ONLY_AST) File "", line 1 ^ SyntaxError: invalid syntax )
10:42
Here's my html code {% extends "layout.html" %} {% block content %} <div class="home"> <h1>Generator</h1> <p>nantoka nantoka</p> <py-env> - pandas </py-env> <p id="csv"></p> <py-script src="/myscript.py"></py-script> </div> {% endblock %} And here's my python code import pandas as pd from pyodide.http import open_url df = pd.read_csv(open_url("https://raw.githubusercontent.com/fromwindowstolinux/Python-Projects/main/Flask%20Project/manga.csv")) csv = Element("csv") csv.write(df.head())
10:43
Is it something to do with pyodide? or I misplaced some syntax in my code?
10:45
Because of line 1 ^ SyntaxError: invalid syntax, I've checked, and seems nothing wrong? Or I should be looking at something else?
10:47
I really feel like I'm in uncharted territory
Avatar
Avatar
ChaoticWombat
Here's my html code {% extends "layout.html" %} {% block content %} <div class="home"> <h1>Generator</h1> <p>nantoka nantoka</p> <py-env> - pandas </py-env> <p id="csv"></p> <py-script src="/myscript.py"></py-script> </div> {% endblock %} And here's my python code import pandas as pd from pyodide.http import open_url df = pd.read_csv(open_url("https://raw.githubusercontent.com/fromwindowstolinux/Python-Projects/main/Flask%20Project/manga.csv")) csv = Element("csv") csv.write(df.head())
Leonard Wolf 24/05/2022 11:00
Try download the file on the github and reading from it once
Avatar
ChaoticWombat 24/05/2022 11:01
Aight I'll try that
โœ… 1
Avatar
Avatar
ChaoticWombat
I'm getting the error of JsException(PythonError: Traceback (most recent call last): File "/lib/python3.10/site-packages/_pyodide/_base.py", line 421, in eval_code CodeRunner( File "/lib/python3.10/site-packages/_pyodide/_base.py", line 237, in __init__ self.ast = next(self._gen) File "/lib/python3.10/site-packages/_pyodide/_base.py", line 141, in _parse_and_compile_gen mod = compile(source, filename, mode, flags | ast.PyCF_ONLY_AST) File "", line 1 ^ SyntaxError: invalid syntax )
sure about src="/myscript.py" ? could be "./myscript.py" ?
Avatar
Avatar
ChaoticWombat
I really feel like I'm in uncharted territory
we all are, we all are.
Avatar
Jeff Glass 24/05/2022 13:21
Depending on how you're serving your files, either /myscripy.py or ./myscript.py could be correct.
13:21
@ChaoticWombat But to ask the question - you are using some kind of local web server to serve your files, yes? Adjacent files on your filesystem aren't visible to the browser unless there's a server of some kind running
13:22
See antocuni's comment on https://github.com/pyscript/pyscript/issues/257#issuecomment-1119595062 for an example (edited)
paths: can only be used if the page is being served out of a webserver, but not out of an HTML page on the filesystem. This is for good reasons and it&#39;s related to #179, but currently py-sc...
13:23
Either embedding the code inside a py-script tag or serving it locally gives me this, which I think is what you're expecting
Avatar
ChaoticWombat 24/05/2022 13:24
HOLYYY! Yeah that's what I was tryna get!
๐Ÿ‘ 1
13:29
I've been going around and around for hours
Avatar
Jeff Glass 24/05/2022 13:35
Heck yeah!
Avatar
Does anyone know how to solve errors like missing wheels? I get an error for a missing tokenizer module which is not directly used in the code but I assume by other modules pyodide.asm.js:14 Uncaught (in promise) PythonError: Traceback (most recent call last): File "/lib/python3.10/asyncio/futures.py", line 201, in result raise self._exception File "/lib/python3.10/asyncio/tasks.py", line 234, in __step result = coro.throw(exc) File "/lib/python3.10/site-packages/micropip/_micropip.py", line 183, in install transaction = await self.gather_requirements(requirements, ctx, keep_going) File "/lib/python3.10/site-packages/micropip/_micropip.py", line 173, in gather_requirements await gather(*requirement_promises) File "/lib/python3.10/asyncio/futures.py", line 284, in __await__ yield self # This tells Task to wait for completion. File "/lib/python3.10/asyncio/tasks.py", line 304, in __wakeup future.result() File "/lib/python3.10/asyncio/futures.py", line 201, in result raise self._exception File "/lib/python3.10/asyncio/tasks.py", line 232, in __step result = coro.send(None) File "/lib/python3.10/site-packages/micropip/_micropip.py", line 291, in add_requirement await self.add_wheel( File "/lib/python3.10/site-packages/micropip/_micropip.py", line 316, in add_wheel await self.add_requirement(recurs_req, ctx, transaction) File "/lib/python3.10/site-packages/micropip/_micropip.py", line 286, in add_requirement raise ValueError( ValueError: Couldn't find a pure Python 3 wheel for 'tokenizers!=0.11.3,<0.13,>=0.11.1'. You can use `micropip.install(..., keep_going=True)` to get a list of all packages with missing wheels. (edited)
13:50
Basicly what I am trying to achieve is using PyScript to analyze text on the HTML page using ai models like BERT and match the result with a given csv dataset. And finally predict and return the prediction by displaying it on the webpage
Avatar
Jeff Glass 24/05/2022 13:58
It looks like the tokenizers package is written mostly in rust, with Python bindings over the top. Pyodide (which PyScript uses to run Python) pretty much only runs pure-Python packages, or ones specifically compiled into Web Assembly for the browser.
13:59
The list of currently built packages for Pyodide is here (plus any pure-python packages on PyPI, mostly) https://pyodide.org/en/stable/usage/packages-in-pyodide.html
14:00
That said, the author of tokenizers (hugging face) does seem to have some demo code for doing your own tokenizing separate from the library, if that's of interest: https://huggingface.co/spaces/pyodide-demo/gpt2-tokenizer
Avatar
Thanks! I will look into it
Avatar
ChaoticWombat 25/05/2022 07:39
Guys
07:39
I wanna cry
07:39
I almost did it!
07:40
I need some tweaking to make it neater
07:40
TwT
07:40
Thanks a lot for the guidance
07:40
It's a childish project but it means a lot to me
โœจ 2
๐Ÿ˜ 1
Avatar
Avatar
ChaoticWombat
I almost did it!
Leonard Wolf 25/05/2022 07:40
Aha congratulations!
โœจ 1
Avatar
Deleted User 25/05/2022 07:53
Congraaaaatss
โœจ 3
Avatar
Nothing is childish, especially if you learned things ๐Ÿ˜‹ congrats !
โœจ 2
Avatar
Avatar
Danmaek
Nothing is childish, especially if you learned things ๐Ÿ˜‹ congrats !
Leonard Wolf 25/05/2022 12:41
Very true remember we all started with hello world so this stuff was much advance than that!
โœจ 1
Avatar
ChaoticWombat 25/05/2022 13:52
๐Ÿ’ช
Avatar
Jeff Glass 25/05/2022 15:46
@ChaoticWombat Congrats!
โœจ 1
Avatar
childish things may be computationally complex and interesting too! keep up the good work!
โœจ 1
Avatar
EnchantedPower 26/05/2022 15:48
hello is it possible to add pygame into pyscript
Avatar
Jeff Glass 26/05/2022 16:05
@EnchantedPower Not currently - there's some upstream work getting pygame built in WebAssembly. Once that's possible, it sounds like there will still be some work to be done to get it working in Pyodide, but it sounds like that's the major hurdle? (edited)
16:06
This is based on the discussion here: https://github.com/pyodide/pyodide/issues/289
Hello, I'm from the pygame project, and I'd like to get pygame working with pyodide. Here's the pygame issue tracking things: pygame/pygame#718 cheers,
Avatar
Can you actually write files to disk ? Im a total beginner I was using: def write(*args): data = Element('pwd').element.value with open('log.txt', 'w') as f: f.write(data)
Avatar
Avatar
NikiHax
Can you actually write files to disk ? Im a total beginner I was using: def write(*args): data = Element('pwd').element.value with open('log.txt', 'w') as f: f.write(data)
Jeff Glass 27/05/2022 14:57
Are you looking to read/write from your local harddisk?
Avatar
yes
Avatar
Avatar
NikiHax
Can you actually write files to disk ? Im a total beginner I was using: def write(*args): data = Element('pwd').element.value with open('log.txt', 'w') as f: f.write(data)
Leonard Wolf 27/05/2022 14:58
How to get/edit a html tag with pyscript?
Avatar
well even if I put "123" in the write function it wont write a file
Avatar
Avatar
NikiHax
yes
Jeff Glass 27/05/2022 14:59
So the short answer is - reading from is easy but requires a couple more steps, writing to is somewhat more complicated
14:59
By default your web browser tries really hard not to let any old website just download files to your filesystem, which is a good thing
โœ… 1
Avatar
mhm i get that
15:00
however Im running pyscript on my raspberry pi
15:00
and calling it from my webbrowser
15:00
i want it to write a file to my raspberry
Avatar
Jeff Glass 27/05/2022 15:01
Fair enough, but you're still using a browser, and the Python/PyScript is still running in the browser, so it's going to be treated with the same security gloves that the browser uses for all sites, more or less
15:01
(Not specific to PyScript, this is an issue with Javascript/other webscripting too)
15:01
That said
Avatar
so pyscript is always computing within my browser ?
Avatar
Jeff Glass 27/05/2022 15:02
Correct
15:02
More specifically
Avatar
What I want to do is make a website where you can store data which will get encrypted
15:02
and place it on the raspberry
Avatar
Jeff Glass 27/05/2022 15:02
It's running on a version of the Python interpretter that's been compiled into WebAssembly, running within your browser
15:02
Ah gotcha
Avatar
so it being decrypted in my browser is wonderful so it wont just get ssl encrypted whenever this would be on a website
15:03
crypted*
Avatar
Jeff Glass 27/05/2022 15:03
I'm not sure if this solves all the encyption issues, but as far as saving the file, you can ask the browser to "download" a file using javascript/pyscript: https://stackoverflow.com/a/54626214
I want to be able to download a given file when pressing a button.The file will be provided via an API call.For now, I will have it in my local storage. So my folder is something like : rootFolder...
Avatar
mhm in the first step i'd like it to store the data on server side
15:04
this is more about client side saving
15:05
also for decrypting it will be enough when I get the encrypted data visible and having pyscript decrypt it
15:05
so only me will know the decrypted cipher
15:05
im not familiar with js btw
Avatar
Jeff Glass 27/05/2022 15:08
So just so I'm clear - (1) the website is being server from the raspberry pi, (2) the user uploads some data using the website, (3) it gets encrypted on the client side, (4) you store the encrypted contents on the raspberry pi, and (5) when the user wants to see it, the raspberry pi sends it back to the user encrypted and PyScript descripts it within their browser
15:08
Have I got that right?
15:08
I guess (6) Then they can also re-download it
Avatar
Exactly
15:09
However my first start would be sending a string and saving it to a txt file for example
15:09
i followed some youtube video for a password generator
15:15
so as its client sided i guess referring to a file within the dir of the script wont make sense
15:15
as it will look for the file locally ?
Avatar
Jeff Glass 27/05/2022 15:19
Yeah, and "locally" in that sense is "within the built-in virtual filesystem inside the browser provided by Emscripten"
15:20
Hello guys, I&#39;m not sure if it&#39;s a mistake or if I&#39;m doing something wrong. Can anybody help me? The function below works well on the console. But when I try to print using ...
15:20
Ok, here's a really rough example of file downloading:
15:20
<style> input{ border-width: 4px; } </style> <body> <p>Type a string here to save it to file:</p> <label for="Type a password here:"></label><input type="text" id="pwd"> <br><br> <p>File name: </p> <head> <script defer src="https://pyscript.net/alpha/pyscript.js"></script> </head> <body> <label for="Type a password here:"></label><input type="text" id="filename"> <button id="download">Download</button> <py-script> from js import document, Blob, window from pyodide import create_proxy def _download_contents_of_field(field_name): text_to_save = document.getElementById("pwd").value blob_of_text = Blob.new(([text_to_save])) url = window.URL.createObjectURL(blob_of_text) file_name = document.getElementById("filename").value link = document.createElement("a"); link.download = file_name; link.href = url; link.click() download_contents_of_field = create_proxy(_download_contents_of_field) document.getElementById("download").addEventListener("click", download_contents_of_field) </py-script> </body> (edited)
โค๏ธ 1
โ™ฅ๏ธ 1
15:21
Two input fields for a "password" and filename, and a button that says download
15:22
When the download button is clicked, the function _download_contents_of_field gets the value from the password field, turns it into a "blob" object, creates a download link to that blob of text (that link lives nowhere on the page yet, but that's fine), then clicks that link
Avatar
Jeff Glass 27/05/2022 15:23
The create_proxy function is necessary because of how Pyodide (which runs Python in the browser) handles interacts with Javscript promises
Avatar
do i need to import envs ?
Avatar
Avatar
NikiHax
do i need to import envs ?
Jeff Glass 27/05/2022 15:23
Ah no, you'll need to import pyscript.js
15:23
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>
Avatar
oops i just copied
Avatar
Jeff Glass 27/05/2022 15:24
Yeah sorry, I should have clarified
Avatar
its fine thanks for your help, time and energy
15:24
that worked !
15:25
so my best bet to post data to the raspberry would be some kind of upload php script ?
15:25
and have pyscript push the data there
Avatar
Jeff Glass 27/05/2022 15:25
Possibly? Or some kind of upload via Javascript? I confess I'm not super familiar with the options there
Avatar
1. pyscript encrypts data 2. pyscript opens a webrequest to some kind of upload php script (data alrdy encrypted)
Avatar
Jeff Glass 27/05/2022 15:25
(Fwiw I went back and edited the above code to make it a full copy-able example for anyone finding this in the future)
๐Ÿ‘ 1
Avatar
I never do webdevelopment =P just giving it a try since pyscript
Avatar
Jeff Glass 27/05/2022 15:26
Nice!
15:26
Then I think PyScript is doing the thing it was hoping to do, getting peoples feet wet in web dev
15:26
Good on ya for jumping right in!
Avatar
brb i will experiment with the possibilities today
15:30
Well I could combine PHP and pyscript
15:30
Have pyscript do the encryption
15:30
And PHP will upload
15:30
Aka insert into database
Avatar
Jeff Glass 27/05/2022 15:41
Certainly possible
Avatar
ok i now got it working using pyscript for encryption and then uploading to mysql with php
16:04
Thanks for your help though !
Avatar
Jeff Glass 27/05/2022 16:23
Happy to - glad you got it working!
โœจ 1
16:44
combination of pyscript and php
16:44
might aswell use pyscript for upload wether this works idk but im frightened wether you could draw out the database logins
Avatar
Jeff Glass 27/05/2022 17:46
Very nice - if you have the code somewhere, maybe stick a link over in #โ•ฐโ•ธi-made-this so others can use it for inspiration?
17:46
Or honestly just a link to the demo
Avatar
it cant find crypto
21:26
im implementing encryption and decryption atm
21:26
but its missing crypto
21:29
Avatar
geekchick25 27/05/2022 22:22
Hello, Iโ€™m wondering if itโ€™s possible to do websockets with PyScript on the front end?
22:24
With a FastAPI backend
Avatar
how do you import libs
Avatar
Avatar
NikiHax
how do you import libs
Jeff Glass 28/05/2022 12:28
If theyโ€™re in the standard library, just import them as usual
12:29
If theyโ€™re not, add their name to the py-env tag to have micropip install them into the environment
12:29
Note that packages need to either have a pure Python wheel on PyPI, or be built specifically for pyodide https://pyodide.org/en/stable/usage/packages-in-pyodide.html
๐Ÿ‘ 1
12:30
If you get an error about โ€œcannot find a pure Python sheen forโ€ฆโ€, probably the module is using a C, C#, rust etc extension that isnโ€™t compiled for WASM yet
Avatar
let me try cryptography then
13:06
it says cryptography is in pyodide
13:06
but i still get the same error
13:07
Avatar
exploitgeeky 28/05/2022 13:09
NEED HELP dhooks packages not working
13:09
?
Avatar
Please correct me if i'm wrong
13:25
Available packages are only the ones available in pyodide (here https://github.com/pyodide/pyodide/tree/main/packages)
13:26
Or you need to import whl file (which is like a compilated lib in zip)
13:27
the github page says this
13:27
Pyodide is a Python distribution for the browser and Node.js based on WebAssembly - pyodide/test_cryptography.py at main ยท pyodide/pyodide
13:29
I have no clue what Im doing =( im just tryng to get it run for now =(
13:29
I just need any kind of encryption for my pyscript
Avatar
Hey, Anyone know where can I find list of libraries that can be directly imported in PyScript? I am working lot on PyScript now a days and now curious if I can use tensorflow? Also, if tf is not available, how can I contribute? \
Avatar
exploitgeeky 28/05/2022 13:36
how can i import dhooks in pyscript
Avatar
Avatar
exploitgeeky
how can i import dhooks in pyscript
I think it is yet not available in Pyodide so I am not sure if you can import it in PyScript at this time or not. (edited)
Avatar
exploitgeeky 28/05/2022 13:44
i can use micro.pip
13:44
to import but i need whl url of that package
Avatar
Because micropip is available to use in Pyodide
13:45
Pure Python packages with wheels can be loaded directly @exploitgeeky
13:47
https://pyodide.org/en/stable/usage/packages-in-pyodide.html Found this, maybe helpful @exploitgeeky
โ˜บ๏ธ 1
Avatar
exploitgeeky 28/05/2022 13:47
thanks
Avatar
Guys, How to add pys-onClick on dynamically added element? Note: setAttribute not working
Avatar
Original message was deleted or could not be loaded.
Leonard Wolf 29/05/2022 03:00
#โ•ฐโ•ธqueries-you-faced
๐Ÿ‘ 1
Avatar
Anyone can help me getting a cryptography module working with pyscript?
21:52
Its rlly hard to find documentation cuz when u google you find "python scripts"
Avatar
Leonard Wolf 30/05/2022 03:04
Umm maybe if cryptography has a wheel you can use it
Avatar
Hello! I had a question. Is it possible to trigger a pyscript when a button is clicked?
Avatar
Avatar
Shayokh
Hello! I had a question. Is it possible to trigger a pyscript when a button is clicked?
Yeah, pys-onClick as attribute in element
Avatar
Avatar
Siddharth
Yeah, pys-onClick as attribute in element
Thanks man! Any artical or code snippet regarding it?
Avatar
Avatar
Shayokh
Thanks man! Any artical or code snippet regarding it?
Jeff Glass 30/05/2022 22:49
Avatar
Thanks a lot man!
Avatar
Avatar
Shayokh
Thanks man! Any artical or code snippet regarding it?
Try this <body> <button class='rslt' id="rslt" pys-onClick="run_this">Click here!</button> <div id="output"></div> <py-script> def run_this(self): pyscript.write('output', 'Hello World') </py-script> </body>
๐Ÿ‘ 1
Avatar
Simple question. I am taking file input in HTML where I am selecting image, how to show image when submit button is hit in PyScript?
Avatar
Avatar
Siddharth
Simple question. I am taking file input in HTML where I am selecting image, how to show image when submit button is hit in PyScript?
Leonard Wolf 31/05/2022 06:51
Maybe if you want you can use the traditional js and just update the dom when the button is clicked
Avatar
Avatar
Leonard Wolf
Maybe if you want you can use the traditional js and just update the dom when the button is clicked
Actually, I need to use that file in PyScript to process. How can I do that?
Avatar
Avatar
Siddharth
Actually, I need to use that file in PyScript to process. How can I do that?
Leonard Wolf 31/05/2022 07:07
Am not sure abt how to update the dom element maybe either @Jeff Glass or @JJ can help you with that!
๐Ÿ‘ 1
Avatar
Avatar
Siddharth
Yeah, pys-onClick as attribute in element
Where one can find a list of all pyscript tags?
Avatar
Avatar
Spaturno
Where one can find a list of all pyscript tags?
py-script and py-env are only two tags. Attributes you can learn from examples.
Avatar
I've read about a py-button somewhere.
Avatar
Need to check. I will get back to you once I find something or some list. Good docs for PyScript is necessary but we don't have one unfortunately.
โค๏ธ 1
Avatar
Avatar
Spaturno
Where one can find a list of all pyscript tags?
Jeff Glass 31/05/2022 15:48
Not that this clarifies what each of them do, but from the source on GitHub, in the main.ts file, we can see that the current pyscript tags are: py-script py-repl py-env py-box py-button py-title py-inputbox py-register-widget py-loader and py-config. (edited)
โค๏ธ 1
Avatar
Avatar
Jeff Glass
Not that this clarifies what each of them do, but from the source on GitHub, in the main.ts file, we can see that the current pyscript tags are: py-script py-repl py-env py-box py-button py-title py-inputbox py-register-widget py-loader and py-config. (edited)
thank you! there's the info I was looking for
๐Ÿ‘ 1
Avatar
Jeff Glass 31/05/2022 15:48
Not all of these are meant to be used directly by the end-user. For example, py-loader is what causes the semi-transparent "loading runtime" overlay on the screen while the runtime loads.
Avatar
Avatar
Siddharth
Simple question. I am taking file input in HTML where I am selecting image, how to show image when submit button is hit in PyScript?
Jeff Glass 31/05/2022 15:50
Do you have a working example of uploading the file on the page?
Avatar
Avatar
Jeff Glass
Do you have a working example of uploading the file on the page?
It's like I am using <input type="file">
Avatar
Avatar
Jeff Glass
Not that this clarifies what each of them do, but from the source on GitHub, in the main.ts file, we can see that the current pyscript tags are: py-script py-repl py-env py-box py-button py-title py-inputbox py-register-widget py-loader and py-config. (edited)
Leonard Wolf 31/05/2022 16:06
#โ•ฐโ•ธqueries-you-faced please!
Avatar
Avatar
Jeff Glass
Not all of these are meant to be used directly by the end-user. For example, py-loader is what causes the semi-transparent "loading runtime" overlay on the screen while the runtime loads.
understood, thanks
Avatar
Avatar
Siddharth
Simple question. I am taking file input in HTML where I am selecting image, how to show image when submit button is hit in PyScript?
Jeff Glass 31/05/2022 20:12
I'll confess to getting fully nerd-sniped by this question, and spent my lunch break whipping up a demo of how to handle image-uploading using Pyscript, as well as processing images with Pillow in-browser. It's a little big to embed here, so it's hosted over on my website: https://jeff.glass/post/pyscript-image-upload/
How to upload images into Pyscript and work with them in Pillow in-browser
โœ… 1
20:15
There is a slightly-odd thing going on, where if you right-click the image and do 'Save-Image-As', the image saves, but as a .txt file, and the inspector doesn't seem to recognize it as an image... something about how I'm handling the MIME types maybe?
Avatar
Leonard Wolf 01/06/2022 02:27
Thats awsm stuff!
Avatar
@Jeff Glass that is something amazing
04:19
Really amazing work, Thanks a lot...
Avatar
Jan-Hendrik Mรผller 03/06/2022 19:46
Short question: I've found an amazing Matplotlib example here https://github.com/pyscript/pyscript/blob/main/examples/matplotlib.html which I slightly adopted to display two figures. But pyscript only displays one figure. Does someone have an idea how both images can be displayed ? Thanks in advance, and here is the code: <html> <head> <title>Matplotlib</title> <meta charset="utf-8"> <link rel="icon" type="image/x-icon" href="./favicon.png"> <link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" /> <script defer src="https://pyscript.net/alpha/pyscript.js"></script> <py-env> - matplotlib - scikit-image - numpy </py-env> </head> <body> <div id="mpl"></div> <py-script output="mpl"> from skimage import data import matplotlib.pyplot as plt fig1, ax1 = plt.subplots() ax1.imshow(data.camera(), cmap="gray") fig2, ax2 = plt.subplots() ax2.imshow(data.cat()) fig1 fig2 </py-script> </body> </html>
Home Page: https://pyscript.net Examples: https://pyscript.net/examples - pyscript/matplotlib.html at main ยท pyscript/pyscript
Avatar
Avatar
Jan-Hendrik Mรผller
Short question: I've found an amazing Matplotlib example here https://github.com/pyscript/pyscript/blob/main/examples/matplotlib.html which I slightly adopted to display two figures. But pyscript only displays one figure. Does someone have an idea how both images can be displayed ? Thanks in advance, and here is the code: <html> <head> <title>Matplotlib</title> <meta charset="utf-8"> <link rel="icon" type="image/x-icon" href="./favicon.png"> <link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" /> <script defer src="https://pyscript.net/alpha/pyscript.js"></script> <py-env> - matplotlib - scikit-image - numpy </py-env> </head> <body> <div id="mpl"></div> <py-script output="mpl"> from skimage import data import matplotlib.pyplot as plt fig1, ax1 = plt.subplots() ax1.imshow(data.camera(), cmap="gray") fig2, ax2 = plt.subplots() ax2.imshow(data.cat()) fig1 fig2 </py-script> </body> </html>
Jeff Glass 03/06/2022 23:08
So youโ€™re probably only seeing fig2, since thatโ€™s the last thing in the script and is, by default, returned to be printed to the screen
23:09
Not at a computer at the moment, but you may be able to just do print(fig1) print(fig2)
Avatar
Avatar
Jeff Glass
So youโ€™re probably only seeing fig2, since thatโ€™s the last thing in the script and is, by default, returned to be printed to the screen
Jan-Hendrik Mรผller 04/06/2022 07:48
Yep, I am only seeing fig2. Thanks for your idea! However, that gives me on the screen only the text: Figure(640x480) Figure(640x480)
Avatar
DennisChou 04/06/2022 08:20
This works for me <html> <head> <title>Matplotlib</title> <meta charset="utf-8"> <link rel="icon" type="image/x-icon" href="./favicon.png"> <link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" /> <script defer src="https://pyscript.net/alpha/pyscript.js"></script> <py-env> - matplotlib - scikit-image - numpy </py-env> </head> <body> <div id="mpl"></div> <div id="mpl2"></div> <py-script> from skimage import data import matplotlib.pyplot as plt fig1, ax1 = plt.subplots() ax1.imshow(data.camera(), cmap="gray") fig2, ax2 = plt.subplots() ax2.imshow(data.cat()) pyscript.write("mpl",fig1) pyscript.write("mpl2",fig2) </py-script> </body> </html> (edited)
โค๏ธ 1
๐ŸŽ‰ 1
Avatar
Avatar
DennisChou
This works for me <html> <head> <title>Matplotlib</title> <meta charset="utf-8"> <link rel="icon" type="image/x-icon" href="./favicon.png"> <link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" /> <script defer src="https://pyscript.net/alpha/pyscript.js"></script> <py-env> - matplotlib - scikit-image - numpy </py-env> </head> <body> <div id="mpl"></div> <div id="mpl2"></div> <py-script> from skimage import data import matplotlib.pyplot as plt fig1, ax1 = plt.subplots() ax1.imshow(data.camera(), cmap="gray") fig2, ax2 = plt.subplots() ax2.imshow(data.cat()) pyscript.write("mpl",fig1) pyscript.write("mpl2",fig2) </py-script> </body> </html> (edited)
Jan-Hendrik Mรผller 04/06/2022 09:35
Thanks a lot, this is exactly what I was looking for!
Avatar
DennisChou 04/06/2022 16:48
I think this used to work but is no longer working. <html lang="en"> <head> <script defer src="https://pyscript.net/alpha/pyscript.js"></script> </head> <p id="x"></p> <py-button label="add one"> def on_click(event): global x # need to declare global x+=1 pyscript.write('x', x) </py-button> <py-script> x=0 pyscript.write('x',x) </py-script> </html>
Avatar
DennisChou 04/06/2022 16:57
๐Ÿ› Bug Uncaught Error: This borrowed proxy was automatically destroyed at the end of a function call. Try using create_proxy or create_once_callable. The object was of type &quot;function&qu...
Avatar
Avatar
Jan-Hendrik Mรผller
Short question: I've found an amazing Matplotlib example here https://github.com/pyscript/pyscript/blob/main/examples/matplotlib.html which I slightly adopted to display two figures. But pyscript only displays one figure. Does someone have an idea how both images can be displayed ? Thanks in advance, and here is the code: <html> <head> <title>Matplotlib</title> <meta charset="utf-8"> <link rel="icon" type="image/x-icon" href="./favicon.png"> <link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" /> <script defer src="https://pyscript.net/alpha/pyscript.js"></script> <py-env> - matplotlib - scikit-image - numpy </py-env> </head> <body> <div id="mpl"></div> <py-script output="mpl"> from skimage import data import matplotlib.pyplot as plt fig1, ax1 = plt.subplots() ax1.imshow(data.camera(), cmap="gray") fig2, ax2 = plt.subplots() ax2.imshow(data.cat()) fig1 fig2 </py-script> </body> </html>
i'd go with subplots.
20:12
oh someone already replied
Avatar
Jan-Hendrik Mรผller 07/06/2022 21:14
Short question: In the project https://github.com/kolibril13/pyscript-emoji-skimage with @Jeff Glass pyscript takes about 10 seconds to initialize. Now I want to update the CSS to fine tune the div alignment, I have to wait again 10 seconds to see how it looks. Is there a way to tweak the website appearance this more quickly, without the long reload? My current environment is VS Code+ Live Preview.
Contribute to kolibril13/pyscript-emoji-skimage development by creating an account on GitHub.
Avatar
Jeff Glass 07/06/2022 21:15
@Jan-Hendrik Mรผller To be honest no, not a great way as far as I know - as far as I'm aware that's about how long the Pyodide runtime takes to initialize (edited)
21:17
Though, I hope I'm wrong and someone knows better!
Avatar
Avatar
Jeff Glass
@Jan-Hendrik Mรผller To be honest no, not a great way as far as I know - as far as I'm aware that's about how long the Pyodide runtime takes to initialize (edited)
Jan-Hendrik Mรผller 07/06/2022 21:18
Thanks! That would be really nice if this could be possible.
Avatar
Is there any way to have input() in pyscript not be an alert? I'm trying to make a text based game and having that doesn't let you do anything else/see anything else (edited)
00:00
Avatar
DennisChou 08/06/2022 05:49
can you show your code?
Avatar
Avatar
colton
Is there any way to have input() in pyscript not be an alert? I'm trying to make a text based game and having that doesn't let you do anything else/see anything else (edited)
Jeff Glass 08/06/2022 15:12
So the short answer is - at this point, you're probably better-off using an <input> item within your html, then using either a py-button or a regular <button> to read its contents and display your output to the screen Edit: There's also py-inputbox, which seems like it will be very useful to you when it supports the keydown event https://realpython.com/pyscript-python-in-browser/#pyinputbox (edited)
15:13
This "flight booker" demo shows inputting text into an <input>, then when the button below it is clicked, reading its contents and acting on the https://jeff.glass/post/7-guis-pyscript/#flight-header\
Avatar
Jeff Glass 08/06/2022 15:42
I've opened an enhancement issue on Github to possibly allow overriding the default behavior of input() in the future. This is supported upstream by Pyodide, but not yet by Pyscript. https://github.com/pyscript/pyscript/issues/502
Avatar
Avatar
Jan-Hendrik Mรผller
Short question: In the project https://github.com/kolibril13/pyscript-emoji-skimage with @Jeff Glass pyscript takes about 10 seconds to initialize. Now I want to update the CSS to fine tune the div alignment, I have to wait again 10 seconds to see how it looks. Is there a way to tweak the website appearance this more quickly, without the long reload? My current environment is VS Code+ Live Preview.
You can live edit css in chrome or firefox developer tools and see an immediate result
Avatar
just remember to paste it somewhere you can save before you refresh
๐Ÿ‘ 1
Avatar
Avatar
John S
You can live edit css in chrome or firefox developer tools and see an immediate result
Jan-Hendrik Mรผller 08/06/2022 21:57
I've just tired that, and it works great, thanks for this idea! Another question: Is there a way, or even a best practice, to make the python script run in both scenarios: On the one hand, as a script that can be imported locally and on the other hand in the pyscript browser context? (edited)
Avatar
Jan-Hendrik Mรผller 09/06/2022 10:12
Here is a simple example from this blog post: https://www.jhanley.com/pyscript-loading-python-code-in-the-browser/ import js from js import document def main(): msg = document.getElementById("msg") msg.innerHTML = 'Hello world' main() running this example locally with normal python will as expected bring the error: ModuleNotFoundError: No module named 'js' To make this work both ways, one could e.g. add try-except blocks: try: import js from js import document except ModuleNotFoundError: pass def main(): my_string = "Hello World" try: msg = document.getElementById("msg") msg.innerHTML = my_string except NameError: print(my_string) main() But is there maybe a better way to do this? (edited)
Avatar
Avatar
Jan-Hendrik Mรผller
I've just tired that, and it works great, thanks for this idea! Another question: Is there a way, or even a best practice, to make the python script run in both scenarios: On the one hand, as a script that can be imported locally and on the other hand in the pyscript browser context? (edited)
Jeff Glass 09/06/2022 13:29
I don't know if is ideal or a best practice, but the Pyodide FAQ has a couple ways of determining if your code is running in Pyodide or Emscription https://pyodide.org/en/stable/usage/faq.html#how-to-detect-that-code-is-run-with-pyodide, like import sys if "pyodide" in sys.modules: # running in Pyodide and import platform if platform.system() == 'Emscripten': # running in Pyodide or other Emscripten based build
๐Ÿ‘ 1
Avatar
ferstaberinde 11/06/2022 12:07
hello ๐Ÿ™‚ apologies for what may be the most basic question, but: why is it that when you include HTML tags in a print() statement, they don't render?
12:08
(e.g., print("<em>Hello</em>") renders without the <em> tag being applied)
Avatar
Avatar
ferstaberinde
hello ๐Ÿ™‚ apologies for what may be the most basic question, but: why is it that when you include HTML tags in a print() statement, they don't render?
Jeff Glass 11/06/2022 23:51
Interesting - are you working in a py-script tag or a py-repl?
23:52
Just a real quick test (on mobile, sorry) I see <em> tags working in the REPL2 example
23:52
Avatar
Jeff Glass 12/06/2022 17:17
Ok I take it back, seeing what you're seeing (tags not rendering) when using print() inside a pyscript tag, though they do work within a REPL...
Avatar
ferstaberinde 12/06/2022 17:28
@Jeff Glass - sorry for not responding sooner; do you think this is a deliberate decision / browser limitation, or something that may be added later?
Avatar
Jeff Glass 12/06/2022 17:34
Digging a little deeper, it seems like this is a recognized issue - see https://github.com/pyscript/pyscript/issues/347
17:35
Right now, all text inside in py-script tags are stripped of HTML tags before being processed, which is causing the behavior you're seeing.
17:35
Fabio P (lead Pyscript dev) says that "ultimately we'll need to design a way for users to opt-in/opt-out escaping" -
17:37
For now, that link has some workarounds. Putting the text in an external Pyscript file and importing it also gets around the issue for now ### my_string_file.py formatted_string = "<em>Hello, world!</em> ### my_page.html <py-env> - paths: - ./my_string_file.py </py-env> <py-script> import my_string_file print(my_string_file.formatted_string) </py-script> Output: Hello, world! (edited)
Avatar
ferstaberinde 12/06/2022 17:38
thanks for pointing me in the direction of that thread, will read-up ๐Ÿ™‚
๐Ÿ‘ 1
Avatar
rx (ping me) 13/06/2022 01:31
Hey, been messing around with shimming requests and I stumbled upon some work from https://github.com/bartbroere/requests/tree/pyodide-requests This work was started from the pyodide side but it seems fruitful in terms of getting a shim of requests working, however, I'm unable to get a working demo in-side of py-script despite the other getting it to work inside a pyodide demo. I essentially git clone'd this and manually built a wheel so I could reference the specific branch of this repo as apart of my <py-env>. It gets really close until I get to a JsException: NetworkError: Failed to execute 'send' on 'XMLHttpRequest' I'm reaching my limits here but I'm wondering if atleast this may be useful to y'all and perhaps we can explore this together.
A simple, yet elegant, HTTP library. Contribute to bartbroere/requests development by creating an account on GitHub.
01:32
The main idea here would hopefully be a drop-in replacement for getting requests functionality so that other libraries can function!
Avatar
Avatar
rx (ping me)
The main idea here would hopefully be a drop-in replacement for getting requests functionality so that other libraries can function!
That would be handy sure, but that may not be best for future, requests is synchronous and that design is not good for web or single thread embedded systems. It also could be time wasted working on that because I don't think the problem is at module level. The problem is that cpython has no mean yet to pause running code and go take care of I/O (from within or outside wasm vm) and then resume. Some bits about that problem here https://github.com/ethanhs/python-wasm/issues/12 (edited)
Avatar
Avatar
pmp-p
That would be handy sure, but that may not be best for future, requests is synchronous and that design is not good for web or single thread embedded systems. It also could be time wasted working on that because I don't think the problem is at module level. The problem is that cpython has no mean yet to pause running code and go take care of I/O (from within or outside wasm vm) and then resume. Some bits about that problem here https://github.com/ethanhs/python-wasm/issues/12 (edited)
rx (ping me) 14/06/2022 07:39
ah, thanks for the reference. I do agree that it may not be the best experience, however, without any sort of drop-in replacement, the several thousands of libraries that use requests will instantly not work in pyodide. there are different levels of shimming and hacks to get things working, whether someone patches the requests library or figures out some solution to the core http.client. the main benefit for getting something like a shim'd requests isn't for normal use-case, but mostly to make nice with other libraries which use it.
Avatar
but shimming as ethan smith said would only be a medium term solution ( and force the use of worker+atomic which are far from avail everywhere )
07:52
if problem is fixed at core level, requests will "just work"
07:52
( i know because i've fixed that problem on another python implementation already )
Avatar
rx (ping me) 14/06/2022 08:01
I see what your saying, but there are other issues with requests more than just blocking / async stuff, particularly, it's based in sockets which don't make sense at all in a browser (or so I read from other threads).
08:01
but i suppose it's all pieces in a puzzle, it's all alpha haha
Avatar
well for sockets, the quick and easy way is to put a websockify proxy in front of the "old" socket server (https://github.com/novnc/websockify ) (edited)
08:03
for client socket it's no problem, but yeah listening sockets are they really need async (edited)
Avatar
rx (ping me) 14/06/2022 08:04
oh interesting, hm. thanks for the info ๐Ÿ‘ I think what i'll end up doing is patching the libraries I need to use the js fetch api and doing some weird stuff for blocking
08:04
but, ofc, i'm going to end up in some mess haha
Avatar
yeah that's why i suggest to wait a bit things clear up
08:06
even the http.fetch uses xhr with a hack in pyodide it's not really the good way either
08:07
python just need a small kernel to handle I/O nicely with its own loop that's all, and everybody will be happy
Avatar
does someone actually host a pyscript based on 3.11b3 ?
Avatar
Doesn't most of the HTTP parts of requests live in urllib3?
Avatar
Avatar
pmp-p
does someone actually host a pyscript based on 3.11b3 ?
Jeff Glass 14/06/2022 20:47
Not I've heard about - presumably, you could Pyodide on 3.11b3 and point your py-env tag to it: https://github.com/pyscript/pyscript/blob/03fee56a54f3eac8c3fab69a5bd4365eadafd02c/docs/tutorials/getting-started.md#the-py-config-tag
20:47
Just curious - why?
20:47
Looking to play with 3.11, or do you have a specific thing in mind?
Avatar
Avatar
Jeff Glass
Just curious - why?
i wanted to build pygame dynamic for pyodide, and try numpy from pyodide in pygame-wasm ( which is 3.11b3 + pygame static) (edited)
20:48
cross over could be fun
20:48
some pygame games use numpy
Avatar
Jeff Glass 14/06/2022 20:52
Ah gotcha - what features from 3.11b3 are you using?
Avatar
hmm the core emscripten support for cross compilation for the CI, the sys.platform=='emscripten' , asyncio with a monkey patch, and of course no docker (edited)
Avatar
ha i also use ctypes/cffi for 2D physic engine
Avatar
Hello everyone, do we already have a solution for creating new html tags?
04:02
Avatar
Avatar
Bruno
Hello everyone, do we already have a solution for creating new html tags?
Jeff Glass 15/06/2022 14:20
So if you're trying to keep everything in Pyscript, you could do: <div id="id_div"></div> <py-script> div = Element("id_div") html_card = create("div", classes="alert alert-success") html_card.element.innerHTML = "&lt;strong&gt;Success!&lt;/strong&gt; This alert could indicate a successful or positive action" _ = div.element.appendChild(html_card.element) </py-script> Though personally, I think that's a little harder to read than just using some of the node attributes/methods directly: <div id="id_div"></div> <py-script> card = document.createElement("div") card.classList.add("alert", "alert-success") card.innerHTML = "&lt;strong&gt;Success!&lt;/strong&gt; This alert could indicate a successful or positive action" _ = document.getElementById("id_div").appendChild(card) </py-script>
โœ… 1
Avatar
Jeff Glass 15/06/2022 14:44
FWIW - The _ = ... at the end of each block is to prevent Pyscript printing the return value of the final statement
14:44
Avatar
SushiWaUmai 15/06/2022 15:21
Is it possible to import non wheel packages via py-env?
Avatar
Avatar
SushiWaUmai
Is it possible to import non wheel packages via py-env?
Jeff Glass 15/06/2022 16:26
Depends how they're packaged - what you looking to load?
Avatar
Avatar
Jeff Glass
Depends how they're packaged - what you looking to load?
SushiWaUmai 16/06/2022 12:37
I'm trying to load the package pretty-midi, but it seems like it is not possible since it is not a pure python3 wheel
12:38
Or is there any workaround for this?
Avatar
hello i need help my scipt is stuck here
Avatar
DennisChou 23/06/2022 00:11
Can you share the script
Avatar
Hey, I was wondering how you can import modules that are made with an __init__.py file. For example, I have my html page, which gets its functionality from a main python file, like so: <py-script src="./main.py"></py-script>. That part works fine, but when that main.py file imports from a module like from my_module import my_function, then it fails. I have put a py-env tag in the head of the html file, which looks like this:<py-env> - pandas - paths: - ./my_module </py-env> in which mymodule is a folder containing an `_init.py` file. Pandas gets imported fine, but the my_module part produces a ModuleNotFound error. (edited)
Avatar
Hi, first of thanks for developing this amazing project ๐Ÿ™‚ Second, the question I have <py-env> - package1 - package1 </py-env> Just read that to add packages you need this in your code. But I dont get the part where it says how to add packages that are not from the standard library.
23:07
So for example in my python file I import these packages: import json import requests from ruamel.yaml import YAML import os import time import capitalize
23:08
how can I get to install them? I just went through https://github.com/pyscript/pyscript/blob/main/examples/toga/freedom.html but honestly I still dont get it
Avatar
hey everyone i want to create a button that executes my pyScript I wanna create a py script that uses the input of the user (in a html input) and then outputs the response of the Api Thanks for any help <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" /> <script defer src="https://pyscript.net/alpha/pyscript.js"></script> </head> <body> <h1>PyScript - HTTP Requests pyfetch</h1> <label for="example">Let's submit some text</label> <input type="text" id="test-input"> <input type="submit" value="Send"> <div id="request_output"></div> <input type="text" id="test-input"> <py-button on_click> <py-script> from pyodide.http import pyfetch import asyncio varName = Element('test-input').element.value varURL = "https://api.agify.io?name=" + varName response = await pyfetch(url=varURL, method="GET") output = f"{await response.json()}" print(output) </py-script> </py-button> </body> </html> (edited)
Avatar
Avatar
Fluffy
hey everyone i want to create a button that executes my pyScript I wanna create a py script that uses the input of the user (in a html input) and then outputs the response of the Api Thanks for any help <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" /> <script defer src="https://pyscript.net/alpha/pyscript.js"></script> </head> <body> <h1>PyScript - HTTP Requests pyfetch</h1> <label for="example">Let's submit some text</label> <input type="text" id="test-input"> <input type="submit" value="Send"> <div id="request_output"></div> <input type="text" id="test-input"> <py-button on_click> <py-script> from pyodide.http import pyfetch import asyncio varName = Element('test-input').element.value varURL = "https://api.agify.io?name=" + varName response = await pyfetch(url=varURL, method="GET") output = f"{await response.json()}" print(output) </py-script> </py-button> </body> </html> (edited)
This is how I would do it. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" /> <script defer src="https://pyscript.net/alpha/pyscript.js"></script> </head> <body> <h1>PyScript - HTTP Requests pyfetch</h1> <label for="example">Let's submit some text</label> <input type="text" id="test-input"> <button id="submit-btn" pys-onClick="submit_request">Submit</button> <div id="request_output">Response Shown Here</div> <py-script> from pyodide.http import pyfetch import asyncio async def submit_request(*args): varName = Element('test-input').element.value varURL = "https://api.agify.io?name=" + varName response = await pyfetch(url=varURL, method="GET") output = f"{await response.json()}" pyscript.write('request_output',output) </py-script> </body> </html>
05:24
Of course, format the response as you like
Avatar
Avatar
Thrice
Hey, I was wondering how you can import modules that are made with an __init__.py file. For example, I have my html page, which gets its functionality from a main python file, like so: <py-script src="./main.py"></py-script>. That part works fine, but when that main.py file imports from a module like from my_module import my_function, then it fails. I have put a py-env tag in the head of the html file, which looks like this:<py-env> - pandas - paths: - ./my_module </py-env> in which mymodule is a folder containing an `_init.py` file. Pandas gets imported fine, but the my_module part produces a ModuleNotFound error. (edited)
Jeff Glass 25/06/2022 19:44
So unfortunately, that functionality is not currently possible - Pyscript currently takes each of the resources specified by โ€˜pathsโ€™, fetchs them one by one, and copies them into the same location in the browserโ€™s virtual file system as the Python script is running in. The import statement finds other scripts in the same folder, but of course this breaks modules (edited)
19:48
Clearly, this would be great functionality to have, if a Little difficult on the technical side. I opened an issue about it on Github (https://github.com/pyscript/pyscript/issues/519) a couple weeks ago. In the meantime you might check out this workaround by mudream of compiling the module to a wheel so itโ€™s a single file import: https://community.anaconda.cloud/t/workaround-for-importing-folder-by-compiling-wheel/23103 (edited)
Avatar
Ok, thanks for the advice
Avatar
When using pyfetch for making API requests, what are the possible parameters that can be put into the request? e.g., when using the requests module, I could have something like get = requests.get(url,params={'key':'value'}) where get_params is a dictionary of information that I want; and when making a put request, I could have put = requests.put(url,data={'key':'value'},headers={"Content-Type":"application/json"}) So, what are the equivalents of params, data, and headers for pyfetch? (edited)
Avatar
Avatar
Thrice
When using pyfetch for making API requests, what are the possible parameters that can be put into the request? e.g., when using the requests module, I could have something like get = requests.get(url,params={'key':'value'}) where get_params is a dictionary of information that I want; and when making a put request, I could have put = requests.put(url,data={'key':'value'},headers={"Content-Type":"application/json"}) So, what are the equivalents of params, data, and headers for pyfetch? (edited)
Jeff Glass 28/06/2022 08:03
FWIW, Pyfetch is just a convenience wrapper around the JavaScript fetch API. https://pyodide.org/en/stable/usage/api/python-api.html#pyodide.http.pyfetch To set the request method, set the value of the method key within the init argument to Pyfetch (its second argument). Data can be including under the body key of the same unit object; similarly for headers. Params can be passed in several ways, including by โ€œmanuallyโ€ appending them to the request URL, but I believe within JS the current preferred way is using a URLSearchParams object, which takes a literal string or a sequence of key/value pairs - itโ€™s toString() is the properly formatted params https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/URLSearchParams
Avatar
Jeff Glass 28/06/2022 08:20
Sorry wish I could give a more explicit example, currently on mobile
Avatar
Alright, I think I got that working, thanks, but I have one more question about pyfetch. When I'm requesting from a particular url, I get an error saying that my request has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. I understand the general reason why this error appears, but the thing is, when I make the exact same request with the python requests module, it has no problem and makes the request just fine. I was wondering if maybe there is something to do with pyfetch that requires extra permissions or something? I am using an API key and a client ID to make this request, if that is relevant.
Avatar
Jeff Glass 28/06/2022 18:37
Just for clarity, can I ask what the API is?
18:39
The lack of an access-control header is odd for a public API - have you tried adding mode: cors to the init object? (edited)
Avatar
Jeff Glass 28/06/2022 18:56
CORS is a restriction that your browser (the client) places on scripts to prevent them requesting/using data they shouldnโ€™t be from other origins. The Python interpreter doesnโ€™t know/care anything about CORS, so it retrieves and allows the use of that data without issues.
18:57
At least, thatโ€™s how I understand CORS, Iโ€™m sure a more seasoned JS dev would have a more nuanced explanation
Avatar
whatsavalue 30/06/2022 09:51
im getting a JsException(PythonError: Traceback (most recent call last): File "/lib/python3.10/site-packages/_pyodide/_base.py", line 429, in eval_code .run(globals, locals) File "/lib/python3.10/site-packages/_pyodide/_base.py", line 300, in run coroutine = eval(self.code, globals, locals) File "", line 5, in ModuleNotFoundError: No module named 'PIL' ) error, i have <py-env> - Pillow </py-env> in the html. why is PIL missing?
Avatar
Avatar
Jeff Glass
I'll confess to getting fully nerd-sniped by this question, and spent my lunch break whipping up a demo of how to handle image-uploading using Pyscript, as well as processing images with Pillow in-browser. It's a little big to embed here, so it's hosted over on my website: https://jeff.glass/post/pyscript-image-upload/
whatsavalue 30/06/2022 10:00
i was using this post as reference but pillow just doesnt exist for some reason. how did you get it to appear?
Avatar
Jeff Glass 30/06/2022 12:38
Huh, thatโ€™s odd. Just getting on a plane so Iโ€™ll have to get back to you
12:39
Do you have any errors from Micropip in the console?
12:40
Indentation might also be important - I can see I did <py-env> - Pillow </py-env>
Avatar
Avatar
Jeff Glass
Indentation might also be important - I can see I did <py-env> - Pillow </py-env>
whatsavalue 30/06/2022 17:02
17:03
yeah that was the issue
Avatar
I've got a question regarding network requests. How to do I call an API without blocking the UI thread? Like in JS you can just give fetch a callback function to call when the backend responds. Is there an equivalent for pyscript?
Avatar
Avatar
Calvin_
I've got a question regarding network requests. How to do I call an API without blocking the UI thread? Like in JS you can just give fetch a callback function to call when the backend responds. Is there an equivalent for pyscript?
use asyncio for that
Avatar
whatsavalue 01/07/2022 11:10
when i try to pyfetch www.someothersite.com/... it tries to fetch www.mydomain.com/www.someothersite.com/...
11:11
how do i make it fetch normally (edited)
Avatar
Avatar
whatsavalue
how do i make it fetch normally (edited)
Avatar
Avatar
whatsavalue
https
show us the pyfetch() code you used ?
Avatar
whatsavalue 01/07/2022 16:48
async def imageopenGETBYTES(text): text = text.replace("\\","/") text = text.replace("//","/") text = text.replace("./","") if( not text.startswith("http") ): url = f"https://raw.githubusercontent.com/relt-1/WindowCreator/main/{text}" else: url = text response = await pyfetch(url) if response.status == 200: return await response.bytes() it is a part from a pillow Image.open conversion
16:49
because i figured that this would be easier than just replacing each Image.open with a fetch
Avatar
Avatar
pmp-p
show us the pyfetch() code you used ?
whatsavalue 01/07/2022 16:52
.
Avatar
and where does it go instead of raw.githubusercontent.com ?
Avatar
whatsavalue 01/07/2022 16:53
its for a custom url
16:53
so if i want an imgur link i can use the same command
16:54
the code is here
16:54
and the part that is causing problems is this part
16:54
but weirdly
16:54
it works if you download the html and run it locally
Avatar
with custom url i get a CORB error https://chromestatus.com/feature/5629709824032768
Cross-Origin Read Blocking (CORB) is an algorithm that can identify and block dubious cross-origin resource loads in web browsers before they reach the web page. CORB reduces the risk of leaking sensitive data by keeping it further from cross-origin web pages. In most browsers, it keeps such data out of untrusted script execution contexts. In br...
Avatar
Avatar
pmp-p
with custom url i get a CORB error https://chromestatus.com/feature/5629709824032768
whatsavalue 01/07/2022 16:58
i use firefox
Avatar
same, it's a server problem not browser side
Avatar
whatsavalue 01/07/2022 16:59
Avatar
yes definitively the same error
Avatar
whatsavalue 01/07/2022 17:00
then what do i do
17:00
?
17:00
and why does it work with raw.githubusercontent.com
Avatar
change the fetching method to one that does not respect CORB
Avatar
Avatar
whatsavalue
and why does it work with raw.githubusercontent.com
because github.io allows raw.githubusercontent.com
Avatar
Avatar
pmp-p
change the fetching method to one that does not respect CORB
whatsavalue 01/07/2022 17:01
what fetching method do i use?
Avatar
try a dom element ?
Avatar
whatsavalue 01/07/2022 17:02
?
17:02
its my first time doing web dev btw
Avatar
a standard img tag should get the image
Avatar
whatsavalue 01/07/2022 17:02
and how do i get the img tag's data?
Avatar
the corb stuff is for scripts
Avatar
Avatar
whatsavalue
and how do i get the img tag's data?
via a blob iirc
Avatar
whatsavalue 01/07/2022 17:03
and how do i do that?
Avatar
no idea, tbh i would copy paste from mdn or stack overflow
Avatar
whatsavalue 01/07/2022 17:04
have no idea what you are talking about
17:04
what is a blob?
Avatar
all things that browser can load via tags is accessible via a blob
17:07
Avatar
whatsavalue 01/07/2022 17:08
and how do i convert it into a pillow image
Avatar
probably via a function from pyodide_js or writing it to a file from FS object
Avatar
Avatar
pmp-p
probably via a function from pyodide_js or writing it to a file from FS object
whatsavalue 01/07/2022 21:51
this tells me nothing to be honest
21:52
i would like some actual code to see so i can know whats the logic behind it
Avatar
sorry i just started pyscript about today, i was focused on mainline python and pygame-wasm instead of pyodide recently
22:03
tbh i'm still waiting for pyscript to run 3.11 before i dive in
Avatar
Avatar
whatsavalue
i would like some actual code to see so i can know whats the logic behind it
have you tried building a xmlhttprequest with the correct mimetype ?
22:05
maybe there are other options than binary fetch nope corb affects both fetch and XHR for image type (edited)
Avatar
Cyber Cryptic 02/07/2022 06:52
import json def danm(*args, **kwargs): username = document.getElementById("username").value password = document.getElementById("password").value print(username, password) #take username and password and check if they are in the json file #if they are,print user already exists #if they aren't, add their username and password to the json file #and print user created with open(r"E:\codez\spiral\login.json","r") as f: users = json.load(f) if username in users: print("user already exists") else: users[username] = password print("user created") with open(r"E:\codez\spiral\login.json", "w") as f: json.dump(users, f)
06:52
error 44
06:52
file not found
06:52
looked up but couldn't figure out a solution to it
Avatar
Cyber Cryptic 02/07/2022 07:01
@Bruno
07:01
welp
Avatar
Cyber Cryptic 02/07/2022 07:12
@Blastoderm help
Avatar
whatsavalue 02/07/2022 09:05
you have to host the file on a web server
09:05
and use pyfetch
09:05
but what youre doing would need some sql stuff
Avatar
Avatar
Cyber Cryptic
import json def danm(*args, **kwargs): username = document.getElementById("username").value password = document.getElementById("password").value print(username, password) #take username and password and check if they are in the json file #if they are,print user already exists #if they aren't, add their username and password to the json file #and print user created with open(r"E:\codez\spiral\login.json","r") as f: users = json.load(f) if username in users: print("user already exists") else: users[username] = password print("user created") with open(r"E:\codez\spiral\login.json", "w") as f: json.dump(users, f)
Jeff Glass 02/07/2022 19:51
So @whatsavalue is right that you'll need to use a (simple) web server to make your local files available for the browser to use. There's lots of ways to do this (especially if you're using an IDE like VS Code or Pycharm), but you can also create a local server in pure Python: see this example in the Pyscript issues https://github.com/pyscript/pyscript/issues/257#issuecomment-1119595062
19:53
That said, for accessing 'local' files, the preferred way is to use <py-env> and paths rather than pyfetch directly. For example: <py-env> - paths: - path/on/your_server/to/test.json </py-env> <py-script> import json with open("test.json","r") as f: value = json.load(f) print(value) </py-script>
19:55
Note that paths places the located file "in the same folder as" the Python script, so you open the file as you would any adjacent file in Python as simply "test.json" as opposed to "the/fully/qualified/path." You can put fully qualified URL's in the paths section, but there's no guarentee they'll be loaded correctly - paths is intended for files you're hosting that are (relatively) likely to be fetched correctly
20:01
Downloading files is a somewhat thornier issue in the browser, but here's a really rough example I put together earlier: https://discord.com/channels/972017612454232116/972020206538997822/979751023071604849
Avatar
Deleted User 04/07/2022 04:43
Is it currently possible to run a script from html? like python3 bot.py ? and run it on my server? that would..be amazing and be a huge plus to my hosting service im offering!
Avatar
Avatar
Deleted User
Is it currently possible to run a script from html? like python3 bot.py ? and run it on my server? that would..be amazing and be a huge plus to my hosting service im offering!
pyscript normally runs things on client, what do you mean by "and run it on my server" ?
10:08
be warned that it won't be easy - right now - to sandbox something like a complete jupyter python kernel with wasm
Avatar
Avatar
Deleted User
Is it currently possible to run a script from html? like python3 bot.py ? and run it on my server? that would..be amazing and be a huge plus to my hosting service im offering!
Jeff Glass 04/07/2022 17:54
Like @pmp-p said, thereโ€™s probably easier ways of running Python in your server than using Pyscript
Avatar
Avatar
pmp-p
pyscript normally runs things on client, what do you mean by "and run it on my server" ?
Deleted User 04/07/2022 17:56
just wanna execute scripts based on key being used, for example key 125123 has the ability to "see" and "start/stop" bot.py, so by clicking a button they can start their bot, or, stop it! OR by typing into a text box, either way.
17:56
users have no ability to edit files, so, just keeping the bot online
17:56
if it ever stops
17:56
py-script looks super useful and cool though, ima def be using it ๐Ÿ˜„
Avatar
Jeff Glass 04/07/2022 18:50
Mmm so roughly speaking, if bot.py is running on your server, youโ€™d need some way for the what the client sees on their web interface to affect the status of behavior of that script. That sounds to me like making API calls to a server, thatโ€™s going to handle those calls and decide how to affect the running scripts
18:52
What makes that API call on the client end could be JS, Pyscript (probably via JS) etc, the choice is up to you
18:52
User validation, ie โ€œuser with this identity can take these actionsโ€ is again a server-side behavior that doesnโ€™t really have to do with Pyscript per se
18:53
Though if you like writing Python either the Django or Flask frameworks may be of interest
Avatar
options = { 'series': [44, 55, 13, 43, 22], 'chart': { 'width': 380, 'type': 'pie', }, 'labels': ['Team A', 'Team B', 'Team C', 'Team D', 'Team E'], 'responsive': [{ 'breakpoint': 480, 'options': { 'chart': { 'width': 200, }, 'legend': { 'position': 'bottom' } } }] } chart = ApexCharts.new(document.querySelector("#chart"), options) chart.render()
18:14
it seems that ApexCharts is having a hard time parsing the options dict (edited)
18:14
specifically cant find 'type' in 'charts' (edited)
18:14
wondering if i gotta do something special or whatever
Avatar
Jeff Glass 06/07/2022 18:19
Hmmm are you seeing JsException: TypeError: Cannot read properties of undefined (reading 'type') )
Avatar
Jeff Glass 06/07/2022 18:21
Just for testing purposes, what type of tag is #chart
Avatar
a div
18:23
i can get a chart if i load the data into the div and then do this
18:23
options = { 'series': [44, 55, 13, 43, 22], 'chart': { 'width': 380, 'type': 'pie', }, 'labels': ['Team A', 'Team B', 'Team C', 'Team D', 'Team E'], 'responsive': [{ 'breakpoint': 480, 'options': { 'chart': { 'width': 200, }, 'legend': { 'position': 'bottom' } } }] } chart_el = Element('chart') chart_el.element.setAttribute('data-options', json.dumps(options)) ApexCharts.initOnLoad()
18:24
with chart being this
18:24
<div id="chart" data-apexcharts data-options=""> </div>
Avatar
Jeff Glass 06/07/2022 18:27
Interesting, that doesn't work for me
Avatar
dont forget import json from js import ApexCharts
Avatar
Jeff Glass 06/07/2022 18:28
ah thank you, hadn't imported json ๐Ÿ˜…
18:30
Seems like this also works: chart = ApexCharts.new(document.querySelector("#chart"), json.dumps(options)) chart.render()
Avatar
pretty sure i tried that, but ill try again
Avatar
Jeff Glass 06/07/2022 18:31
Oh no, my mistake
18:31
Was using the wrong .py file as src, you were right, that fails with 'cannot read properties of undefined' still
Avatar
if i do to_js(options) and print it to the console i can see that type is wrapped in quotes
Avatar
Jeff Glass 06/07/2022 18:37
So probably not some weird "Python thinks this is the type keyword" issue then
18:37
Same with json.dumps(options) it looks like
Avatar
Jeff Glass 06/07/2022 18:51
Just to ask - are you working in vanilla HTML, or within a framework (react, angular etc)?
Avatar
vanilla html
18:55
i think it's the quotes
18:55
here is raw java output of options
18:55
18:56
and here is from pyscript
18:56
18:56
actually it looks like the data structure is very different
18:57
ohhh
18:57
javascript has it as an object. pyscript has it as a map
Avatar
Jeff Glass 06/07/2022 18:57
ohhhhh
Avatar
from js import ApexCharts, Object options = { 'series': [44, 55, 13, 43, 22], 'chart': { 'width': 380, 'type': 'pie', }, 'labels': ['Team A', 'Team B', 'Team C', 'Team D', 'Team E'], 'responsive': [{ 'breakpoint': 480, 'options': { 'chart': { 'width': 200, }, 'legend': { 'position': 'bottom' } } }] } chart = ApexCharts.new(document.querySelector("#chart"), to_js(options, dict_converter=Object.fromEntries)) chart.render()
19:02
that works
Avatar
Jeff Glass 06/07/2022 19:03
Excellent!
19:03
Well sleuthed
Avatar
thanks for helping (edited)
Avatar
Jeff Glass 06/07/2022 19:03
Hey no sweat, you did pretty much the whole thing
19:04
Wanna drop a quick sentence in #โ•ฐโ•ธqueries-you-faced about being being careful of maps vs. objects in arguments?
19:04
That channel's pretty much "Here's one sentence of a problem and a one-sentence summary of the solution", just to help folks find answers
Avatar
ok sure
Avatar
Jeff Glass 06/07/2022 19:06
Thanks, from all of us in the future ๐Ÿ‘ (edited)
Avatar
i dont know why pyodide would default to map when javascript makes it an object by default
Avatar
Jeff Glass 06/07/2022 19:15
I presume it's so round-trip conversations are consistant - dict to map to dict makes sense, but if the default was dict to object, the conversion back to Python would be ambiguous
19:15
Just a guess
19:17
Does explain why dumping it to a string in the html attributes worked though, JS got to parse the string as an object like it wanted
Avatar
word
Avatar
hey guys, currently im building a simple ui to manage sqlite entries. So far reading the sql database works fine, but i cant add a new entry to it Is there something like you can read files like .db but cant write into them
Avatar
Avatar
Alpaycino
hey guys, currently im building a simple ui to manage sqlite entries. So far reading the sql database works fine, but i cant add a new entry to it Is there something like you can read files like .db but cant write into them
Jeff Glass 13/07/2022 22:02
Do you have a code snippet we can look at? Either pasting here in a code block or a link to a GitHub gist or similar?
Avatar
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
Avatar
Hello, I have a question regarding PyScript and iframes.
04:29
Currently, if I write the "hello world" example to an iframe, it will not work. However, the same code obviously works, just not in an iframe environment. (edited)
04:29
So does PyScript support iframes?
04:35
or is there a known workaround I'd need to implement for this to work?
Avatar
Avatar
pmp-p
via a blob iirc
whatsavalue 23/07/2022 10:01
does not work
10:02
"the operation is insecure ๐Ÿค“" when trying to get data from img which src was imgur (edited)
Avatar
Avatar
whatsavalue
"the operation is insecure ๐Ÿค“" when trying to get data from img which src was imgur (edited)
that's tough
Avatar
Avatar
pmp-p
that's tough
whatsavalue 23/07/2022 10:29
i found a better way
Avatar
which is ?
Avatar
whatsavalue 23/07/2022 10:29
10:30
and the img tag has to have crossorigin="anonymous"
10:31
im still not sure if it works
10:31
but it fetches the data successfully
10:31
atleast on the html on my hard drive
Avatar
yeah lots of things that worked in the past with XHR are starting to fail
Avatar
whatsavalue 23/07/2022 10:32
its complaining about the type of the byte array
10:32
trying to fix that
10:32
rn
Avatar
whatsavalue 23/07/2022 10:40
yep
10:43
@pmp-p IT WORKS!!!
10:43
Free and open-source error message generator. Supports Windows XP, Windows 95, Windows 7 and more!
10:44
Avatar
Avatar
CHAN
Currently, if I write the "hello world" example to an iframe, it will not work. However, the same code obviously works, just not in an iframe environment. (edited)
Jeff Glass 23/07/2022 14:24
Huh, interesting. Are you seeing any errors in the browser console? Are you seeing any output at all, or the just the โ€œcode being read as textโ€ where the Python is just dumped onto the page?
Avatar
Avatar
Jeff Glass
Huh, interesting. Are you seeing any errors in the browser console? Are you seeing any output at all, or the just the โ€œcode being read as textโ€ where the Python is just dumped onto the page?
The code is just being read as plain text. No errors in the console, which I do find odd
Avatar
Avatar
CHAN
The code is just being read as plain text. No errors in the console, which I do find odd
Jeff Glass 23/07/2022 18:47
Thatโ€™s usually a sign that Pyscript.js isnโ€™t loading - the browser doesnโ€™t know what to do with the text in the <py-script> tag, so it just dumps the text into the DOM.
Avatar
Avatar
Jeff Glass
Thatโ€™s usually a sign that Pyscript.js isnโ€™t loading - the browser doesnโ€™t know what to do with the text in the <py-script> tag, so it just dumps the text into the DOM.
Ah, I see. Wouldnโ€™t Pyscript.js not loading generate some kind of error? Like, โ€œfile not foundโ€? Iโ€™m not near my PC at the moment, but Iโ€™ll test some things later
Avatar
Jeff Glass 23/07/2022 18:50
Yeah similarly away from a computer, but I would think so? Maybe the <script> tags itself is being prevented from executing within the iframe?
18:50
I guess just to ask, are you loading Pyscript in the iframe or only at the document level?
Avatar
Iโ€™m creating an iframe element (which lets me manipulate its DOM without any โ€œcross-originโ€ issues), and using document.write to generate its contents. This works when Iโ€™m using other resources (I can load jQuery via the CDN link in a script element, or W3.css via a stylesheet element), but for not for pyscript
18:56
In a bit I can post some demo code if youโ€™d like, but weโ€™re both not near a PC so it may not help at the moment ๐Ÿ˜‚
๐Ÿ‘ 1
Avatar
Jeff Glass 23/07/2022 19:00
Yeah makes sense! Curious what this ends up being
19:03
Hmmm one side thought - are you using document.write to create the a py-script tag? If I recall right, by default Pyscript only executes tags that are present when Pyscript.js loads
Avatar
Avatar
Jeff Glass
Hmmm one side thought - are you using document.write to create the a py-script tag? If I recall right, by default Pyscript only executes tags that are present when Pyscript.js loads
This could be the problem? Everything is written to the DOM at the same time. Iโ€™ll try staggering whatโ€™s written with a code demo later
Avatar
hi - how can we call python function from javascript function? # main.py def something(): print("from python") <py-script src="./main.py"> </py-script> <script> function myFunction(){ something() } </script> ^ this doesn't work - Uncaught ReferenceError: something is not defined
09:15
https://pyscript.net/ mentions Python with JavaScript: Bi-directional communication between Python and Javascript objects and namespaces -- how can we achieve this?
Avatar
Leonard Wolf 24/07/2022 10:53
Aha #โ•ฐโ•ธqueries-you-faced
โœ… 1
Avatar
Avatar
CHAN
This could be the problem? Everything is written to the DOM at the same time. Iโ€™ll try staggering whatโ€™s written with a code demo later
Jeff Glass 25/07/2022 16:40
Think I found an answer to why Py-script doesn't seem to work in iFrames - as succictly put by this StackOverflow answer, customElementsRegistry is a window Global object; an iFrame is another window https://stackoverflow.com/a/67901528
16:41
(Py-script uses customElements to define the behavior of its tags <py-script> <py-repl> etc) (edited)
16:42
So Pyscript won't associate a <py-script> tag within an iFrame as a Pyscript object (in Typescript) that actually has code that needs to run etc, since it doesn't know there are customElements inside the iframe
16:42
that said, as a workaround, you can just import Pyscript.js inside the iframe (edited)
16:43
So these snippets (in the context of the hello_world example) work: <!-- hello_world.html --> ... <script defer src="https://pyscript.net/latest/pyscript.js"></script> <body> <iframe src="frame.html" title="MyFrame" style="width: 100%;"></iframe> <py-script> print(f"{1 + 1 = }") <py-script> </body> ... <!-- frame.html --> <span>Hello from inside an iframe</span> <script defer src="https://pyscript.net/latest/pyscript.js"></script> <py-script> from datetime import datetime now = datetime.now() now.strftime("%m/%d/%Y, %H:%M:%S") </py-script> (edited)
Avatar
Ah, okay I see. Iโ€™ll test this out in a bit
16:46
Thanks for all your help!!
Avatar
Jeff Glass 25/07/2022 16:47
Anytime! ๐Ÿ‘
Avatar
Blastoderm 27/07/2022 21:54
Hello! I wrote this question on Reddit a month ago: https://www.Reddit.com/r/PyScript/comments/vf2136/esm_module_mjs_in_pyscript/ But no 1 showed up yet. ๐Ÿ˜ฆ
2 votes and 0 comments so far on Reddit
21:56
I wonder if PyScript can use the import() operator inside its Python code: https://developer.Mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import (edited)
The import() call, commonly called dynamic import, is a function-like expression that allows loading an ECMAScript module asynchronously and dynamically into a potentially non-module environment.
Avatar
Avatar
Blastoderm
Hello! I wrote this question on Reddit a month ago: https://www.Reddit.com/r/PyScript/comments/vf2136/esm_module_mjs_in_pyscript/ But no 1 showed up yet. ๐Ÿ˜ฆ
Jeff Glass 28/07/2022 15:45
For a really basic example of just using .mjs files within Pyscript, you could do something like: // add.mjs function addTwoNumbers(a, b) { return a + b; } <!-- index.html --> <script src="add.mjs"></script> <py-script> from js import addTwoNumbers print(f"{addTwoNumbers(1, 2)= }") </py-script> (edited)
15:46
That doesn't really answer your question about using import() though, assuming you actually want to do dynamic imports
15:51
My guess is that it's easier to handle dynamic imports by executing JS functions from pyscript, rather than trying to call Javascripts import() directly?
15:52
You could potentially using Pyscript to create a new <script> tag with the statements you want to run, and append it to the DOM
Avatar
Avatar
Blastoderm
I wonder if PyScript can use the import() operator inside its Python code: https://developer.Mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import (edited)
Jeff Glass 28/07/2022 16:14
I don't think it's possible to use javascript's import() directly, since it's not a function, just a keyword that looks like a function, but I could be wrong there (edited)
Avatar
Newbie here, simple app idea, using fpdf2, user inputs data into form, it generates unique pdf and downloads it. 1) Will fpdf 2 work with PyScript? If so, I can't get it to output pdf file. 2) How do I get files to download? requests/PyFetch? I'm a more lost here. Any help would be greatly appreciated!!
Avatar
Avatar
Jeff Glass
<style> input{ border-width: 4px; } </style> <body> <p>Type a string here to save it to file:</p> <label for="Type a password here:"></label><input type="text" id="pwd"> <br><br> <p>File name: </p> <head> <script defer src="https://pyscript.net/alpha/pyscript.js"></script> </head> <body> <label for="Type a password here:"></label><input type="text" id="filename"> <button id="download">Download</button> <py-script> from js import document, Blob, window from pyodide import create_proxy def _download_contents_of_field(field_name): text_to_save = document.getElementById("pwd").value blob_of_text = Blob.new(([text_to_save])) url = window.URL.createObjectURL(blob_of_text) file_name = document.getElementById("filename").value link = document.createElement("a"); link.download = file_name; link.href = url; link.click() download_contents_of_field = create_proxy(_download_contents_of_field) document.getElementById("download").addEventListener("click", download_contents_of_field) </py-script> </body> (edited)
I'm trying to wrap my head around this in the hopes to generate custom pdfs and have users download them... so far I'm just getting a grasp on JS + Python in the browser... argh, my head. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script defer src="https://pyscript.net/alpha/pyscript.js"></script> <!-- <link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" /> --> <title>Download Example</title> </head> <body> <!-- String --> <label for="string">Type a string here to save it to file:</label> <br><br> <input type="text" id="string"> <br><br> <!-- Filename --> <label for="filename">File name:</label> <br><br> <input type="text" id="filename"> <br><br> <!-- Download Buttons --> <button id="download">Download</button> <div id="notification"></div>
02:02
<py-script> from js import document, Blob, window from pyodide import create_proxy notification_area = Element('notification') # Creates download function, passes def _download_contents_of_field(field_name): # JS grabs id string value, stores it text_to_save = document.getElementById("string").value # Saves string value to blob variable blob_of_text = Blob.new(([text_to_save])) # Stores blob in url to be made as link url = window.URL.createObjectURL(blob_of_text) # JS grabs id filename value, stores it file_name = document.getElementById("filename").value # Check inputs are not empty if not text_to_save == "" and not blob_of_text == "": # Creates a tag/attribute link = document.createElement("a") link.download = file_name link.href = url link.click() notification_area.write('') else: notification_area.write('Inputs Empty') # Download variable stores proxied download function download_contents_of_field = create_proxy(_download_contents_of_field) # Gets id download button, waits for click and passes download variable document.getElementById("download").addEventListener("click", download_contents_of_field) </py-script> </body> </html>
Avatar
Avatar
Ash
Newbie here, simple app idea, using fpdf2, user inputs data into form, it generates unique pdf and downloads it. 1) Will fpdf 2 work with PyScript? If so, I can't get it to output pdf file. 2) How do I get files to download? requests/PyFetch? I'm a more lost here. Any help would be greatly appreciated!!
Jeff Glass 29/07/2022 03:28
Seems like a nifty idea! Here's a basic working example I put together. (You'll have to work out all the fpdf2 parts yourself, I just copied the example code from the fpdf2 docs website): from fpdf import FPDF import io from js import document, window, Uint8Array, File #Some very basic PDF creation, using the example on the fpdf documentation pdf = FPDF() pdf.add_page() pdf.set_font('helvetica', size=12) pdf.cell(txt="hello world") #Create a BytesIO object (acts like a file in memory) to hold PDF output my_stream = io.BytesIO() #Write bytes of PDF to BytesIO object pdf.output(my_stream) #Create new File object (see JS File API). #Casting to Uint8Array necessary to prevent my_stream's value from being interpretted #as one giant number pdf_file = File.new([Uint8Array.new(my_stream.getvalue())], "hello_world.pdf", {"type": "application/pdf"}) #Get a URL to reference this in-memory file object url = window.URL.createObjectURL(pdf_file) #Create link (wihtout placing it in DOM) with download attribute, point it at our URL, and click it link = document.createElement("a") link.download = 'hello_world.pdf' link.href = url link.text = "Download PDF" _ = document.body.appendChild(link) #assignment prevents Pyodide/pyscript from printing return value
โ™ฅ๏ธ 1
03:29
Of course at the end, there's no reason you'd have to create the link element manually and add it to the DOM - you could have the link already on your page and just point the link's href and download attributes appropriately (edited)
03:33
My general strategy for using python modules that are meant to output to a file is: (1) Create a BytesIO object (acts like an 'in-memory file', mostly) (2) Use the module to write to that BytesIO object as if it were the file (3) Create a new (js) File object, casting the BytesIO object to a (js) Uint8Array with the appropriate MIME type (4) Create a url for that object using createObjectURL (5) Link to that object, being sure to set the download attribute so the file downloads instead of just opening in the browser
Avatar
Avatar
Jeff Glass
Seems like a nifty idea! Here's a basic working example I put together. (You'll have to work out all the fpdf2 parts yourself, I just copied the example code from the fpdf2 docs website): from fpdf import FPDF import io from js import document, window, Uint8Array, File #Some very basic PDF creation, using the example on the fpdf documentation pdf = FPDF() pdf.add_page() pdf.set_font('helvetica', size=12) pdf.cell(txt="hello world") #Create a BytesIO object (acts like a file in memory) to hold PDF output my_stream = io.BytesIO() #Write bytes of PDF to BytesIO object pdf.output(my_stream) #Create new File object (see JS File API). #Casting to Uint8Array necessary to prevent my_stream's value from being interpretted #as one giant number pdf_file = File.new([Uint8Array.new(my_stream.getvalue())], "hello_world.pdf", {"type": "application/pdf"}) #Get a URL to reference this in-memory file object url = window.URL.createObjectURL(pdf_file) #Create link (wihtout placing it in DOM) with download attribute, point it at our URL, and click it link = document.createElement("a") link.download = 'hello_world.pdf' link.href = url link.text = "Download PDF" _ = document.body.appendChild(link) #assignment prevents Pyodide/pyscript from printing return value
Thanks for your reply, I really appreciate the detail!! I'll spend a bit of time with it ๐Ÿ™‚ ๐Ÿ™‚ (edited)
06:38
Well your code works like a dream... amazing.
Avatar
Avatar
Jeff Glass
My general strategy for using python modules that are meant to output to a file is: (1) Create a BytesIO object (acts like an 'in-memory file', mostly) (2) Use the module to write to that BytesIO object as if it were the file (3) Create a new (js) File object, casting the BytesIO object to a (js) Uint8Array with the appropriate MIME type (4) Create a url for that object using createObjectURL (5) Link to that object, being sure to set the download attribute so the file downloads instead of just opening in the browser
Here's the rough WIP https://mrashleyball.github.io/pd-gen/ I'm also new to JS, so no idea how to get the download button working as intended... but with a bit more time I'll get there. Thanks again for your help, it's one thing to learn Python locally and a WHOLE other thing within the browser, fun fun fun! (edited)
Avatar
Avatar
Jeff Glass
Seems like a nifty idea! Here's a basic working example I put together. (You'll have to work out all the fpdf2 parts yourself, I just copied the example code from the fpdf2 docs website): from fpdf import FPDF import io from js import document, window, Uint8Array, File #Some very basic PDF creation, using the example on the fpdf documentation pdf = FPDF() pdf.add_page() pdf.set_font('helvetica', size=12) pdf.cell(txt="hello world") #Create a BytesIO object (acts like a file in memory) to hold PDF output my_stream = io.BytesIO() #Write bytes of PDF to BytesIO object pdf.output(my_stream) #Create new File object (see JS File API). #Casting to Uint8Array necessary to prevent my_stream's value from being interpretted #as one giant number pdf_file = File.new([Uint8Array.new(my_stream.getvalue())], "hello_world.pdf", {"type": "application/pdf"}) #Get a URL to reference this in-memory file object url = window.URL.createObjectURL(pdf_file) #Create link (wihtout placing it in DOM) with download attribute, point it at our URL, and click it link = document.createElement("a") link.download = 'hello_world.pdf' link.href = url link.text = "Download PDF" _ = document.body.appendChild(link) #assignment prevents Pyodide/pyscript from printing return value
I've still got to finish it, the pdfs it generates are still default nearly. But again, thank you! I don't know if I would've been able to do it without your help, especially not within the day.
Avatar
Jeff Glass 29/07/2022 15:35
@Ash Absolutely! That site's looking decent already, and from the context text it seems like a useful use case. (edited)
โค๏ธ 1
15:36
FWIW If you want to combine the 'download' and 'generate' buttons, you can add link.click() to the end of your generate function, to have JS click the hidden download link it creates
Avatar
Ah thanks, yeah it's something that I actually need. And that's exactly what I want to do, you read my mind!
๐Ÿฅณ 1
Avatar
Avatar
Jeff Glass
My general strategy for using python modules that are meant to output to a file is: (1) Create a BytesIO object (acts like an 'in-memory file', mostly) (2) Use the module to write to that BytesIO object as if it were the file (3) Create a new (js) File object, casting the BytesIO object to a (js) Uint8Array with the appropriate MIME type (4) Create a url for that object using createObjectURL (5) Link to that object, being sure to set the download attribute so the file downloads instead of just opening in the browser
Question (again sorry), is reading/opening a file the same? I'm trying to build out the pdf by adding an image via pdf.image() etc. but I keep getting FileNotFoundError: [Errno 44] No such file or directory: 'cert.png' - but it's there!
Avatar
Deleted User 30/07/2022 14:13
Why isnt this working? <py-env>
  • web3
</py-env> <p id="text"></p> <py-script > import web3 bsc = 'https://bsc-dataseed.binance.org/' web3 = Web3(Web3.HTTPProvider(bsc)) print(web3.isConnected()) </py-script>
14:14
my page only loads, but nothing shows up
Avatar
Deleted User 30/07/2022 14:39
something is up with my web3 module
14:39
I have installed it but when I use <py-env></py-env> the site keeps on loading
Avatar
Avatar
Deleted User
I have installed it but when I use <py-env></py-env> the site keeps on loading
Jeff Glass 30/07/2022 16:43
If you open up the developer console in your browser, do you see any error messages? Possibly something about micropip or pure Python wheels? Either way, itโ€™s unlikely youโ€™ll be able to use this particular library without significant modifications - just from a quick glance at the docs/GitHub, it looks like it makes heavy use of requests and sockets, neither of which are directly available in a browser environment (edited)
Avatar
Avatar
Ash
Question (again sorry), is reading/opening a file the same? I'm trying to build out the pdf by adding an image via pdf.image() etc. but I keep getting FileNotFoundError: [Errno 44] No such file or directory: 'cert.png' - but it's there!
Jeff Glass 30/07/2022 16:45
To ask the question - is the cert.png โ€˜thereโ€™ as in on-your-harddrive, or are you using a server program/setup to actually make it available to the browser? (edited)
Avatar
Deleted User 30/07/2022 18:33
File "/lib/python3.10/site-packages/micropip/_micropip.py", line 286, in add_requirement raise ValueError( ValueError: Couldn't find a pure Python 3 wheel for 'parsimonious<0.9.0,>=0.8.0'. You can use micropip.install(..., keep_going=True) to get a list of all packages with missing wheels.
Avatar
Avatar
Jeff Glass
To ask the question - is the cert.png โ€˜thereโ€™ as in on-your-harddrive, or are you using a server program/setup to actually make it available to the browser? (edited)
There as in on-the-harddrive. I'm still not quite grasping the 'file system to browser' relationship. I'm reading https://www.jhanley.com/pyscript-files-and-file-systems-part-1/ which is helping.
This will be the first of several articles that provides details on Pyscript files and file systems. I will discuss the different virtual file systems and how to access files located on the desktop. I have put each example in... Continue Reading โ†’
Avatar
Dam, still a bit lost with serving images so that the browser can use it. Any help would be greatly apprecaited ๐Ÿ™‚ Locally, to add an image to be used it's as simple as pdf.image('image.png') - it's confusing because if I just navigate in the browser to localhost/image.png it's there, the browser can see it. But to have PyScript fetch that image.png file to then allow fpdf2 to use it, computer says no. Even when reading/watching tutorials, everything explains user interaction is required, which makes sense. But in my case, sure the user selects 'generate' - but they're not uploading a file, the file is already there. Why is this so hard (for me)?
Avatar
Avatar
Ash
Dam, still a bit lost with serving images so that the browser can use it. Any help would be greatly apprecaited ๐Ÿ™‚ Locally, to add an image to be used it's as simple as pdf.image('image.png') - it's confusing because if I just navigate in the browser to localhost/image.png it's there, the browser can see it. But to have PyScript fetch that image.png file to then allow fpdf2 to use it, computer says no. Even when reading/watching tutorials, everything explains user interaction is required, which makes sense. But in my case, sure the user selects 'generate' - but they're not uploading a file, the file is already there. Why is this so hard (for me)?
Jeff Glass 31/07/2022 22:00
Happy to help if we can! Can you drop your code somewhere we can have a look, maybe in a pastebin?
Avatar
Avatar
Deleted User
File "/lib/python3.10/site-packages/micropip/_micropip.py", line 286, in add_requirement raise ValueError( ValueError: Couldn't find a pure Python 3 wheel for 'parsimonious<0.9.0,>=0.8.0'. You can use micropip.install(..., keep_going=True) to get a list of all packages with missing wheels.
Jeff Glass 31/07/2022 22:02
Ah yes, so micropip (part of the Pyodide runtime) is only able to load pure python wheels from PyPI. (Looks like parismonious only has a source tarball https://pypi.org/project/parsimonious/#files). You may be able to download the source, build a wheel yourself and include it?
Avatar
Avatar
Jeff Glass
Happy to help if we can! Can you drop your code somewhere we can have a look, maybe in a pastebin?
Sorry for delayed reply, thank you! All code is here: https://github.com/mrashleyball/iwasthere John Hanley helped via the PyScript forums and I've now got an image working with fpdf2 and PyScript... almost. I'm still getting my head around async and await ... but the program is freezing once the generate function is called. You can test it here to see what I mean: https://iwasthere.mrash.co/ (edited)
I Was There: Certificate Generator for Professional Development - GitHub - mrashleyball/iwasthere: I Was There: Certificate Generator for Professional Development
Avatar
I'm trying to call a JS function from pyscript, are there instructions for that?
Avatar
Avatar
John S
I'm trying to call a JS function from pyscript, are there instructions for that?
Jeff Glass 03/08/2022 21:12
The most thorough explanation is in the pyodide docs (https://pyodide.org/en/stable/usage/type-conversions.html#importing-javascript-objects-into-python) The short answer is: you can import any object in Javascript's "Global This" scope into Python using from js import ... Example: from js import console, Document my_p_tag = Document.getElementById("first_paragraph") console.log(f"first_paragraph contains {my_p_tag.innerHTML}"} If you ended up logging JSProxy objects or PyProxy objects, the to_js and toPy functions from the pyodide API reference are helpful (https://pyodide.org/en/stable/usage/api-reference.html)
Avatar
I use that for DOM manipulation, but I'm trying to call a JS function and I can't quite get it. The function runs from the console. Is there some namespace detail I'm missing?
Avatar
Jeff Glass 03/08/2022 21:37
Hmmm can you share your code as it is so far? Either as an embed here or on pastebin if itโ€™s longer? (edited)
Avatar
I've found the script in documents.scripts.modals
21:38
modals is the name of the script
21:42
It'll take some simplifying
21:54
I figured it out, the <script> element with the missing function was after the py-script <script> (edited)
๐Ÿ‘ 1
21:54
๐Ÿคฆโ€โ™‚๏ธ
21:55
thank you!
Avatar
Jeff Glass 03/08/2022 21:57
Nice!
Avatar
Jeff Glass 03/08/2022 23:16
@Ash It mostly works for me too! Just a couple of tweaks to the last 'paragraph' of your generate function: # Remove spaces and return with dashes for filename fname, lname, event = rmvSpaces(fname), rmvSpaces(lname), rmvSpaces(event) #link = document.getElementById('generate') Removed link = document.createElement("a") # << Added link.setAttribute('download', f'iwasthere-{fname}-{lname}-{event}-{time_now}.pdf'.lower()) link.setAttribute('href', url) #link.setAttribute('text', 'Download') link.click() # << Add this! # link.setAttribute('class', 'button is-success')
23:20
You want to separate manipulation of the link that's already on the page that the user clicks, from the "hidden" link (never even added to the DOM) that the browser "clicks" to download the file after the pdf is done generating - in the code above, the link object is the hidden link.
23:22
If you want to change the link appearance after download, you could add something like this below that code: onscreen_link = document.getElementById('generate') onscreen_link.setAttribute('class', 'button is-success') (edited)
23:25
It works! It does seem like pdf.image() is taking ages to add the image data to the pdf... like 30-60 seconds for me. Downscaling that image or changing to a compressed file type like jpeg might help?
Avatar
Avatar
Jeff Glass
It works! It does seem like pdf.image() is taking ages to add the image data to the pdf... like 30-60 seconds for me. Downscaling that image or changing to a compressed file type like jpeg might help?
Wow! Awesome!! I'll spend some more time with this soon, thank you so much. Now you can use that certificate as evidence wherever you like haha
๐Ÿ˜ 1
Avatar
Avatar
Ash
Wow! Awesome!! I'll spend some more time with this soon, thank you so much. Now you can use that certificate as evidence wherever you like haha
Jeff Glass 03/08/2022 23:38
Glad to be of help!
Avatar
Avatar
Jeff Glass
It works! It does seem like pdf.image() is taking ages to add the image data to the pdf... like 30-60 seconds for me. Downscaling that image or changing to a compressed file type like jpeg might help?
๐Ÿฅณ 1
Avatar
zaki ๐Ÿ‡ฑ๐Ÿ‡ฐ 07/08/2022 23:04
Hi, sorry if this has already been asked, but what's the best way to transfer either
  • A certain data from a JS script to a Pyscript function
or
  • A whole image from a JS script (Jcrop to be specific) to Pillow?
Avatar
Avatar
zaki ๐Ÿ‡ฑ๐Ÿ‡ฐ
Hi, sorry if this has already been asked, but what's the best way to transfer either
  • A certain data from a JS script to a Pyscript function
or
  • A whole image from a JS script (Jcrop to be specific) to Pillow?
Jeff Glass 08/08/2022 19:14
From a quick glance, it looks like jcrop is mostly a UI library, and isn't responsible for actually cropping the image, true?
19:15
You'd need to retrieve the state of the cropping widget, then use the properties of the widget to actually do the cropping in Pillow https://jcrop.com/objects/rect.html#properties
Avatar
Avatar
Jeff Glass
You'd need to retrieve the state of the cropping widget, then use the properties of the widget to actually do the cropping in Pillow https://jcrop.com/objects/rect.html#properties
zaki ๐Ÿ‡ฑ๐Ÿ‡ฐ 08/08/2022 19:23
thanks. My question was, suppose I use a tuple /array in js to store this rect properties, is it accessible by default in pyscript? (edited)
Avatar
Jeff Glass 08/08/2022 20:22
It is if it exists at the global scope, yes. But if you're doing anything event-driven, IMHO it's safer to retrieve the relevant data at access time
20:22
So, rather than storage the data in a JS array and reading to it from Python, write an event in Python that just retrieves the appropriate data
20:23
This was my testing script (supply your own picture.jpeg):
20:23
<head> <link rel="stylesheet" href="https://unpkg.com/jcrop/dist/jcrop.css"> <script src="https://unpkg.com/jcrop"></script> <script defer src="https://pyscript.net/latest/pyscript.js"></script> </head> <body> <img src="./picture.jpg" id="target"> <p id="output"></p> <script> var stage = Jcrop.attach('target'); </script> <py-script> from js import stage from pyodide import create_proxy def print_x(widget, evt): pos = widget.pos Element("output").write([pos.x,pos.y,pos.w,pos.h], append="True") stage.listen('crop.change', create_proxy(print_x)); </py-script> </body> (edited)
20:25
Made a couple edits just now to take out some unnecessary cruft in there
20:25
Honestly you could probably do it all in Pyscript... lemee see...
Avatar
zaki ๐Ÿ‡ฑ๐Ÿ‡ฐ 08/08/2022 20:27
that works! Wait let me get this clear,, you called stage.listen from within pyscript? But its a JS method? does that work? Is it because you did from js import stage?
Avatar
Jeff Glass 08/08/2022 20:28
Yep! Pyodide (the runtime underneath Pyscript) does some amazing things with type conversion, so once you grab a javascript object, you can access its properties and methods just like it was a Python object https://pyodide.org/en/stable/usage/type-conversions.html
20:29
There's edge cases where it breaks, and a couple of funky bits of syntax - biggest is that, since there's no new keyword in Python, you instantiate new JS objects in Python with x = Object.new() instead of the JS's x = new Object().
20:30
But mostly it just works
Avatar
zaki ๐Ÿ‡ฑ๐Ÿ‡ฐ 08/08/2022 20:31
Alright, thank you so much! Really appreciate the help and the advice not to use global variables. Your approach is so much better than what I had in mind.
Avatar
Jeff Glass 08/08/2022 20:32
No sweat! Really neat idea marrying the Jcrop library with PyScript - I think the possibilities for putting the vast number of JS libraries together with the ease of Python is what makes this so exciting
โค๏ธ 1
Avatar
hey guys, i am having trouble reading from other files in my pyscript project
00:13
i want to read from a folder of csv files, but the file i specify to read this files always says it cannot read it
00:14
this is only when i try to run my server. if i run the python file locally then it can read the files
00:14
i think when i specify the file path in my py-env, it loses access to all the other files in my project. how do i stop that?
Avatar
Jeff Glass 10/08/2022 00:15
Hmmm that doesnโ€™t sound like a familiar problem. Do you have a code snippet we can look at?
Avatar
<py-env> - paths: - gradecalculations.py - matplotlib </py-env> <py-script> import gradecalculations info = document.getElementById("search").innerHTML info = info.strip().split() x = gradecalculations.getCourse(info) gradecalculations.generateCourseImage(x[0]) </py-script>
00:16
this is my pyscript. but i have another folder called gradedata which has a bunch of csv files
00:16
inside gradecalculations.py, i am accessing these csv files. but when i try to access and read from them, it says it cant find them
00:16
courseList = [] for year in range(2022,1997,-1): with open('gradedata/grades{}.csv'.format(year)) as csv_file: csv_reader = csv.reader(csv_file, delimiter=',') next(csv_reader) for row in csv_reader: course1 = Course(row[0],row[1],row[2],row[3],row[4],row[5],row[6],int(row[7]),int(row[8]),int(row[9]),int(row[10]),int(row[11]),float(row[12])) courseList.append(course1)
00:16
here is inside gradecalculations.py
00:18
it was also having troubles from reading from anotehr python file
00:18
that might be complicated, let me simplify it for you
00:19
say i have a file called a.py, and another called b.py. i access a.py functions inside b.py, but i also want to use b.py functions. how would i go about using this proccess inside my pyscript?
Avatar
Jeff Glass 10/08/2022 00:21
Ah so there's two things going on with file loading
00:23
gradecalculations.py in being loaded from the server into the virtual filesystem by <py-env> paths:...., so import works fine. But your various 'grades{}.csv' files are not, so with open('gradedata/grades{}.csv'.).... fails (edited)
Avatar
i see, how would i access them in that case?
00:24
specify it in the path?
Avatar
Jeff Glass 10/08/2022 00:25
Rather than try to list them all in paths, you can you pyodide.http.pyfetch https://pyodide.org/en/stable/usage/api/python-api.html#pyodide.http.pyfetch to fetch them from the server and bring them into the file system
00:26
Pyscript actually uses pyfetch under the hood to load the files you specify in paths - the python code in loadFromFile is probably a good starting point https://github.com/pyscript/pyscript/blob/bd7a20309bab76347f0a1731cdb18192238c8420/pyscriptjs/src/interpreter.ts#L44-L61
00:28
The second thing, and it's small - using paths (or copy-pasting that python code) brings all the files into the same folder as your script in the virtual file system, so once it's fetched, you'd open ./grade1999.csv not ./gradedata/grade1999.csv, for example
Avatar
ok i see
00:30
just curious, is there a way to do something like <py-env> - paths: - gradedata/* - gradecalculations.py - matplotlib </py-env>
00:30
or something along the lines of importing everything from a folder
00:30
through paths
00:30
@Jeff Glass
00:31
since right now all my csv files are in gradedata folder
Avatar
Jeff Glass 10/08/2022 00:38
Hang on one sec, working on some demo code...
00:43
The gist is - you can use pyfetch inside your for loop to load the files when you need them
00:44
for year in range(2022,1997,-1): load_file_from_server(filename...) with open('./grades().csv')....: #do stuff
00:45
There isn't a generic way to import everything from a folder via paths, since on the web something like localhost:5500/gradedata/ isn't a folder, it's a URL
00:49
A couple folks have invented workarounds, including: (edited)
Avatar
thank you. i have another question. i am generating an image using plt.savefig("graph.png", bbox_inches='tight',pad_inches = .5). inside my gradecalculations.py file. can i simply access this inside my html file in my pyscript code?
00:50
since it is in a virtual environment
Avatar
Jeff Glass 10/08/2022 01:13
So personally I find it easier to work with images in-memory - if you save it to the virtual filesystem, you end up having to load it back into memory to manipulate it, since browser elements cannot directly access the virtual file system AFAIK
01:14
Here's a working example (assuming matplotlib is in py-env and you have a tag with id "graphlocation" somewhere: import matplotlib.pyplot as plt from io import BytesIO from js import document, File, Uint8Array, window #create plot plt.plot([1,2,3,4,10]) #Save plot to BytesIO (file-like in-memory object) plot_file = BytesIO() plt.savefig(plot_file, bbox_inches='tight',pad_inches = .5) #Create File object JS knows how to work with, casting BytesIO to Uint8Array processed_file = File.new([Uint8Array.new(plot_file.getvalue())], "graph.png", {type: "image/png"}) #Create img tag, point it at new File, add to DOM: my_img = document.createElement("img") my_img.src = window.URL.createObjectURL(processed_file) destination = document.getElementById("graphlocation").appendChild(my_img);
Avatar
Avatar
Jeff Glass
So personally I find it easier to work with images in-memory - if you save it to the virtual filesystem, you end up having to load it back into memory to manipulate it, since browser elements cannot directly access the virtual file system AFAIK
so if i save the image to the virtual filesystem, i can't access it for later?
Avatar
Jeff Glass 10/08/2022 01:26
You surely can, but to make a File object that javascript knows what to do with, you have to read the bytes out of that file anyway, like so: import matplotlib.pyplot as plt from js import document, File, Uint8Array, window #create plot plt.plot([1,2,3,4,10]) #Save plot to BytesIO (file-like in-memory object) plt.savefig('graph.png', bbox_inches='tight',pad_inches = .5) #Create File object JS knows how to work with, casting BytesIO to Uint8Array with open("graph.png", 'rb') as infile: processed_file = File.new([Uint8Array.new(infile.read())], "graph.png", {type: "image/png"}) #Create img tag, point it at new File, add to DOM, etc...
01:28
So you use savefig to save the file, and then using open( ... mode = 'rb') to read it back in again, which seems like an extra step to me, but that might also just be personal preference
01:28
FWIW, John Hanley has a pretty good writeup on the virtual filesystem, though he doesn't talk much about saving and loading specific file types from places like matplotlib or PIL https://www.jhanley.com/pyscript-files-and-file-systems-part-1/
01:29
(So I'm gonna write up that part ๐Ÿ˜)
Avatar
Avatar
Jeff Glass
You surely can, but to make a File object that javascript knows what to do with, you have to read the bytes out of that file anyway, like so: import matplotlib.pyplot as plt from js import document, File, Uint8Array, window #create plot plt.plot([1,2,3,4,10]) #Save plot to BytesIO (file-like in-memory object) plt.savefig('graph.png', bbox_inches='tight',pad_inches = .5) #Create File object JS knows how to work with, casting BytesIO to Uint8Array with open("graph.png", 'rb') as infile: processed_file = File.new([Uint8Array.new(infile.read())], "graph.png", {type: "image/png"}) #Create img tag, point it at new File, add to DOM, etc...
ok a little confused lol, say I have a <img> in my html, with id="content". and obviously i have graph.png saved in my filesystem. how would i do something similar to the pywrite function where i use the id to replace the content of that image with graph.png image?
01:39
ik if it was text, it would be pyscript.write('content', "hello")
01:39
but how would i do it for images?
Avatar
Jeff Glass 10/08/2022 01:45
So fair warning, the way that output works in PyScript is going to change in the very near future - see #622 from Fabio, lead PyScript dev https://github.com/pyscript/pyscript/issues/622
01:46
That said, for now at least you can do Element("content").write(plt)
01:47
And you don't actually even need to output it to a file
01:48
plt.plot([1,2,3,4,9]) Element("content").write(plt)
01:49
(Not the question you asked, sorry, but neat nonetheless)
Avatar
and thats for something like <img id="content"> ?
Avatar
Jeff Glass 10/08/2022 01:50
(This is mostly a hack for matplotlib - Pyscript treats objects with a savefig meethod specially and does this for you)
01:50
Exactly
Avatar
doesn't seem to be working for me
Avatar
Jeff Glass 10/08/2022 01:52
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" /> <script defer src="https://pyscript.net/latest/pyscript.js"></script> <py-env> - matplotlib </py-env> </head> <body> <div id="content"></div> <py-script> import matplotlib.pyplot as plt plt.plot([1,2,3,4,10]) Element("content").write(plt) </py-script> </body> </html>
01:52
Oh sorry, I see what happened, I was using a div
Avatar
ok let me try using a div
Avatar
Jeff Glass 10/08/2022 01:52
Yeah, images don't display (have?) innerHTML, which is what write() sets (edited)
Avatar
ok, looks like that works, thanks!
๐Ÿ‘ 1
Avatar
zaki ๐Ÿ‡ฑ๐Ÿ‡ฐ 17/08/2022 20:06
I've been reading up on the interoperability between JS and PyScript, and was wondering how one would have a function in js/Py be called from the other. So far, it seems to be possible using a button to, for example, trigger a python function, but what if it needs to be automated (in particular, when an item in HTML is onChange) to run a python function between lines of JS code?
20:09
I want to have a small layer of Pillow manipulating a certain image, which is triggered by my existing JavaScript onChange event. I've looked at Jeff Glass' neat implementation on his site but the entire thing is in PyScript and was just wondering if it's possible without rewriting each chunk into a specific language. The proxies that pyodide provide can only transfer object data between the runtimes, no? Does that also include methods? Like I write a certain function in PyScript, pass it via a proxy to js and run it within js?
20:11
Would be really nice if anyone could point me towards something that shows, for example, a way to embed some python code (or even just maybe like run PythonFunction() into a JavaScript chunk. Or am I taking the wrong approach here?
Avatar
zaki ๐Ÿ‡ฑ๐Ÿ‡ฐ 17/08/2022 20:41
Also @Jeff Glass, just a quick heads up -> Your example code on the pillow page has an issue with these not being updated output_upload_pillow and file-upload-pillow to the new names, it was throwing a few exception errors when I tried it out. May confuse other beginners like me. ๐Ÿ˜„ Thank you for the code!
๐Ÿ‘ 1
Avatar
Jeff Glass 17/08/2022 21:13
@zaki ๐Ÿ‡ฑ๐Ÿ‡ฐ Thanks! I'll take a look at it
21:13
And more thoughts on your questions to come later - they are good questions
Avatar
Jeff Glass 18/08/2022 14:23
@zaki ๐Ÿ‡ฑ๐Ÿ‡ฐ So, I believe the answers to "can you do __" are currently as follows:
  • Pass Javascript functions to Python (PyScript): Yes โœ…
  • Pass Javascript functions to Python (Pyodide): Yes โœ…
  • Pass Python Functions in Pyodide to JS - Yes โœ…
  • Pass Python functions in PyScript to JS - No โŒ JS Functions in PyScript:
<script> function addTwoNumbers(x, y){ return x + y } </script> <py-script> from js import addTwoNumbers, console console.log("Adding 1 and 2 in Javascript: " + str(addTwoNumbers(1, 2))) </py-script> JS Functions in Pyodide: <script> async function main() { let pyodide = await loadPyodide() result = pyodide.runPython(` from js import addTwoNumbers, console console.log("Adding 1 and 2 in Javascript: " + str(addTwoNumbers(1, 2))) `) } main(); </script> Python (Pyodide) Functions in JS: <script type="module"> const pyodideRuntime = await loadPyodide() pyodideRuntime.runPython(` def multiplyTwoNumbers(x, y): return (x * y) `) console.log("Multiplying 2 and 3 in Python: " + pyodideRuntime.globals.get('multiplyTwoNumbers')(2, 3)) </script> But because PyScript doesn't currently expose its Pyodide instance, and since only one Pyodide instance can be running at a time, I don't believe there's a (non-hacky) way to access objects defined in a PyScript block directly in JavaScript. There's an open issue about this which I hope someone picks up soon https://github.com/pyscript/pyscript/issues/494
โค๏ธ 1
14:24
This is an issue with how PyScript currently implements wrapping Pyodide and not a fundamental roadblock, it's just not something I think PyScript currently makes possible
Avatar
Avatar
zaki ๐Ÿ‡ฑ๐Ÿ‡ฐ
Also @Jeff Glass, just a quick heads up -> Your example code on the pillow page has an issue with these not being updated output_upload_pillow and file-upload-pillow to the new names, it was throwing a few exception errors when I tried it out. May confuse other beginners like me. ๐Ÿ˜„ Thank you for the code!
Jeff Glass 18/08/2022 14:39
Also, I fixed that older post to load the displayed code directly from the .py files that the page is running, so they'll always be in sync. ๐Ÿ‘
โค๏ธ 1
Avatar
Jeff Glass 19/08/2022 16:01
@zaki ๐Ÿ‡ฑ๐Ÿ‡ฐ Did some more thinking about it, and shared some work-in-progress over on the Anaconda PyScript forums https://community.anaconda.cloud/t/accessing-objects-created-in-pyscript-from-javascript/31551/3 It boils down to creating a Javascript function to dynamically create variables using JavaScript's eval(): <script> function createObject(x, variableName){ let execString = variableName + " = x" console.log("Running `" + execString + "`"); eval(variableName + " = x") } </script>
๐Ÿ‘€ 1
Avatar
Just took 2 screenshots from my app, is it possible to code this with PyScript HTML and CSS? No JavaScript in between?
Avatar
zaki ๐Ÿ‡ฑ๐Ÿ‡ฐ 02/09/2022 14:44
I feel like adding JS to that can simplify a lot of the code
Avatar
You'll probably need js at least to fetch (edited)
Avatar
๐Ÿ‘‹ I was wondering if someone could help me understand why refreshing the page requires such a long load time. I can see that through the network tab, the main pyscript files are cached, and I know initialization has something to do with web assembly, but that's pretty much where my knowledge ends :p
07:07
I guess this delves a bit into how pyodide works as well
Avatar
Hi there! I'm trying to dynamically change an image, I'm saving an image with PIL to the cwd ("./img.png"), Then setting the src tag to "" and then back to "./img.png". But it doesn't work, it only shows the "./img.png" on cwd and when I call the func to update the image it doesn't change on my pc
Avatar
Avatar
Ilai
Hi there! I'm trying to dynamically change an image, I'm saving an image with PIL to the cwd ("./img.png"), Then setting the src tag to "" and then back to "./img.png". But it doesn't work, it only shows the "./img.png" on cwd and when I call the func to update the image it doesn't change on my pc
I don't think that the browser can directly modify the files on your computer. You'd have to have a "save file" link which has the file data as a javascript blob which has the download attribute. not sure if you can do that in pyscript without javascript
Avatar
Avatar
Frogger
I don't think that the browser can directly modify the files on your computer. You'd have to have a "save file" link which has the file data as a javascript blob which has the download attribute. not sure if you can do that in pyscript without javascript
I already tried that and it worked, I would rather to display the image, but even if I do the download, I create the image with PIL (Pillow) but cant get the content
Avatar
I guess you could also right click and save image?
Avatar
Jeff Glass 10/09/2022 21:54
@Ilai So the src tag in an html page references a (relative or absolute) URL, so you can't use it to reference files within the Emscripten file system (where your file saved to)
21:55
To make the file available to the HTML/JS environment, you can use JavaScript's File interface to create a "virtual" file and point the src attribute to that. (It's virtual in that it doesn't exist anywhere on the web except in the browser window, but it can be referenced by an HTML element or other JavaScript as if it was)
21:56
For PIL specifically, you may find this writeup I made a couple months ago helpful: https://jeff.glass/post/pyscript-image-upload/
How to upload images into Pyscript and work with them in Pillow in-browser
21:57
For what it's worth, if you're just generating the image and displaying it, you don't actually have to save it to the Emscripten file system and re-load it - you can just write the bytes of the PIL/Pillow image to a BytesIO object and use those as the data for the File object
Avatar
Thanks!
Avatar
How can I share an image with navigator.share? im trying to do it like this: async def share_eq(): base64url = document.getElementById('img').src blob = await (await fetch(base64url)).blob() file = File.new([blob], 'eq.png', {type: blob.type}) print("file", file) navigator.share({ title: 'Generated EQ', text: 'txt', files: [file], }) (edited)
Avatar
Jeff Glass 11/09/2022 15:09
Does navigator.canShare() think the file itself is sharable?
Avatar
Avatar
Jeff Glass
Does navigator.canShare() think the file itself is sharable?
Do I use it like this? : navigator.canShare(file)
15:11
It says AttributeError: canShare. I just read that the website has to be https, Ill try that now
15:15
@Jeff Glass So it does work in https, but it now gives me this error: Future exception was never retrieved\nfuture: <Future finished exception=JsException(TypeError: Failed to execute 'share' on 'Navigator': No known share data fields supplied. If using only new fields (other than title, text and url), you must feature-detect them first.)>
15:15
and it returns true at canShare
Avatar
Jeff Glass 11/09/2022 15:18
Out of curiosity, what browser is this in?
Avatar
Tried on firefox and opera
Avatar
Jeff Glass 11/09/2022 15:18
Fwiw this is all beyond anything Iโ€™ve tried, I just think itโ€™s a neat question
Avatar
alright, thanks!
Avatar
Jeff Glass 11/09/2022 15:19
Looks like navigator.share has to be enabled via a setting in Firefox, though in theory opera should have worked? https://developer.mozilla.org/en-US/docs/Web/API/Navigator/share#browser_compatibility
Avatar
tried in chrome too now
15:20
doesnt work
Avatar
Jeff Glass 11/09/2022 15:20
Hmm
15:23
Does making the file type something like โ€œimage/pngโ€ change anything?
Avatar
same thing
15:25
This error is giving a lot of info tho
Avatar
Avatar
Ilai
@Jeff Glass So it does work in https, but it now gives me this error: Future exception was never retrieved\nfuture: <Future finished exception=JsException(TypeError: Failed to execute 'share' on 'Navigator': No known share data fields supplied. If using only new fields (other than title, text and url), you must feature-detect them first.)>
How would I "feature-detect"
Avatar
Jeff Glass 11/09/2022 15:26
Tbh I was googling that tooโ€ฆ most of the references seem to be talking about using โ€œcanShareโ€ which already seems to succeedโ€ฆ
Avatar
I even tried using url, instead of files, and still same error
Avatar
Jeff Glass 11/09/2022 15:42
So this works for me: <button onclick="share()">Share Me</button> <script> function share() { navigator.share({url: "http://www.example.com"}) } </script> (edited)
Avatar
ill try that
Avatar
Avatar
Jeff Glass
So this works for me: <button onclick="share()">Share Me</button> <script> function share() { navigator.share({url: "http://www.example.com"}) } </script> (edited)
So I should just connect with js too?
Avatar
Jeff Glass 11/09/2022 15:48
Hmmm actually, hereโ€™s a stray though - try assigning your contents for the data argument to a variable and log it and see what type it is
15:49
@hosler tracked down an issue at one point where a function required a JS object, but it was being passed from PyScript as a Map https://discord.com/channels/972017612454232116/972020206538997822/994301014024933417
Avatar
most javascript i found looks for an object instead of a map
Avatar
Avatar
Jeff Glass
So this works for me: <button onclick="share()">Share Me</button> <script> function share() { navigator.share({url: "http://www.example.com"}) } </script> (edited)
I used this and it worked!! Thnak you sm!
๐Ÿ‘ 1
Avatar
not sure why everyone uses data objects instead of regular dictionaries in their js projects
15:59
goes against python thinking in my head
Avatar
Jeff Glass 11/09/2022 16:03
I think partly cause a JS map is broader than a Python dict - anything can be a key in a JS map, any object or primitive, whereas an objectโ€™s keys have to be integer string or โ€œsymbolโ€ (identifier) (edited)
16:04
So as an analog to function parameters, object makes a bit more sense IMHO, but yeah in general Maps have some quite nice properties
Avatar
Hi again, Is there any way to reduce loading time of pyscript?
Avatar
Jeff Glass 11/09/2022 18:50
Not a simple way that I know of, but I know itโ€™s a discussion point of the Pyscript core devs, as well as for the Pyodide runtime folks https://github.com/pyodide/pyodide/issues/646
Avatar
i mitigate it by making my app a single page application (edited)
Avatar
Jeff Glass 11/09/2022 19:13
Out of curiosity, do you end up doing the SPA via Pyscript or are you using another framework?
Avatar
mostly pyscript
19:13
with a little vanilla javascript here and there
19:14
populating the DOM with python is so refreshing
Avatar
@hosler Whatcha building?
Avatar
just a front end for my django project
Avatar
timewilltell 15/09/2022 10:54
Does anyone know why I keep getting this error please. Trying to build an online image editor with pyscript. I cannot find the solution on Google. Thank you
Avatar
Jeff Glass 15/09/2022 12:19
So the โ€œjsโ€ package isnโ€™t really a package - itโ€™s Pyodideโ€™s wizardry to import objects from the JavaScript global scope.
12:21
You can use # type: ignore to remove the warning; ideally someday weโ€™ll have stronger tooling than this, but given thereโ€™s not a way for an IDE to know whatโ€™s going to be in the global browser scope at runtimeโ€ฆ
Avatar
Deleted User 16/09/2022 03:45
can i use web3py with pysource, it wasnt possible a few weeks ago, has this changed
Avatar
Avatar
Deleted User
can i use web3py with pysource, it wasnt possible a few weeks ago, has this changed
Jeff Glass 19/09/2022 17:11
web3py appears to make significant use of websockets. Because opening a socket from within a browser window is not supported by any browser (and is unlikely to be), web3py is not likely to work in PyScript/Pyodide any time soon.
Avatar
Avatar
Jeff Glass
web3py appears to make significant use of websockets. Because opening a socket from within a browser window is not supported by any browser (and is unlikely to be), web3py is not likely to work in PyScript/Pyodide any time soon.
tedpatrick 30/09/2022 17:35
Here is an example connecting to the XRipple Mainnet using WebSocket <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>WebSocket</title> <link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css"/> <script defer src="https://pyscript.net/latest/pyscript.js"></script> </head> <body> <py-script> from pyodide.ffi import create_proxy import js import json ws = js.WebSocket.new('wss://s1.ripple.com/') def events(e=None): if e.type == 'close': js.console.log( e.type ) if e.type == 'error': js.console.log( e.type ) if e.type == 'message': m = json.loads(e.data) js.console.log( m ) if e.type == 'open': js.console.log( e.type ) # send message when connected e.target.send(json.dumps( { "id": 1, "command": "server_info" })) js.console.log(e) proxy = create_proxy(events) ws.addEventListener('close',proxy) ws.addEventListener('error',proxy) ws.addEventListener('message',proxy) ws.addEventListener('open',proxy) </py-script> </body> </html>
Avatar
Hello, i put my python code in file html with pyscript and i have this during longtime, someone can help me plz ?
Avatar
Jeff Glass 02/10/2022 23:11
Are there any errors in the development console of the browser?
Avatar
Avatar
Jeff Glass
Are there any errors in the development console of the browser?
i had this
Avatar
Jeff Glass 03/10/2022 13:46
So the last line on that second image says that PyScript/micropip isn't able to locate the package on PyPI. I'm not super familiar with py-sat, but from their getting started, it looks like their package on PyPI is python-sat, so that's what you'd want to include in your py-env/py-config https://pysathq.github.io/installation/
13:46
And then import pysat.solvers or from pysat.solvers import ....
13:47
(Assuming I found the right package when I googled "pysat solvers" ๐Ÿ˜… )
Avatar
Okay ty, I gonna try (edited)
Avatar
it seems that the problem is different: the network request is missing because pypi.org doesn't have the proper CORS headers. But I'm not very familiar with this stuff so I might be wrong
13:54
if this is the case, it might happen that we cannot install any package from pypi, but I think that this was working before, wasn't it? Maybe pypi.org changes its behavior?
Avatar
Jeff Glass 03/10/2022 13:57
I think that's a red herring - it looks like @Lyes is trying to load the pysat-solvers package (which doesn't exist on PyPI), so micropip tries to load https://pypi.org/pypi/pysat-solvers/json to get the metadata... and that page 404's, but it also sends back content with a malformed CORs header (edited)
13:58
So loading from PyPI still works fine, it just gives an unfortunately worded error because of the way they send their 404 when the package isn't found
13:59
Avatar
ah ok, makes sense! Thanks
14:08
we should improve our error reporting, for sure
Avatar
Jeff Glass 03/10/2022 14:08
Definitely
Avatar
Avatar
Jeff Glass
I think that's a red herring - it looks like @Lyes is trying to load the pysat-solvers package (which doesn't exist on PyPI), so micropip tries to load https://pypi.org/pypi/pysat-solvers/json to get the metadata... and that page 404's, but it also sends back content with a malformed CORs header (edited)
I had this error now ModuleNotFoundError: No module named 'pysat' Maybe it's not possible because to install pysat I have to make pip install pysat in my shell (edited)
Avatar
Jeff Glass 03/10/2022 19:15
Can you share you full source, either here in a code block or in a pastebin? https://pastebin.com/
Avatar
this link
Avatar
Jeff Glass 03/10/2022 19:26
Thank you! Tremendously helpful You'll want to add this somewhere inside the <head> tag: <py-config> packages = ['python-sat'] </py-config> That will cause micropip to load python-sat from PyPI
19:26
19:26
That's what I see when I run your code as written, with the added py-config tag
Avatar
Avatar
Jeff Glass
That's what I see when I run your code as written, with the added py-config tag
Thank you very much it works ๐Ÿ™‚
๐Ÿฅณ 1
Avatar
If I have my own pure-python library, how can I import it from a pyscript script?
Avatar
like, I could copy-paste it all into a single source file, but is there something less janky I can do?
Avatar
Russell Keith-Magee 06/10/2022 00:23
@retr0id Package your code as a .whl, then add the wheel as a dependency in your py-config block. That's effectively the equivalent of pip importing your module; so in your python code, you can import module and use it as you would normally.
Avatar
thanks!
Avatar
JayneWayne 06/10/2022 20:51
In my python script i want to get data from an internal IP-Address, something like a Database. Can i use the PyScript assets served on https://pyscript.net or do i need to install it localy?
Run Python code in your HTML.
Avatar
Jeff Glass 06/10/2022 21:05
For that particular case, I don't think it will make a difference?
21:06
But I confess I'm not quite understanding your question - what are you using to fetch the data?
Avatar
JayneWayne 06/10/2022 21:37
Yes, this is probably because I am just starting to program. I am trying to retrieve data from an internal IP address, but unfortunately the page gets stuck in the loading process. Below is an excerpt: <py-config type="json"> { "packages": ["pyModbusTCP"] } </py-config> <py-script> from pyModbusTCP.client import ModbusClient client = ModbusClient(host="192.168.xxx.xxx", port=xxx) client.open() PV = str(client.read_holding_registers(67)) PV = PV.replace('[', '') PV = PV.replace(']', '') PV = int(PV) </py-script>
Avatar
Jeff Glass 06/10/2022 22:32
No worries! We all start somewhere - thanks for posting your code, it always helps
22:33
In this case, it's not an issue with local vs. sourced install, but rather that PyScript can't load the pyModbusTCP package from PyPI
22:33
If you open the developer tools in your browser of choice, you can see the underlying error that Pyodide (the Cpython-in-WASM Python interpreter) gives https://balsamiq.com/support/faqs/browserconsole/
22:34
22:38
To go a little deeper - the maintainers of that particular package have provided source files but not a prebuilt wheel for that package - it looks to be a pretty simple pure-python package, so building a wheel yourself is an option https://packaging.python.org/en/latest/tutorials/packaging-projects/#generating-distribution-archives
Avatar
Russell Keith-Magee 07/10/2022 00:07
Hi folks - I'm seeing some wierd, inconsistent behavior in a PyScript project. I'm loading almost all my code from locally cached wheels; and in that code, I'm using importlib.metadata to retrieve name/version details about the packages that have been loaded. About 50% of the time, I get a failure from importlib.metadata saying that the package doesn't exist - but if you restart the webserver and reload, it works. It feels like some sort of race condition with interpreter startup, but I'm not sure where I'd start looking to investigate.
00:08
I've tried reducing to a minimal example, but that example works 100% of the time.
Avatar
Jeff Glass 07/10/2022 00:10
Woof... intermittent problems are the worst... this is on 2022.09.1 as released or a fresh/unstable build?
Avatar
Jeff Glass 07/10/2022 00:18
possibly related issue from pyodide? Forgive me if you've already seen, it discusses similar topics https://github.com/pyodide/pyodide/pull/2569
Avatar
Russell Keith-Magee 07/10/2022 00:41
@Jeff Glass Yes, this is on 2202.09.1 (well - latest, which I believe is pinned to 2022.09.1)
00:44
I hadn't seen that issue - I'm not sure if it's directly related though; it seems to suggest an internal locking issue with micropip; in my case, micropip is finished, Pyscript has reported [pyscript/runtime] PyScript page fully initialized
00:45
and it's successfully running the code from the wheel from which metadata is missing; it's just the metadata that can't be found.
Avatar
JayneWayne 07/10/2022 12:46
I have managed to get the module running. Unfortunately, I still can not query my data, because I now get a connection error. The port is normally accessible via telnet. Unfortunately, the browser gives me here quite little information. Why is that? Where can I get more information?
12:47
Avatar
I have a question, sorry I can't send more than 2000 chars so I uploaded it as a file
Avatar
sounds like netcdf4 relies on some other libraries. probably c or c++
02:29
from what i understand, pyodide only supports pure python. packages that rely on other language library have to be specially built into pyodide (edited)
Avatar
Jeff Glass 09/10/2022 02:31
Yeah what @hosler said. It looks like netcdf4 is written largely in Cython.
Avatar
Description Package for working with weather radar data. Repo is https://github.com/ARM-DOE/pyart Also added netcdf4 (closes #1582) Checklists Add a CHANGELOG entry Add / update tests
02:32
soon
02:33
if all you need is data crunching, then i suggest you "proxy" it through an api. import it into a django app and then deliver the results to pyscript
02:36
not really the spirit of pyscript, but it's a workaround that can work
Avatar
<!DOCTYPE html> <link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css"/> <script defer src="https://pyscript.net/latest/pyscript.js"></script> <h3> <py-script> from pyodide.http import pyfetch import asyncio async def submit_request(*args): varName = Element('test-input').element.value response = await pyfetch(url="https://api.lanyard.rest/v1/users/852825042630475827", method="GET") output = f"{await response.json()}" pyscript.write('request_output', output) </py-script> </h3> why this doesnt show anything?
Avatar
Jeff Glass 09/10/2022 22:17
The first argument to Pyscript.write is the id of the html tag you want to write to, and it doesnโ€™t look like you have a tag with id=โ€œrequest_outputโ€
Avatar
i just want to print the result
22:23
i mean... to just put it there
Avatar
Jeff Glass 09/10/2022 22:24
So two things you can do (of many)
22:25
i dont really know how to use that... im used to requests...
Avatar
Jeff Glass 09/10/2022 22:25
(And apologies, on mobile, so these are untested but I think correct)
Avatar
Jeff Glass 09/10/2022 22:27
Your fetch syntax is correct
22:27
Your output syntax is not
22:27
To directly output โ€œin placeโ€
22:27
Just end your script with the line output
22:28
By default, if Python run in Pyodide ends with an expression, that value will be output
22:29
For example, and this is tested: from pyodide.http import pyfetch import asyncio response = await pyfetch(url="https://api.lanyard.rest/v1/users/852825042630475827", method="GET") output = f"{await response.json()}" output
22:31
oh nevermind... i added in the label instead of external file and works
Avatar
Jeff Glass 09/10/2022 22:31
๐Ÿ‘
22:32
how could i reach the endpoints though?
Avatar
Jeff Glass 09/10/2022 22:32
Like the data in the json? Like say the value of โ€œsuccessโ€? (True)?
Avatar
like... output['data']['discord']['username']
22:33
to return me R3FL3X
Avatar
Avatar
Jeff Glass
Like the data in the json? Like say the value of โ€œsuccessโ€? (True)?
yeah
22:36
that would work
Avatar
Jeff Glass 09/10/2022 22:39
from pyodide.http import pyfetch import asyncio response = await pyfetch(url="https://api.lanyard.rest/v1/users/852825042630475827", method="GET") output = await response.json() output['data']['discord_user']['username']
22:40
The key change is not interpreting the results of response.json() as a string - Pyodide maps it to a dict for you
Avatar
oh just like normal
Avatar
Jeff Glass 09/10/2022 22:40
Yep!
Avatar
Avatar
Jeff Glass
Yep!
thanks it works
22:49
btw how can i align these?
22:50
22:50
when i just had the text there they were in a straight line and now with pyscript the moved (edited)
22:51
it was like that
Avatar
Jeff Glass 10/10/2022 02:42
Ah yeah, so multiple outputs from Pyscript by default go to separate divs, which will be on separate lines
02:42
(At least for now)
02:42
Concatenate the strings before outputting if possible, or adjust the html formatting
Avatar
oke thanks a lot!
Avatar
I have starlette running, I successfully have pyscript running in a template within Starlette, but I would like to use my own modules, and I'm struggling with what that should look like instead of writing my code between <py-script> tags I would like to <py-script> import mymodule mymodule.myfunction() </py-script> (edited)
20:01
So far I have been unable to import mymodule.
Avatar
Jeff Glass 10/10/2022 20:33
To include modules (and other files) on the web you can use the <py-config> tag: <py-config> paths = ["./mymodule.py"] </py-config>
20:33
This assumes that mymodule.py is at the same relative path as your HTML file (whether you're using a local live server, deploying to the web, etc
20:34
You can also use fully qualified URLS: ... paths = ["https://example.com/path/to/some/resources/mymodule.py"]
Avatar
Jeff Glass 10/10/2022 21:30
(Note that prior to 2022.09.1, this functionality was in a tag called <py-env>, so prexisting examples may use a similar syntax in that tag to load files)
Avatar
@Jeff Glass Thank you so much. I've been trying to use <py-env>. I'll give <py-config> a shot and see what happens.
Avatar
Avatar
hosler
if all you need is data crunching, then i suggest you "proxy" it through an api. import it into a django app and then deliver the results to pyscript
Thank you for your workaround. I hope netcdf4 will be supported on pyodide in the near future
Avatar
how can i import a custom wheel the same way as pyodide.loadPackage
21:22
it seems the wasm build does not export emscripten_run_preload_plugins so it is not possible to compile a wasm assembly (.so > 4KiB ) library asynchronously
Avatar
Is it possible to freeze/prebuild a pyscript environment? On each load it fetches the wheels for all the dependencies i listed... (edited)
Avatar
Avatar
pbsds
Is it possible to freeze/prebuild a pyscript environment? On each load it fetches the wheels for all the dependencies i listed... (edited)
maybe use a service worker to cache wheels requests
Avatar
But can't i sidestep the dependency resolution process entirely? Micropip has a freeze function. Can i use it somehow to create a single zip which bundles pyodide and the dependencies together?
Avatar
Avatar
pbsds
But can't i sidestep the dependency resolution process entirely? Micropip has a freeze function. Can i use it somehow to create a single zip which bundles pyodide and the dependencies together?
sure that is what pygbag does, but if you have big .so in the archive i guess it won't work out of the box on pyscript
04:38
all wasm files need to be streamed for compilation wherever they come from
04:43
tbh i think micropip is over-engineered, and still does not do the job. i had more success with pypa/installer and cpython-wasm
Avatar
yeah i confirm micropip.install only works on firefox when there are big binary deps or i did something wrong.
Avatar
<py-script> from js import console from datetime import datetime from request import request </py-script> i have this in my html, but i still getting no module named "request". Why?
Avatar
Jeff Glass 16/10/2022 00:08
Are you looking for the requests module?
Avatar
jesus
โค๏ธ 1
00:09
i want to die right now
00:09
thankyou, and sorry for bothering x)
Avatar
Jeff Glass 16/10/2022 00:09
No worries! But spoilers requests isnโ€™t going to work in Pyscript.
Avatar
Jeff Glass 16/10/2022 00:21
Thereโ€™s commentary on it in this GitHub issue: https://github.com/pyodide/pyodide/issues/398, but youโ€™re better off doing something like: https://docs.pyscript.net/latest/howtos/http-requests.html
Avatar
thats what i did
Avatar
Jeff Glass 16/10/2022 00:21
Oh
00:21
Ohhhhh
00:21
So sorry
00:22
So not Pythonโ€™s requests from stdlib
00:22
Is that code in a file thatโ€™s in the same folder as your main html file?
Avatar
i use pyfetch
Avatar
Avatar
hosler
i use pyfetch
so did i now
00:24
thing is
00:24
my pys-onClick="exec_func" is not getting fired
00:24
and my pyfetch is inside of the exec_func
Avatar
i havent tried that yet. im still using create_proxy
Avatar
Jeff Glass 16/10/2022 00:25
Hmm this probably isnโ€™t the issue, but fwiw โ€œpys-onclickโ€ is deprecated
00:26
The new syntax for pys-onclick=โ€œfooโ€ is py-click=โ€œfoo()โ€
00:26
The attribute name has changed, and it contains a string to eval rather than the name of a callable
Avatar
any info bout when docs are going to get an update?
Avatar
Jeff Glass 16/10/2022 00:29
Thereโ€™s a PR about it now (from 2 days ago) that just need a review and merge ๐Ÿ˜… https://github.com/pyscript/pyscript/pull/852
00:29
TBH, the project and docs has been evolving fast enough that itโ€™s not at the โ€œevery PR must include fixes to the docsโ€ stage.
Avatar
dayum, cant wait for this project to get further ahead
00:30
i just love python and cant get rid of it x)
00:30
if i cant do both front and back with it. Ill never leave haha
Avatar
Jeff Glass 16/10/2022 00:31
I hear that!
Avatar
Jeff Glass 16/10/2022 00:42
Going back - even the old pys-onClick syntax should work though deprecatedโ€ฆ not sure why your function wouldnโ€™t be firing
Avatar
Avatar
Jeff Glass
Going back - even the old pys-onClick syntax should work though deprecatedโ€ฆ not sure why your function wouldnโ€™t be firing
it kept telling me that my exec_func wasnt defined
Avatar
Jeff Glass 16/10/2022 00:55
Huh
Avatar
Jeff Glass 16/10/2022 01:27
Is it being defined in another file@thatโ€™s not being loaded maybe?
Avatar
Avatar
Jeff Glass
Is it being defined in another file@thatโ€™s not being loaded maybe?
nope, directly in the same file
10:07
sorry for late reply, was sleepin x)
Avatar
can't import panel, did pyodide dropped panel? (edited)
Avatar
Avatar
Ayato
can't import panel, did pyodide dropped panel? (edited)
Jeff Glass 16/10/2022 13:59
Do you have a py-config tag that includes โ€œpanelโ€ in its packages list?
Avatar
Jeff Glass 16/10/2022 14:06
Py-config does a lot of things, but one of them is to instruct micropip to try to load packages from PyPI. Hereโ€™s an example: <py-config> packages = [ "bokeh", "numpy", "pandas", "panel==0.13.1" ] </py-config>
Avatar
Whats the diff betwin the import inside of the py-config and the import from etc.. inside of the py-script tags?
Avatar
<py-config> is what determines which packages are installed in the page. It is basically the equivalent of pip install or conda install
Avatar
Avatar
antocuni
<py-config> is what determines which packages are installed in the page. It is basically the equivalent of pip install or conda install
ye but where those packages come from? like, if i get it right, in order to use packages i need to install them. if i just import the module inside of the py-config tag dont i need to install it before?
Avatar
in <py-config> you don't import packages. In <py-config> you can specify many configurations options, and among those there is the packages = [...] option, which represents the list of packages which are installed by pyscript
14:42
also note that the text inside a <py-config> tag is in TOML format (or optionally JSON), not python
Avatar
Avatar
Jeff Glass
Py-config does a lot of things, but one of them is to instruct micropip to try to load packages from PyPI. Hereโ€™s an example: <py-config> packages = [ "bokeh", "numpy", "pandas", "panel==0.13.1" ] </py-config>
Thank you, I missed that out. But it's strange panel it is not available in pyodide console (https://pyodide.org/en/stable/console.html), while numpy and pandas is
Avatar
Avatar
Jeff Glass
Py-config does a lot of things, but one of them is to instruct micropip to try to load packages from PyPI. Hereโ€™s an example: <py-config> packages = [ "bokeh", "numpy", "pandas", "panel==0.13.1" ] </py-config>
oh btw what's the difference between py-config packages and py-env? I see some examples uses py-env to include packages instead of py-config
Avatar
Avatar
Ayato
oh btw what's the difference between py-config packages and py-env? I see some examples uses py-env to include packages instead of py-config
Jeff Glass 17/10/2022 01:45
So py-env was deprecated in the recent 2022.09.1 - it used to contain what is now the 'packages' and 'paths' fields that are in py-config, as a way of communicating "what's involved in setting up the 'environment' for this program"
01:45
2022.09.1 merged that into py-config so all the configuration lives together
Avatar
Avatar
Ayato
Thank you, I missed that out. But it's strange panel it is not available in pyodide console (https://pyodide.org/en/stable/console.html), while numpy and pandas is
Jeff Glass 17/10/2022 01:52
A bunch of packages are actually included with Pyodide, including pandas, numpy, opencv-python, ffmpeg, etc, which is why they're included in the REPL. The full list is here https://pyodide.org/en/stable/usage/packages-in-pyodide.html
Avatar
Avatar
Jeff Glass
So py-env was deprecated in the recent 2022.09.1 - it used to contain what is now the 'packages' and 'paths' fields that are in py-config, as a way of communicating "what's involved in setting up the 'environment' for this program"
oh I see
Avatar
Avatar
Jeff Glass
A bunch of packages are actually included with Pyodide, including pandas, numpy, opencv-python, ffmpeg, etc, which is why they're included in the REPL. The full list is here https://pyodide.org/en/stable/usage/packages-in-pyodide.html
ok, but why panel isn't in that list?
Avatar
Jeff Glass 17/10/2022 02:01
Because it doesnโ€™t need a build specially for Pyodide. Packages in that list, especially packages like numpy that rely heavily on c extension code, require various patches and modifications to run in Pyodide/WASM. Check out, for example, the numpy patches folder in the Pyodide GitHub https://github.com/pyodide/pyodide/tree/main/packages/numpy
02:01
So for those packages, the Pyodide maintainers or contributors work out the patches, build those libraries โ€œin treeโ€ and include them in the Pyodide distribution
02:02
Other packages may not be included because they either donโ€™t require special patching, or because nobodyโ€™s done the work to do the patching yet
02:02
In this case, the work on Panel is being done upstream, so itโ€™s unlikely to require a special build to be distributed with Pyodide
Avatar
oh I see
Avatar
anyone happen to have a good pyscript serializer for submitting form data? specifically one that handles select multiple fields
Avatar
sure. here you go hosler
17:27
formData = FormData.new(e.target.parentElement) payload = {} for entry in formData: key = entry[0] value = entry[1] if key in payload: if isinstance(payload[key], list): payload[key].append(value) else: payload[key] = [payload[key], value] else: payload[key] = value (edited)
Avatar
tedpatrick 17/10/2022 19:15
Tomorrow at 11AM CST we are hosting the PyScript Community Call. Details: https://github.com/pyscript/pyscript/issues/836
On October 18, 2022, at 11 AM CST, the PyScript Core Team will host our 1st PyScript Community Call. PyScript Community Call Status Report Highlights Q&amp;A We will be organizing the Q&amp...
Avatar
plushteddy 18/10/2022 17:25
Hey
17:25
Is it possible to use routing with PyScript?
Avatar
Avatar
plushteddy
Is it possible to use routing with PyScript?
Jeff Glass 18/10/2022 17:25
How do you mean? Like the way React or Vue does routing?
Avatar
plushteddy 18/10/2022 17:26
i mean like domain.com/{route} (edited)
Avatar
Jeff Glass 18/10/2022 17:28
Well, you can always get the current url as window.location.href, and make decisions based on that
17:28
But PyScript isn't a framework for sites - its a package for running Python, which exists at a somewhat lower level
Avatar
Avatar
Jeff Glass
But PyScript isn't a framework for sites - its a package for running Python, which exists at a somewhat lower level
plushteddy 18/10/2022 17:29
but why is it described as a framework?
Avatar
Avatar
Jeff Glass
Well, you can always get the current url as window.location.href, and make decisions based on that
plushteddy 18/10/2022 17:29
this is js wright?
Avatar
Jeff Glass 18/10/2022 17:29
Perhaps "framework" is not the best choice of words ๐Ÿ˜…
17:29
Yep, you'd do
17:29
from js import window if window.location.href ...
Avatar
plushteddy 18/10/2022 17:30
and how can i render a html after is got the href?
Avatar
Avatar
plushteddy
and how can i render a html after is got the href?
you can add elements to the dom
Avatar
Avatar
pmp-p
you can add elements to the dom
plushteddy 18/10/2022 18:00
would you mind to code me a code example of the router, im a absolut beginner in JavaScript and i can't find anything on the Internet
Avatar
afaik the event system is still in design but you can react to some events already i made a small experiment here https://github.com/pygame-web/showroom/blob/main/pyscript/unify.html
Avatar
Jeff Glass 18/10/2022 18:03
I think looking PyScript to provide a router is... probably not going to be super successful. You could create a router with PyScript if you wanted one, but it doesn't have one like React or something has
Avatar
plushteddy 18/10/2022 18:06
so do you guys have any other ideas?
Avatar
well i've a seen a js library doing routing on html blobs generated at runtime ( sorry can remember the name atm ) but that may be complicated to use until event system is stable (edited)
Avatar
@plushteddy I have the vague impression that you are looking for something which works server side and not client side. What do you want to do?
Avatar
Avatar
antocuni
@plushteddy I have the vague impression that you are looking for something which works server side and not client side. What do you want to do?
plushteddy 18/10/2022 19:35
No, client side ๐Ÿ˜„ I do a website for my develoment products
Avatar
then you do internal routing in the same way as you do in JS, which is to inspect the value of window.location.href and decide what to do depending on it. I guess that in JS there are libraries to automate that, but not in pyscript
Avatar
Avatar
antocuni
then you do internal routing in the same way as you do in JS, which is to inspect the value of window.location.href and decide what to do depending on it. I guess that in JS there are libraries to automate that, but not in pyscript
plushteddy 18/10/2022 20:19
And how can i load the html?
Avatar
I don't understand the question, sorry ๐Ÿคทโ€โ™‚๏ธ
Avatar
How do I load an image as a sprite? JS it would be new Image() but what is the equivalent in pyscript? trying to call Image() I get an error.
Avatar
Avatar
antocuni
I don't understand the question, sorry ๐Ÿคทโ€โ™‚๏ธ
plushteddy 19/10/2022 05:42
So u want to create a website with simple routing, like domain.com/<route>
Avatar
Avatar
rhacer
How do I load an image as a sprite? JS it would be new Image() but what is the equivalent in pyscript? trying to call Image() I get an error.
Jeff Glass 19/10/2022 12:25
JS: new Image(some, args) Pyodide: Image.new(some, args)
Avatar
@Jeff Glass Thank you very much.
02:39
and this is my html
02:39
769 bytes
Avatar
Jeff Glass 20/10/2022 02:43
Because of the way the requests module in the stdlib works, it doesn't work natively in Pyodide/in the browser. Check out this howto in the documentation for other strategies for getting HTML content: https://docs.pyscript.net/latest/howtos/http-requests.html
Avatar
Avatar
Jeff Glass
Because of the way the requests module in the stdlib works, it doesn't work natively in Pyodide/in the browser. Check out this howto in the documentation for other strategies for getting HTML content: https://docs.pyscript.net/latest/howtos/http-requests.html
AttributeError: 'FetchResponse' object has no attribute 'text' FetchResponse only suppport json?
Avatar
Avatar
Ayato
AttributeError: 'FetchResponse' object has no attribute 'text' FetchResponse only suppport json?
oh it's string in pyfetch
Avatar
how to get all html script of a website using pyodide.http? So I can manipulate it using beautifulsoup open_url and pyfetch doesn't seem to work for me
Avatar
Avatar
Ayato
AttributeError: 'FetchResponse' object has no attribute 'text' FetchResponse only suppport json?
FabioRosado 20/10/2022 10:00
Since this is using the pyodide http you might need to use .string() I havenโ€™t tried it yet so I am assuming from the docs https://pyodide.org/en/stable/usage/api/python-api/http.html#pyodide.http.FetchResponse.string
Avatar
Avatar
FabioRosado
Since this is using the pyodide http you might need to use .string() I havenโ€™t tried it yet so I am assuming from the docs https://pyodide.org/en/stable/usage/api/python-api/http.html#pyodide.http.FetchResponse.string
You're right, thank you
Avatar
FabioRosado 20/10/2022 10:02
No worries hopefully this will help ๐Ÿ˜„
Avatar
Avatar
antocuni
then you do internal routing in the same way as you do in JS, which is to inspect the value of window.location.href and decide what to do depending on it. I guess that in JS there are libraries to automate that, but not in pyscript
plushteddy 20/10/2022 16:50
๐Ÿ‘ 1
Avatar
If I use the <py-script src="/path/to/foo.py"></py-script> how do I teach Pylance about the location of the "js" module?
Avatar
I was hoping to remedy the problem by building py-script, but it appears that there are no real installation instructions yet (at least not that I can find).
Avatar
Avatar
rhacer
I was hoping to remedy the problem by building py-script, but it appears that there are no real installation instructions yet (at least not that I can find).
Jeff Glass 22/10/2022 02:24
The contributing guide is decent instructions for building PyScript https://github.com/pyscript/pyscript/blob/main/CONTRIBUTING.md
02:24
But the โ€œjsโ€ pseudo-module and teaching pylance about it isโ€ฆ well, thereโ€™s not a good solution to that I have seen. (edited)
Avatar
Aha, thank you so very much.
02:25
If I build locally can I not just add things to my pythonpath?
Avatar
Jeff Glass 22/10/2022 02:39
You canโ€ฆ except the โ€œjsโ€ module doesnโ€™t actually exist as such, itโ€™s Pyodide trickery that makes the JavaScript global namespace appear as a module to Python at runtime
02:41
Pyodide adds an additional module finder (JsFinder) to the import process, and uses that to register JavaScript objects (well, proxies of them) as Python modules when you try to import from js https://github.com/pyodide/pyodide/blob/1485732211816a545887b154fc41ce3781785ff7/src/pyodide-py/pyodide/_importhooks.py#L36
Avatar
Thank you very much. I appreciate your time.
02:47
As always nothing is smooth. I'm going to have to do some trickery to update my WSL version of Ubuntu to get a recent enough version of node!
Avatar
Jeff Glass 22/10/2022 02:48
Oof yeah! That is gonna be a fun time ๐Ÿ˜…
Avatar
I will likely have better luck on my Synology, so I may go that route we'll see.
02:58
I appreciate your time, I've been a back-end guy for the last 20+ years, and haven't done anything front-end for likely 18 of those 20, so this is a huge voyage of discovery for me.
Avatar
Jeff Glass 22/10/2022 02:59
Any time! Happy to - itโ€™s a super interesting space to poke around in, for sure.
Avatar
Thank you, I appreciate it.
Avatar
Avatar
rhacer
As always nothing is smooth. I'm going to have to do some trickery to update my WSL version of Ubuntu to get a recent enough version of node!
FabioRosado 22/10/2022 12:30
Not sure if it helps, but you could install nvm so you don't have to upgrade the base node version (this allows you to have as many versions as you want) https://github.com/nvm-sh/nvm (edited)
Avatar
FabioRosado 22/10/2022 18:22
Hey folks Iโ€™m trying to add more types to pyscript but Iโ€™m unsure what proxy is in the pywidget any idea? ๐Ÿค” โ€” edit โ€” Itโ€™s PyProxy - I think typing in pyodide is a recent thing? At least I remember an issue talking about this haha (edited)
Avatar
theblindkarp 26/10/2022 23:03
How do you use <py-button> in pyscript? I've been looking around and can't find anything. Is it not functional yet? Also: <py-inputbox>. How do i take the inputs from both of these? (edited)
Avatar
Don't use py-button, we are considering to kill it. I suggest to use a normal <button> with a py-click attribute. See for example this: view-source:https://pyscript.net/examples/todo.html (look at the source code)
Avatar
theblindkarp 26/10/2022 23:09
ok thank you
Avatar
theblindkarp 26/10/2022 23:27
can someone run through how to make a button with a command thru pyscript. I cannot figure it out
Avatar
theblindkarp 26/10/2022 23:44
here is my attempt. It says "loveCalc is not defined"
23:44
720 bytes
Avatar
Avatar
theblindkarp
Click to see attachment ๐Ÿ–ผ๏ธ
look at the example which I linked you above. You should use py-click=loveCalc()>, not pys-onClick. Note also tht loveCalc() must be CALLED inside py-click
Avatar
theblindkarp 27/10/2022 00:01
That helps a lot! thank you!
Avatar
theblindkarp 27/10/2022 00:29
Now im having problems with input values. how do you get input values from an input box? cooltext is not taking the text inside the input.
00:29
809 bytes
Avatar
Jeff Glass 27/10/2022 00:34
You'll need to get the value from the input box inside your loveCalc function
00:34
def loveCalc(*args, **kwargs): cooltext = Element("test-input").value cooltxt = str(cooltext) Element("testlabel").element.innerText = cooltxt
Avatar
Avatar
theblindkarp
Now im having problems with input values. how do you get input values from an input box? cooltext is not taking the text inside the input.
Jeff Glass 27/10/2022 00:35
Or even just def loveCalc(*args, **kwargs): Element("testlabel").element.innerText = Element("test-input").value
Avatar
theblindkarp 27/10/2022 00:36
oH!
00:36
i forgot about local variables
Avatar
Jeff Glass 27/10/2022 00:36
Well, more that you want to be updating/getting the value of the inputbox when you click the submit button - the way you had it, cooltext is set once when the script runs and never again
Avatar
theblindkarp 27/10/2022 00:37
yeah u right
00:37
i understand
Avatar
Jeff Glass 27/10/2022 00:37
๐Ÿ‘
Avatar
theblindkarp 27/10/2022 00:37
i've used other python guis and its sort of the same type of thing
00:37
thanks for the help
Avatar
Jeff Glass 27/10/2022 00:37
Any time! Keep the questions coming!
Avatar
I tried to fetch data using pyfetch, but I get an error like this. What's wrong?
903 bytes
458 bytes
Avatar
Avatar
Ayato
I tried to fetch data using pyfetch, but I get an error like this. What's wrong?
event handlers need to take an argument: try to write: async def fetch_data(event): ...
Avatar
Avatar
antocuni
event handlers need to take an argument: try to write: async def fetch_data(event): ...
it works, thank you
๐ŸŽ‰ 1
Avatar
how to make pyfetch run with no-cors mode?
Avatar
or rather, is it possible?
Avatar
Avatar
Ayato
how to make pyfetch run with no-cors mode?
Jeff Glass 31/10/2022 12:28
Any keyword args you pass pyfetch are passed as additional optional parameters to JavaScript's fetch api https://pyodide.org/en/stable/usage/api/python-api/http.html?highlight=pyfetch#pyodide.http.pyfetch https://developer.mozilla.org/en-US/docs/Web/API/fetch#parameters So you can do: from pyodide.http import pyfetch result = await pyfetch("http://www.example.com", mode="no-cors")
๐Ÿ‘ 1
Avatar
Hi all - new here and new to many topics but I was wondering how (or if it's even possible) to create a Chrome Extension using PyScript? I followed the tutorial https://developer.chrome.com/docs/extensions/mv3/getstarted/ and added the usual boiler-plate <script defer src="https://pyscript.net/latest/pyscript.js%22%3E</script> but no joy ๐Ÿ˜ฆ I'm convinced this would open PyScript up to to huge audience potentially, but I (and others) would need a little help getting started... Any links/resources/suggestions would be VERY welcome, thank you. (edited)
Avatar
Avatar
Pfython
Hi all - new here and new to many topics but I was wondering how (or if it's even possible) to create a Chrome Extension using PyScript? I followed the tutorial https://developer.chrome.com/docs/extensions/mv3/getstarted/ and added the usual boiler-plate <script defer src="https://pyscript.net/latest/pyscript.js%22%3E</script> but no joy ๐Ÿ˜ฆ I'm convinced this would open PyScript up to to huge audience potentially, but I (and others) would need a little help getting started... Any links/resources/suggestions would be VERY welcome, thank you. (edited)
tedpatrick 03/11/2022 08:21
Seeing some HTMLEntities in the script tag you posted. <script defer src="https://pyscript.net/latest/pyscript.js"></script> (edited)
Avatar
Avatar
tedpatrick
Seeing some HTMLEntities in the script tag you posted. <script defer src="https://pyscript.net/latest/pyscript.js"></script> (edited)
Thanks Ted, that was just a copy/paste artifact. Here's the full html and error message:
17:26
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width,initial-scale=1" /> <title>PyScript Hello World</title> <link rel="icon" type="image/png" href="favicon.png" /> <link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" /> <script defer src="https://pyscript.net/latest/pyscript.js"></script> </head> <body> Hello world! <br> This is the current date and time, as computed by Python: <py-script> from datetime import datetime now = datetime.now() now.strftime("%m/%d/%Y, %H:%M:%S") </py-script> </body> <script src="popup.js"></script> </html>
17:26
Refused to load the script 'https://pyscript.net/latest/pyscript.js' because it violates the following Content Security Policy directive: "script-src 'self'". Note that 'script-src-elem' was not explicitly set, so 'script-src' is used as a fallback.
17:28
It pops up the text "Hello world! This is the current date and time, as computed by Python:" but errors out at <py-script>
17:30
And popup.js is simply console.log("Script finished!")
Avatar
tedpatrick 03/11/2022 17:31
Are you seeing this on a public url? on a local server?
Avatar
Local server Ted
Avatar
tedpatrick 03/11/2022 17:32
Content Security Policy is typically set by the server in use. Any server details?
17:32
Or is this being loaded into an iframe. That also has CSP cases.
Avatar
It'll just be defaults... I'm running Chrome on a Windows 10 Laptop; Opening "Manage Extensions" with "Developer Mode" on
17:33
No iframe
Avatar
tedpatrick 03/11/2022 17:33
Ah are you running this in an extension?
Avatar
Yessir
Avatar
tedpatrick 03/11/2022 17:34
Ah... welcome to a very strict environment.
17:35
Yes the browser security model varies depending on the context of where things are loaded. Extensions require all dependencies to be local (script-src 'self')
17:35
As Extensions have access to fileio and other more guarded system apis, the security context is much stricter.
Avatar
Is there a way of using PyScript (locally?) as part of an extension do you know, Ted?
Avatar
tedpatrick 03/11/2022 17:37
I spent some time finding all the key dependencies to run fully local, including localizing pyodide off the cdn... One sec... finding url.
Avatar
tedpatrick 03/11/2022 17:44
If you were to write all these files to a local folder in the extension with a <py-config> denoting the runtime as local, this should all work. https://pyscript.net/latest/pyscript.js https://pyscript.net/latest/pyscript.css https://cdn.jsdelivr.net/pyodide/v0.21.2/full/pyodide.js https://cdn.jsdelivr.net/pyodide/v0.21.2/full/pyodide_py.tar https://cdn.jsdelivr.net/pyodide/v0.21.2/full/pyodide.asm.js https://cdn.jsdelivr.net/pyodide/v0.21.2/full/pyodide.asm.data https://cdn.jsdelivr.net/pyodide/v0.21.2/full/repodata.json https://cdn.jsdelivr.net/pyodide/v0.21.2/full/pyodide.asm.wasm https://cdn.jsdelivr.net/pyodide/v0.21.2/full/packaging-21.3-py3-none-any.whl https://cdn.jsdelivr.net/pyodide/v0.21.2/full/distutils.tar https://cdn.jsdelivr.net/pyodide/v0.21.2/full/micropip-0.1-py3-none-any.whl <py-config> autoclose_loader = true [[runtimes]] src = "pyodide/pyodide.js" name = "pyodide-0.21.2" lang = "python" </py-config> (edited)
17:45
We need to make this localization part of the pyscript CLI as a feature
๐Ÿ‘ 1
17:46
Also updating to pyodide v0.21.3 is key ๐Ÿ™‚
Avatar
Thanks so much Ted, I'll give that a try. Yes I think if it was available right out of the box it would open PyScript up to a huge audience of Python devs who have always wanted to create a Chrome Extension but never tried! If I can get it all working I'll write up a short tutorial "Create a Chrome Extension using PyScript".
Avatar
Avatar
tedpatrick
Also updating to pyodide v0.21.3 is key ๐Ÿ™‚
0.21.3 or 0.21.2?
Avatar
tedpatrick 03/11/2022 17:49
I agree on the extensions front but it is a very complex context to work in. CSP is a minefield for good reason. Extensions can do pretty most anything and that is a double edged sword.
17:51
17:51
But make sure to run the app and see exactly what URLS or loaded from JS Console:Network tab
17:52
As the whls are loading from relative urls
Avatar
wow, this is awesome! The only drawback is that it creates very big extensions. But once we have micropython it could be a very cool use case
๐Ÿคฉ 1
Avatar
Appreciate your time @tedpatrick . I'm still doing something wrong obviously... if you have the time and motivation, would you be kind enough to have a look at my simple example here please: https://github.com/PFython/ChromeAWSOM (edited)
18:23
I created setup.py to download the dependencies you listed, but suspect my hello.html has something wrong in the 'boiler-plate' code (edited)
Avatar
tedpatrick 03/11/2022 19:08
Ok here is a repo that supports packaging the runtime for local/offline/Extension use. https://github.com/tedpatrick/pyscript-local-runtime
Contribute to tedpatrick/pyscript-local-runtime development by creating an account on GitHub.
๐Ÿ‘ 1
Avatar
so a js.Image has an onload that takes an anonymous function. how does that work in pyscript? I've tried assigning a lambda and that doesn't seem to work. I can pass a function name in, but then I can't pass any arguments.
Avatar
Avatar
rhacer
so a js.Image has an onload that takes an anonymous function. how does that work in pyscript? I've tried assigning a lambda and that doesn't seem to work. I can pass a function name in, but then I can't pass any arguments.
Jeff Glass 03/11/2022 19:37
Lambda does seem to work for me: from js import console, document, Image img = Image.new() img.src = "cg.png" img.onload = lambda _: console.log("Loaded!") document.body.appendChild(img)
19:37
19:38
Unless I'm misunderstanding
Avatar
Then I am likely just being dumb. One sec and let me use your example.
Avatar
Jeff Glass 03/11/2022 19:39
(That's on 2022.09.1, current /latest, if it matters)
Avatar
Well that is working perfectly, now to figure out why my implementation wasn't working.
๐Ÿ‘ 1
Avatar
Jeff Glass 03/11/2022 19:41
If you did want to use a named function there's always functools.partial
Avatar
Now that is something I knew absolutely nothing about. Thank you.
Avatar
Avatar
rhacer
Now that is something I knew absolutely nothing about. Thank you.
Jeff Glass 03/11/2022 20:05
No sweat - if you figure out what the hitch was in your original code, Iโ€™d be curious, since youโ€™re likely not the first/last to hit whatever it is
Avatar
will do, I have a feeling it's me not lambdaing correctly. but I'll get there.
Pfython started a thread. 04/11/2022 08:03
Avatar
any example code of Pyodide? making request to api?
Avatar
@il0vecats The above docs are outdated -- specifically, I mean the section about https://docs.pyscript.net/latest/guides/http-requests.html#pyscript-html-code where py-env tag is still being used. One of my PR fixes and updates this but it isn't merged yet. Basically, py-env is deprecated and py-config should be used.
Avatar
oh, that's worth an issue!
Avatar
FabioRosado 07/11/2022 11:04
I can tackle that today unless someone wants to?
๐Ÿ™Œ 1
Avatar
pls, go for it! ๐Ÿ™‚
Avatar
@FabioRosado I already have it in my PR 914
12:02
Yet to be merged though hehe
Avatar
FabioRosado 07/11/2022 12:02
Oh right! I need to read better (or new glasses?) ๐Ÿ˜›
๐Ÿค“ 1
๐Ÿง 1
Avatar
Because it contains other things also
Avatar
FabioRosado 08/11/2022 19:07
Just wondering if I am missing something, the py-box is only really used to add flex and width to a div right? I'm just wondering since I started working on deprecating these elements and wonder what to suggest to users - a flexy div?
Avatar
Avatar
FabioRosado
Just wondering if I am missing something, the py-box is only really used to add flex and width to a div right? I'm just wondering since I started working on deprecating these elements and wonder what to suggest to users - a flexy div?
Jeff Glass 08/11/2022 19:23
There's the "widths" attribute of <py-box> that creates child divs with specified proportional flex-ness... but I'm not sure how much it's really been used. Personally I'd let it go https://github.com/pyscript/pyscript/blob/1345449d574d816a539bbe534d66650791a3f2b8/pyscriptjs/src/components/pybox.ts#L50-L66 (edited)
Avatar
Avatar
Jeff Glass
There's the "widths" attribute of <py-box> that creates child divs with specified proportional flex-ness... but I'm not sure how much it's really been used. Personally I'd let it go https://github.com/pyscript/pyscript/blob/1345449d574d816a539bbe534d66650791a3f2b8/pyscriptjs/src/components/pybox.ts#L50-L66 (edited)
FabioRosado 08/11/2022 19:28
Thanks! yeah I'm working on the issue to deprecate py-title, py-input, py-button and py-box. I'l suggest that folks use the flex-box class instead. As you mentioned, not sure if folks have been using it much ๐Ÿ˜„
๐Ÿ‘ 1
Avatar
it would be nice to have a collection of "things which people did with pyscript" so that we can check what it's actually used and what is not
Avatar
Avatar
antocuni
it would be nice to have a collection of "things which people did with pyscript" so that we can check what it's actually used and what is not
FabioRosado 08/11/2022 20:27
Yeah would be cool to have these analytics
Avatar
maybe we use twitter and this channel to ask people to send us links/repos/html files of things they did. But then we need a place where to keep track of them
Avatar
Jeff Glass 08/11/2022 22:47
@losphilly hopefully this tag works! So a couple of things going on with your question about loading the 'qrng' and 'qiskit' packages from the other channel
22:51
One is - the qrng package seems to have some poorly configured metadata - it should like 'qiskit' as a dependency but doesn't, so one seems to have to install it separately. This is what causes the ModuleNotFoundError: No module named 'qiskit' error if you only include qrng in <py-config> packages...
22:54
Including qiskit in the packages leads to a new error: ValueError: Can't find a pure Python 3 wheel for 'qiskit-terra<0.8,>=0.7'. See: https://pyodide.org/en/stable/usage/faq.html#micropip-can-t-find-a-pure-python-wheel That link explains more, but essentially, Pyodide (the default runtime used by PyScript) requires packages to either: (a) have a released .whl that is only Python (i.e that ends in "py3-none-any.whl") or (b) that has been specifically built for/with Pyodide, possibly with patches to account for some system resources that are missing/different from ones in a 'normal' desktop environment. That list is here: https://pyodide.org/en/stable/usage/packages-in-pyodide.html
Avatar
#tag Do tags work?
Avatar
Avatar
antocuni
maybe we use twitter and this channel to ask people to send us links/repos/html files of things they did. But then we need a place where to keep track of them
I was looking for tags in Discord e.g. #cool but maybe using the cross chat might help? #โ•ฐโ•ธpython-help It didn't cross post this message to the python-help I'm pretty darn sure that this problem of collecting useful stuff in discord is a common problem. There should be a common solution ๐Ÿ™‚ (edited)
Avatar
Avatar
antocuni
maybe we use twitter and this channel to ask people to send us links/repos/html files of things they did. But then we need a place where to keep track of them
The admin could create a new 'helpful' user so that anything sent to @helpful could be collected?
Avatar
no, what I was talking about is something more structured. E.g., when I need to understand if and how a certain pyscript feature it's used, I'd love to have a git repo full of .html files which I can just grep
Avatar
Some one would have to collect said html and put it into the repo. Maybe you could share @antocuni what you have already collected?
Avatar
I don't have collected anything ๐Ÿ˜…. But it's something which I would like to have, so if someone wants to tackle this task it would be very welcome and a very good and appreciated way to contribute to PyScript development
13:49
(in case anyone does, please make sure to ask the original authors what is the license, else we cannot include the files in a repo)
Avatar
Avatar
antocuni
no, what I was talking about is something more structured. E.g., when I need to understand if and how a certain pyscript feature it's used, I'd love to have a git repo full of .html files which I can just grep
Jeff Glass 09/11/2022 13:57
Is this a different thing than the PyScript Collective repo? I know that's currently featuring mostly resources and "fleshed-out" examples, but I don't know that it has to be only that
Avatar
I think that the point of pyscript-collective is precisely to be curated: i.e., "good examples", bug free, good coding practices, etc.
14:10
I want the opposite: I want to see all the horrible things which people do ๐Ÿ˜‚. But in a good way: if we see that there are "bad patterns", we can try to find solutions
๐Ÿ‘ 2
Avatar
this might be a better place to ask. question: anyone got pyarrow or polars working in pyodide or new micropython in the browser yet? looking for some examples for exploratory data work
Avatar
takacsjanos 10/11/2022 23:30
hi
23:31
I try to embed a file in my pyscript app, but it does not show up in the virtual file system
23:33
<py-config type="json"> { "fetch": [{ "from": "Hipparcos.csv", "to_folder": "/home/pyodide", "to_file": "Hipparcos.csv" }] } </py-config>
23:33
but os.listdir(os.getcwd()) is empty
23:34
what am I missing?
Avatar
takacsjanos 11/11/2022 00:02
if anyone runs into this one: the fetch does not work, instead I did this at the beginning of my python script: from pyodide.http import pyfetch import asyncio async def get_files(list): for name in list: response = await pyfetch(url=name, method="GET") with open(name, 'wb') as f: f.write(await response.bytes()) await get_files(["Hipparcos.csv", "other_data.txt"])
Avatar
Avatar
takacsjanos
what am I missing?
Jeff Glass 11/11/2022 02:28
To be honest, I'm not sure... your example works for me, with a file called "Hipparcos.csv" as a resource at the same relative URL as the source file, except for the file name
02:28
To ask an obvious question - you're linking against a current build version of PyScript or /unstable, yeah? fetch configs aren't in 2022.09.1 (latest). (edited)
02:30
<py-config type="json"> { "fetch": [{ "from": "Hipparcos.csv", "to_folder": "/home/pyodide", "to_file": "Hipparcos.csv" }] } </py-config> <py-script> import os display(f"{os.getcwd()=}") display(f"{os.listdir(os.getcwd())=}") </py-script>
02:30
02:34
Also for what it's worth, to_folder defaults to "." and from defaults to "" (relative URL prefix), so if you want to just load a file from the relative URL "Hipparcos.csv" to a spot in the filesystem in the same folder that scripts are executed in, you can just do: <py-config type="json"> { "fetch": [{ "files": ["Hipparcos.csv"] }] } </py-config> (edited)
Avatar
Avatar
Jeff Glass
Also for what it's worth, to_folder defaults to "." and from defaults to "" (relative URL prefix), so if you want to just load a file from the relative URL "Hipparcos.csv" to a spot in the filesystem in the same folder that scripts are executed in, you can just do: <py-config type="json"> { "fetch": [{ "files": ["Hipparcos.csv"] }] } </py-config> (edited)
takacsjanos 11/11/2022 04:16
thanks. I've tried the files option too, also without success for the moment, I'm going to stick with pyfetch btw, I have a working skyfield python lib in my browser!
04:20
the only thing I had to modify in skyfield was one of its dependencies, sgp4
Avatar
Avatar
Jeff Glass
<py-config type="json"> { "fetch": [{ "from": "Hipparcos.csv", "to_folder": "/home/pyodide", "to_file": "Hipparcos.csv" }] } </py-config> <py-script> import os display(f"{os.getcwd()=}") display(f"{os.listdir(os.getcwd())=}") </py-script>
takacsjanos 11/11/2022 04:24
and to answer your question, I'm using "https://pyscript.net/latest/pyscript.js", and the documentation said I can use fetch configs, so I tried
Avatar
Avatar
takacsjanos
and to answer your question, I'm using "https://pyscript.net/latest/pyscript.js", and the documentation said I can use fetch configs, so I tried
Jeff Glass 11/11/2022 04:46
Ohhhh well thereโ€™s an issue for us to solve - weโ€™re pushing new updates to the docs with every merge, but they probably want to be associated with a specific pinned version, kinda like how Pyodide (and others) do it
04:47
Fetch configs are merged into the main branch as of a few days ago, and the docs got updated, but they wonโ€™t be part of a release until the next release (soon!)
04:48
Apologies for the confusion - and nice workaround!
Avatar
takacsjanos 11/11/2022 05:00
no problem. I know the pain of generating docs, we had something similar at work. ( commits from branches overwrote the docs until we restrictred the api docs generation to the main branch. and we run this job separately for releases. )
Avatar
Avatar
Jeff Glass
Ohhhh well thereโ€™s an issue for us to solve - weโ€™re pushing new updates to the docs with every merge, but they probably want to be associated with a specific pinned version, kinda like how Pyodide (and others) do it
FabioRosado 11/11/2022 06:35
Oh dang yeah we need to fix that ๐Ÿ˜ฌ
Avatar
Hi @takacsjanos I am gonna look at this today. Thanks for trying the fetch stuff. Such examples will help make it more robust.
Avatar
FabioRosado 11/11/2022 07:52
Just opened an issue for the docs ๐Ÿ˜„
Avatar
well, it's a more general problem. Basically, I think we should kill /latest, or if we really want to keep it, we should never use it in our docs and examples
08:02
it doesn't make any sense: /latest is something which works for 1 month and then it will be probably be broken forever
08:02
our docs should contain references to the specific version of pyscript which they belong to
Avatar
@takacsjanos -- Seems like the example worked as @Jeff Glass rightly pointed out. So yeah, we need to fix the docs.
Avatar
Hi @tedpatrick - As discussed, here's a "friends link" to the Medium article I just published: https://medium.com/@petefison/6c6b0e2e1573?source=friends_link&sk=98a71e5b8204b0c4c53819cb4858a2a0 (edited)
In Python?!! Thatโ€™s right, and itโ€™s pretty painless with PyScriptโ€ฆ
๐Ÿคฉ 3
๐Ÿ‘๐Ÿฝ 1
09:07
Would be delighted for you to Tweet/share (edited)
Avatar
Avatar
Pfython
Hi @tedpatrick - As discussed, here's a "friends link" to the Medium article I just published: https://medium.com/@petefison/6c6b0e2e1573?source=friends_link&sk=98a71e5b8204b0c4c53819cb4858a2a0 (edited)
amazing!
๐Ÿ˜‡ 1
Avatar
Avatar
antocuni
well, it's a more general problem. Basically, I think we should kill /latest, or if we really want to keep it, we should never use it in our docs and examples
FabioRosado 11/11/2022 09:41
Yah that would make sense, I wasn't sure if we wanted to use latest/stable or something else In the past I've used different branches so docs would be build from a specific version but we can probably do the same with versioning ๐Ÿค”
Avatar
personally, I would kill /latest altogether because I don't really understand what is its utility, but it might yet another case of antonios-unpopular-opinions ๐Ÿ˜…
Avatar
FabioRosado 11/11/2022 09:52
Is the /latest like a nightly build kind of thing? ๐Ÿค”
Avatar
/unstable corresponds to the origin/main, i.e. it's rebuilt every time we merge a PR /latest corresponds to the latest release
09:57
so, /unstable might have a value for testing purposes, but for /latest you could just use the explicit release
Avatar
FabioRosado 11/11/2022 09:59
Ah gotcha that makes sense thanks ๐Ÿ˜„
Avatar
mason norberg 13/11/2022 13:55
Hey quick question I'm wanting to do some image manipulation, and I'm try to add some text with it but if I try anything with truetype function to use my own font it doesn't work. I would love to know why this isn't working and or if it doesn't work at all.
Avatar
Avatar
Taras
this might be a better place to ask. question: anyone got pyarrow or polars working in pyodide or new micropython in the browser yet? looking for some examples for exploratory data work
so, nobody? or not possible in the browser yet?
Avatar
Avatar
mason norberg
Hey quick question I'm wanting to do some image manipulation, and I'm try to add some text with it but if I try anything with truetype function to use my own font it doesn't work. I would love to know why this isn't working and or if it doesn't work at all.
Leonard Wolf 13/11/2022 15:40
You have .ttf file for font and are you using pillow for manipulation? (edited)
Avatar
Avatar
Taras
so, nobody? or not possible in the browser yet?
Jeff Glass 13/11/2022 16:40
I haven't seen anyone come through the discord/reddit/forum with work on either of those packages, no It looks like neither pyarrow or polars publish a pure python wheel (i.e. a *py3-none-any.whl), which pyodide relies upon to load a package from PyPI. (Though there's experimental support for Rust https://blog.pyodide.org/posts/rust-pyo3-support-in-pyodide/) So building those packages in a Pyodide-friendly way would probably need to be a first step (edited)
๐Ÿ‘ 1
Avatar
Avatar
Leonard Wolf
You have .ttf file for font and are you using pillow for manipulation? (edited)
mason norberg 13/11/2022 19:15
Yea
19:17
Itโ€™s actually a ttf file
Avatar
Avatar
mason norberg
Yea
Leonard Wolf 14/11/2022 03:41
font = ImageFont.truetype("font.ttf", 28) (edited)
03:41
Try like this
Avatar
Avatar
mason norberg
Itโ€™s actually a ttf file
Leonard Wolf 14/11/2022 03:42
Yeah typo
Avatar
Avatar
Leonard Wolf
font = ImageFont.truetype("font.ttf", 28) (edited)
mason norberg 14/11/2022 12:50
I'm still getting the same error
Jeff Glass started a thread. 14/11/2022 13:30
Avatar
Avatar
Jeff Glass
I haven't seen anyone come through the discord/reddit/forum with work on either of those packages, no It looks like neither pyarrow or polars publish a pure python wheel (i.e. a *py3-none-any.whl), which pyodide relies upon to load a package from PyPI. (Though there's experimental support for Rust https://blog.pyodide.org/posts/rust-pyo3-support-in-pyodide/) So building those packages in a Pyodide-friendly way would probably need to be a first step (edited)
good read. Thanks for sharing it. I passed it on to Polars devs to review those steps for whenever they are ready to make their lib work in pyodide.
Avatar
Does anyone know how to use pyscript in a next.js project everytime I try to do something other than "print('hello')", like functions I get a massive error?
Avatar
Hello, I get this error when I try to every make a python function in my python script: PythonError: Traceback (most recent call last): File "/lib/python3.10/asyncio/futures.py", line 201, in result raise self.exception File "/lib/python3.10/asyncio/tasks.py", line 232, in step result = coro.send(None) File "/lib/python3.10/site-packages/_pyodide/_base.py", line 506, in eval_code_async await CodeRunner( File "/lib/python3.10/site-packages/_pyodide/_base.py", line 241, in init_ self.ast = next(self._gen) File "/lib/python3.10/site-packages/_pyodide/_base.py", line 142, in _parse_and_compile_gen mod = compile(source, filename, mode, flags | ast.PyCF_ONLY_AST) File "", line 1 for i in range(9): print(i) def func(): print('function works') ^^^ SyntaxError: invalid syntax
Avatar
Avatar
ThoGos
Hello, I get this error when I try to every make a python function in my python script: PythonError: Traceback (most recent call last): File "/lib/python3.10/asyncio/futures.py", line 201, in result raise self.exception File "/lib/python3.10/asyncio/tasks.py", line 232, in step result = coro.send(None) File "/lib/python3.10/site-packages/_pyodide/_base.py", line 506, in eval_code_async await CodeRunner( File "/lib/python3.10/site-packages/_pyodide/_base.py", line 241, in init_ self.ast = next(self._gen) File "/lib/python3.10/site-packages/_pyodide/_base.py", line 142, in _parse_and_compile_gen mod = compile(source, filename, mode, flags | ast.PyCF_ONLY_AST) File "", line 1 for i in range(9): print(i) def func(): print('function works') ^^^ SyntaxError: invalid syntax
FabioRosado 15/11/2022 12:30
Hello ๐Ÿ‘‹ can you share your code?
Avatar
Index.tsx:
12:58
_document.tsx
Avatar
Avatar
ThoGos
Index.tsx:
Jeff Glass 15/11/2022 14:15
You were asking about running this in next.js yeah? I fiddled with it a bit yesterday - best as I can tell (and I don't know that much about next/react, sorry), it's stripping whitespace out of the jsx/tsx, so <py-script> for i in range(9): print(i) def func(): print('function works') </pyscript> becomes <py-script>for i in range(9): print(i) def func(): print('function works')</py-script> and the indentation breaks. As for how to fix it? 20 minutes of googling "How to not strip whitespace in next.js" didn't get me anywhere obvious ๐Ÿ˜…, but perhaps there's a way that's more obvious if you know the framework
Avatar
Paul Everitt 15/11/2022 14:30
@Jeff Glass In the old days, people wrapped their inline JS in a CDATA <script> <![CDATA[ var x = 1; ]]> </script> (edited)
๐Ÿ‘ 1
14:31
Perhaps PyScript's tag parser thingy could try first for a CDATA node when getting the <py-script> body
Avatar
FabioRosado 15/11/2022 14:51
Interestingly, the issue seems to be when adding multiple things inside a py-script tag. When it tried to run just the for loop it worked ๐Ÿค”
Avatar
Avatar
Jeff Glass
You were asking about running this in next.js yeah? I fiddled with it a bit yesterday - best as I can tell (and I don't know that much about next/react, sorry), it's stripping whitespace out of the jsx/tsx, so <py-script> for i in range(9): print(i) def func(): print('function works') </pyscript> becomes <py-script>for i in range(9): print(i) def func(): print('function works')</py-script> and the indentation breaks. As for how to fix it? 20 minutes of googling "How to not strip whitespace in next.js" didn't get me anywhere obvious ๐Ÿ˜…, but perhaps there's a way that's more obvious if you know the framework
so, we should expect the Python identation issues in PyScript too? ๐Ÿ˜†
Avatar
FabioRosado 15/11/2022 15:04
You are still writing python code ๐Ÿ™‚
Avatar
Avatar
FabioRosado
You are still writing python code ๐Ÿ™‚
yeah, I know, but omg
Avatar
Avatar
Jeff Glass
You were asking about running this in next.js yeah? I fiddled with it a bit yesterday - best as I can tell (and I don't know that much about next/react, sorry), it's stripping whitespace out of the jsx/tsx, so <py-script> for i in range(9): print(i) def func(): print('function works') </pyscript> becomes <py-script>for i in range(9): print(i) def func(): print('function works')</py-script> and the indentation breaks. As for how to fix it? 20 minutes of googling "How to not strip whitespace in next.js" didn't get me anywhere obvious ๐Ÿ˜…, but perhaps there's a way that's more obvious if you know the framework
Thank you, I'll check it out ๐Ÿ™‚
Avatar
in that case, many many many web frameworks will be fuddling pyscript code blocks and creating issues for devs to resolve purely due to indentation. some things just never change
Avatar
yeah, I bet many bundlers will mess that up
Avatar
Avatar
Taras
in that case, many many many web frameworks will be fuddling pyscript code blocks and creating issues for devs to resolve purely due to indentation. some things just never change
FabioRosado 15/11/2022 15:07
I don't follow, indentation is a specific part of Python. Pyscript is not a new language ๐Ÿ™‚
Avatar
Avatar
FabioRosado
I don't follow, indentation is a specific part of Python. Pyscript is not a new language ๐Ÿ™‚
right, but beyond vanilla js and html, I expect you guys to have many similar issues in web apps.
Avatar
FabioRosado 15/11/2022 15:15
This is pretty terrible but escaping new lines/whitespaces work <py-script> for i in range(9):{"\n"} {" "}{" "}print(i){"\n"} {"\n"} def func():{"\n"} {" "}{" "}print('function works'){"\n"} </py-script> I'm totally not going into a rabbit hole trying to see if we can override jsx behaviour of striping newlines and spaces ๐Ÿ˜ฌ
15:19
Oh... here's how to do it ๐Ÿ˜„
15:21
So we need to stop jsx from stripping whitespace which is a bit annoying, but we can use dangerouslSetInnerHTML to pass the python function: const data = ` for i in range(9): print(i) def func(): print('function works') ` return ( <py-script dangerouslySetInnerHTML={{__html: data}} />
15:22
You can also pass the data directly into __html: althoug I got some issues with whitespace so using a variable was a bit easier to deal with white space ๐Ÿ™‚
Avatar
Avatar
FabioRosado
This is pretty terrible but escaping new lines/whitespaces work <py-script> for i in range(9):{"\n"} {" "}{" "}print(i){"\n"} {"\n"} def func():{"\n"} {" "}{" "}print('function works'){"\n"} </py-script> I'm totally not going into a rabbit hole trying to see if we can override jsx behaviour of striping newlines and spaces ๐Ÿ˜ฌ
Thank you so much, this is so weird lol, but I works (so that's good). Do you know if it's possible to write this code in a seperate python file and then "import" it into the py-script component and then run off that? And if you do can you show me? Thanks ๐Ÿ™‚
Avatar
Avatar
ThoGos
Thank you so much, this is so weird lol, but I works (so that's good). Do you know if it's possible to write this code in a seperate python file and then "import" it into the py-script component and then run off that? And if you do can you show me? Thanks ๐Ÿ™‚
FabioRosado 15/11/2022 15:51
Yeah this way is much better and easier to work with: const data = ` for i in range(9): print(i) def func(): print('function works') ` return ( <py-script dangerouslySetInnerHTML={{__html: data}} /> As for the files i was trying that first but next does some weird stuff with folders and I kept getting a file not found. Will need to poke at it a bit more (edited)
Avatar
Avatar
FabioRosado
Yeah this way is much better and easier to work with: const data = ` for i in range(9): print(i) def func(): print('function works') ` return ( <py-script dangerouslySetInnerHTML={{__html: data}} /> As for the files i was trying that first but next does some weird stuff with folders and I kept getting a file not found. Will need to poke at it a bit more (edited)
Okay, have fun :), but yeah I implemented that way and it works perfectly, so thank you so much for that ๐Ÿ™‚
Avatar
BTW does anyone know how I can import packages like numpy and such?
Avatar
Avatar
ThoGos
BTW does anyone know how I can import packages like numpy and such?
Jeff Glass 15/11/2022 17:35
For sure - in version 2022.09.1 (the latest release), you install packages using <py-config> packages:... like so: <py-config> packages=['numpy'] </py-config> <py-script> import numpy as np print(np.arange(15).reshape(3,5)) </py-script>
17:37
Under the hood, <py-config> pacakges=... uses a tool called micropip (a lot like pip, but Pyodide-specific) to install the list of packages
Avatar
Thanks ๐Ÿ™
Avatar
Greetings to everyone! I am new to PyScript. "Is there any way to keep the <py-script> code within another python file and utilise that file as the src attribute, for example <py-script src="example.py" ></py-script>?" (edited)
Avatar
Avatar
Deepak
Greetings to everyone! I am new to PyScript. "Is there any way to keep the <py-script> code within another python file and utilise that file as the src attribute, for example <py-script src="example.py" ></py-script>?" (edited)
yeah, I think that would be ideal vs. the proposed somewhat hacky way above ๐Ÿ™‚ https://discord.com/channels/972017612454232116/972020206538997822/1042104595557658664
Avatar
Avatar
Taras
yeah, I think that would be ideal vs. the proposed somewhat hacky way above ๐Ÿ™‚ https://discord.com/channels/972017612454232116/972020206538997822/1042104595557658664
Jeff Glass 16/11/2022 16:41
There is! Exactly as you say, you can use <py-script src=โ€œโ€ฆโ€> to load the way from an external file
16:42
Fabioโ€™s example above, I think, is explicitly a workaround to avoid stripping whitespace when running in next.js
16:43
See, for example, the โ€œtodoโ€ example on GitHub: https://github.com/pyscript/pyscript/blob/main/examples/todo.html
Home Page: https://pyscript.net Examples: https://pyscript.net/examples - pyscript/todo.html at main ยท pyscript/pyscript
๐Ÿ‘ 1
16:45
Fair warning - exactly what the semantics are around โ€œsrc=โ€œ may change in future versions of PyScript, but right now, itโ€™s the same putting the contents of the references file inside the <py-script> tag https://github.com/pyscript/pyscript/issues/895
Avatar
Avatar
Jeff Glass
There is! Exactly as you say, you can use <py-script src=โ€œโ€ฆโ€> to load the way from an external file
that does look good!
Avatar
Avatar
Jeff Glass
Fabioโ€™s example above, I think, is explicitly a workaround to avoid stripping whitespace when running in next.js
Thanks โ˜บ๏ธ
Avatar
Hey, i need some help i have a simple html with a slider but now i want the saved variable from the html code in a python variable.
09:52
My main idea is to control a servo with the website using an raspberypi
09:53
Thats the code i have right now
952 bytes
Avatar
ok nvm i dont need it right now couse my raspberry pi stoped working xD first i need to buy a new one (edited)
Avatar
I am having an issue importing a local module. I am trying to do the http example from the website. https://docs.pyscript.net/latest/guides/http-requests.html this has you make a file called request.py and include it in the same directory as the html. the problem is that even though the file is there, I always get no module named request error. the code works if i include the function from the request file in the html code, but it always fails to import. any ideas?
13:32
I've also tried loading the html directly and from an http server and both have the same result (edited)
Jeff Glass started a thread. 20/11/2022 14:25
Jeff Glass started a thread. 20/11/2022 14:33
Avatar
FabioRosado 21/11/2022 19:54
I'm writing a tutorial for our docs and I found some interesting edge cases when using [[fetch]] originally I was simply fetching the https://pyscript.net/examples/todo.py and utils and trying to import it, but things failed because Element was not defined.
Avatar
Error: PyScript is not defined. Hi, I am at a bit of a loss, I am just getting started with pyscript and am going through the documentation, not sure what I have done wrong here, probably something very simple:
Avatar
Avatar
Mehall
Error: PyScript is not defined. Hi, I am at a bit of a loss, I am just getting started with pyscript and am going through the documentation, not sure what I have done wrong here, probably something very simple:
FabioRosado 21/11/2022 20:11
Hello @Mehall welcome! I think we changed the behaviour when we killed a bunch of globals but perhaps @Jeff Glass knows more since he created the example. Can you try running this? pyscript.runtime.globals.get("x") I'm using a dev version locally ๐Ÿ™‚ (edited)
Avatar
Unfortunately there is no change in behavior, still "PyScript is not defined"
Avatar
Jeff Glass 21/11/2022 20:57
@Mehall Hi Michael! Sorry for the confusion - this is unfortunately another case of our documentation getting ahead of the releases. The pyscript js module is part of an upcoming release, and isn't present in 2022.09.1 (the current /latest)
21:00
You can still use the more "direct" methods of getting access to Python's globals in JS (with eval() etc), but here's a simpler option that @hood illustrated for me: <py-script> import js js.pyscriptGlobals = globals() x = 42 </py-script> <button onclick="showX()">Click Me to Get 'x' from Python</button> <script> function showX(){ console.log(`In Python right now, x = ${pyscriptGlobals.get('x')}`) } </script>
Avatar
Thank you so much! This is the first time I have worked with a project so young in development but it is super cool, and I get things like this happen. This functionality is actually a lot more what I was after, being able to pass functions and objects between js and python is awesome!
Avatar
Avatar
Mehall
Thank you so much! This is the first time I have worked with a project so young in development but it is super cool, and I get things like this happen. This functionality is actually a lot more what I was after, being able to pass functions and objects between js and python is awesome!
to be fair the excellent Python/JS integration is entirely merit of pyodide
๐Ÿ‘ 1
Avatar
what is the practice for passing a python dictionary to javascript as an object?
23:13
when I log the object to console, it says it's a Proxy
Avatar
Avatar
tuxprint
what is the practice for passing a python dictionary to javascript as an object?
Jeff Glass 21/11/2022 23:17
You can use to_js, optionally with the dict_converter parameter, like so: import js from pyodide.ffi import to_js my_dict = { "pyscript": 1, "is": 2, "neat": 3 } js.console.log(my_dict) # Proxy js.console.log(to_js(my_dict)) #Map js.console.log(to_js(my_dict, dict_converter=js.Object.fromEntries)) # Object
โค๏ธ 1
Avatar
Avatar
Jeff Glass
You can use to_js, optionally with the dict_converter parameter, like so: import js from pyodide.ffi import to_js my_dict = { "pyscript": 1, "is": 2, "neat": 3 } js.console.log(my_dict) # Proxy js.console.log(to_js(my_dict)) #Map js.console.log(to_js(my_dict, dict_converter=js.Object.fromEntries)) # Object
awesome, thanks! I'll give it a go!
23:21
works like a charm!
๐ŸŽ‰ 1
23:26
Thank you
Avatar
@tedpatrick what about a subchannels for alternate pyscript cores ( eg maybe #pyscript-runtimes ). @ntoll and I are wishing you wonderfull hollydays ๐Ÿ˜‰
๐Ÿ‘ 1
๐Ÿšข 1
Avatar
Hey, i am about to try and controll an led on my respbeery pi and it works fine with python and now i got the code in the html so i can controll it with a website but now it says me it doesnt have the rpi libary/module which is installed on my pi and works
Avatar
Avatar
KCLucas
Hey, i am about to try and controll an led on my respbeery pi and it works fine with python and now i got the code in the html so i can controll it with a website but now it says me it doesnt have the rpi libary/module which is installed on my pi and works
Because PyScript is run in the browser, and because the browser doesn't have direct access to your GPIO pins, I'm afraid your code won't work. However, I'd add that you're using Raspberry Pi's own GPIO library. A far better offering is GPIOZero (https://gpiozero.readthedocs.io/), which has a mechanism for configuring "remote" GPIO. This could (note, I'm not sure, it's something for you to investigate) allow you to use the GPIOZero in your browser to talk to the GPIO pins on your device via some sort of local network connection.
Avatar
@KCLucas why not play around and report back what you find..?
Avatar
the thing is i am new to all the gpio stuff and just try to work around and what i was think about also to try is just run a py program for that i have a big idea but first i try some stuff like that xD my main goal is to stream the camera output to my occulus quest 2 and when i am looking around the cam also turns but also i want to try it what my first goal is stream the camera to a website where i can see it from the whole world so i can watch the cam whos pointing to my 3d printer so i can watch it from evrywhere and also a stop button on the website which turns my printer off when its like printing shit ๐Ÿฅฒ but first i reinstall my respberry pi couse i installed so much libarys that the camera stoped working ๐Ÿ˜‚ (edited)
18:40
and for all that website stuff i am new so i tried pyscript couse i know more about python than website stuff like html and php
Avatar
OK... so my advice is not to use PyScript , but the version of Python that comes with the Raspberry Pi (while remembering to use GPIO Zero rather than Rpi.GPIO). The GPIO Zero docs should help a lot.
Avatar
I dont know if you know stuff about Raspberry pi and stuff but my goal right now is to control a servo with this instead of writing it to the console. But i just can use the servo libary but yeah thats the problem i cant use this couse its not installed in pyscript but also dont know how do do it else. Do you know how i could controll this with a website from my phone or so?
849 bytes
Avatar
Avatar
KCLucas
I dont know if you know stuff about Raspberry pi and stuff but my goal right now is to control a servo with this instead of writing it to the console. But i just can use the servo libary but yeah thats the problem i cant use this couse its not installed in pyscript but also dont know how do do it else. Do you know how i could controll this with a website from my phone or so?
It's the same problem as GPIO: you cannot control the rpi hardware from pyscript. Remember: pyscript executes python in the browser: this means that if you are opening the page on your mobile, python runs on your mobile, even if the HTML page is physically stored on the rpi. So, unless your mobile phone has a servo motor, "drive the servo from pyscript' doesn't make sense ๐Ÿ™‚ NOTE: this is not completely true: I can imagine ways for pyscript and the rpi hardware to interact and eventually what you want to do might be supported. But at the moment of writing this is not there yet, sorry ๐Ÿคทโ€โ™‚๏ธ
Avatar
ok than ill try the other way python Script where I have the HTML inside Thai works for a camera livestream I got it working
Avatar
Please do let us know how you get on. The GPIO Zero core dev is a good friend of mine and he was intrigued to know about what you were up to when I mentioned it to him. ๐Ÿ‘
10:03
Put simply, it's early days for PyScript but as @antocuni says, it could work in the future, but we need to do a whole bunch of development before we get there (and a lot of things need to fall into place).
10:03
Best of luck..! ๐Ÿš€
Avatar
hey guys, I am installing a module, pymystem3. This module acts as a wrapper for the command-line program: https://yandex.ru/dev/mystem/. (Google translated version: https://yandex-ru.translate.goog/dev/mystem/?_x_tr_sl=auto&_x_tr_tl=en&_x_tr_hl=en-GB. The module downloads the binary to bin. Then the module offers an interface to the binary. When I try to do this with PyScript there is an error because the code is expecting to be ran on a generic os. Any ideas on how to get around this? (edited)
ะŸั€ะพะณั€ะฐะผะผะฐ MyStem ะฟั€ะพะธะทะฒะพะดะธั‚ ะผะพั€ั„ะพะปะพะณะธั‡ะตัะบะธะน ะฐะฝะฐะปะธะท ั‚ะตะบัั‚ะฐ ะฝะฐ ั€ัƒััะบะพะผ ัะทั‹ะบะต.
ะŸั€ะพะณั€ะฐะผะผะฐ MyStem ะฟั€ะพะธะทะฒะพะดะธั‚ ะผะพั€ั„ะพะปะพะณะธั‡ะตัะบะธะน ะฐะฝะฐะปะธะท ั‚ะตะบัั‚ะฐ ะฝะฐ ั€ัƒััะบะพะผ ัะทั‹ะบะต.
07:48
One work around I am currently toying with in my head is to move the binary to the same directory as my code and then rewriting the code of the python module to directly use that instead of trying to install it. But I am not sure if that will be visible to the python once it is running in the browser
Avatar
Avatar
Mehall
hey guys, I am installing a module, pymystem3. This module acts as a wrapper for the command-line program: https://yandex.ru/dev/mystem/. (Google translated version: https://yandex-ru.translate.goog/dev/mystem/?_x_tr_sl=auto&_x_tr_tl=en&_x_tr_hl=en-GB. The module downloads the binary to bin. Then the module offers an interface to the binary. When I try to do this with PyScript there is an error because the code is expecting to be ran on a generic os. Any ideas on how to get around this? (edited)
I didn't look into it, but if it relies on a binary executable it won't work out of the box in PyScript: binary files are CPU-specific and OS-specific: to be able to use it on PyScript/Pyodide, at first you would need to compile it for WASM. But even then, you cannot easily run external binaries on WASM because there is no OS to take care of that. TL;DR: it might be possible but it require the authors of pymystem3 to do some work andd build a pyodide-specific version of the library
08:13
(assuming that the source code of this mystem program is accessible; if it's binary only the only solution is to ask the authors of mystem to provide a WASM version)
Avatar
damn thats annoying, might have to just resort to doing this project using an old fashioned django server.
Avatar
Avatar
Mehall
damn thats annoying, might have to just resort to doing this project using an old fashioned django server.
Paul Everitt 24/11/2022 12:43
That is likely the best route. Use Python-compatible "function as a service" such as Google Cloud Run as an API endpoint. Have the front-end just act as something that instruments it.
Avatar
Avatar
Paul Everitt
That is likely the best route. Use Python-compatible "function as a service" such as Google Cloud Run as an API endpoint. Have the front-end just act as something that instruments it.
Thank you!
Avatar
Paul Everitt 24/11/2022 14:03
@Mehall Now if you want to be really crazy: https://github.com/copy/v86
x86 virtualization in your browser, recompiling x86 to wasm on the fly - GitHub - copy/v86: x86 virtualization in your browser, recompiling x86 to wasm on the fly
Avatar
Avatar
Paul Everitt
@Mehall Now if you want to be really crazy: https://github.com/copy/v86
That is ridiculously awesome, but this side project is already becoming a bit of a rabbit hole, and if I spend any longer on this repository I am not going to see the outside for a few weeks.
Avatar
Avatar
Paul Everitt
@Mehall Now if you want to be really crazy: https://github.com/copy/v86
That's insane.
Avatar
anyone managed to load .parquet files in pyodide? should I try it with pandas.read_parquet ?
Avatar
Avatar
Taras
anyone managed to load .parquet files in pyodide? should I try it with pandas.read_parquet ?
I guess I can start with this example and see where it takes me ๐Ÿ™‚ https://pyscript.net/examples/altair.html
Avatar
FabioRosado 25/11/2022 14:55
I haven't tried with .parqet files, but for csv something like this works: import pandas from pyodide.http import open_url data = pd.read_csv(open_url(url)) I wonder if opening a parquet file will complain
Avatar
Avatar
FabioRosado
I haven't tried with .parqet files, but for csv something like this works: import pandas from pyodide.http import open_url data = pd.read_csv(open_url(url)) I wonder if opening a parquet file will complain
ha, yeah, csv is easy. parquet not so much. I did try it and it requires either pyarrow or fast parquet dependency to read those data files. Otherwise, we get this ๐Ÿ˜ฆ
15:35
my page code: <html> <head> <title>Chicago Crimes PyScript App</title> <meta charset="utf-8"> <link rel="icon" type="image/x-icon" href="./favicon.png"> <link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" /> <script defer src="https://pyscript.net/latest/pyscript.js"></script> <py-config> packages = [ "pandas" ] </py-config> </head> <body> <div id="info"></div> <py-script output="info"> import pandas as pd from pyodide.http import open_url from datetime import datetime from js import console from js import document info_element = document.getElementById('info') info_element.innerText = 'Loading data ...' start_time = datetime.now() # read 2022 crimes parquet data data_url = ( 'https://raw.githubusercontent.com/RandomFractals/chicago-crimes/main/data' ) crimes_data_url = f'{data_url}/crimes-2022.parquet' crimes = pd.read_parquet(open_url(crimes_data_url)) # calculate and display data load time end_time = datatime.now() load_time = (end_time - start_time).total_seconds() status = f'load time: {load_time} seconds' console.log(status) info_element.innerText = status </py-script> </body> </html>
15:36
I know that pyarrow doesn't work in pyodide. will see if fastparquet does
15:37
btw, this ticket is still open: https://github.com/pyodide/pyodide/issues/2933
๐Ÿ Package Request Package Name and Version: pyarrow / latest Package URL: https://github.com/apache/arrow Package Dependencies that needs to be resolved first: ?
15:39
if that doesn't work, I can load it via JS lib, but then I do want to process data with Py for the viz parts. So, I guess the q. would be how time consuming is JS to Py objects passing and serialize/deserialize in that process.
Avatar
FabioRosado 25/11/2022 15:40
Oh yeah sorry forgot to mention that you might need one of those ๐Ÿ˜„ Iโ€™ve seen a bunch of those errors when dask stopping shopping with it
Avatar
is dask even pyodide compatible?
Avatar
FabioRosado 25/11/2022 15:41
Now im curious to see if it works Iโ€™m not home at the moment but Iโ€™m happy to play around if it helps
15:41
Never tried but Iโ€™m not sure it will work
Avatar
I can't go with CSV b/c even for not full 2022 Chicago crimes CSV is 46MB ๐Ÿ™‚ vs parquet which is about 14Mb and should be passible to demo
Avatar
Avatar
FabioRosado
Now im curious to see if it works Iโ€™m not home at the moment but Iโ€™m happy to play around if it helps
nah, I'll play with it this weekend and keep you posted here. Don't spend your time on it. I am surprised I don't see anyone trying it with parquet or arrow data yet ๐Ÿ˜ฆ
Avatar
nope. fastparquet doesn't work with pyodide either. so, basically there is no way to read parquet or arrow data files or load them in duckdb with PyScript or in pyodide atm. Kinda disappointing b/c I don't see myself achieving much with large CSV data files for the app demos I wanted to build with Py in a browser.
Avatar
takacsjanos 25/11/2022 18:41
hi If I only want to include a python lib, and make it available to javascript, what is the preferred method to signal that all python components are loaded? Currently I have this at the end of my python init script: from js import window window.setTimeout(window.main, 0) pass (and on the js side, I define the window.main before the python script.) Is this the way to go, or is there some other mechanism to have a callback run after initializing the python library?
Avatar
@takacsjanos it's probably simpler to use Pyodide directly for this: async loadPythonLib(){ const py = await loadPyodide(); await py.loadPackage("micropip"); const micropip = py.pyimport("micropip"); await micropip.install("some_lib"); return pyodide.pyimport("some_lib"); } const pythonLibPromise = loadPythonLib(); and then to get the lib you can await pythonLibPromise.
Avatar
takacsjanos 25/11/2022 18:49
all this micropip boilerplate is handled nicely by <py-config>
18:50
I was wondering if there is an option, or a planned option, for providing the js callback for the 'load finished' event
Avatar
Avatar
takacsjanos
I was wondering if there is an option, or a planned option, for providing the js callback for the 'load finished' event
Jeff Glass 25/11/2022 18:54
Thereโ€™s one planned, but not yet implemented. Also potentially having PyScript dispatch events to document at various points in the lifecycle
18:57
You way of implementing the singalling when Pyscript is loaded is as good as any; personally, Iโ€™be been using <py-script> import js js.document.dispatchEvent(js.Event.new(โ€œpyscript-loadedโ€) </py-script> <script> document.addEventListener(โ€œpyscript-loadedโ€, () => {main()}) </script>
Avatar
Another option would be: globalThis.pythonLibPromise = new Promise(resolve => globalThis.resolvePythonLibPromise = resolve); and then in Python: from js import resolvePythonLibPromise resolvePythonLibPromise(thelib)
18:58
if you want a promise you can await for the lib
Avatar
takacsjanos 25/11/2022 19:02
thanks lifecycle events will be helpful. I guess I only need to wait for that feature as fast as I can. ๐Ÿ™‚
Avatar
Avatar
takacsjanos
thanks lifecycle events will be helpful. I guess I only need to wait for that feature as fast as I can. ๐Ÿ™‚
Jeff Glass 25/11/2022 19:37
Should be implementable for some lifecycle events via a plugin in the next release, I think. Depends on exactly how the semantics of Python plug-ins shake out
Avatar
FabioRosado 25/11/2022 19:54
I think Antonio was also working on some lifecycle changes (unless it was implemented in the python plugins PR :D)
Avatar
how to pass a python variable to javascript variable? Anyone can help me?
00:25
How can i grab the message variable to js variable <py-repl> message= "Hi John" </py-repl>
Avatar
Avatar
Enes
how to pass a python variable to javascript variable? Anyone can help me?
Jeff Glass 26/11/2022 00:36
Thereโ€™s a handful of ways, but one of the cleanest I think is: x = 42 import js js.my_python_variable = x (edited)
00:37
The โ€œjsโ€ module is a (quite clever) mapping of the JavaScript global namespace into python done by Pyodide (edited)
Avatar
Avatar
Taras
nope. fastparquet doesn't work with pyodide either. so, basically there is no way to read parquet or arrow data files or load them in duckdb with PyScript or in pyodide atm. Kinda disappointing b/c I don't see myself achieving much with large CSV data files for the app demos I wanted to build with Py in a browser.
so, given that. I'll be trimming columns in my CSV data file to put out some pyscript demo, aka data slimming in the olden way
Avatar
ok. so, I slimmed down my data. trying to read CSV. Data file has timestamps, getting this error now:
13:44
is datetime not in pyscript/pyodide?
Avatar
Avatar
Taras
is datetime not in pyscript/pyodide?
Jeff Glass 26/11/2022 13:54
It definitely is - the error references โ€œdatatimeโ€. Typo? or perhaps variable name? (edited)
Avatar
Avatar
Jeff Glass
It definitely is - the error references โ€œdatatimeโ€. Typo? or perhaps variable name? (edited)
nope, I think it's pyodide or pyscript failing to load datetime in pandas for parsing date fileds. My simple data loading script is: # read 2022 crimes parquet data data_url = ( 'https://raw.githubusercontent.com/RandomFractals/chicago-crimes/main/data' ) crimes_data_url = f'{data_url}/crimes-2022-slim.csv' crimes = pd.read_csv(open_url(crimes_data_url), parse_dates=['Date'], cache_dates=True, low_memory=False) (edited)
14:13
there is no datatime field in my csv data file. Oddly enough this works in .ipynb ๐Ÿ™‚
Avatar
Jeff Glass 26/11/2022 15:03
huh, that code sample loads without error for me (in pyscript unstable), and adding display(crimes) to the end gives: (edited)
15:03
15:04
What's on line 18 of your full code, assuming the original error message is accurate?
Avatar
Jeff Glass 26/11/2022 15:13
(Which references "datatime" not "datetime")
Avatar
Hey, Is there a way to capture print output in a pyrepl. So say a user types print('hello') in the pyrepl, how would my code store the 'hello' output in a variable?
Avatar
Avatar
Jeff Glass
(Which references "datatime" not "datetime")
oh, there was a typo. my bad. thanks for trying it tho.
๐Ÿ‘ 1
Avatar
Avatar
shahmaty
Hey, Is there a way to capture print output in a pyrepl. So say a user types print('hello') in the pyrepl, how would my code store the 'hello' output in a variable?
Jeff Glass 26/11/2022 20:14
Not in what I would call a clean way at this point noโ€ฆ the internal plug-in architecture in the upcoming release allows for hooking into stdout, but Iโ€™m not sure if thatโ€™s available to extend for non-core plug-ins
20:15
Out of curiosity, what are you looking to do with that value/variable?
Avatar
I want to compare that value and if its equal then something happens to the webpage
Avatar
How can i grab the output of py repl
20:22
how can i grab the output of this with javascript?
20:22
anyone knows?
20:23
im stuck here for almost 2 whole days!
Avatar
Avatar
Taras
there is no datatime field in my csv data file. Oddly enough this works in .ipynb ๐Ÿ™‚
btw @Jeff Glass I did try loading *.csv.zip and that def. doesn't work in pyscript. will just stick with plain CSV and gen some matplotlib charts tomorrow (edited)
Avatar
Avatar
Taras
btw @Jeff Glass I did try loading *.csv.zip and that def. doesn't work in pyscript. will just stick with plain CSV and gen some matplotlib charts tomorrow (edited)
will do it in this repo ticket and then probably publish it via github pages. just sharing this for those who want to follow that one page app dev ... https://github.com/RandomFractals/chicago-crimes/issues/30
Avatar
oh, I could def. do custom css, but firgured I'd ask: is there a default dataframe.css somewhere in pyscript or pandas lib we can use to style tabular data results? This is too plain:
Avatar
Avatar
Taras
oh, I could def. do custom css, but firgured I'd ask: is there a default dataframe.css somewhere in pyscript or pandas lib we can use to style tabular data results? This is too plain:
probably would be good to add some basic html table styles to your main https://pyscript.net/latest/pyscript.css
21:34
oh, and yeah, I could use pandas df.style, but that reqires jinja2 and shows this error. once again works just fine in Jupyter ipynb ๐Ÿคทโ€โ™‚๏ธ
21:35
don't mean to annoy you guys. Just wanted to share my pyscript journey here. overall, I find it very beta given that most pacakges barely work and commonly fail on dependencies
Avatar
Jeff Glass 26/11/2022 22:45
@Enes @shahmaty I've opened issue #988 over on GitHub, a feature enhancement for the kinds of infrastructure we'd need to implement to make your ideas possible https://github.com/pyscript/pyscript/issues/988. They're not far off from what's currently extant in the dev builds, but not there yet.
Avatar
Avatar
Taras
btw @Jeff Glass I did try loading *.csv.zip and that def. doesn't work in pyscript. will just stick with plain CSV and gen some matplotlib charts tomorrow (edited)
Jeff Glass 26/11/2022 22:46
Can you share a code sample for this example? I'd love to dig in and understand a little more why it failed
Avatar
Avatar
Enes
how can i grab the output of this with javascript?
FabioRosado 26/11/2022 22:51
You could try grabbing the contents of the output div with Element(โ€˜div idโ€™).inner_html you need to look at the dev tools for the div id, I donโ€™t remember out of the top of my head
Avatar
Avatar
Taras
btw @Jeff Glass I did try loading *.csv.zip and that def. doesn't work in pyscript. will just stick with plain CSV and gen some matplotlib charts tomorrow (edited)
FabioRosado 26/11/2022 22:53
I donโ€™t think it works because you canโ€™t open the zip file out of the box although there is a trick in JavaScript to do it, not sure how well it would work let me look
22:55
This is what I was looking at the other day https://stackoverflow.com/a/34439627
I'm working on hybrid mobile app using html5/js. It has a function download zip file then unzip them. The download function is not problem but I don't know how to unzip file (using javascript). Many
Avatar
Jeff Glass 26/11/2022 23:02
I think I'm missing something: this works fine for me (in unstable) <py-config> packages = ['pandas'] [[fetch]] files = ['dogs.csv.zip'] </py-config> <py-repl> import pandas as pd crimes_df = pd.read_csv('dogs.csv.zip') display(crimes_df) </py-repl> (edited)
23:02
23:03
Where dogs.csv.zip is a zipped csv file with some data on good dogs
Avatar
Avatar
Jeff Glass
Where dogs.csv.zip is a zipped csv file with some data on good dogs
FabioRosado 26/11/2022 23:04
Thereโ€™s some data missing, all dogs are good dogs ๐Ÿ˜›
โค๏ธ 1
Avatar
Jeff Glass 26/11/2022 23:05
(Using paths= and print() in 2022.09.1, the data still loads into the dataframe but the output isn't as nice, just ends up as a string)
Avatar
FabioRosado 26/11/2022 23:06
Oh I forgot that pandas could read from zipped files ๐Ÿ˜ฌ
Avatar
Jeff Glass 26/11/2022 23:08
Thanks for the link about unzipping on the JS side though - every time I think "well surely you can't do that in pure JS" I'm usually wrong ๐Ÿ˜…
Avatar
FabioRosado 26/11/2022 23:29
Haha yeah I was wondering if we could maybe unzip/untar pyodide and thought the same then I found that answer ๐Ÿคฏ
Avatar
takacsjanos 27/11/2022 00:54
is there an npm package for pyscript? so that I don't have to download everything from pyscript.net every time
Avatar
Avatar
Jeff Glass
Can you share a code sample for this example? I'd love to dig in and understand a little more why it failed
that pyscript is in my public repo. see data folder there if you want to try changing it to output df style, or load zip csv data file, and Jupyter notebooks in the /notebooks folder ... https://github.com/RandomFractals/chicago-crimes/blob/main/apps/pyscript/index.html
Avatar
Avatar
Jeff Glass
I think I'm missing something: this works fine for me (in unstable) <py-config> packages = ['pandas'] [[fetch]] files = ['dogs.csv.zip'] </py-config> <py-repl> import pandas as pd crimes_df = pd.read_csv('dogs.csv.zip') display(crimes_df) </py-repl> (edited)
yeah, idk. maybe it's url related. In my test page I am loading from github content, and in ipynb I just load it from the relative data folder path. That works just fine in a Jupyter notebook:
Avatar
Avatar
Jeff Glass
I think I'm missing something: this works fine for me (in unstable) <py-config> packages = ['pandas'] [[fetch]] files = ['dogs.csv.zip'] </py-config> <py-repl> import pandas as pd crimes_df = pd.read_csv('dogs.csv.zip') display(crimes_df) </py-repl> (edited)
you are using some [[fetch]] shorthand in your example. will see if I can find the docs on that
12:14
oh, I am guessing my data load of zip did not work b/c pyodide.open_url returns io.StringIO and pandas probably expects binary stream to load zipped data.
Avatar
well, using pyodide's pyfetch might be a better way of loading zipped data. from faq: https://pyodide.org/en/stable/usage/faq.html#how-can-i-load-external-files-in-pyodide
Avatar
Avatar
Taras
you are using some [[fetch]] shorthand in your example. will see if I can find the docs on that
Jeff Glass 27/11/2022 15:42
Apologies here - at one point I thought I had seen you use display(), which made me think you were working with the current build/unstable version. But I see youโ€™re on /latest. Slightly different syntax for loading files in 2022.09.1 (latest), but same idea - moving files from the web into the emscriten local file system where Pandas etc can interact with them
Avatar
Jeff Glass 27/11/2022 15:56
Both the paths= and display() syntax are ways of fetching files from the network and storing them in the emscripten local file system where they can be interacted with
15:56
They use (or used) pyfetch under the hood (edited)
15:58
The syntax for paths is (in 2022.09.1): <py-config> paths=[โ€˜some/abs/or/relative/url/to/file.txtโ€™] </py-config> At load, this creates a file called file.txt in the same local folder where scripts are run, so you could do with open(โ€˜file.txtโ€™, โ€˜rโ€™) as fp()โ€ฆ (edited)
15:59
Or in your case, open_csv
16:00
In the upcoming release, this is being replaced with a slightly more verbose syntax that allows loading multiple files in a way that preserves their directory structure
Avatar
that's a lot of good info. so, does that fetch prefetch file content and then stores it in some global fs page context for processing with pyscript? How does that work with remote data sources, i.e. public files hosted via https://
12:03
I am going to leave that part as is for now. Will add some Altair charts to publish that page first.
Avatar
btw, I have been thinking of how PyScript would be used in this scenario: 1. data loaded in pyscript. 2. JS charting lib loaded in the page. 3. Data is processed in PyScript and passed on to JS for visualizations. I believe that would be the more common data flow I'd use. Is this the current recommended approach to achieve that with PyScript, via object proxies: https://docs.pyscript.net/latest/guides/passing-objects.html#exporting-individual-python-objects
Avatar
Avatar
Taras
btw, I have been thinking of how PyScript would be used in this scenario: 1. data loaded in pyscript. 2. JS charting lib loaded in the page. 3. Data is processed in PyScript and passed on to JS for visualizations. I believe that would be the more common data flow I'd use. Is this the current recommended approach to achieve that with PyScript, via object proxies: https://docs.pyscript.net/latest/guides/passing-objects.html#exporting-individual-python-objects
Jeff Glass 28/11/2022 13:24
I'm not sure things have settled down enough to have any one recommended approach, but I think the thing to do, where possible, is to call the JS visualization library from within PyScript
13:25
The Vizzu example from the other channel, for example, does import Vizzu inside Pyscript, chart = Vizzu.default.new("myVizzu") to create a new chart, and then uses chart's methods/attributes to control the chart within PyScript
Avatar
Avatar
Jeff Glass
The Vizzu example from the other channel, for example, does import Vizzu inside Pyscript, chart = Vizzu.default.new("myVizzu") to create a new chart, and then uses chart's methods/attributes to control the chart within PyScript
yeah, I guess it makes sense to save on data serialization time. Altho, not all charting libs are like Vizzu and some might not have Py lib or work with pyodide
Avatar
Jeff Glass 28/11/2022 13:30
If the library doesn't have Python integrations, you can actually do import lib_name from js to grab proxies for the JS library objects itself
๐Ÿค” 1
13:31
Let me try to find a good example...
Avatar
oh, interesting. yeah, I saw that part in either your or pyodide docs
Avatar
Jeff Glass 28/11/2022 13:32
It's super nifty and a feature of Pyodide - import js or from js import treats the JS global namespace as a module to be imported from. As in from js import console or import js; js.document.getElementById(...). It's really nifty
๐Ÿ‘ 1
13:35
I didn't know until @hood told me: you can also assign to the js namespace to create objects in the js global namespace: <py-script> import js x = 42 js.x_in_javascript = x js.console.log(js.window) </py-script>
Avatar
yep. that is really cool. thx for the tip, once again ๐Ÿ™‚
๐Ÿ‘ 1
Avatar
why is my code2 var empty?
Avatar
์˜์›ํ•œ์ฐฐ๋‚˜ 01/12/2022 07:02
hello! Does anyone know how to set the loading event listener of the pyscript library in JavaScript here?
07:04
Avatar
Avatar
์˜์›ํ•œ์ฐฐ๋‚˜
hello! Does anyone know how to set the loading event listener of the pyscript library in JavaScript here?
FabioRosado 01/12/2022 09:06
We currently set an event listener for the messages that appear, it's called py-status-message can you use that?
Avatar
Avatar
FabioRosado
We currently set an event listener for the messages that appear, it's called py-status-message can you use that?
์˜์›ํ•œ์ฐฐ๋‚˜ 01/12/2022 09:09
wow~! i don't know about "py-status-message". where is py-status-message api info? thank you!
Avatar
Avatar
Enes
why is my code2 var empty?
FabioRosado 01/12/2022 09:09
Itโ€™s hard to say without seeing the full code care to share it?
Avatar
Avatar
์˜์›ํ•œ์ฐฐ๋‚˜
wow~! i don't know about "py-status-message". where is py-status-message api info? thank you!
FabioRosado 01/12/2022 09:10
Thatโ€™s an internal one that we use to push messages to the splash screen but perhaps itโ€™s useful to document this ๐Ÿ˜„
โ˜๏ธ 2
๐Ÿ‘ 1
Avatar
Avatar
FabioRosado
Thatโ€™s an internal one that we use to push messages to the splash screen but perhaps itโ€™s useful to document this ๐Ÿ˜„
์˜์›ํ•œ์ฐฐ๋‚˜ 01/12/2022 09:11
I just want to show the Timer to the user after the pyscript is all ready. So, we want to register an event listener for loading completion.
Avatar
Avatar
FabioRosado
Thatโ€™s an internal one that we use to push messages to the splash screen but perhaps itโ€™s useful to document this ๐Ÿ˜„
์˜์›ํ•œ์ฐฐ๋‚˜ 01/12/2022 09:12
First of all, thanks for the info. As for py-status-message, I'll try it myself.
Avatar
FabioRosado 01/12/2022 09:19
Ah gotcha there may be a different way to do it, when the splash screen is up we create a div with the id โ€˜pyscript_loading_splashโ€™ then once it finishes loading we remove the element
Avatar
Avatar
FabioRosado
Ah gotcha there may be a different way to do it, when the splash screen is up we create a div with the id โ€˜pyscript_loading_splashโ€™ then once it finishes loading we remove the element
์˜์›ํ•œ์ฐฐ๋‚˜ 01/12/2022 09:57
wow good idea! i will try it.
Avatar
Avatar
FabioRosado
Thatโ€™s an internal one that we use to push messages to the splash screen but perhaps itโ€™s useful to document this ๐Ÿ˜„
yeah, I'd like to learn more about it, and see if there is an api we can add more handlers and messages to that page loader, like data loading status in the page I shared yesterday ๐Ÿค”
Avatar
Avatar
Taras
yeah, I'd like to learn more about it, and see if there is an api we can add more handlers and messages to that page loader, like data loading status in the page I shared yesterday ๐Ÿค”
FabioRosado 01/12/2022 14:47
I havenโ€™t tried this but you should be able to grab the splash element and do .log to generate the log messages. This is the relevant portion of the code https://github.com/pyscript/pyscript/blob/main/pyscriptjs/src/plugins/splashscreen.ts#L46-L48 I think we should def write something about this feature haha
Home Page: https://pyscript.net Examples: https://pyscript.net/examples - pyscript/splashscreen.ts at main ยท pyscript/pyscript
๐Ÿ‘ 1
Avatar
tedpatrick 01/12/2022 18:51
Publishing my PyScript Advent of Code here: https://magenta-crisp-99d98c.netlify.app/
Avatar
Avatar
tedpatrick
Publishing my PyScript Advent of Code here: https://magenta-crisp-99d98c.netlify.app/
Jeff Glass 01/12/2022 19:36
Solutions to the annual holiday coding challenge, in Python and PyScript
โค๏ธ 1
Avatar
tedpatrick 01/12/2022 19:38
Ah the way you handled input, love it.
Avatar
Avatar
FabioRosado
Itโ€™s hard to say without seeing the full code care to share it?
Someone else already helped me bro, but thank you for your will to help
Avatar
tedpatrick 02/12/2022 12:40
Day 2: Pyscript Advent of code https://magenta-crisp-99d98c.netlify.app/
Avatar
benjiallen 03/12/2022 15:54
is there any good guidance, articles or the like, for writing files to the virtual file system and then persisting that data between page loads? I'm aware of https://www.jhanley.com/blog/pyscript-files-and-file-systems-part-1/ and while that article is super useful, i'm looking for examples of persisting data.
Avatar
Avatar
benjiallen
is there any good guidance, articles or the like, for writing files to the virtual file system and then persisting that data between page loads? I'm aware of https://www.jhanley.com/blog/pyscript-files-and-file-systems-part-1/ and while that article is super useful, i'm looking for examples of persisting data.
FabioRosado 03/12/2022 17:31
What kind of data do you want to persist between pages? Perhaps session storage or local storage could be used if you can serialise them to strings
Avatar
You can use the IDBFS to mount folders in the file system that will persist. The code for this looks something like: from asyncio import Future from pyodide.ffi import create_proxy from pyodide_js import FS import os def mount_ibdfs(mountdir): os.mkdirs(mountdir) FS.mount(FS.filesystems.IDBFS, {}, mountdir) async def initialize_idbfs(): """This populates the mounted folder with the persisted files. Wait for it to finish before trying to access the folder! """ fut = Future() def set_result(err): if err is None: # If err is None, fs was successfully initialized. # We should probably handle the other case?? fut.set_result(None) FS.syncfs(True, create_proxy(set_result)) await fut async def persist_idbfs(): """This persists changes to the idbfs folder. Wait for it to complete before exiting page or changes will be lost! """ fut = Future() def set_result(err): if err is None: # If err is None, fs was successfully persisted. # We should probably handle the other case?? fut.set_result(None) FS.syncfs(True, create_proxy(set_result)) await fut mount_ibdfs("/some/directory") await initialize_idbfs() p = Path("/some/directory/afile.txt") if p.exists(): print(f"{p} exists, contains: {p.read_text()}") else: print(f"{p} does not exist") import random p.write_text(f"hello! {random.randint(0,100)}") await persist_idbfs()
18:44
code not tested
Avatar
Does anyone have an example of posting form data using pyfetch?
Avatar
also generating random bytes?
Avatar
ah, secrets module
10:12
got it
Avatar
Avatar
hood
You can use the IDBFS to mount folders in the file system that will persist. The code for this looks something like: from asyncio import Future from pyodide.ffi import create_proxy from pyodide_js import FS import os def mount_ibdfs(mountdir): os.mkdirs(mountdir) FS.mount(FS.filesystems.IDBFS, {}, mountdir) async def initialize_idbfs(): """This populates the mounted folder with the persisted files. Wait for it to finish before trying to access the folder! """ fut = Future() def set_result(err): if err is None: # If err is None, fs was successfully initialized. # We should probably handle the other case?? fut.set_result(None) FS.syncfs(True, create_proxy(set_result)) await fut async def persist_idbfs(): """This persists changes to the idbfs folder. Wait for it to complete before exiting page or changes will be lost! """ fut = Future() def set_result(err): if err is None: # If err is None, fs was successfully persisted. # We should probably handle the other case?? fut.set_result(None) FS.syncfs(True, create_proxy(set_result)) await fut mount_ibdfs("/some/directory") await initialize_idbfs() p = Path("/some/directory/afile.txt") if p.exists(): print(f"{p} exists, contains: {p.read_text()}") else: print(f"{p} does not exist") import random p.write_text(f"hello! {random.randint(0,100)}") await persist_idbfs()
initialize_idbfs and persist_idbfs seems to be exactly the same code: is it intended or a typo?
10:05
I'm trying to run a scrapping script through PyScript, problem is that I get this error in console
10:05
SSL is imported correctly as well as all the other modules
Avatar
FabioRosado 05/12/2022 10:06
Hello ๐Ÿ‘‹ how are you passing your urls? Are you including https?
10:07
I'm passing the url as string throught a PyInput (edited)
10:07
Retrouvez les derniรจres valeurs et cotations disponibles de Rouble russe (RUB) - quotidien sur l'Usine Nouvelle.
10:08
The script treats it this way :
Avatar
I'm having trouble calling a python function from javascript. it gives me an error for PyScript not defined. pything = PyScript.globals.runtime.get('thing'); pything(); changing PyScript to pyscript or pyodide does not work either. some error about not defined
Avatar
Avatar
tuxprint
I'm having trouble calling a python function from javascript. it gives me an error for PyScript not defined. pything = PyScript.globals.runtime.get('thing'); pything(); changing PyScript to pyscript or pyodide does not work either. some error about not defined
FabioRosado 05/12/2022 14:22
we have a slight issue where we used to deploy docs to /latest so some of the things you see in the docs aren't released yet. Once we do a release this will be fixed. You can point your code to https://pyscript.net/unstable/pyscript.js you should be able to get it by doing pyscript.globals.runtime.get('thing') (note the lowercase it's important)
Avatar
Avatar
FabioRosado
we have a slight issue where we used to deploy docs to /latest so some of the things you see in the docs aren't released yet. Once we do a release this will be fixed. You can point your code to https://pyscript.net/unstable/pyscript.js you should be able to get it by doing pyscript.globals.runtime.get('thing') (note the lowercase it's important)
changing to unstable gives me the same error for pyscript not found
14:35
Uncaught TypeError: pyscript.globals is undefined
Avatar
FabioRosado 05/12/2022 14:38
apologies I messed up the order of things. It should be pyscript.runtime.globals.get instead
Avatar
Avatar
FabioRosado
apologies I messed up the order of things. It should be pyscript.runtime.globals.get instead
we definitely need a better way to expose python functions to JS
Avatar
Avatar
FabioRosado
apologies I messed up the order of things. It should be pyscript.runtime.globals.get instead
that got it to work! thanks
Avatar
Avatar
antocuni
we definitely need a better way to expose python functions to JS
FabioRosado 05/12/2022 14:39
Yeah, I always have to check which order we are doing it
Avatar
how stable is unstable.
14:40
I would rather link to something that does not change
Avatar
the current unstable is very close to be the upcoming 2022.12.1 release
14:40
unstable changes every time we merge a PR
Avatar
ok, cool. so I can link to unstable for now then and switch to new release when it is ready
Avatar
but we plan to release soon, so in a few days (hopefully) what is in unstable will be frozen into a release.
Avatar
Avatar
tuxprint
ok, cool. so I can link to unstable for now then and switch to new release when it is ready
yes! For comparison, we also have /latest which points to the latest release. So now /latest is a synonim for 2022.09.1, but soon will point to 2022.12.1
๐Ÿ”ฅ 1
Avatar
so this does not work in the min version of unstable btw
Avatar
Hi, i want to experiment with scraping, I have used beautifulSoup and Scrapy, I have not got BS4 to fetch download from an external URL yet, (i got it working locally). So i'm confused about how to impliment the pyfetch thing, I tried to follow some forum advice and created my own request.py file - but i just got errors .. I tried to load a .whl file ๐Ÿ™‚ but, it didn't work for me either ๐Ÿ™‚ I know PyScript is in alpha, but i'm sure this is user error :).. I'm wondering if someone can help me out with getting a .whl loaded correctly. I've found an interesting package I'd like to try, called newspaper3k .. it follows the compatibility rules... https://pypi.org/project/newspaper3k/#files "py3-none-any.whl" I'm running out of vscode with live server, I'm linking to the /latest/ css + js <link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" /> <script defer src="https://pyscript.net/latest/pyscript.js"></script> </head> <body> <py-config> packages = [ How do i import the whl ? ] </py-config> <py-script> (edited)
Simplified python article discovery & extraction.
Avatar
Avatar
LeeSlacks
Hi, i want to experiment with scraping, I have used beautifulSoup and Scrapy, I have not got BS4 to fetch download from an external URL yet, (i got it working locally). So i'm confused about how to impliment the pyfetch thing, I tried to follow some forum advice and created my own request.py file - but i just got errors .. I tried to load a .whl file ๐Ÿ™‚ but, it didn't work for me either ๐Ÿ™‚ I know PyScript is in alpha, but i'm sure this is user error :).. I'm wondering if someone can help me out with getting a .whl loaded correctly. I've found an interesting package I'd like to try, called newspaper3k .. it follows the compatibility rules... https://pypi.org/project/newspaper3k/#files "py3-none-any.whl" I'm running out of vscode with live server, I'm linking to the /latest/ css + js <link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" /> <script defer src="https://pyscript.net/latest/pyscript.js"></script> </head> <body> <py-config> packages = [ How do i import the whl ? ] </py-config> <py-script> (edited)
FabioRosado 06/12/2022 14:38
You should be able to use packages = ['newspaper3k'] and it will be downloaded if it contains a pure python 3 wheel which from your message seems like it does ๐Ÿ˜„
14:39
Also welcome!
Avatar
ok, that's promising, thank you!
14:39
so anything in pypi .. that has a pure python whl, should work ?
Avatar
FabioRosado 06/12/2022 14:41
That's correct, pyodide contains some ported libraries as well. Micropip will look into pyodide ported libs first before looking into pypi
14:41
At the moment you need to open dev tools to look at the console to see if there are are errors
14:41
when we do a release, the error will be rendered in the page ๐Ÿ™‚
Avatar
yes.. i'm looking at all those errors ๐Ÿ™‚ haha
Avatar
FabioRosado 06/12/2022 14:41
awesome
Avatar
so since i have managed to get beautifulSoup working locally, my problem is - how do i get the data from the web ..
14:42
i am able to use beautifulSoup on local files (i've tested other html files in the same local directory)
Avatar
Avatar
FabioRosado
That's correct, pyodide contains some ported libraries as well. Micropip will look into pyodide ported libs first before looking into pypi
super cool ๐Ÿ™‚
Avatar
FabioRosado 06/12/2022 14:46
It's been a while since I used beautifulsoup, but you can use pyodide's pyfetch to fetch urls. Here's a link to the unstable version (it contains the most up to do date code, we will do a release very soon and these will be 'latest' soon) ๐Ÿ˜„ https://docs.pyscript.net/unstable/guides/http-requests.html#pyodide-http-pyfetch-and-fetchresponse
Avatar
thanks i'll take a look.
14:54
should the green lines - be updated to unstable ?
Avatar
I like the new red text error message! I get that error from /unstable/ - it seems like the example code needs to be updated. I'm not savvy enough to tackle it myself. better I just wait I think haha.
Avatar
Avatar
LeeSlacks
I like the new red text error message! I get that error from /unstable/ - it seems like the example code needs to be updated. I'm not savvy enough to tackle it myself. better I just wait I think haha.
Jeff Glass 06/12/2022 15:11
Hi Lee! Which example code are you looking at on that page specifically? I know @FabioRosado just pushed an update to the docs fixing a lot of the example code on that page about 40 minutes ago, but possibly we missed something
Avatar
FabioRosado 06/12/2022 15:13
The unstable version contains the last push to our main branch. As we are about to do a release, the top level await will be unavailable, that's why I gave you the url with unstable so once we release, you wont see the error message ๐Ÿ™‚
๐Ÿ‘ 1
15:13
as forthe url in the script, you should use latest for now
๐Ÿ‘ 1
Avatar
Avatar
Jeff Glass
Hi Lee! Which example code are you looking at on that page specifically? I know @FabioRosado just pushed an update to the docs fixing a lot of the example code on that page about 40 minutes ago, but possibly we missed something
it's just the pyfetch example code that Fabio shared https://docs.pyscript.net/unstable/guides/http-requests.html
Avatar
I get this with /latest/ - using the example code from that page.
22:50
and this is the request.py file (from the example code page - i removed the comments)
Avatar
Hello everyone! We are developing a web project in which we need to get the bluetooth data of the surrounding devices (ibeacons) with Python. Actually, we tried to do this on the client side with the google web bluetooth api, but we couldn't because the modules were not working. We want to receive the data we can get with the Python Bleak library on the client side and save it in the session. We think we can do this with the best pyscript. However, our problem is: "The Bleak Library does not work with the pyscript library". How can I solve this problem? Could you please add the Bleak library among the libraries that can work integrated with pyscript? Or does anyone have a different suggestion?
Avatar
add the Bleak library among the libraries that can work integrated with pyscript?
Looks to me like this would require writing an Emscripten backend for bleak. Probably would require a major effort.
Avatar
hi, I'm sorry i don't know how to paste code properly. But i wanted to let you know @FabioRosado, that i was able to get this example code working if I made these changes (also see screenshot): I removed [[fetch]] and swapped 'files' for 'paths' .. in the html section of the example on this page. - the request.py file was then imported and ran correctly. https://docs.pyscript.net/latest/guides/http-requests.html
Avatar
tedpatrick 08/12/2022 01:07
@LeeSlacks I am fairly certain that [[fetch]] is not available on the https://pyscript.net/latest/pyscript.js build. If you change to the unstable https://pyscript.net/unstable/pyscript.js build, it will work or you can wait a few days for 2022.12.1 release. ๐Ÿ™‚
Avatar
yes i tried that but it had some error with not being able to locate the file - hold on .. i'll check
01:33
this is linking to /unstable/ .. with [[fetch]] put back .. with 'paths' still there..
01:34
but this works with unstable. [[fetch]] + files = <link rel="stylesheet" href="https://pyscript.net/unstable/pyscript.css"/> <script defer src="https://pyscript.net/unstable/pyscript.js"></script> <py-config> [[fetch]] files = ["request.py"] </py-config>
Avatar
either way, that example code is for /latest/. Not to worry, I'll get there in the end! Hey @Jeff Glass I just realised it was your response: here https://community.anaconda.cloud/t/web-scraping-with-pyscript/29705 .. that i was trying to get working for myself. I'm having trouble with the: from pyodide.http import open_url page_html = open_url('www.example.com') something something CORS / pyfetch something --- I'm missing something but I can't figure it out - sorry for noobing out on you guys! (edited)
Avatar
Avatar
tedpatrick
@LeeSlacks I am fairly certain that [[fetch]] is not available on the https://pyscript.net/latest/pyscript.js build. If you change to the unstable https://pyscript.net/unstable/pyscript.js build, it will work or you can wait a few days for 2022.12.1 release. ๐Ÿ™‚
(thx Ted!)
Avatar
Hello ๐Ÿ™‚ I'm building a pyscript app, it's pretty simple: HTML form > validate/send values to python > python with xlsxwritter generates xl file > save xl file to pyscript env > download file to user locally. I'm mainly struggling with the HTML form & validating/sending values. I would like to know the best way to go about building the HTML/CSS side of things? . I looked at Svelte and it seemed pretty good! but can pyscript work with that? I couldnt find anything definitive. Also, where can I find all of the different pyscript commands? e.g. I didn't know this was a thing: pyscript.write instead of importing JS and using getElementById("some_id").innerHTMLorsomthingidontremember (edited)
19:41
also, will I incur a big performance hit if I choose to use python import of JS rather than writing pure JS?
Avatar
Avatar
alice--
also, will I incur a big performance hit if I choose to use python import of JS rather than writing pure JS?
in the past usually for speed js > brython > python. but some python compiled code to wasm can be way faster than js too, as already seen now with mypyc. So there's is no definitive answer for sure
Avatar
Jeff Glass 08/12/2022 21:02
Hi @alice-- ! For form validation and website building... well, there's many many options out there, from Svelte to a simple static site a large framework. Everyone's going to have their own preferences. PyScript tends to work with most frameworks if you use the <py-script src="..."> attribute to load your code from a separate python file. We've seen examples here of frameworks (next/React) that strip whitespace from all files, which mangles all inline python inside <py-script> tags in horrible ways. There's a fair bit of documentation on the various pyscript functions is included in the upcoming release, and the recommended means of writing to a page element has been wrapped into a new display() function. If you want to see what that can do to simplify your workflow, point your source links to: <link rel="stylesheet" href="https://pyscript.net/snapshots/2022.12.1.RC1/pyscript.css"/> <script defer src="https://pyscript.net/snapshots/2022.12.1.RC1/pyscript.js"></script> And check out this tutorial that @FabioRosado wrote on Writing Content to an Element: https://docs.pyscript.net/unstable/tutorials/writing-to-page.html. (It refers to functionality, like display() and <py-terminal> only present in that upcoming release) There's often a performance hit for using Python vs vanilla js, though it's a bit hard to quantify. For simple user interactions, like processing/downloading a file on a button click, I wouldn't sweat it too much, but that's just me. (edited)
Avatar
wow thank you for the great reply @Jeff Glass. looking forward to the next release! what you are all making is super amazing.
Avatar
Avatar
pmp-p
@tedpatrick what about a subchannels for alternate pyscript cores ( eg maybe #pyscript-runtimes ). @ntoll and I are wishing you wonderfull hollydays ๐Ÿ˜‰
so @tedpatrick @ntoll any decision about a sub channels for alternative cores ?
Avatar
Hi again! ๐Ÿ‘€ been struggling with this one for a couple hours.. hoping someone can point me the right direction ๐Ÿ™‚ I have a the main file index.html, this file calls <py-script src="interactions.py"> (this all works) within interactions.py, their is another function, which references a new file: variable_form.html, where for a given numeric input X within index.html we read variable_form.html and string replace the Element ID's. The problem I have is in pyscript, I just get error file not found (due to pyodide enviroment I assume?) - or somtimes a CORS policy error. So, I tried to fetch the files instead, I hosted it on my one-drive, and got 403 Forbidden. I have all whole repo dev.azure, but don't think I can fetch a single file from the repo?
Avatar
Avatar
alice--
Hi again! ๐Ÿ‘€ been struggling with this one for a couple hours.. hoping someone can point me the right direction ๐Ÿ™‚ I have a the main file index.html, this file calls <py-script src="interactions.py"> (this all works) within interactions.py, their is another function, which references a new file: variable_form.html, where for a given numeric input X within index.html we read variable_form.html and string replace the Element ID's. The problem I have is in pyscript, I just get error file not found (due to pyodide enviroment I assume?) - or somtimes a CORS policy error. So, I tried to fetch the files instead, I hosted it on my one-drive, and got 403 Forbidden. I have all whole repo dev.azure, but don't think I can fetch a single file from the repo?
Jeff Glass 09/12/2022 18:53
Hi! Couple quick questions - I'm assuming you're running pyscript from https://pyscript.net/latest/pyscript.js, yeah?
Avatar
yeah! I tried both unstable and latest, stuck with latest as then fetch stoped throwing errors
Avatar
Jeff Glass 09/12/2022 18:54
For sure - are you using paths = in py-config to get variable_form.html?
Avatar
I seen the tutorial, and saved my html files as a github gist, but could not find it in piodyde env
18:54
<py-config> [[fetch]] from = "https://raw.githubusercontent.com/pyscript/pyscript/main/pyscriptjs/src/python/" files = ["pyscript.py"] [[fetch]] from = "https://gist.githubusercontent.com/laralice/3d47cad6522da9f1b46382c9a4d397dc/raw/c83b003788f3692a20ce901a37fcf6956090cbb1/insts.html" files = ["insts.html"] </py-config>
18:54
thats my config
Avatar
Jeff Glass 09/12/2022 18:55
Ah, so [[fetch]] is only a feature in in unstable (unreleased) currently
18:56
Let me try your config
18:58
Hmmm I also see a 404 on fetching the second file...
Avatar
this is my error on the unstable build
18:59
latest build doesnt throw anything ๐Ÿคทโ€โ™€๏ธ
Avatar
Jeff Glass 09/12/2022 19:00
True, but that's because the Latest build doesn't know anything about the keyword fetch, so it silently ignores it
Avatar
I'm mostly confused how the index file is able to reference other files within a local DIR, but the python part can't ๐Ÿ˜”
19:00
ahh okay, that solves that ๐Ÿ˜„
Avatar
Jeff Glass 09/12/2022 19:00
You mean how the index file can use <py-script src="...."?
Avatar
yeah!
Avatar
Jeff Glass 09/12/2022 19:01
Ok so! This I can explain
19:05
The Pyodide runtime (which underlies PyScript as we know it) uses a compilation of the CPython interpreter into Web Assembly, using a program called Emscripten. Emscripten, to allow programs which normally don't run in a browser to work, creates a virtual file system that the compiled program interacts with.
Avatar
cause in my mind, if python <py-script src="desktop/test.py" can be accessed, then any local files referencded within test.py should also be accessible ๐Ÿคฏ
Avatar
Jeff Glass 09/12/2022 19:06
So when you using normal Python functions that interact with the filesystem, it's this virtual filesystem in the browser window that you're interacting with. If you want to see the contents of this filesystem, try: <py-script> import os for root, dirs, files in os.walk("/", topdown=False): for name in files: print(os.path.join(root, name)) for name in dirs: print(os.path.join(root, name)) </py-script>
19:06
(open(), import(), os.walk etc all work with the virtual filesystem)
Avatar
Avatar
alice--
cause in my mind, if python <py-script src="desktop/test.py" can be accessed, then any local files referencded within test.py should also be accessible ๐Ÿคฏ
Jeff Glass 09/12/2022 19:07
Accessible by Python? Or by something else
19:08
Carrying on, Python doesn't "know" it's operating in a web browser - the interpreter is just running in a filesystem. So if you want it to have access to files to manipulate, you need to get them into the Virtual Filesystem
19:08
That's what [[fetch]] (and its predecessor paths=) do - they load content from the web and download into the virtual filesystem (edited)
19:12
Oh and
19:12
The second half of your <py-config> should be: <py-config> [[fetch]] from = "https://gist.githubusercontent.com/laralice/3d47cad6522da9f1b46382c9a4d397dc/raw/c83b003788f3692a20ce901a37fcf6956090cbb1/insts.html" to_file = "insts.html" </py-config>
Avatar
yeah, I meant accessible by python, but I'm just thinking about it wrong. I get what you're saying though :). I've managed to create a file in the virtual system and serve it to users - just not the other way round I guess! and that's what I've been trying to do today. Starting from local imports, to trying to import from one drive etc. But, from what you have clarified (thank you very much), my best course of action is try the paths= syntax along with the github.gist host
โค๏ธ 1
Avatar
Jeff Glass 09/12/2022 19:12
(to_file, not files)
Avatar
once again Jeff, thank you very much for the help. โค๏ธ
๐Ÿค˜ 1
Avatar
hey team, I'm trying to do a beautifulSoup webscrapy thing - I have the html as a string - but I've done that bit in a js <script> tag .. This is my pyscript .. but I dont really understand how to get the variable into the pyscript.. <py-script> from bs4 import BeautifulSoup from js import document def beaty_soup(html): ## this is me being a noob with the 'html' variable from the js script page_html = html ## this is me being a noob with the 'html' variable from the js script soup = BeautifulSoup(page_html, 'html.parser') def print_self_and_children(tag, indent = 0): print("_" * indent + str(tag.name)) if hasattr(tag, 'children'): for child in tag.children: if hasattr(child, 'name') and child.name is not None: print_self_and_children(child, indent = indent + 2) print_self_and_children(soup) </py-script>
08:57
i call the pyscript from js like this beaty_soup(html);
Avatar
all good ๐Ÿ™‚ i figured it out - .. haha but .. now i need to be a better coder
Avatar
Can I use pyscript in conjunction with flask? Not too sure about some processor heavy dataframe manipulations
09:38
And can I skip learning JavaScript now that pyscript is around? I.e. can you make websites work really nicely in the same way with pyscript as you can with JavaScript?
Avatar
Let me know if there is more tuning I can do to speed up my demo app data loading. I trimmed down my data file to 3.25 MB csv gzip containing over 215K records & changed my PyScript to load .gz instead of 20MB of CSV data. This shaves a few more seconds and loads that app data in about 8 secs total with runtime. See updated app: https://randomfractals.github.io/chicago-crimes/apps/pyscript/ Gzip data loading I added in PyScript: # read slimmed down 2022 crimes CSV data in gzip format data_url = 'https://raw.githubusercontent.com/RandomFractals/chicago-crimes/main/data' crimes_data_url = f'{data_url}/crimes-2022-slim.csv.gz' data_response = await fetch(crimes_data_url) crimes_data = await data_response.arrayBuffer() crimes = pd.read_csv(BytesIO(crimes_data.to_py()), sep=',', compression='gzip', parse_dates=['Date'], cache_dates=True, low_memory=False) (edited)
Avatar
์˜์›ํ•œ์ฐฐ๋‚˜ 13/12/2022 08:50
hi everyone~
08:51
Is there any way to prevent infinite loop?
08:52
08:52
in pyscript on web html
08:53
Can I force a running program to close?
Avatar
using asynchronous loops is a solution
Avatar
Jeff Glass 13/12/2022 13:53
@์˜์›ํ•œ์ฐฐ๋‚˜ There was some good discussion of this just yesterday in the #deleted-channel thread - like @pmp-p says, at this point, if you're just looking to not lock up the UI, you can run it as a coroutine: import asyncio async def solution(n1, n2): answer = 0 for i in range(100000000000000): print(i) solution_task = asyncio.create_task(solution(1, 2)) solution_ask.add_done_callback(do_something_with_result)
Avatar
Avatar
์˜์›ํ•œ์ฐฐ๋‚˜
Can I force a running program to close?
Jeff Glass 13/12/2022 13:54
Currently no - if you have a while True: pass loop, for example, you must close/crash and re-open the tab. The same is true of JavaScript, incidentally. The long-term solution for PyScript is likely going to be running the Python code in a Web Worker (i.e. separate thread), so that long-running loops don't lock up th whole window
Avatar
Avatar
Jeff Glass
Currently no - if you have a while True: pass loop, for example, you must close/crash and re-open the tab. The same is true of JavaScript, incidentally. The long-term solution for PyScript is likely going to be running the Python code in a Web Worker (i.e. separate thread), so that long-running loops don't lock up th whole window
์˜์›ํ•œ์ฐฐ๋‚˜ 13/12/2022 14:48
Thank you for your mentions. T.T
Avatar
์˜์›ํ•œ์ฐฐ๋‚˜ 14/12/2022 05:27
wow! i found new version of pyscript today!.. so.. my code is error...T.T
05:28
<py-env></py-env> not working T.T and evaluate() function is not working T.T
Avatar
Avatar
Jeff Glass
Currently no - if you have a while True: pass loop, for example, you must close/crash and re-open the tab. The same is true of JavaScript, incidentally. The long-term solution for PyScript is likely going to be running the Python code in a Web Worker (i.e. separate thread), so that long-running loops don't lock up th whole window
์˜์›ํ•œ์ฐฐ๋‚˜ 14/12/2022 05:48
thank you!
05:50
How can I get output and errors from pyscript? I was doing it like this.
Avatar
์˜์›ํ•œ์ฐฐ๋‚˜ 14/12/2022 05:58
Avatar
์˜์›ํ•œ์ฐฐ๋‚˜ 14/12/2022 06:30
first, i made element of <py-terminal>
06:30
06:31
and then, on my javascript code, i use pyscript module api function
06:31
06:32
is this way best?
Avatar
์˜์›ํ•œ์ฐฐ๋‚˜ 14/12/2022 08:36
i found another way
08:37
i will use my event listener. so i have a freedom anytime.
Avatar
How would I paste text from the users clipboard? js.navigator.clipboard.writeText() works great to copy to the users clipboard, but js.navigator.clipboard.readText() just gives AttributeError: readText when trying to paste from the clipboard
Avatar
hmm, maybe an issue with firefox? seems to work fine in chrome
Avatar
Avatar
tuxprint
hmm, maybe an issue with firefox? seems to work fine in chrome
Jeff Glass 14/12/2022 18:39
Hmm interestesting yeah, this works fine for me on Chromium: <div id="output"></div> <button id="btn" py-click="paste_to_div()">Click to Log</button> <py-script> import js from pyodide.ffi import to_js async def paste_to_div(*args): div = js.document.getElementById("output") div.innerText += await js.navigator.clipboard.readText() </py-script>
18:40
From the MDN Docs on clipboard.readText:
Avatar
Avatar
Jeff Glass
Hmm interestesting yeah, this works fine for me on Chromium: <div id="output"></div> <button id="btn" py-click="paste_to_div()">Click to Log</button> <py-script> import js from pyodide.ffi import to_js async def paste_to_div(*args): div = js.document.getElementById("output") div.innerText += await js.navigator.clipboard.readText() </py-script>
thanks for your insight! Much appreciated!
Avatar
I'm assuming the answer is no but is there a workaround for this error: RuntimeError: asyncio.run() cannot be called from a running event loop For context, I'm trying to use the ffmpeg_streaming video.dash.output() which seems to use asyncio
15:39
the actual function I'm calling it from is not using asyncio
Avatar
Avatar
tuxprint
I'm having trouble calling a python function from javascript. it gives me an error for PyScript not defined. pything = PyScript.globals.runtime.get('thing'); pything(); changing PyScript to pyscript or pyodide does not work either. some error about not defined
running this on the latest release works, but using the min version does not. Uncaught ReferenceError: pyscript is not defined (edited)
16:17
for context, calling a python function from javascript using pyscript.min.js gives that error. the full pyscript.js works as expected
16:19
https://pyscript.net/releases/2022.12.1/pyscript.js that is the link I am using (edited)
Avatar
Avatar
Jns
I'm assuming the answer is no but is there a workaround for this error: RuntimeError: asyncio.run() cannot be called from a running event loop For context, I'm trying to use the ffmpeg_streaming video.dash.output() which seems to use asyncio
Jeff Glass 16/12/2022 16:31
You may some some luck by assigning one of the working webloop functions https://jeff.glass/post/pyscript-asyncio/#webloop to asyncio.run. I.e. import asyncio asyncio.run = asyncio.ensure_future ...
๐Ÿ‘ 1
16:32
In a really quick and dirty test, video.dash.output no longer crashes with this reassignment. No idea if it does what it's supposed to though
Avatar
thats a really interesting solution lmao
16:33
new errors but unrelated to the prior problem so thats good ๐Ÿ˜„
16:33
thanks for the help
Avatar
Jeff Glass 16/12/2022 16:33
๐Ÿ˜… Anytime!
Avatar
Avatar
tuxprint
running this on the latest release works, but using the min version does not. Uncaught ReferenceError: pyscript is not defined (edited)
Jeff Glass 16/12/2022 16:42
Huh! Interesting... I'm seeing the same thing...
16:44
And similarly, no global 'pyscript' object in JS when using the minified version, but it's present using the full version, when loading from latest
16:48
Confirmed this happens with both 2022.12.1 as released and when building locally....
16:50
Ah! There is a single line missing from our rollup config; the minified version does not specify the output name. Great catch @tuxprint, creating a PR for this now
๐Ÿ”ฅ 1
Avatar
Avatar
Jeff Glass
Ah! There is a single line missing from our rollup config; the minified version does not specify the output name. Great catch @tuxprint, creating a PR for this now
awesome! glad that I could be of help
Avatar
is there an equivalent to js.document.getElementById().click() in pyscript?
Avatar
FabioRosado 18/12/2022 13:15
I havenโ€™t tried but I think you could do something like: Element(<el id>).element.click
13:16
We are rethinking the Element api and itโ€™s likely to change in the future to make it easier to use ๐Ÿ˜„
Avatar
Avatar
FabioRosado
I havenโ€™t tried but I think you could do something like: Element(<el id>).element.click
thank you! I swear I tried that yesterday and it did not work, but it is working now lol.
Avatar
FabioRosado 18/12/2022 13:18
Good to know I wasnโ€™t sure if it would haha
Avatar
Can someone help me understand how the pyscript filesystem works? I'm creating a .zip file on the pyscript/pyodide? file system but I'd like to allow the user of my site to download it but I can't really find any documentation about how to do this. Does anyone have any tips/knowledge?
21:23
I've tried to convert to base64 and retrieving it that way but it doesn't work with larger files because URLs have maximum number of characters
Avatar
I figured it out, if anyone also needs to get files from the internal file systems you can do so with the following general steps: 1) base64 encode the file, 2) pass it to javascript as a base64 string, 3) in javascript convert the base64 string to a blob, 4) use URL.createObjectURL to give it a URL, then add it to an <a> as usual
01:18
i dunno if there's an easier way but that worked for me
Avatar
Jeff Glass 19/12/2022 01:46
You can also create a js File object with the binary data of the object, then use createObjectUrl to get a URL for that
Avatar
I tried doing this but ran into problems - really not sure why. I was able to create and download a ".zip" file but it wasn't recognized by windows
Avatar
Jeff Glass 19/12/2022 01:50
If memory serves there are sometimes issues with signed/unsigned number conversion to JS - casting the data to a UInt8Array solved a similar-sounding issue for me.
Avatar
ah interesting. I ended up doing that when I finally got it working but it was just part of some code openai generated for me as an example so I didn't even consider that was the problem
01:51
I'm curious if it'd still work without tthat
Avatar
Jeff Glass 19/12/2022 01:53
Another possible option is to use PyProxy.get_buffer() to directly reference the underlying data in Web Assembly and pass that data to the File constructor without copying.
Avatar
is there documentation for that?
01:56
I had written a tutorial on file upload/download back during 2022.06.1 and never published itโ€ฆ maybe that would be worth reviving
Avatar
๐Ÿ˜„ I would have loved to find it a few hours ago haha
๐Ÿ˜… 1
01:56
I found a lot of your other tutorials and have been referencing them a lot
Avatar
Using the [[fetch]] I found an error that requires the use of a Webserver if you want to use files from your local drive. Why is this the case/itโ€™s not working for me, have I missed something? I tried https://github.com/pyscript/pyscript/issues/257#issuecomment-1119595062
paths: can only be used if the page is being served out of a webserver, but not out of an HTML page on the filesystem. This is for good reasons and it&#39;s related to #179, but currently py-sc...
Avatar
FabioRosado 20/12/2022 10:11
If I remember correctly thatโ€™s an issue caused by cors, Iโ€™ll have a look
Avatar
Avatar
char
Using the [[fetch]] I found an error that requires the use of a Webserver if you want to use files from your local drive. Why is this the case/itโ€™s not working for me, have I missed something? I tried https://github.com/pyscript/pyscript/issues/257#issuecomment-1119595062
Jeff Glass 20/12/2022 13:00
Can you share your code here, or in a pastebin if itโ€™s quite long?
Avatar
Here's the code: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" /> <script defer src="https://pyscript.net/latest/pyscript.js"></script> <title>Display Image</title> <py-config> [[fetch]] from = "C:\Users\admin\PycharmProjects\pyscript_practice\" files = ["image.png"] </py-config> </head> <body> <py-script> import image.png display("image.png", target="picture-here") </py-script> <div id="picture-here"></div> </body> </html>
06:38
This is the error message: (PY0001): PyScript: Access to local files (using "Paths:" in <py-config>) is not available when directly opening a HTML file; you must use a webserver to serve the additional files. See this reference on starting a simple webserver with Python.
Avatar
Avatar
Jns
I figured it out, if anyone also needs to get files from the internal file systems you can do so with the following general steps: 1) base64 encode the file, 2) pass it to javascript as a base64 string, 3) in javascript convert the base64 string to a blob, 4) use URL.createObjectURL to give it a URL, then add it to an <a> as usual
base64 encoding might add more overhead to your data loading. I ended up using gz to compress my data file recently and loaded it with pyodide in my simple demo app: python # read slimmed down 2022 crimes CSV data in gzip format data_url = 'https://raw.githubusercontent.com/RandomFractals/chicago-crimes/main/data' crimes_data_url = f'{data_url}/crimes-2022-slim.csv.gz' data_response = await fetch(crimes_data_url) crimes_data = await data_response.arrayBuffer() crimes = pd.read_csv(BytesIO(crimes_data.to_py()), sep=',', compression='gzip', parse_dates=['Date'], cache_dates=True, low_memory=False) see: https://github.com/RandomFractals/chicago-crimes/blob/main/apps/pyscript/index.html (edited)
Avatar
Avatar
char
Here's the code: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" /> <script defer src="https://pyscript.net/latest/pyscript.js"></script> <title>Display Image</title> <py-config> [[fetch]] from = "C:\Users\admin\PycharmProjects\pyscript_practice\" files = ["image.png"] </py-config> </head> <body> <py-script> import image.png display("image.png", target="picture-here") </py-script> <div id="picture-here"></div> </body> </html>
Jeff Glass 21/12/2022 13:45
Ah, so the "from" parameter of a fetch configuration is a URL - it's looking for resources on the network, and generally local resources ("C:\Users...") are not available on the network. The purpose of using something like python -m http.server 8080 is explicitly make files on your computer available on the network, using a server program. The exact URL those files are available at will depend on what location you run that command from, but assuming your terminal is within your pyscript_practice folder when you run it, your image file will be available at localhost:8080/image.png
Avatar
takacsjanos 22/12/2022 08:12
hello I downloaded pyodide, and try to serve it from localhost, but I get this error: [plugin:vite:import-analysis] Failed to resolve import "node-fetch" from "pyodide/pyodide.js". Does the file exist? I use vite to develop the javascript project, which uses some python libs. My guess is that vite does not set the correct mime type headers, but I have no idea how to fix it.
Avatar
ok, I am slowly getting there, as I have no js or web development background I am really lost a lot of the time so thanks! I tried to use display('image.png') and display('http://localhost:8080/image.png') both not working
Avatar
Avatar
takacsjanos
hello I downloaded pyodide, and try to serve it from localhost, but I get this error: [plugin:vite:import-analysis] Failed to resolve import "node-fetch" from "pyodide/pyodide.js". Does the file exist? I use vite to develop the javascript project, which uses some python libs. My guess is that vite does not set the correct mime type headers, but I have no idea how to fix it.
FabioRosado 22/12/2022 10:38
Are you passing the pyodide to the pyconfig?
Avatar
Avatar
FabioRosado
Are you passing the pyodide to the pyconfig?
takacsjanos 22/12/2022 11:47
"runtimes": [{ "src": "/pyodide/pyodide.js", "name": "pyodide-0.22.0a3", "lang": "python" }],
11:48
and the pyodide.js is loaded, the error comes when it tries to do an async import for node-fetch
Avatar
FabioRosado 22/12/2022 11:53
I haven't used vite much tbh, so this is a long shot, are you able to install node-fetch? I'm assuming that if it hangs there, then the runtime import was successful and its hanging in that line
Avatar
Avatar
FabioRosado
I haven't used vite much tbh, so this is a long shot, are you able to install node-fetch? I'm assuming that if it hangs there, then the runtime import was successful and its hanging in that line
takacsjanos 22/12/2022 11:57
when I delete the runtimes block, everything loads fine (but slow on my current internet connection). so it's going to be a vite issue. I'm not up to date on what supposed to be the best js dev env. nowdays, I also tried parcel, but it's worse than vite (edited)
Avatar
FabioRosado 22/12/2022 12:02
I heard good thinks about vite and @Paul Everitt uses it in the collective. It's supposed to be very fast when bundling things. I wonder if maybe that's why it's breaking. Out of curiosity, can you try pyodide's version v0.21.3? To see if you get the same error? The reason why I am saying. is because that version is trying to import node fetch with: await import(/* webpackIgnore: true */ "node-fetch")).default, But 0.22.0a3 removed the webpackIgnore, just wonder if maybe that can cause some problems ๐Ÿค” Either that or it will fail silently
12:02
If you have the code somewhere public I'm happy to experiment with things as well if its easier
Avatar
takacsjanos 22/12/2022 12:10
ok, I'll try to use 0.21.3
12:17
Uncaught SyntaxError: Cannot use import statement outside a module (at pyodide.js:1:1)
Avatar
takacsjanos 22/12/2022 12:27
there is no code to publish yet. it's just a simple index.html with a py-config, and an unzipped pyodide release. I use the latest pyscript, also served by vite from localhost. when it loads the runtime from cdn then it works fine, but when the runtime is served from localhost, then it fails.
Avatar
FabioRosado 22/12/2022 12:36
I've only found this issue on github: https://github.com/node-fetch/node-fetch/issues/1408 What version of vite are you using? ๐Ÿค”
Avatar
takacsjanos 22/12/2022 12:49
"vite": "^3.0.9"
Avatar
Avatar
takacsjanos
Uncaught SyntaxError: Cannot use import statement outside a module (at pyodide.js:1:1)
Paul Everitt 22/12/2022 13:13
Yeh, welcome to hell. ๐Ÿ˜€ I learned a lot, chasing that error.
13:15
@takacsjanos I have a 20-part tutorial on Pyodide, Vite, Vitest if you're interested. But I confess: (a) I'll expect you to help and (b) I could never get a build step working.
๐Ÿ‘€ 1
Avatar
Avatar
Paul Everitt
Yeh, welcome to hell. ๐Ÿ˜€ I learned a lot, chasing that error.
takacsjanos 22/12/2022 13:23
well, I'm partially interested. It would be good to use pyodide from localhost on a slow internet connection. But since it's working with CDN, I decided it's better to keep coding my hobby project instead of hunting bugs that only affect me during development. But if you have a proposed solution, I can help with trying it out.
Avatar
Paul Everitt 22/12/2022 13:26
@takacsjanos In that case, my writeup is likely 100x more than what you're after. It's aimed at convincing people to develop via Vitest instead of browser reload.
Jeff Glass started a thread. 22/12/2022 17:17
Avatar
Is there a proper way to use pyscript with es6 yet? Would love to use it in a react app without having to dangerously set inner html ๐Ÿ˜…
Avatar
Avatar
Paul Everitt
@takacsjanos I have a 20-part tutorial on Pyodide, Vite, Vitest if you're interested. But I confess: (a) I'll expect you to help and (b) I could never get a build step working.
I'm pretty well versed in building vite if you still want help ๐Ÿ˜„ I imagine we'd probably need a plugin of some kind in order to get it to understand the pyscript code syntax
Avatar
Paul Everitt 23/12/2022 13:51
@Bryan Here's my super-long writeup. It's mostly aimed at Vitest, not Vite. I had hoped to get bundling working, but failed. https://pyodide-components.readthedocs.io/en/latest/
Avatar
Avatar
Paul Everitt
@Bryan Here's my super-long writeup. It's mostly aimed at Vitest, not Vite. I had hoped to get bundling working, but failed. https://pyodide-components.readthedocs.io/en/latest/
in your viteconfig.js file setting minify to false might get bundling working
20:10
this guide is looking pretty good though! very detailed ๐Ÿ˜„
20:10
I'll dig into this further after the holidays
Avatar
Avatar
Bryan
in your viteconfig.js file setting minify to false might get bundling working
Paul Everitt 23/12/2022 20:11
Thanks for the tip. Would probably help if I made a teeny tiny example in a repo and we could riff on that. Is the Node-oriented, test-first approach something you're interested in?
Avatar
so I work on a few larger projects like https://blocks.glass this is a next.js/react application with a pretty large codebase, I'd like to be able to use pyscript for some bits and pieces, like in my example I recently posted in #โ•ฐโ•ธi-made-this https://discord.com/channels/972017612454232116/973504848530120714/1055351071343652864 but right now bundling pyscript code doesn't really seem like a possible thing, or if it is possible, it's really hard to figure out how to do. starting with a basic vite project seems like the clearest starting point to me to eventually merging in with larger projects
The first holographic sharing platform built for 3D creators.
Avatar
Paul Everitt 23/12/2022 20:15
Here's something that might be an inspiration: https://github.com/pyodide/pyodide-webpack-plugin
A Webpack plugin for integrating pyodide into your project. - GitHub - pyodide/pyodide-webpack-plugin: A Webpack plugin for integrating pyodide into your project.
๐Ÿ‘€ 2
Avatar
ohhh shiny ๐Ÿ˜„
Avatar
Paul Everitt 23/12/2022 20:16
Some talk in the Pyodide repo about Webpack, e.g. https://github.com/pyodide/pyodide/search?q=webpack&type=discussions
Pyodide is a Python distribution for the browser and Node.js based on WebAssembly - Search ยท webpack ยท pyodide/pyodide
Avatar
Have any one tried to get pyscript working on ionic - reactnative - apache cordova - phone gap , etc ?
Avatar
Avatar
Sokary
Have any one tried to get pyscript working on ionic - reactnative - apache cordova - phone gap , etc ?
Jeff Glass 25/12/2022 23:08
Not that I think weโ€™ve seen come through the discord! Some interest in using it with electron, though thatโ€™s quite a different beast. Seems like a fresh area to check out!
Avatar
Why do i only get the last value printed out? I should get the output starting from 1 up to 99. This is using <py-repl> (edited)
09:29
This is using <py-script>: I should get an output like this: (edited)
Avatar
Py repl only prints the last line, i want it to be printing all lines, how can i achieve that
Avatar
FabioRosado 26/12/2022 09:51
What version of Pyscript are you using? The repl output was fixed in the last release
Avatar
Avatar
FabioRosado
What version of Pyscript are you using? The repl output was fixed in the last release
Ohh I think I am using the old version, since I downloaded the pyscript.js on a local place
Avatar
Avatar
FabioRosado
What version of Pyscript are you using? The repl output was fixed in the last release
Thank you for your help btw! ๐Ÿ™‚
Avatar
FabioRosado 26/12/2022 11:23
No worries happy to help ๐Ÿ˜€
๐Ÿ‘ 1
Avatar
Is there a way to clear the <py-terminal>? Or do I just need a way to delete the nested/child tags? (edited)
Avatar
Avatar
Enes
Is there a way to clear the <py-terminal>? Or do I just need a way to delete the nested/child tags? (edited)
Jeff Glass 26/12/2022 14:17
Thereโ€™s not an API for it via Pyscript, but yeah you could use document.getElementsByTagName("py-terminal")[0].innerHTML = โ€œโ€
Avatar
Avatar
Jeff Glass
Thereโ€™s not an API for it via Pyscript, but yeah you could use document.getElementsByTagName("py-terminal")[0].innerHTML = โ€œโ€
Thanks Jeff it works, but now the problem is that the black background also disappears lol
14:32
maybe i should delete the child tags anyways, which may be a workaround (edited)
Avatar
Jeff Glass 26/12/2022 14:45
Without any content, the py-terminal renders to height zero, like a div. Using height or min-height should also keep it visible?
14:47
Might need to target the inner element with the py-terminal class
๐Ÿ‘ 1
Avatar
Avatar
Jeff Glass
Without any content, the py-terminal renders to height zero, like a div. Using height or min-height should also keep it visible?
This also doesnt seem to work
14:51
i re inserted: <pre class="py-terminal"></pre>
14:52
the black background appears again
14:52
but now if i run the play button it doesnt do anything lol
14:52
one problem solved, got a new problem ๐Ÿ˜ฆ
14:56
i solved it bro!!
14:56
the solution: document.getElementsByClassName("py-terminal")[0].innerHTML=""
Avatar
Avatar
Jeff Glass
Might need to target the inner element with the py-terminal class
Thank you soo much brooo
๐ŸŽ‰ 1
Avatar
Is there a way to prevent the terminal from appending the code?
Avatar
Avatar
Enes
Is there a way to prevent the terminal from appending the code?
Jeff Glass 26/12/2022 22:32
You could potentially override sys.displayhook to clear the py-terminal before writing new content to it?
Avatar
Avatar
Jeff Glass
You could potentially override sys.displayhook to clear the py-terminal before writing new content to it?
I have never heard of sys.displayhook ๐Ÿ˜ฆ
22:39
Havenโ€™t tested using it for this specific case (currently on mobile), but it gives you a measure of control over how REPL expressions are output
22:40
The better answer - Iโ€™ve got a couple PRโ€™s in the works that add plugin triggers before/after REPL execution; you could use those to clear the output location
Avatar
What is a PR? ๐Ÿ™‚
22:41
i mean the abbreviation
Avatar
Avatar
Jeff Glass
The better answer - Iโ€™ve got a couple PRโ€™s in the works that add plugin triggers before/after REPL execution; you could use those to clear the output location
I think I will just have to think of a new workaround since the documentation about "sys.displayhook" seems very complex to understand ๐Ÿ˜ฆ
๐Ÿ‘ 1
Avatar
Avatar
Enes
What is a PR? ๐Ÿ™‚
Jeff Glass 26/12/2022 22:55
Ah no worries - by PR I meant a โ€œPull Requestโ€ - an upcoming change to the PyScript codebase on GitHub. (edited)
๐Ÿ‘ 1
Avatar
Avatar
Enes
I think I will just have to think of a new workaround since the documentation about "sys.displayhook" seems very complex to understand ๐Ÿ˜ฆ
Jeff Glass 26/12/2022 22:57
Sounds good! Iโ€™ll try to remember to ping you when (if) the callbacks for REPL execution get merged into the codebase
๐Ÿ‘ 1
Avatar
Avatar
Jeff Glass
Sounds good! Iโ€™ll try to remember to ping you when (if) the callbacks for REPL execution get merged into the codebase
Thanks mate for your time ๐Ÿ™‚ I really appreciate it!!!
Avatar
Avatar
Jeff Glass
Sounds good! Iโ€™ll try to remember to ping you when (if) the callbacks for REPL execution get merged into the codebase
MAybe I can insert document.getElementsByClassName("py-terminal")[0].innerHTML="";
23:10
I found the place where the button event listener is
23:10
so if I could place that line somewhere, i think that would solve the problem
Avatar
Brooo I fixed it! I inserted the document.getElementsByClassName("py-terminal")[0].innerHTML=""
๐Ÿ”ฅ 1
23:23
into here:
23:24
I never thought that that would solve it ๐Ÿ™‚
Avatar
Avatar
Jeff Glass
Not that I think weโ€™ve seen come through the discord! Some interest in using it with electron, though thatโ€™s quite a different beast. Seems like a fresh area to check out!
That would be a great use case for desktop apps to use pyodide since when bundled with the other libraries in the app, I believe it would be a more responsive experience in terms of pyodide runtime loading vs. similar apps in the browser.
Avatar
Avatar
Taras
That would be a great use case for desktop apps to use pyodide since when bundled with the other libraries in the app, I believe it would be a more responsive experience in terms of pyodide runtime loading vs. similar apps in the browser.
iirc electron has an insecure filesystem access mode, that should be avoided, i found that when experimenting https://github.com/pmp-p/Pylone so i stopped since people may not find out if the runtime is in secure or insecure mode (edited)
Avatar
add to that the capacity to read/write arbitrary files on local filesystem
15:10
at the time i thought that could hurt python wasm reputation, it's way better to start only with https://developer.mozilla.org/en-US/docs/Web/Progressive_web_apps which are secure by default
Avatar
Avatar
Jeff Glass
Not that I think weโ€™ve seen come through the discord! Some interest in using it with electron, though thatโ€™s quite a different beast. Seems like a fresh area to check out!
i think it's almost the same cus electron is just a browser so what works here will work there
Avatar
Avatar
Sokary
i think it's almost the same cus electron is just a browser so what works here will work there
Jeff Glass 28/12/2022 01:09
In theory yeah - some of the ways that Pyodide loads additional modules might need to be accounted for. And maybe other oddities. But if you give it a try, please do post about it here! (edited)
Avatar
i tried it using Transcrypt and it's working just fine with "EXPO" -> "React Native" but it's not that good cus u can't use any python library in this approach but you can use any Js library any ways i will give this one a try to and will share the news in here to
Avatar
G'day! I'm sure its just a rookie error but... I'm trying to use pyfetch to make an API call... the call successfully hits my API which is returning status code 200, but on the client side I see response.status=0 (I am awaiting the call)...
Avatar
Avatar
Martin
G'day! I'm sure its just a rookie error but... I'm trying to use pyfetch to make an API call... the call successfully hits my API which is returning status code 200, but on the client side I see response.status=0 (I am awaiting the call)...
Jeff Glass 28/12/2022 13:45
Hello! In theory there's nothing fancy required to get the response status... do you mind sharing your code? (edited)
Avatar
Jeff Glass 28/12/2022 13:54
Here's a brief example using the "reqres" demo API: <div id="result"></div> <py-script> from pyscript import display from pyodide.http import pyfetch import asyncio async def get_api_result(url, **kwargs): result = await pyfetch(url, **kwargs) display(f"Request Status: {result.status}", target="result") body = await result.json() display(f"Request Body: {body}", target="result") asyncio.ensure_future(get_api_result('https://reqres.in/api/users/2')) </py-script> (edited)
Avatar
Hi @Jeff Glass - thanks for the example. I've found the problem - I'm hitting a CORS issue... and I had tried putting mode="no-cors" in the call to fetch but (now I know) that just provides an opaque response that hides the status code and body from the client-side javascript ๐Ÿ™‚ (edited)
Avatar
Avatar
Martin
Hi @Jeff Glass - thanks for the example. I've found the problem - I'm hitting a CORS issue... and I had tried putting mode="no-cors" in the call to fetch but (now I know) that just provides an opaque response that hides the status code and body from the client-side javascript ๐Ÿ™‚ (edited)
might need a cors broker
Avatar
FabioRosado 28/12/2022 20:42
I was reading about cors the other day when working on fetching plugins. It seems chrome is particular picky with fetching things from localhost, it tends to give cors issues no matter what you use in the fetch options ๐Ÿ˜ฉ
Avatar
Yes - so I discovered! I worked around it with setting the allowed hosts in my local config... config strikes again! ๐Ÿ™‚
Avatar
Rafael Souza 29/12/2022 23:00
Hey all! I am not super versed in async functions and all... I am having a SUPER hard time with a function. I hope someone here can give a hint of what I am doing wrong. I have a refresh function attached to a py-click button refresh. After clicking it, it shows one single call in the network tab and that's it. It hangs and does nothing. I have looked around but didn't find a solution.
Avatar
Avatar
Rafael Souza
Hey all! I am not super versed in async functions and all... I am having a SUPER hard time with a function. I hope someone here can give a hint of what I am doing wrong. I have a refresh function attached to a py-click button refresh. After clicking it, it shows one single call in the network tab and that's it. It hangs and does nothing. I have looked around but didn't find a solution.
Jeff Glass 29/12/2022 23:08
You should await response.json()
23:09
On mobile and not near a computer to test, sorry! But thatโ€™s one thing that jumps out (edited)
Avatar
Rafael Souza 29/12/2022 23:14
It didn't work either... I am going to upload the code to github and share the link here so it's easier for you to test it.
๐Ÿ‘ 1
Avatar
Jeff Glass 29/12/2022 23:14
Brilliant, thank you!
Avatar
Rafael Souza 29/12/2022 23:19
A simple weather monitoring app. Contribute to rafaellucas3/weatherwatch development by creating an account on GitHub.
23:19
๐Ÿ™‚
Avatar
FabioRosado 30/12/2022 00:39
Im on the phone as well and didnโ€™t look much in-depth but can you try adding a __init__.py file in the code folder and add it to the fetch config as well? Since you are importing things such as data from main I wonder if maybe you are having issues with the imports. But again I havenโ€™t really tested it looked much in-depth at the code
Avatar
Rafael Souza 30/12/2022 03:45
still hanging with one call on the network console
03:46
thank you for the help ๐Ÿ˜ข
Avatar
Rafael Souza 30/12/2022 18:48
Did some digging here today... found out that if I try to save the json file inside the virtual filesystem it hangs and don't work. If I only append the jsons inside of a list it just works lol.
18:48
18:50
nvm I saw that my scrape_data function doesn't need to be async since I abstracted the request into a separate function... now I see a different error that was a silent before.
18:53
I would also get a CORS error if I had the header set to {"Content-type": "application/json"} (no idea why). This whole requests is sooo mind numbing BUT I still love all of this. I wish I knew more so I could collaborate to the project to make this easier.
Avatar
Rafael Souza 30/12/2022 20:49
Well I was able to make it work for the most part, this was entirely on me not really understanding how async functions work and specially with pyscript where there seem to be an async loop running in the background. The only thing I could not make work was saving the jsons in the virtual filesystem. My work around was basically use a list to save the results instead.
Avatar
Avatar
Rafael Souza
nvm I saw that my scrape_data function doesn't need to be async since I abstracted the request into a separate function... now I see a different error that was a silent before.
I've been hitting CORS too... application/json is not one of the content types allowed in a "simple" request. Allowed types are: application/x-www-form-urlencoded multipart/form-data text/plain Any other type triggers a pre-flight CORS check which is when the madness begins...
Avatar
mauj_mishra 02/01/2023 11:20
Hi guys, I am not able to run cv2.videoCapture on the pyscript. I have raised an issue at link - https://github.com/pyscript/pyscript/issues/1085. Please check it out
Home Page: https://pyscript.net Examples: https://pyscript.net/examples - Issues ยท pyscript/pyscript
Avatar
mauj_mishra 04/01/2023 07:19
Hi, can you please tell me how to render the image from cv2 to html. Because in cv2 the value of image is bunch on numpy arrays, so its causing an issue when rendering
Avatar
Avatar
mauj_mishra
Hi, can you please tell me how to render the image from cv2 to html. Because in cv2 the value of image is bunch on numpy arrays, so its causing an issue when rendering
Jeff Glass 04/01/2023 18:06
You might try Pillow's fromarray() method, to create a PIL image as in their documentation https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.fromarray
Avatar
So Iโ€™m a huge fan of pyscript overall itโ€™s great, but I do have one question thatโ€™s a pretty big blocker to using it in production Right now Iโ€™ve got a pretty basic app here: https://detect-a-quilt.vercel.app It uses a few libraries to do some image computation, but the big issue is that it requires 50MB+ of code to run, is there a way to tree shake or minify the code to be more reasonable?
Avatar
Avatar
Bryan
So Iโ€™m a huge fan of pyscript overall itโ€™s great, but I do have one question thatโ€™s a pretty big blocker to using it in production Right now Iโ€™ve got a pretty basic app here: https://detect-a-quilt.vercel.app It uses a few libraries to do some image computation, but the big issue is that it requires 50MB+ of code to run, is there a way to tree shake or minify the code to be more reasonable?
Jeff Glass 05/01/2023 03:42
Just from a quick glance - are you fetching some of the python packages twice? Curious why opencv and numpy appear twice in the network list.
Avatar
As far as Iโ€™m aware no, but am doing some tricks to get it working in react/next
Avatar
Jeff Glass 05/01/2023 03:53
Not really an immediate answer, but one thing being actively discussed over in the Pyodide GitHub is distributing the packages as compiled .pyc files instead of the .py files. Improvements in file size, but losses in readable tracebacks.
Avatar
Avatar
Jeff Glass
Not really an immediate answer, but one thing being actively discussed over in the Pyodide GitHub is distributing the packages as compiled .pyc files instead of the .py files. Improvements in file size, but losses in readable tracebacks.
Thatโ€™s a neat idea, itโ€™d kind of be equivalent of minifying the code that most javascript build tools do
04:45
Thereโ€™s another technique from javascript land called tree shaking which would also be neat to see here
Avatar
Hi. How can I fetch every file from a folder into pyscript for use in python?
Avatar
Avatar
arsh
Hi. How can I fetch every file from a folder into pyscript for use in python?
only python files ? if no wasm library or binary files i think that's the py-config tag that handles that, iirc the fetch section (edited)
Avatar
Avatar
arsh
Hi. How can I fetch every file from a folder into pyscript for use in python?
FabioRosado 07/01/2023 12:34
As @pmp-p mentioned you can use the fetch in your py-config. Hereโ€™s the link to the fetch reference docs: https://docs.pyscript.net/latest/reference/elements/py-config.html#a-name-fetch-fetch-a Thereโ€™s also a tutorial that shows you how to use it https://docs.pyscript.net/latest/tutorials/py-config-fetch.html Let us know if this helps ๐Ÿ˜„
benjiallen started a thread. 08/01/2023 12:43
Avatar
Hi I get this error when loading a python file using this. Does anyone know a quick fix?
Avatar
Jeff Glass 08/01/2023 14:03
From the โ€œfile://C:/devโ€ section of the error, it looks like youโ€™re opening a local file directly in your browser from the file system. Youโ€™ll want to use a simple server program to serve your files. python3 -m http.server from the base directory will work, or many IDEโ€™s have server extensions that serve the same purpose. (edited)
Avatar
brilliant thank you!
๐Ÿ‘ 1
Avatar
measure_zero 08/01/2023 17:56
Hi, is there an example showing two way JS/Python interaction in a Jupyterlite cell? Like handling events in Python, sending data back and forth etc? (edited)
Avatar
์˜์›ํ•œ์ฐฐ๋‚˜ 09/01/2023 08:15
Nice to meet you guys~ I need some help. How can I run a new run while initializing the results of the previous run when using pyscript.interpreter.runPythonAsync? If you run it several times, the values โ€‹โ€‹of previously used variables remain.
08:16
this is my code examples.
Avatar
Avatar
์˜์›ํ•œ์ฐฐ๋‚˜
Nice to meet you guys~ I need some help. How can I run a new run while initializing the results of the previous run when using pyscript.interpreter.runPythonAsync? If you run it several times, the values โ€‹โ€‹of previously used variables remain.
Jeff Glass 09/01/2023 11:59
Just clarify - do you want previously used variables to remain? Or do you want them to be uninitialized each time?
Avatar
Avatar
Jeff Glass
Just clarify - do you want previously used variables to remain? Or do you want them to be uninitialized each time?
์˜์›ํ•œ์ฐฐ๋‚˜ 09/01/2023 12:00
I want to run on new state(init state) always.
12:01
I always want to initialize
12:04
how to refresh instance or state? (pyscript or pyscript.interpreter instance)
Avatar
Avatar
์˜์›ํ•œ์ฐฐ๋‚˜
I always want to initialize
Jeff Glass 09/01/2023 12:09
One option would be to pass an empty dict to runPythonAsync to use as the globals dictionary. I believe this will cause only the __builtins__ to be available https://pyodide.org/en/stable/usage/faq.html#how-can-i-execute-code-in-a-custom-namespace (edited)
Avatar
Avatar
Jeff Glass
One option would be to pass an empty dict to runPythonAsync to use as the globals dictionary. I believe this will cause only the __builtins__ to be available https://pyodide.org/en/stable/usage/faq.html#how-can-i-execute-code-in-a-custom-namespace (edited)
์˜์›ํ•œ์ฐฐ๋‚˜ 09/01/2023 12:10
oh my god~ jeff Glass is my god~!
๐Ÿค— 1
12:10
very x 100 thank you
Avatar
Jeff Glass 09/01/2023 12:10
Note that the few things PyScript imports for you typically (display(), the HTML class, and the pyscript module) may need to be imported in your script as well (edited)
12:10
Happy to help!
Avatar
Hello , I just wanted to ask , can i create HTML elements and then append it to an element as a child directly from pyscript?
Avatar
i did this , but does not work , from js import document list = document.createElement("li"); anchor = document.createElement("a"); list.appendChild(anchor) Element(anchor).write("Test") document.getElementById("lists").appendChild(list) (edited)
Avatar
Jeff Glass 12/01/2023 06:14
It is; one thing that sticks out here is that the Element constructor takes an element id https://github.com/pyscript/pyscript/blob/5a3c414c8f3f0bc1ecaa473f117a06a7e43762cc/pyscriptjs/src/python/pyscript.py#L221
โค๏ธ 1
Avatar
mason norberg 15/01/2023 20:51
I'm trying to import a path into pyscript using <py-config> and its not working. but when I use the same code on a different html file the file the importing works perfectly.
Avatar
Avatar
mason norberg
I'm trying to import a path into pyscript using <py-config> and its not working. but when I use the same code on a different html file the file the importing works perfectly.
Jeff Glass 15/01/2023 22:38
Can you share your py-config code? Iโ€™m not sure what you mean by โ€œusing the same code on a different HTML fileโ€
Avatar
mason norberg 15/01/2023 23:22
Working Code <py-config> packages = ['Pillow'] paths = ['Roboto-Black.ttf'] </py-config> Not Working code <py-config> packages = ['Pillow'] paths = ['Roboto-Black.ttf'] </py-config> (edited)
Avatar
Avatar
Jeff Glass
Can you share your py-config code? Iโ€™m not sure what you mean by โ€œusing the same code on a different HTML fileโ€
mason norberg 15/01/2023 23:23
^^
Avatar
Jeff Glass 15/01/2023 23:35
Are they using different versions of PyScript? In version 2022.12.1, paths was replaced by fetch configurations: https://docs.pyscript.net/latest/reference/elements/py-config.html#a-name-fetch-fetch-a
23:36
Which version of Pyscript are you using?
Avatar
mason norberg 16/01/2023 03:33
That was the issue, I was more than likely using an older version but now itโ€™s all working fine
๐Ÿ‘ 1
Avatar
<button id="Submit" py-onClick()="initial">Submit</button> but the function below is not getting called: def initial(*args, **kwargs): pyscript.write("output", "Hello") I am really new to this, spent a lot of time on trying to solve this issue. Would be great if someone could help me out on this. Thanks in advance. (edited)
Avatar
Jeff Glass 17/01/2023 19:11
@VoRt3X Hey Vortex - the syntax is py-click = "initial('foo', 'bar')", Note that the name of the attribute is py-*, where * is the name of a browser event, and the value is a string of executable Python code
๐Ÿ‘ 1
19:15
The docs for that are a known area of improvement - we had a PR (#852) that's still stagnating, and I believe @FabioRosado is currently actively writing-rewriting the docs
Avatar
Hello I am using an external lib called tinydb, it's a json database, anyway after even adding it to the env it's still saying this
20:15
Avatar
Avatar
brayden
Hello I am using an external lib called tinydb, it's a json database, anyway after even adding it to the env it's still saying this
Jeff Glass 17/01/2023 21:42
It sounds like (and guessing from the visible tinydb on the page) you're using <py-env>, which was deprecated in version 2022.11.1 and removed in version 2022.12.1. The tag is now called <py-config>, as in: <py-config> packages = ['tinydb'] </py-config>
Avatar
Thanks
Avatar
Avatar
Jeff Glass
It sounds like (and guessing from the visible tinydb on the page) you're using <py-env>, which was deprecated in version 2022.11.1 and removed in version 2022.12.1. The tag is now called <py-config>, as in: <py-config> packages = ['tinydb'] </py-config>
(PY1001): Unable to install package(s) 'tinydb,datetime,pytz,time,os'. Reason: Can't find a pure Python 3 Wheel for package(s) 'tinydb,datetime,pytz,time,os'. See: https://pyodide.org/en/stable/usage/faq.html#micropip-can-t-find-a-pure-python-wheel for more information.
23:05
I tried the solution on the webpage
23:05
let zipResponse = await fetch("myfiles.zip"); let zipBinary = await zipResponse.arrayBuffer(); pyodide.unpackArchive(zipBinary, "zip");โ€™โ€™ (edited)
Avatar
Avatar
brayden
(PY1001): Unable to install package(s) 'tinydb,datetime,pytz,time,os'. Reason: Can't find a pure Python 3 Wheel for package(s) 'tinydb,datetime,pytz,time,os'. See: https://pyodide.org/en/stable/usage/faq.html#micropip-can-t-find-a-pure-python-wheel for more information.
Jeff Glass 17/01/2023 23:56
Can you paste your py-config code here?
Avatar
Avatar
Jeff Glass
Can you paste your py-config code here?
<py-config> packages = ["tinydb", "pytz"] </py-config>
00:00
I fixed tinydb
00:00
But a couple other libraries aren't working
00:01
like datetime
Avatar
Avatar
brayden
like datetime
Jeff Glass 18/01/2023 00:36
datetime is in the standard lib, no need to install it. Just import datetime
00:43
Same with time and os
Avatar
Avatar
Jeff Glass
datetime is in the standard lib, no need to install it. Just import datetime
Thank you, while we're here I am getting this error
00:50
Traceback (most recent call last): File "/lib/python3.10/site-packages/_pyodide/_base.py", line 435, in eval_code .run(globals, locals) File "/lib/python3.10/site-packages/_pyodide/_base.py", line 304, in run coroutine = eval(self.code, globals, locals) File "<exec>", line 136, in <module> IndexError: list index out of range
00:50
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
00:50
code
Avatar
I fixed the error
01:10
now it just doesn't update the database
Avatar
Jeff Glass 18/01/2023 01:51
Hmm so not entirely sure the breadth of it, but one thing - time.sleep() will hang forever. With no pre-emptive multitasking/threads, we have no way to say to the โ€œkernelโ€: โ€œHey, please suspend my call stack and start it up again laterโ€ (edited)
01:53
However, coroutines (cooperative multitasking) do work - you can turn the functions into coroutines using async def, using await asyncio.sleep(), and schedule them using one of the loop methods: https://jeff.glass/post/pyscript-asyncio/ (edited)
Avatar
Avatar
Jeff Glass
Hmm so not entirely sure the breadth of it, but one thing - time.sleep() will hang forever. With no pre-emptive multitasking/threads, we have no way to say to the โ€œkernelโ€: โ€œHey, please suspend my call stack and start it up again laterโ€ (edited)
Iโ€™ll look into it. Also just one thing
02:23
I assume itโ€™s supposed to say science but I was just pointing it out
๐Ÿ˜… 1
Avatar
Jeff Glass 18/01/2023 02:27
Thanks!
02:28
Itโ€™s certainly not a proofreading museum ๐Ÿ˜ฌ
Avatar
Pyscript initialization was getting stuck for me on mobile at Setting up virtual environment.... I managed to track it down to pyodide.runPython just freezing on iOS 15.4.1 Safari, although it's working on all other phones I tested. Switching Pyodide versions 0.21.3 to 0.22.0 made it work on my phone and was wondering when we could expect an update?
22:26
Also wanted to ask if there's a way we could access specific pyscript versions, like https://pyscript.net/2022.12.1/pyscript.js
Avatar
Avatar
Phil25
Also wanted to ask if there's a way we could access specific pyscript versions, like https://pyscript.net/2022.12.1/pyscript.js
Jeff Glass 19/01/2023 22:35
There is! The url per release looks like https://pyscript.net/releases/{version}/pyscript.js
22:36
So in your example, https://pyscript.net/releases/2022.12.1/pyscript.js
Avatar
Awesome thanks ๐Ÿ˜„
Avatar
Jeff Glass 19/01/2023 22:38
Also really interesting about it freezing on a specific version of safari/iOSโ€ฆ was it freezing for all Python code, like print(โ€œhello world?โ€)? If itโ€™s indeed a Pyodide issue Iโ€™m sure theyโ€™d love to know about it.
Avatar
Guess I could open an issue, but it does seem to work on the latest version
22:40
And yeah, even an empty call was just freezing the page
Avatar
Can anyone help me on this? I did create a venv and installed the module and did change the interpreter. So the code in .py is showing no error but on running the HTML file it's showing like this. (edited)
Avatar
Avatar
VoRt3X
Can anyone help me on this? I did create a venv and installed the module and did change the interpreter. So the code in .py is showing no error but on running the HTML file it's showing like this. (edited)
FabioRosado 20/01/2023 14:24
Can you share your code by any chance? That will help, but you mentioned that you created a venv. PyScript runs in the browser and not in your local machine like a python session. If you aren't adding the dependency to pyscript, you will have to do such with: <py-config> packages = ["prettytable", ] </py-config> You may need to add all the other dependencies that you may be using.
14:24
Another thing important to note is that not all packages may be available. Quite a lot of them have been ported to run in the browser using WASM and some packages that have a pure python wheel can be used as well (edited)
Avatar
Thanks for your reply. I will share you the code.
Avatar
Is the issue with the module 'pretty table'? Or should I install the module outside venv? (edited)
Avatar
FabioRosado 20/01/2023 19:02
I'm having some issues with mounting a IDBF file system. I can write files using FS.writeFile but if I use python's open to write the file, it doesn't get added into the file system ๐Ÿค” Any ideas? Maybe @Jeff Glass
Avatar
Avatar
FabioRosado
I'm having some issues with mounting a IDBF file system. I can write files using FS.writeFile but if I use python's open to write the file, it doesn't get added into the file system ๐Ÿค” Any ideas? Maybe @Jeff Glass
Jeff Glass 20/01/2023 21:37
Hmmm I don't have a good answer here. There's some implications in the Pyodide tests that FS.syncfs() is async? https://github.com/pyodide/pyodide/blob/main/src/tests/test_filesystem.py (edited)
Avatar
Jeff Glass 20/01/2023 21:56
Here's the script I'm testing with (on pyscript unstable fwiw). It hangs on FS.syncfs(True...)... but in the Chrome developer tools I can see it being written to the indexedDB (application tab)
21:57
import pyodide_js import pyodide import js import asyncio from datetime import datetime FS = pyodide_js.FS def j(d: dict): return pyodide.ffi.to_js(d, dict_converter=js.Object.fromEntries) def error(err): js.console.log(err) mount_dir = '/data' print("hi") async def synctest(writeonce = False): if not FS.isDir(mount_dir): FS.mkdir(mount_dir) FS.mount(FS.filesystems.IDBFS, j({}), mount_dir) if not writeonce: js.console.log("Reading from FS") await FS.syncfs(True, pyodide.ffi.create_proxy(error)) #True => load from file system js.console.log("done") js.console.log("Writing to embedded fs") filename = mount_dir + '/' + ''.join([char for char in str(datetime.now()) if char.isalnum()]) + '.txt' with open(filename, 'w') as fp: fp.write(f"Written at {filename}") js.console.log("done") js.console.log(f"{FS.readdir(mount_dir)= }") js.console.log("Syncing to IDBFS") await FS.syncfs(False, pyodide.ffi.create_proxy(error)) js.console.log("done") asyncio.ensure_future(synctest(writeonce=False))
Avatar
I am trying to do translations form my webapp. I am calling the function from my if name check. the probelm is that I would like to maker the function async so that I can all it at a later time without blocking. I'm trying to avoid having to do a page refresh as the pyodide environment can take a significant amount of time to load. (low power mobile devices can take 30-40 seconds)
15:15
I really need to await this function as it sets a global variable, but using something like asyncio.gather or ensure_future without an await exectuets the next line of code before the global variable is defined and errors out.
Avatar
@Jeff Glass You need to make a new promise, something like: from pyodide.code import run_js from pyodide_js import FS tmp = run_js("(FS, dir) => () => new Promise((res) => FS.syncfs(dir, res))") load_from_disk = tmp(FS, True) save_to_disk = tmp(FS, False) await load_from_disk() # ... await save_to_disk()
Avatar
Avatar
Jeff Glass
Here's the script I'm testing with (on pyscript unstable fwiw). It hangs on FS.syncfs(True...)... but in the Chrome developer tools I can see it being written to the indexedDB (application tab)
FabioRosado 21/01/2023 20:22
Thank you let me give it a try and see. checked pyodide's tests and there's some test using Path to write to a file. Maybe I need to do the invalidate cache trick ๐Ÿค” The tests do have it there. using writeFile seems to work, but now i can't load it on refresh. From your code I think I missed some important steps haha
Avatar
I don't understand why the code Jeff pasted above doesn't give TypeError:ย objectย NoneTypeย can'tย beย usedย inย 'await'ย expression. I don't think invalidate_caches should matter here
Avatar
Jeff Glass 21/01/2023 22:04
Thanks @hood, wrapping the operation in a Promise works perfectly! Much appreciated. @FabioRosado Here's my above code again but working, with Hood's 'tmp' incorporated (as op_factory). Each time the page loads, it loads the existing files from IDBFS, prints them, writes one more, syncs back to IDBGFS, and prints everything again. (edited)
1.48 KB
Avatar
Avatar
tuxprint
I am trying to do translations form my webapp. I am calling the function from my if name check. the probelm is that I would like to maker the function async so that I can all it at a later time without blocking. I'm trying to avoid having to do a page refresh as the pyodide environment can take a significant amount of time to load. (low power mobile devices can take 30-40 seconds)
Jeff Glass 21/01/2023 22:57
@tuxprint is there a chance you could share a code snippet? It sounds like you maybe want to create a future or task that you manually manage the lifetime of by later cancelling it?
Avatar
Spent way too much time on this than I should have, so if anyone tries to look it up, here's a simple debounce decorator in pyscript def debounce(delay: float): def decorator(func): def wrapper(*args, **kwargs): if hasattr(wrapper, "_handle"): wrapper._handle.cancel() to_call = functools.partial(func, *args, **kwargs) wrapper._handle = pyscript.loop.call_later(delay, to_call) return wrapper return decorator (edited)
Avatar
Avatar
Phil25
Spent way too much time on this than I should have, so if anyone tries to look it up, here's a simple debounce decorator in pyscript def debounce(delay: float): def decorator(func): def wrapper(*args, **kwargs): if hasattr(wrapper, "_handle"): wrapper._handle.cancel() to_call = functools.partial(func, *args, **kwargs) wrapper._handle = pyscript.loop.call_later(delay, to_call) return wrapper return decorator (edited)
tedpatrick 23/01/2023 12:42
Thoughts on decorators for debouncing and events? Looking to get this in as a new proposal for pyscript import js from pyscript.events import handler,debounce @handler @debounce def foo(): js.console.log("foo called") @handler @debounce def boo(e): js.console.log("boo called with event") js.console.log( e ) @handler @debounce def loo(*e): js.console.log("loo called with *event") js.console.log( e[0] ) js.console.log( e[1] ) @handler(event="click") @debounce def doc_click(*e): js.console.log("doc_click called with *event") js.console.log( e[0] ) (edited)
Avatar
This looks good, how would @handler be used?
13:18
Would be cool to have something like @handler(id="some-elem", event="click") which does add_event_listener underneath (edited)
โœ… 1
Avatar
Avatar
Jeff Glass
@tuxprint is there a chance you could share a code snippet? It sounds like you maybe want to create a future or task that you manually manage the lifetime of by later cancelling it?
sorry for the delayed response. I have fixed the error by refactoring my code a bit
๐Ÿ‘ 1
Avatar
Is is normal for a disabled button to still register clicks and execute py-onclick?
Avatar
Avatar
tuxprint
Is is normal for a disabled button to still register clicks and execute py-onclick?
Jeff Glass 25/01/2023 22:00
That's not the behavior I'm seeing with the following code: <button py-click="logHello()" disabled>Click me</button> <py-script> def logHello(): import js js.console.log("HI!") </py-script>
22:01
Where are we differing?
22:02
Cause either way, that doesn't sound like desirable behavior for pyscipt ๐Ÿ˜ฌ
Avatar
Avatar
Jeff Glass
That's not the behavior I'm seeing with the following code: <button py-click="logHello()" disabled>Click me</button> <py-script> def logHello(): import js js.console.log("HI!") </py-script>
yes, I was thinking that was weird sice I have other places where it is working. but I realize. the other place is disabled by default, this place is enabled by default then set to disabled in python code
Avatar
Avatar
Jeff Glass
Cause either way, that doesn't sound like desirable behavior for pyscipt ๐Ÿ˜ฌ
I would say lol
22:04
it visually looks like a disabled button, but clicks still activate my function
Avatar
Jeff Glass 25/01/2023 22:04
Huh odd, this successfully disables the button for me: <button py-click="logHello()" id="btn">Click me</button> <py-script> import js js.document.getElementById("btn").disabled = True def logHello(): js.console.log("HI!") </py-script> (edited)
22:05
I.e. it starts enabled, then once PyScript has finished initializing, it gets styled to disabled and stops emitting events on click
Avatar
hmm. this is being disabled by a function. that one that runs when you click the button
22:07
if I click the button 5 times it will run the function all 5 times even though the button is disabled for 4 of them. other weirdness is that I add a check to return if the button is disabled, and it will come back false even thought the button has been disabled
Avatar
Jeff Glass 25/01/2023 22:07
Now that's extra weird
22:07
Got a code snippet you can share?
22:07
Cuz that is... very odd indeed
22:08
(Getting in the car now, will loop back later)
Avatar
<button class="send-tab-send-button" id="send-tab-send-button" py-onclick="sendTx()" type="button">Send</button> async def sendTx(): send_button = Element("send-tab-send-button") if send_button.element.disabled: print("hello") send_button.element.disabled = True this will never print hello ๐Ÿ˜ฆ
Avatar
idk, I cant seem to recreate this in other code. but the code in my project for some reason has the behavior I was describing
Avatar
Avatar
tuxprint
<button class="send-tab-send-button" id="send-tab-send-button" py-onclick="sendTx()" type="button">Send</button> async def sendTx(): send_button = Element("send-tab-send-button") if send_button.element.disabled: print("hello") send_button.element.disabled = True this will never print hello ๐Ÿ˜ฆ
the reason why is because send_button.element.disabled is always False... unless when you set it to True like you're doing in the last line same behaviour in JS: function jssendTx() { let sendButton = document.getElementById("send-tab-send-button"); if (sendButton.disabled) { console.log("hello"); } sendButton.disabled = true; } I think you might want a different thing in this function (edited)
Avatar
Avatar
3l3ktr4
the reason why is because send_button.element.disabled is always False... unless when you set it to True like you're doing in the last line same behaviour in JS: function jssendTx() { let sendButton = document.getElementById("send-tab-send-button"); if (sendButton.disabled) { console.log("hello"); } sendButton.disabled = true; } I think you might want a different thing in this function (edited)
it's a somewhat complex function. I do reenable it further down. but that example I posted works as expected. its very frustrating that it is not working as expected in my code though
Avatar
ah I see
Avatar
Is there a way to set environment variables? As in, if a library can only except an environement variable as input for something, how would you set that environement variable?
Avatar
Avatar
k01e
Is there a way to set environment variables? As in, if a library can only except an environement variable as input for something, how would you set that environement variable?
Jeff Glass 30/01/2023 12:44
Could you do: import os os.environ["SOME_VAR"] = โ€œvalueโ€ import lib_that_needs_env_vars โ€ฆ (edited)
Avatar
Ill try it! Thanks
๐Ÿ‘ 1
Avatar
Marcos7765 30/01/2023 22:49
Hi there, i'm trying to output mpl graphs into a div with display() in a function such that whenever i call the function, the new graph would overwrite the previous. I run this function when the script loads and as the py-change callback of some inputs. When i use append=False the py-change graphs get put inside the first graph <image> tag and i can't use Element.clear() to get rid of this first image. I've managed to do it with Element.clear() and display() with append=True, but how do i make it with append=False?
Avatar
Avatar
Marcos7765
Hi there, i'm trying to output mpl graphs into a div with display() in a function such that whenever i call the function, the new graph would overwrite the previous. I run this function when the script loads and as the py-change callback of some inputs. When i use append=False the py-change graphs get put inside the first graph <image> tag and i can't use Element.clear() to get rid of this first image. I've managed to do it with Element.clear() and display() with append=True, but how do i make it with append=False?
Jeff Glass 30/01/2023 23:58
Interesting - this sounds very similar to a bug that was fixed in dev just this week. If youโ€™re using /latest, would you try pointing your script tag at https://pyscript.net/unstable/pyscript.js and seeing if using append = False works?
Avatar
Avatar
Jeff Glass
Interesting - this sounds very similar to a bug that was fixed in dev just this week. If youโ€™re using /latest, would you try pointing your script tag at https://pyscript.net/unstable/pyscript.js and seeing if using append = False works?
Marcos7765 31/01/2023 00:14
It worked
Avatar
Jeff Glass 31/01/2023 00:16
Gotcha - I suspect itโ€™s the same as this issue: https://github.com/pyscript/pyscript/pull/1126
00:16
So the fix will roll out with the next release - in the meantime, your workaround is I think as good as any (edited)
Avatar
Deleted User 31/01/2023 16:57
did you fellas already figure out something on how to implement web3
Avatar
Avatar
Deleted User
did you fellas already figure out something on how to implement web3
Jeff Glass 31/01/2023 17:22
If memory serves from our last convo, web3.py makes significant use of pythonโ€™s requests module as well as sockets. For shimming requests, you could look at pyodide-http
Avatar
Hey! 100% new to Pyscript and python, and wanted to ask if this is possible to do. When a button is pressed (in html) start a JS function, that calls on a Pyscript/python script ๐Ÿ™‚
Avatar
Avatar
.kennedyb
Hey! 100% new to Pyscript and python, and wanted to ask if this is possible to do. When a button is pressed (in html) start a JS function, that calls on a Pyscript/python script ๐Ÿ™‚
Jeff Glass 01/02/2023 15:29
It's absolutely possible - it's one the things PyScript/Pyodide is best at.๐Ÿ˜ (edited)
15:31
Just to ask - is your end goal to have a python function called, or to call a JS function and within that call on a Python function? If you just want to run some Python, there's a way to do that more-or-less directly without worrying about JS, if you prefer (edited)
15:34
You can use the py-[event] syntax to call Python functions when the matching event if trigged by an element. For example, with the event "click": <!-- pyscript 2022.12.1 --> <button py-click="helloWorld()" id="myBtn">Say Hi</button> <py-script> def helloWorld(): print("hello, world!") </py-script> (edited)
15:36
If you do want to run some JS as well, here's how to get a reference to a Python function from PyScript into JavaScript, and execute it using JS's onClick syntax: <!-- pyscript 2022.12.1 --> <button onClick="javascriptSaysHi()">Say Hi</button> <script> function javascriptSaysHi(){ console.log("You could do other things with JS here"); const pythonFunc = pyscript.runtime.globals.get("helloWorld"); pythonFunc(); } </script> <py-script> def helloWorld(): print("hello, world!") </py-script> (edited)
15:38
Note that pyscript.runtime isn't a valid object until the Interpreter has loaded - if you try to get that attribute before PyScript has completed loading, you'll get a "No attribute called runtime..." error. But with a button, which the user probably isn't clicking until PyScript has loaded, you should be ok? May be something you have to account for though. (edited)
Avatar
Avatar
Jeff Glass
You can use the py-[event] syntax to call Python functions when the matching event if trigged by an element. For example, with the event "click": <!-- pyscript 2022.12.1 --> <button py-click="helloWorld()" id="myBtn">Say Hi</button> <py-script> def helloWorld(): print("hello, world!") </py-script> (edited)
Doesnโ€™t py-click require id to be defined?
Avatar
Avatar
Phil25
Doesnโ€™t py-click require id to be defined?
Jeff Glass 01/02/2023 15:42
Ohh, excellent point. I was testing my demos with the latest dev version (where ID's are automatically generated if they're not given), but in 2022.12.1 you're absolutely right
15:42
Let me update the examples above...
15:44
Similarly, in 2022.12.1, the Pyodide interface was called runtime, but that's being renamed to interpreter in the next release; fixed that in the example too
15:44
Thanks @Phil25 ! Excellent catch (edited)
Avatar
Avatar
Jeff Glass
Ohh, excellent point. I was testing my demos with the latest dev version (where ID's are automatically generated if they're not given), but in 2022.12.1 you're absolutely right
Oh awesome, thatโ€™s nice
Avatar
Thanks! ๐Ÿ™‚ Is it possible to run some sort of "mouse macro" like pyautogui inside the browser (that start with a button press)? ๐Ÿ™‚
Avatar
Avatar
.kennedyb
Thanks! ๐Ÿ™‚ Is it possible to run some sort of "mouse macro" like pyautogui inside the browser (that start with a button press)? ๐Ÿ™‚
Jeff Glass 01/02/2023 15:50
Hmmm now that I don't know. From just a little googling, I don't think you can move the position of the mouse/pointer itself with JavaScript/PyScript, the browser just won't allow it for security reasons. You can imagine what a malicious website might do if they could seize control of the mouse itself... (edited)
15:51
You can often use object.click() to click on various elements of the DOM, though, just not move the "physical" mouse pointer
Avatar
I donโ€™t think a browser would allow websites to hijack the cursor and click things. If you wanna do it for your site only then @Jeff Glass suggestion is the way to go
Avatar
hi! we're building a library in Rust and we'd like to call it from PyScript, I understand that would mean compiling it to WASM, but then what? ๐Ÿ™‚
Avatar
Presumably you are using pyo3? You can use Pyodide's out of tree build system to build a wheel. This usually is pretty simple: 1. pip install pyodide-build 2. git clone https://github.com/emscripten-core/emsdk.git 3. ./emsdk/emsdk install $(pyodide config get emscripten_version) 4. source emsdk/emsdk_env.sh 5.pyodide build This should build a wheel. Then you need to serve the wheel and put the wheel url in py-config, and you should be able to import and use it. (edited)
Avatar
what are the instances where you have to build a wheel, @hood ? last week I simply used emscripten, compiled it to wasm, served both the js and the wasm file and imported them in pyscript and it just worked
Avatar
Avatar
hood
Presumably you are using pyo3? You can use Pyodide's out of tree build system to build a wheel. This usually is pretty simple: 1. pip install pyodide-build 2. git clone https://github.com/emscripten-core/emsdk.git 3. ./emsdk/emsdk install $(pyodide config get emscripten_version) 4. source emsdk/emsdk_env.sh 5.pyodide build This should build a wheel. Then you need to serve the wheel and put the wheel url in py-config, and you should be able to import and use it. (edited)
ah okay thanks!!
Avatar
Avatar
3l3ktr4
what are the instances where you have to build a wheel, @hood ? last week I simply used emscripten, compiled it to wasm, served both the js and the wasm file and imported them in pyscript and it just worked
Generally I would recommend making wheels out of any Python packages. I guess I would have to hear in more detail what you did and what you were trying to do. There are some cases where it can make sense to build a completely separate Emscripten module from an application, but in that case Pyodide and your built module will have completely separate address spaces, so a pointer produced by one cannot be dereferenced by the other (you can still pass it over as an "opaque pointer" but it has to come back before it can be used). This makes it impossible to link Python modules in the normal way since the Python C API expects objects to return pointers. So if it's a Python module or if it needs to use any Python APIs it must be dynamically loaded into the Pyodide module.
โœจ 2
kawamate1 1
Avatar
aha makes a lot of sense ๐Ÿ™‚
17:31
thanks!
Avatar
Avatar
3l3ktr4
what are the instances where you have to build a wheel, @hood ? last week I simply used emscripten, compiled it to wasm, served both the js and the wasm file and imported them in pyscript and it just worked
what did you compile with emscripten?
Avatar
just a test software I wrote but I'm looking for smth cool to do a real life test
Avatar
Hi @thepeak !
19:58
Hey guys, @thepeak is from Jolocom and I encouraged them to look at integrating the decentralized identity tech with Pyscript
kawamate1 1
๐Ÿ‘‹ 1
Avatar
Avatar
3l3ktr4
just a test software I wrote but I'm looking for smth cool to do a real life test
yes, but what it compiles to outside of webassembly? An executable? A shared library? A Python extension module?
Avatar
c program!
Avatar
Hi I'm kinda new to web development and since I'm experienced with Python so I want to try it out PyScript. Just wondering how do I make subdirectories for my website? Or it is even related to PyScript? e.g. : mysite.com/test (edited)
Avatar
Avatar
qing
Hi I'm kinda new to web development and since I'm experienced with Python so I want to try it out PyScript. Just wondering how do I make subdirectories for my website? Or it is even related to PyScript? e.g. : mysite.com/test (edited)
FabioRosado 10/02/2023 13:17
Hello ๐Ÿ‘‹ Providing that you are serving the same site and you are using html files, if you create a folder and add a index.html inside it, you can navigate to the page. If you want to serve a completely different domain into /test then you need to create some dns records for it
Avatar
Does anyone know a way to set the output value of a python script (via the pyscript Home Assistant integration), into a HA text helper. I have pyscripts that can set my LG TVs brightness, but I don't want to keep running the script if the TV s already at the right brightness. So I have another script that can simply print out the current brightness. I can't seem to find a way to read that number into HA as a variable/helper that I can use to determine whether to run the setter script.
Avatar
Avatar
mark007
Does anyone know a way to set the output value of a python script (via the pyscript Home Assistant integration), into a HA text helper. I have pyscripts that can set my LG TVs brightness, but I don't want to keep running the script if the TV s already at the right brightness. So I have another script that can simply print out the current brightness. I can't seem to find a way to read that number into HA as a variable/helper that I can use to determine whether to run the setter script.
Jeff Glass 10/02/2023 23:32
When you say the PyScript home assistant integration, are you referring to this? https://hacs-pyscript.readthedocs.io/en/latest/
23:32
If so, that is a different โ€œPyScriptโ€ ๐Ÿ˜‡. This discord is centered around this: https://pyscript.net
Run Python code in your HTML.
Avatar
ohhhh my apologies, you are right
Avatar
Avatar
FabioRosado
Hello ๐Ÿ‘‹ Providing that you are serving the same site and you are using html files, if you create a folder and add a index.html inside it, you can navigate to the page. If you want to serve a completely different domain into /test then you need to create some dns records for it
So meaning that I would have different index.html in different folder? Like I would have a index.html file in the test folder for a subdirectory /site and a index.html in the main one?
Avatar
Avatar
qing
So meaning that I would have different index.html in different folder? Like I would have a index.html file in the test folder for a subdirectory /site and a index.html in the main one?
FabioRosado 11/02/2023 07:46
Yes thatโ€™s correct and you would need to load pyscript on each page. If you donโ€™t wish to do that you could use vue to create a single page app. I hope this is not going too in-depth into web dev stuff ๐Ÿ˜„ If it helps I started a vue starter, I still need to improve the readme and the starter but perhaps this gives you some idea https://github.com/FabioRosado/pyscript-vue-starter
A starter template for Pyscript using Vue 3. Contribute to FabioRosado/pyscript-vue-starter development by creating an account on GitHub.
Avatar
Avatar
FabioRosado
Yes thatโ€™s correct and you would need to load pyscript on each page. If you donโ€™t wish to do that you could use vue to create a single page app. I hope this is not going too in-depth into web dev stuff ๐Ÿ˜„ If it helps I started a vue starter, I still need to improve the readme and the starter but perhaps this gives you some idea https://github.com/FabioRosado/pyscript-vue-starter
Alright thanks a lot.
Avatar
FabioRosado 11/02/2023 09:25
Iโ€™ll play around with something else today which may make it easier to create single page applications. But please do let me know if you need help
Avatar
Avatar
FabioRosado
Iโ€™ll play around with something else today which may make it easier to create single page applications. But please do let me know if you need help
Avatar
this is cool
10:47
ive been approaching it the opposite way trying to get npm packages imported into my pyscript
Avatar
socket = WebSocket.new(url) socket.binaryType = 'arraybuffer' socket.onopen = create_proxy(socket_connected)
12:00
how can i send a param to socket_connected function?
12:05
this is the java version im trying to implement in pyscript
12:05
socket.onopen = function (e) { socket.send(username+":" + response.ticket + "\n"); terminalState = terminalStates.connected; };
Avatar
ahh i see. you can pass parameters to the function create_proxy returns
Avatar
although i cant seem to pass socket as a reference
Avatar
ah never mind. cant pass anything using create_proxy. i was just calling the method immediately by doing what i was doing
Avatar
Is create_proxy needed with pyodide wrappers?
13:50
A lot of pyscript tutorials Iโ€™ve found use it but they can be simplified with the wrappers instead
13:53
Figured itโ€™s outdated info, but @hosler checkout pyodide wrappers of you havenโ€™t already
13:53
I could be wrong ofc, maybe your case is very specific
Avatar
ok. for now im just using global vars
Avatar
Avatar
Phil25
Is create_proxy needed with pyodide wrappers?
Jeff Glass 12/02/2023 14:04
I did a write up on create_proxy, and why itโ€™s necessary, and the wrappers at one point at one point: https://jeff.glass/post/pyscript-why-create-proxy/
Pyodide's create_proxy function explained
shock 1
14:05
Though I believe that most PyProxyโ€™s are successfully cleaned during JS garbage collection, unless thereโ€™s a reference cycle between py/js
Avatar
right now im just trying to figure out why everything is printing twice
๐Ÿ‘€ 1
14:05
but thats probably some xtermjs setting
Avatar
Jeff Glass 12/02/2023 14:10
Huh! Thatโ€™s odd
Avatar
Hello, is it possible to send messages to the terminal without the script having to stop? I can't find a way to display text at each print of my python code... All the text is sent at once when the script stops, which is a bit problematic because we have to enter a value each time and it has to return a certain sentence to continue the operation.
Avatar
Just a quick question: Does anyone know how to run a function as soon as pyscript loads completely? not just when the dom loads but when pyscript outputs [pyscript/main] PyScript page fully initialized
Avatar
Avatar
Jake
Just a quick question: Does anyone know how to run a function as soon as pyscript loads completely? not just when the dom loads but when pyscript outputs [pyscript/main] PyScript page fully initialized
Jeff Glass 12/02/2023 18:21
So to be clear, this is after the existing py-script tags on the page have finished running, yea? Assuming theyโ€™re not doing async/coroutine-y stuff? That message is printed to the DOM after the scripts complete, if memory serves
Avatar
Avatar
Jeff Glass
So to be clear, this is after the existing py-script tags on the page have finished running, yea? Assuming theyโ€™re not doing async/coroutine-y stuff? That message is printed to the DOM after the scripts complete, if memory serves
Jeff Glass 12/02/2023 18:28
Possibly the cleanest way would be to use a Plugin which implements the afterStartup() method https://github.com/pyscript/pyscript/blob/25809660efab80046d75314b14c08ff2c5d6194e/pyscriptjs/src/main.ts#L197
18:29
Fabio R created a good guide on Plugins - in the most recent stable release (2022.12.1), only python plugins are possible - JS plugins are in the dev builds, and will be included in the next release https://docs.pyscript.net/unstable/guides/custom-plugins.html
Avatar
@Jeff Glass thank you for the help ๐Ÿ˜„
Avatar
I'm making and python text editor website. Does anyone know how to execute a string of python code at runtime from a worker? My original way was to create a py-script element and append it to the document but that requires access to the DOM which workers don't have.
Avatar
Avatar
Jake
I'm making and python text editor website. Does anyone know how to execute a string of python code at runtime from a worker? My original way was to create a py-script element and append it to the document but that requires access to the DOM which workers don't have.
in there there's a worker based repl https://github.com/python/cpython/tree/main/Tools/wasm, i'm sure you can adapt for an editor
Avatar
Jeff Glass 14/02/2023 12:09
The Pyodide docs also have a solid write up of running Pyodide in a web worker: https://pyodide.org/en/stable/usage/webworker.html
Avatar
and for micropython i guess just multiplex standard output and batched dom requests on stdout fd ( or maybe other fd when using WasmFS - untested from me) (edited)
Avatar
Can someone explain to me why manually adding a <py-terminal> in Svelte results in its not rendering? The odd thing is <py-script> seems to be working just fine.
Avatar
Avatar
Duy
Can someone explain to me why manually adding a <py-terminal> in Svelte results in its not rendering? The odd thing is <py-script> seems to be working just fine.
Jeff Glass 15/02/2023 02:57
Does <py-terminal auto><py-terminal> change the behavior at all?
Avatar
Avatar
Jeff Glass
Does <py-terminal auto><py-terminal> change the behavior at all?
Yes it sort of does, but I should have been more explicit with my issue. I am trying to build a Python Editor using Svelte with SvelteKit. My current solution so far is injecting <py-script> and <py-terminal> as HTML expression into the Svelte component. My initial post was asking why <py-terminal> did not render without in Svelte without being a HTML expression. Sorry should have been more cleared.
Avatar
Avatar
Chronos
Hello, is it possible to send messages to the terminal without the script having to stop? I can't find a way to display text at each print of my python code... All the text is sent at once when the script stops, which is a bit problematic because we have to enter a value each time and it has to return a certain sentence to continue the operation.
could someone help me?
Avatar
hey guys - i'm new to pyscript. for some reason the fetches in my py-config are showing on the webpage when i run the website. not sure if this is meant to happen or if i may be doing something wrong?
Jeff Glass started a thread. 16/02/2023 16:22
Avatar
is it currently possible to use gensim with pyscript? -> I get this error: ImportError: cannot import name 'Mapping' from 'collections' (/lib/python3.10/collections/__init__.py) since Python 3.10 removed Mapping from Collections
19:11
not sure if there's a workaround at all?
Avatar
Avatar
bryce
is it currently possible to use gensim with pyscript? -> I get this error: ImportError: cannot import name 'Mapping' from 'collections' (/lib/python3.10/collections/__init__.py) since Python 3.10 removed Mapping from Collections
Jeff Glass 16/02/2023 20:02
From a bit of playing around, it seems like gensim is pretty significantly tied to Python 3.8 and especially versions of scipy older than 0.10.0... even doing some import-mangling crimes like: import collections collections.Mapping = collections.abc.Mapping leads to other broken dependencies, like the scipy.maxentropy module
Avatar
Avatar
Jeff Glass
From a bit of playing around, it seems like gensim is pretty significantly tied to Python 3.8 and especially versions of scipy older than 0.10.0... even doing some import-mangling crimes like: import collections collections.Mapping = collections.abc.Mapping leads to other broken dependencies, like the scipy.maxentropy module
is there no way to use an earlier version of python or something like that? the project code i wrote heavily uses gensim ๐Ÿ˜… was looking to build it into a web app
Avatar
Jeff Glass 16/02/2023 21:15
You could potentially try running it with an older version of Pyodide; i believe Pyodide 0.17.0 was the last version to use Python 3.8. But the Pyodide API has changed significantly over time (and continues to), so each release of PyScript tends to be pinned to a specific Pyodide version (Pyodide 2022.12.1 uses Pyodide 0.21.3, for example). (edited)
21:16
If you want go that route, the Pyodide Getting Started docs are a good place to start https://pyodide.org/en/stable/usage/quickstart.html, and probably the extant Pyodide 0.17.0 docs https://pyodide.org/en/0.17.0/index.html
21:18
Hmmm it looks like Pyodide 0.17.0 bundled scipy 0.17.0 (by coincidence) which is still too recent, if I'm understanding the import errors from gensim right
Avatar
Hello
19:20
19:20
I want to edit 1 copy.jpg using the PIL module
19:20
In order to get it edited by opening any of the html pages that use it
19:21
firstly is it possible through PyScript?
19:22
I made this greyscale function in order to test if the edits are permanent def make_greyscale(): from PIL import Image img=Image.open('1.jpg') pixels=img.load() width,height=img.size output=Image.new('RGB',(width,height)) pixels_out=output.load() for y in range(height): for x in range(width): avg_colors=tuple([sum([pixels[x,y][i] for i in range(3)])//3 for i in range(3)]) pixels_out[x,y]=avg_colors output.save('1 copy.jpg') alert("Votre image a รฉtรฉ modifiรฉe avec succรจs.")
Avatar
Avatar
arlcun
firstly is it possible through PyScript?
Jeff Glass 21/02/2023 07:39
It is! Though remember that like any web page the user will likely need to upload the file, and download it again after the site has made its modifications
07:40
Hereโ€™s a (fairly old) example of uploading a file, modifying, and downloading. Some of the pyscript/Pyodide specifics are a little out of date, as it was written for version 2022.05.1 (alpha), but the mechanisms for upload/download are still the same: https://jeff.glass/post/pyscript-image-upload/
How to upload images into Pyscript and work with them in Pillow in-browser
Avatar
thanks!
Avatar
can i run pyscript in a debugger yet
Avatar
There is a way to make secrets (secret variables) in PyScript? Such It all runs in browser, all people can see the code of the page and get these secrets. With <py-env>, all can see it by seeing the resource code of the page. if we use dotenv, anyone could see this secret by searching .env in our page, so that doesn't works so how can i make that? (edited)
Avatar
@pico i keep all my secrets server side. i mainly use pyscript in conjunction with a django api. the client has to authenticate via the api before they can use anything that requires secrets
Avatar
@hosler you can... but it's not great add import pdb to your code and breakpoint() where you need I think we're gonna be able to offer a better solution once the webworker patch gets in ๐Ÿ™‚
17:18
im trying to use pyscript to print out input of html
17:18
here is code
17:18
<html> <head> <link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" /> <script defer src="https://pyscript.net/latest/pyscript.js"></script> </head> <body> <py-script> from js import document from pyodide.ffi.wrappers import add_event_listener test1 = document.getElementById("TEST") def testevent(): print(test1) </py-script> <input id="TEST" /> <button py-click="testevent()" id="new-task-btn" class="py-button">TEST111</button> </body> </html>
Avatar
Avatar
Anas
here is code
Jeff Glass 03/03/2023 17:22
You'll want to use the value attribute of the HTML element to get the user-entered value: def testevent(): print(test1.value)
Avatar
ohh thanks!
17:24
that didnt work
17:24
17:25
im basically trying to grab value from input to print
Avatar
Jeff Glass 03/03/2023 19:02
Sorry I should have clarified - that should have just replaced your existing testevent method: <py-script> from js import document test1 = document.getElementById("TEST") def testevent(): print(test1.value) </py-script> <input id="TEST" /> <button py-click="testevent()" id="new-task-btn" class="py-button">TEST111</button> (edited)
Avatar
TobiasPitters 04/03/2023 09:12
Hi all, I would like to build a page where one can execute python with numpy and pandas in a REPL. I am trying to use React here, since I am comfortable using it, but I hit some issues. So adding a <py-script> works with the infamous dangerouslySetInnerHTML={{__html:<py-script>...</py-script>}} . But how do I get py-repl to work? Any advice on this or any projects you could refer me to? Or should I abandon react for this project and use something different? Thanks. Any advice is appreciated.
13:18
so, im working on generator and for some reason when adding input and running the python function nothing happens
13:18
here is code
13:19
Avatar
Avatar
Anas
Hello
clickevent = ',' + '"clickEvent"' +':{'+'"action":'+ f'"{events[event_input]}"' ',' + '"value"' ":" + f'"{value}"' '}' the f'"{events[event_input]}"' should be f'"{events[events.index(event_input.value)]}"'
13:39
events is a list so it needs the index not the item value. also event_input needs to get the value from the element
13:39
also #Colour check for x in color_list: if color == x: print(command) should be #Colour check for x in color_list: if color.value == x: print(command) because you need to get the value of the element (edited)
Avatar
ah ok thanks!
Avatar
Avatar
Anas
ah ok thanks!
no problem! good luck with your project!
Avatar
Avatar
tuxprint
no problem! good luck with your project!
sry but nothing happened
13:48
it doesnt print anything out
Avatar
here are the edits I made. this works for me
13:50
there are still some other places that you need to add .value to, but otherwise it seems to work
Avatar
ok thanks
Avatar
that didnt work. I got another idea
14:00
do anyone knows How can i fetch a python script from github repository?
Avatar
Avatar
Anas
that didnt work. I got another idea
you need to look in the console for errors. assuming you gave it expected input it does work
14:02
14:03
https://docs.pyscript.net/latest/guides/http-requests.html#fetch you can use this to fetch a python script from github. use the raw.githubusercontent link
Avatar
ok thanks
Avatar
Avatar
tuxprint
you need to look in the console for errors. assuming you gave it expected input it does work
you mean these?
Avatar
no, in the browser console
14:06
if color or event is not an expected value, Including capitalization, the script will fail (edited)
Avatar
Avatar
Anas
ok thanks
here is the final thing I did lol. I added .value to the places, also for your checkboxes you want to get the checked state (I'm assuming) so instead of .value I added .checked. it seemed like you needed lowercase, so I convert the bool to a string and lower it like so str(strikethrough.checked).lower()
Avatar
is that correct?
15:42
the fetch
Avatar
Avatar
Anas
is that correct?
https://docs.pyscript.net/latest/tutorials/py-config-fetch.html#fetching-the-files No. This link has a better example of getting a file from GitHub. Also make sure to select raw on GitHub to get the link (edited)
Avatar
Avatar
TobiasPitters
Hi all, I would like to build a page where one can execute python with numpy and pandas in a REPL. I am trying to use React here, since I am comfortable using it, but I hit some issues. So adding a <py-script> works with the infamous dangerouslySetInnerHTML={{__html:<py-script>...</py-script>}} . But how do I get py-repl to work? Any advice on this or any projects you could refer me to? Or should I abandon react for this project and use something different? Thanks. Any advice is appreciated.
FabioRosado 04/03/2023 22:31
Hello ๐Ÿ‘‹ do want to pre-populate py-repl with code? Or just use it?
Avatar
TobiasPitters 05/03/2023 17:56
@FabioRosado thanks for answering, I would do both, prepopulate it and then let the user manupulate it and use it.
Avatar
Avatar
TobiasPitters
@FabioRosado thanks for answering, I would do both, prepopulate it and then let the user manupulate it and use it.
FabioRosado 06/03/2023 08:26
I havenโ€™t tried it yet but the py-repl tag should work like the py-script tag. Due to jsx you will need to use the dangerously html trick like in the py-script tag. Iโ€™ve tested using pyscript with vue and that seems to work very well with just small tweaks to the config
08:27
Itโ€™s still a work in progress but hereโ€™s a starter for vue if you would like to try it https://github.com/FabioRosado/pyscript-vue-starter
Avatar
tedpatrick 07/03/2023 13:13
PyScript.com Beta Access for PyScript Discord Members https://bit.ly/psdcbeta (edited)
๐Ÿš€ 1
15:35
im trying to fetch a python script from my github
15:35
but it seems it doesnt work
Avatar
Avatar
Anas
Hello
it seems that is a private repo. that is not going to work. also you need to put the filename in the files list. so if I wanted to get this json file from this ilink https://raw.githubusercontent.com/bleach86/material-dashboard/master/composer.json it would be as follows [[fetch]] from = "https://raw.githubusercontent.com/bleach86/material-dashboard/master/" files = ["composer.json"] (edited)
15:45
then in your code, if it's a python file you can import it, or if it is something else like my example, you can open it with a with open() statement
15:46
import json with open('composer.json') as f: file = json.loads(f.read()) (edited)
Avatar
ok so im trying to run a function from my python file but nothing happens
17:54
17:55
18:00
and btw im running it on localhost
Avatar
You have to use fetch to get the file
Avatar
that didnt work
Avatar
[[fetch]] files = ['/tellraw.py'] (edited)
18:09
tellraw.py needs to be in the same directory as index.html and it needs to be served from a server
Avatar
Avatar
tuxprint
tellraw.py needs to be in the same directory as index.html and it needs to be served from a server
i did all of that but the function doesnt run
Avatar
Avatar
Anas
i did all of that but the function doesnt run
Jeff Glass 07/03/2023 18:47
What's the error you're getting?
18:48
For what it's worth, you don't need to fetch the file in this case - <py-script src="..."> fetches the file for you
python 1
Avatar
Avatar
Jeff Glass
What's the error you're getting?
no errors rly. it just doesnt work
Avatar
Jeff Glass 07/03/2023 18:57
Huh! Interesting - no errors in the browser's dev console at all?
Avatar
Avatar
Jeff Glass
Huh! Interesting - no errors in the browser's dev console at all?
nope
Avatar
Jeff Glass 07/03/2023 19:12
What's your <script> tag look like? Are you using <script src="https://pyscript.net/latest/pyscript.js"> Or alpha? Or something else?
Avatar
Avatar
Jeff Glass
What's your <script> tag look like? Are you using <script src="https://pyscript.net/latest/pyscript.js"> Or alpha? Or something else?
Avatar
Avatar
Anas
Click to see attachment ๐Ÿ–ผ๏ธ
Jeff Glass 07/03/2023 19:22
Here's a minimal, working example: <head> <script defer src="https://pyscript.net/latest/pyscript.js"></script> <link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css"> </head> <body> <py-script> def hello(): print("HI!") </py-script> <button id="btn" py-click="hello()">Click me!</button> </body> </html>
19:23
Or similarly, with the code in an external file: <head> <script defer src="https://pyscript.net/latest/pyscript.js"></script> <link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css"> </head> <body> <py-script src="demo.py"></py-script> <button id="btn" py-click="hello()">Click me!</button> </body> </html> #demo.py def hello(): print("HI!") (edited)
Avatar
Avatar
Jeff Glass
Here's a minimal, working example: <head> <script defer src="https://pyscript.net/latest/pyscript.js"></script> <link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css"> </head> <body> <py-script> def hello(): print("HI!") </py-script> <button id="btn" py-click="hello()">Click me!</button> </body> </html>
this one works but im talking about fetching
19:23
ok
Avatar
Jeff Glass 07/03/2023 19:24
It's surely possible that this works for me but not for you - if that's the case, I'd be really interested to figure out why! ๐Ÿ˜ฌ
Avatar
Yooo thanks
19:26
it worked!
๐Ÿฅณ 1
Avatar
Avatar
Anas
it worked!
Jeff Glass 07/03/2023 19:26
Woo!
Avatar
Avatar
Jeff Glass
Woo!
bro, apperently it is something have to do with the name
Avatar
Avatar
Anas
bro, apperently it is something have to do with the name
Jeff Glass 07/03/2023 19:29
The name of... the function? Or the file?
Avatar
Avatar
Jeff Glass
The name of... the function? Or the file?
The name of the file
19:30
i just renamed it and it worked
Avatar
Avatar
Anas
i just renamed it and it worked
Jeff Glass 07/03/2023 19:31
Huh... does it break again if you use src="./demo.py? with the "./" (edited)
Avatar
Avatar
Jeff Glass
Huh... does it break again if you use src="./demo.py? with the "./" (edited)
nope
19:31
just when i use tellraw as name it wouldnt work
19:31
but when i change it to anything else
19:31
it does work
โ€ผ๏ธ 1
Avatar
Jeff Glass 07/03/2023 19:32
Wild....
Avatar
anyway, thank u so much for the help
Avatar
Browser cache maybe
Avatar
Jeff Glass 07/03/2023 19:33
<py-script src="./tellraw.py"> does work for me
Avatar
it doesnt for me
19:33
wait
Avatar
Avatar
tuxprint
Browser cache maybe
Jeff Glass 07/03/2023 19:33
This is a good thought
Avatar
maybe because this file is in github?
19:34
this file is being pushed and pulled in github to my pc
Avatar
Jeff Glass 07/03/2023 19:35
Mmm shouldn't make a difference, but possible depending on what local server program you're using, I guess?
Avatar
Jeff Glass 07/03/2023 19:36
For what it's worth, that same demo with the filename tellraw.py still works for me, so I suspect it's something in your environment rather than an issue with PyScript specifically
19:37
btw
19:37
how do I get inputs from my html file to python script?
Avatar
like this?
Avatar
Jeff Glass 07/03/2023 20:10
Like getting the value of an html <input>?
20:11
Typically you want to read from the value attribute. In that case: test = Element("text") my_value = test.element.value
20:11
I think
Avatar
For now, PyScript dont let me use https (the SSL conection) with requests or urlib. There is a way to PyScript to do that?
Avatar
Avatar
pico
For now, PyScript dont let me use https (the SSL conection) with requests or urlib. There is a way to PyScript to do that?
Jeff Glass 11/03/2023 04:51
So neither of those packages work natively in the browser, due to limitations in the browser environment. But both of them are shim-able using the pyodide-http module - thereโ€™s a brand new guide on it here: https://docs.pyscript.net/latest/tutorials/requests.html
Avatar
thanks
Avatar
hello, anyone there? I am getting error while pyscript is setting python environment. Seems like issue is with panel package. <py-config type="json"> { "terminal": false, "packages": ["altair","bokeh" , "numpy", "pandas", "scikit-learn", "panel==0.13.1"] } </py-config> exceptions.ts:52 (PY1001): Unable to install package(s) 'altair,bokeh,numpy,pandas,scikit-learn,panel==0.13.1'. Reason: Can't find a pure Python 3 Wheel for package(s) 'altair,bokeh,numpy,pandas,scikit-learn,panel==0.13.1'. See: https://pyodide.org/en/stable/usage/faq.html#micropip-can-t-find-a-pure-python-wheel for more information.
Avatar
Avatar
Jamshaid
hello, anyone there? I am getting error while pyscript is setting python environment. Seems like issue is with panel package. <py-config type="json"> { "terminal": false, "packages": ["altair","bokeh" , "numpy", "pandas", "scikit-learn", "panel==0.13.1"] } </py-config> exceptions.ts:52 (PY1001): Unable to install package(s) 'altair,bokeh,numpy,pandas,scikit-learn,panel==0.13.1'. Reason: Can't find a pure Python 3 Wheel for package(s) 'altair,bokeh,numpy,pandas,scikit-learn,panel==0.13.1'. See: https://pyodide.org/en/stable/usage/faq.html#micropip-can-t-find-a-pure-python-wheel for more information.
Only certain packages can be installed. They either need to have a 100% python wheel, or they need to be on this list of wasm compiled packages https://pyodide.org/en/stable/usage/packages-in-pyodide.html
Avatar
last friday, same config worked fine.. today this is giving me error without any code change... @tuxprint
Avatar
@tuxprint any help? what I found out is that if I remove panel from packages array in <py-config , everything works. but question is it was working fine even with panel 3 days ago.
Avatar
If i want to seperate my source files and just use them via the src-Attribute like <py-script src="hello.py"></py-script> how do i import the namespace to get some nice IDE tooltips/typehints and autocompletion? and how do i install the library for my IDE to look this up? I tried pip install pyscript but this only seems to have the cli. this minimal file is import pyscript def display_hello(*args,**kwargs): display("Hello World", target="helloDiv") but neither the import nor the display methods can be resolved by pylance despite it is running as expected.
Avatar
Avatar
Jamshaid
@tuxprint any help? what I found out is that if I remove panel from packages array in <py-config , everything works. but question is it was working fine even with panel 3 days ago.
FabioRosado 13/03/2023 12:07
Can you try not pinning the panel version?
Avatar
Avatar
FabioRosado
Can you try not pinning the panel version?
(PY1001): Unable to install package(s) 'altair,bokeh,numpy,pandas,scikit-learn,panel'. Reason: Can't find a pure Python 3 Wheel for package(s) 'altair,bokeh,numpy,pandas,scikit-learn,panel'. See: https://pyodide.org/en/stable/usage/faq.html#micropip-can-t-find-a-pure-python-wheel for more information. Still getting same error. even after removing version of panel..
Avatar
Avatar
FabioRosado
Can you try not pinning the panel version?
this is the traceback
Avatar
Avatar
Jamshaid
last friday, same config worked fine.. today this is giving me error without any code change... @tuxprint
Jeff Glass 13/03/2023 13:07
As for the why this stopped working 3 days ago, PyScript released a new version on Friday evening (UTC). If youโ€™re linking to the /latest version, that would have changed a number of things underneath you. (edited)
13:08
If you want to go back to linking to the previous stable release, point your script tag to https://pyscript.net/releases/2022.12.1/pyscript.js
Avatar
Avatar
Jeff Glass
If you want to go back to linking to the previous stable release, point your script tag to https://pyscript.net/releases/2022.12.1/pyscript.js
exactly.. resolved by downgrading to 2022.12.1
Jeff Glass started a thread. 13/03/2023 13:13
Avatar
FabioRosado 13/03/2023 13:19
I think this issue may be caused by the upgrade to the latest pyodide version. Pyscript latest comes with Pyodide 0.23 (if I remember correctly)
Avatar
Is it possible to generate an html file that has pyodide and all packages embedded instead of needing to load from the web every time? Iโ€™d like to use pyscript offline
Avatar
Avatar
alexb
Is it possible to generate an html file that has pyodide and all packages embedded instead of needing to load from the web every time? Iโ€™d like to use pyscript offline
FabioRosado 14/03/2023 08:17
Hello @alexb ๐Ÿ‘‹ You can definitely download all the packages and import them locally in your html file. @tedpatrick created a script to fetch all the needed things for you to go fully offline https://github.com/tedpatrick/pyscript-local-runtime
Contribute to tedpatrick/pyscript-local-runtime development by creating an account on GitHub.
๐Ÿ™ 1
Avatar
Well it looks like pyscript-local-runtime doesn't download the packages
08:38
just pyodide-core, note also that pyscript-local-runtime is for Pyodide 0.21.x. Since Pyodide 0.22.0 the functionality of pyscript-local-runtime can be achieved by downloading and unpacking the pyodide-core tar file from github releases: https://github.com/pyodide/pyodide/releases/download/0.22.1/pyodide-core-0.22.1.tar.bz2
๐Ÿ™ 1
Avatar
Avatar
hood
just pyodide-core, note also that pyscript-local-runtime is for Pyodide 0.21.x. Since Pyodide 0.22.0 the functionality of pyscript-local-runtime can be achieved by downloading and unpacking the pyodide-core tar file from github releases: https://github.com/pyodide/pyodide/releases/download/0.22.1/pyodide-core-0.22.1.tar.bz2
neat thank you!
Avatar
I made a quick and dirty tool to also save all the Python packages you use: https://github.com/hoodmane/pyscript-local-runtime
A quick and dirty attempt to make pyscript work from localhost - GitHub - hoodmane/pyscript-local-runtime: A quick and dirty attempt to make pyscript work from localhost
๐Ÿคฉ 1
โœ… 1
21:51
I applied it to the altair example and you can see that after doing this all the Python packages come from localhost
21:51
(I also manually downloaded pyscript.js and pyscript.css, the tool doesn't automate that)
Avatar
Do some packages not work in pyscript and why?
22:42
I am trying to use the openai api
22:43
22:43
this is the error i am getting
Avatar
Jeff Glass 18/03/2023 01:24
Exactly - the packages available:
  • Any package on PyPI that is written solely in Python (no C/Rust extensions etc)
01:25
Any package which relies on low-level features available to an OS but not available inside a browser window are unlikely to work (ssl, requests, tkinter, etc).
โค๏ธ 1
Avatar
@Jeff Glass thank you
Avatar
์˜์›ํ•œ์ฐฐ๋‚˜ 21/03/2023 06:45
hi~ everyone~ May I know why 'runPythonAsync' function is moved? from 'pyscript.runtime.interpreter.runPythonAsync' to 'pyscript.runtime._remote.interpreter.runPythonAsync' ?
06:46
and where can i check this changes?
Avatar
hey @์˜์›ํ•œ์ฐฐ๋‚˜ I believe the reason why it was moved is because folks are trying to comparmentalize the code related to interpreters so we can have a more general approach and integrate different interpreters better into pyscript code. currently our architecture is completely dependent on pyodide... but I guess also it is relevant for the introduction of webworkers in pyscript jeff wrote a bit about this in his blogpost https://jeff.glass/post/whats-new-pyscript-2023-03-1/ other than that if you want even more specific pointers I guess the second best place to check might be our CHANGELOG (edited)
What's New in PyScript 2023.03.1
Avatar
Avatar
3l3ktr4
hey @์˜์›ํ•œ์ฐฐ๋‚˜ I believe the reason why it was moved is because folks are trying to comparmentalize the code related to interpreters so we can have a more general approach and integrate different interpreters better into pyscript code. currently our architecture is completely dependent on pyodide... but I guess also it is relevant for the introduction of webworkers in pyscript jeff wrote a bit about this in his blogpost https://jeff.glass/post/whats-new-pyscript-2023-03-1/ other than that if you want even more specific pointers I guess the second best place to check might be our CHANGELOG (edited)
์˜์›ํ•œ์ฐฐ๋‚˜ 21/03/2023 10:25
Thank you for your mention! I will check it.
Avatar
hi! wanted to ask a quick question. can i use the qiskit library on pyscript? when i import it on py config i get this error Can't find a pure Python 3 Wheel for package
Avatar
Avatar
apost
hi! wanted to ask a quick question. can i use the qiskit library on pyscript? when i import it on py config i get this error Can't find a pure Python 3 Wheel for package
Jeff Glass 24/03/2023 14:24
Hello! The short answer is: not easily. As you may know, Python packages that don't rely on external (C, Rust etc.) extensions and are entirely Python can be built as "Pure Python Wheels" (https://realpython.com/python-wheels/#building-a-pure-python-wheel). Any package on PyPI that has published a Pure Python wheel on PyPI is usable in PyScript (unless of course it depends on an unsupported package itself). You can tell if a project has published a Pure Python wheel by checking the files on PyPI and seeing if any end in -none-any.whl For packages that *do *have extensions that must be compiled for a specific system, you can think of the web runtime "platform" (CPython running compiled to Web Assembly running in the browser) as being comparable to other platforms that Python packages are compiled to run on, like Windows + AMD64, or maxosx+intel etc. In order to run, packages that have extensions in C, Rust etc must be built for the webassembly environment. Many packages have indeed been pre-built this way by the Pyodide team (see https://pyodide.org/en/stable/usage/packages-in-pyodide.html). If you reference any from that list in your packages key in <py-config>, it will be installed from the Pyodide distribution's prebuilt packages instead of PyPI. But if you have a package that does require additional compilation and has not yet been built for Pyodide, your options are: try to build it yourself https://pyodide.org/en/stable/development/new-packages.html - and if you do, please submit it as a patch for Pyodide! Or, open a package request issue on the Pyodide repo https://github.com/pyodide/pyodide.
Avatar
Avatar
Jeff Glass
Hello! The short answer is: not easily. As you may know, Python packages that don't rely on external (C, Rust etc.) extensions and are entirely Python can be built as "Pure Python Wheels" (https://realpython.com/python-wheels/#building-a-pure-python-wheel). Any package on PyPI that has published a Pure Python wheel on PyPI is usable in PyScript (unless of course it depends on an unsupported package itself). You can tell if a project has published a Pure Python wheel by checking the files on PyPI and seeing if any end in -none-any.whl For packages that *do *have extensions that must be compiled for a specific system, you can think of the web runtime "platform" (CPython running compiled to Web Assembly running in the browser) as being comparable to other platforms that Python packages are compiled to run on, like Windows + AMD64, or maxosx+intel etc. In order to run, packages that have extensions in C, Rust etc must be built for the webassembly environment. Many packages have indeed been pre-built this way by the Pyodide team (see https://pyodide.org/en/stable/usage/packages-in-pyodide.html). If you reference any from that list in your packages key in <py-config>, it will be installed from the Pyodide distribution's prebuilt packages instead of PyPI. But if you have a package that does require additional compilation and has not yet been built for Pyodide, your options are: try to build it yourself https://pyodide.org/en/stable/development/new-packages.html - and if you do, please submit it as a patch for Pyodide! Or, open a package request issue on the Pyodide repo https://github.com/pyodide/pyodide.
i figured that building a wheel package of qiskit may be the solution. so im gonna give it a try. what i want to accomplish here is have a repl that people can use to play around with qiskit. its part of my thesis. if you know any other way of doing this please let me! much appreciated!
Avatar
i run the steps from pyodide and when i include the .whl on py-config i still get the error
Avatar
สŸแดœแดแด€ 24/03/2023 16:53
Hi friends ! I am in a bit of a roadblock, I need to upload files (locally, offline) betwwen 16 megabytes ranging up to 512 megabytes. I have been using Panel's FileInput widget, which works perfectly with files from 16 to 128 megabytes. But when trying a 512megabyte file, the file simply... doesnt upload. I assume this is a limitation by Panel. Are there any alternatives to panel's fileInput to upload files locally ?
16:58
also: files bigger than 128 make the browser run out of memory. All testing is done with chrome, which should be able to handle big files...
Avatar
Avatar
สŸแดœแดแด€
Hi friends ! I am in a bit of a roadblock, I need to upload files (locally, offline) betwwen 16 megabytes ranging up to 512 megabytes. I have been using Panel's FileInput widget, which works perfectly with files from 16 to 128 megabytes. But when trying a 512megabyte file, the file simply... doesnt upload. I assume this is a limitation by Panel. Are there any alternatives to panel's fileInput to upload files locally ?
Jeff Glass 24/03/2023 17:04
Have you seen the information on the HoloViz FileInput example, on file size limits and increasing them? https://github.com/holoviz/panel/blob/main/examples/reference/widgets/FileInput.ipynb
A high-level app and dashboarding solution for Python - panel/FileInput.ipynb at main ยท holoviz/panel
17:05
See the "Upload Size Limits" section
Avatar
สŸแดœแดแด€ 24/03/2023 17:12
Hmmm
17:13
Iโ€™m not using pn.serve
17:13
Tho I might learn how to do that
Avatar
สŸแดœแดแด€ 24/03/2023 17:40
It seems pn.serve is not usable in pyscript. error: RuntimeError: Cannot serve application in pyodide context.
Avatar
Hi I have a question, is there a way to import turtle in py-script?
Avatar
I tried using turtle functions in web(pyodide) it was not working then i saw that they have removed some inbuilt module from pyodide turtle is one of it.I want turtle to work on web how can i ?
Avatar
there is no pypi link from which I could do the micropip install?
Avatar
A Python/Javascript Turtle package for Pyodide
09:57
My initial target is add turtle module in pyodide. I looked up the document of pyodide and found that it could be loaded by Micropip. So I found pyo-js-turtle. But when I import it, it turns out that
09:59
but pyo-js-turtle has no github repo
10:04
probably you're best off making your own wheel from the basthon turtle implementation and serving it yourself
Avatar
Avatar
hood
probably you're best off making your own wheel from the basthon turtle implementation and serving it yourself
okay thx for the help
Avatar
You will probably also need to look at the code to see how it decides what canvas element to render into
Avatar
when i every open wants to download it
Avatar
Avatar
b
when i every open wants to download it
FabioRosado 27/03/2023 10:47
Is this an extension that you have? When you reload, pyscript will be loaded again so pyodide will be downloaded again
Avatar
The dialog box shows IDM options. A quick googling shows me that it is Internet Download Manager, so I guess it's an extension which interferes with webpages?
Avatar
Hey all, is there a way to dynamically create HTML nodes from scratch via PyScript (without using from js import document and document.createElement) instead of hardcoding them inside <template> tags + using clone? Context: I want to generate HTML tags on-the-fly without having to specify them in code beforehand (e.g., the kind of tag can come from user input via text field), but I want to avoid figuring out how to add type stubs for js, document, and document.createElement. Edit: pyodide has a readily available type stub (https://github.com/pyodide/pyodide/blob/main/src/py/js.pyi), but I have to install the completely unrelated js package so Pylance doesn't raise an error about the js package not being installed. I'd still rather avoid having to import js altogether. (edited)
Jeff Glass started a thread. 02/04/2023 18:18
Avatar
Hi everyone, sorry for the noob question but it doesn't seem to obvious: I would like to pass objects between js <--> python, and I've read the guide here: https://docs.pyscript.net/latest/guides/passing-objects.html But my question is: can this be done without having to put both scripts in the html and making them interact there? As in, is it possible to have some python files in my repo, and the JS scripts run the python files and grab some variables?
Avatar
though for this use case you may want to use Pyodide directly since this part of the pyscript API is unstable
Avatar
Deleted User 06/04/2023 02:26
Hey, im getting this error when trying to ru my code (PY1001): Unable to install package(s) 'requests,browser-cookie3,ctypes,wmi'. Reason: Can't find a pure Python 3 Wheel for package(s) 'requests,browser-cookie3,ctypes,wmi'. See: https://pyodide.org/en/stable/usage/faq.html#micropip-can-t-find-a-pure-python-wheel for more information. (edited)
Avatar
Well there are at least two problems there:
  • requests doesn't work in browser
  • ctypes is part of the Python standard library and is always included
Avatar
Jeff Glass 06/04/2023 17:04
requests should still still install fine from pypi, but it'll break on usage. I'd suggest looking at pyodide-http to patch requests at runtime
Provides patches for widely used http libraries to make them work in Pyodide environments like JupyterLite - GitHub - koenvo/pyodide-http: Provides patches for widely used http libraries to make th...
Avatar
Is building it locally just amount to cloning it, cding into pyscript/pyscriptjs/ then running make setup?
Avatar
Avatar
zkthis
Is building it locally just amount to cloning it, cding into pyscript/pyscriptjs/ then running make setup?
Jeff Glass 07/04/2023 00:56
Should be, yep! Youโ€™ll need node and conda installed first, through make setup will double check and yell at you if you donโ€™t have them
00:56
I think Node 16+ is required at this pointโ€ฆ
Avatar
is there anyway to make pyscript things be detected by VSCode? (edited)
02:44
like this ones
Avatar
Well you can add from pyscript import Element to the top of the file
Avatar
Avatar
Jeff Glass
I think Node 16+ is required at this pointโ€ฆ
Thank you! After that, what do I put in my index to refer to it, instead of <script defer src="https://pyscript.net/latest/pyscript.js"></script>
Avatar
Well it depends on where your source file is compared to the build directory
03:12
And how you serve them
03:13
If you paste your file into pyscript/build and then serve the build folder
03:13
You can use ./pyscript.js
03:14
Try running make examples and then looking in the pyscriptjs/examples folder
Avatar
Ah, I had to make build and it's the file you said (./pyscript.js)
03:15
thank you!
03:15
can I move this build by itself to a different repo?
Avatar
I think if you move pyscript.js and pyscript.css it's good enough
Avatar
oh whoa ok
03:16
is it the same as https://pyscript.net/latest/pyscript.js?
Avatar
But in general for any webpage what you can do is look in the network panel
03:17
I don't think so, I think latest is the most recent release
03:17
Whereas if you built from main you get the development version
03:18
If you git fetch --tags
03:18
Then you'll be able to check out and build various released versions
Avatar
thank you!
Avatar
getting a few dozen errors like this trying to run my little request script: Attempt to set a forbidden header was denied: Connection Attempt to set a forbidden header was denied: Accept-Encoding any idea on what i did wrong?
Avatar
Ah, it appears I have a bad understanding of python and it cannot really be used in websites. Sorry for exposing you to my retardation (edited)
Avatar
im trying to use "PyGithub" module in py-script, and i get PY1001, i tried to read the info in the link but im not really being able to solve it, could anybody give me a hand? (edited)
Avatar
Avatar
peron
im trying to use "PyGithub" module in py-script, and i get PY1001, i tried to read the info in the link but im not really being able to solve it, could anybody give me a hand? (edited)
Jeff Glass 08/04/2023 00:06
PyGithub looks like it should plausibly install, but probably one of its dependencies doesnโ€™t - can you open your browserโ€™s developer console and see if there are more errors there?
Avatar
Avatar
Jeff Glass
PyGithub looks like it should plausibly install, but probably one of its dependencies doesnโ€™t - can you open your browserโ€™s developer console and see if there are more errors there?
i end using JS
00:07
but man, using JS compared to py and pygithub
00:07
was horrible
Avatar
Jeff Glass 08/04/2023 00:15
Ack yeah, itโ€™s a rough jump away from Python. If I had the guess, the issue with PyGithub is that the options for communicating to Web resources (esp sockets) from within a browser window are limited, or look different in an incompatible way with what Python packages expect
Avatar
Avatar
Jeff Glass
requests should still still install fine from pypi, but it'll break on usage. I'd suggest looking at pyodide-http to patch requests at runtime
rx (ping me) 23/04/2023 03:10
is something like this a bad idea in a library that requires requests? import sys if "pyodide" in sys.modules: import pyodide_http pyodide_http.patch_all()
Avatar
Avatar
rx (ping me)
is something like this a bad idea in a library that requires requests? import sys if "pyodide" in sys.modules: import pyodide_http pyodide_http.patch_all()
Jeff Glass 23/04/2023 20:45
I think thatโ€™s a fine idea, assuming youโ€™re exposing the inners of requests to end users in a REPL or similar. But if it gives you the functionality you need, go for it
Avatar
Avatar
rx (ping me)
is something like this a bad idea in a library that requires requests? import sys if "pyodide" in sys.modules: import pyodide_http pyodide_http.patch_all()
Avatar
Also if you have questions about how to test against Pyodide let me know.
Avatar
am quite new to pyscript how do I point pyscript to use a specific python file well actually specify multiple python files? I don't want to write hundreds of lines of code in the body tags nvm got it! (edited)
Avatar
Avatar
hood
Also if you have questions about how to test against Pyodide let me know.
rx (ping me) 25/04/2023 00:40
actually yea, i'd love to test pyodide in github actions
00:41
https://github.com/pyodide/pytest-pyodide this looks like what i need :)
Avatar
Hi There Everyone! I've learned python and it's been a while, but i also know some basic stuff of html and i want to do the python stuff with help of the pyscript. Is there any suggestions from you guys.
06:47
Btw i'm new to pyscript
Avatar
I need some guidance; it should be appreciated
Avatar
Andrea Giammarchi 25/04/2023 11:14
I would suggest to check all examples provided in the repository (or live) to have a basic idea on how stuff works ... then, accordingly with your specific use case or goal, we might provide more specific help ... this is because the amount of things that could be done with pyscript is huge so it's hard to give you a generic guidance beside "please read the getting started and check our demos".
Avatar
@Andrea Giammarchi Thanks
Avatar
@rx (ping me) maybe. Do you have a package? That already has a test suite that passes for native python? You probably want something else
14:27
pytest-pyodide is mostly for writing new Pyodide specific tests
14:27
Anyways if you tell me your use case I can tell you how to handle it
Avatar
does anybody know if we can do any animations with pyscript (edited)
Avatar
and buttons responses
Avatar
Avatar
lawmixer
and buttons responses
Jeff Glass 27/04/2023 15:25
Button responses for sure. The syntax is: <button py-click="foo()"></button> <py-script> def foo(): print("Hi!") </py-script> Where the py-click attribute takes a string of executable Python code, to be executed in the global namespace when the event is triggered
15:26
And the event can be any* event - py-click, py-mouseover, py-focus etc
15:27
(For what it's worth, the exact syntax will be changing in an upcoming release, but in a way that provides some more specificity and options) (edited)
Avatar
Avatar
lawmixer
does anybody know if we can do any animations with pyscript (edited)
Jeff Glass 27/04/2023 15:47
When you say animations...
Avatar
I've successfully used jQuery for animations/transitions with pysctipt
16:34
Also css based animations
Avatar
LuckyBastard13 28/04/2023 07:13
How do I install dlib to pyscript? ๐Ÿ˜ฆ
Avatar
Avatar
LuckyBastard13
How do I install dlib to pyscript? ๐Ÿ˜ฆ
That is in c++ so it will have to be compiled to wasm to work. https://pyodide.org/en/stable/development/new-packages.html
Avatar
is there a way to get if a user clicks on a h1 element?
Avatar
Avatar
lawmixer
is there a way to get if a user clicks on a h1 element?
Avatar
this is a button element, not an h1 element
Avatar
Avatar
lawmixer
this is a button element, not an h1 element
You can do the same thing with any element (edited)
Avatar
oh ok
Avatar
Deleted User 30/04/2023 06:32
hello
06:35
does anybody know if it is possible to connect to a localhost port from pyscript? I did try to execute: import ib_insync

Connect to the IB API

ib = ib_insync.IB() ib.connect('127.0.0.1', 7497, clientId=1) and it gave me the error: API connection failed: NotImplementedError()
06:36
The library ib_insync uses eventkit, nest-asyncio and numpy. I configured that all in a toml file as packages. Any recommendations?
Avatar
Can somebody point me to a good resource that explains what are the bottlenecks when transfering data from python to js. I am trying to re-implement my old project that you can view here https://game-of-life-particles.glitch.me. However, right now it is extremely laggy, I suspect that it is bcs of synchronization between js and pyscript (I could be wrong).
Avatar
If you use a Python buffer to store the data it is possible to access the buffer from both js and Python so that data transfer is zero copy
Avatar
Avatar
hood
If you use a Python buffer to store the data it is possible to access the buffer from both js and Python so that data transfer is zero copy
are you talking about this buffer https://docs.python.org/3/c-api/buffer.html ? or is it some pyscript specific implementation of buffer?
Certain objects available in Python wrap access to an underlying memory array or buffer. Such objects include the built-in bytes and bytearray, and some extension types like array.array. Third-part...
Avatar
Perfect โค๏ธ thanks, that helps a ton
Avatar
Anything that implements Cpython buffer protocol
Avatar
that's crazy that js is able to acess the buffer without copying it
Avatar
It should also be mutable
๐Ÿ–ค 1
Avatar
Hi guys one question, how can I make pyscript work locally I mean without using those links
00:41
Hi guys how can I use pyscript locally without using this scripts
00:41
00:41
<head> <link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" /> <script defer src="https://pyscript.net/latest/pyscript.js"></script> </head>
Avatar
Avatar
branzel
Hi guys one question, how can I make pyscript work locally I mean without using those links
Jeff Glass 02/05/2023 02:20
What exactly are you wanting to do? Not include the links? Run offline?
Avatar
Avatar
Jeff Glass
What exactly are you wanting to do? Not include the links? Run offline?
my first goal is to run it offline in my local machine, my second goal is to make it a python package, so it would be easy for others to do the same. How can I achieve my first goal? (edited)
Jeff Glass started a thread. 02/05/2023 15:55
Avatar
Is there a way to dynamically load up a py-repl with python source code? <py-repl auto-generate="true" id="repl"> </py-repl> const repl = document.getElementById("repl"); repl.innerText = 'var = "Imagine I am dynamic"'; (edited)
massover started a thread. 03/05/2023 03:09
Avatar
Eullerzin o brabo 11/05/2023 01:53
hey beauty i'm from brazil i'm 14 years old and i wanted to know if sqlite3 supports pyscript pressing this to develop a project of mine can someone help me?
Avatar
Hi you guys Do we have pyscript version for Typescript?
Avatar
Eullerzin o brabo 11/05/2023 02:56
no
02:56
how to install
Avatar
Avatar
phuong
Hi you guys Do we have pyscript version for Typescript?
Andrea Giammarchi 11/05/2023 15:14
you mean ... as an npm module to import and use? how do you see/think that would work, when there's python code to deal with, and not JS/TS? really curious about this request, thank you!
Avatar
is it possible to do any of the things that pyscript does during load ahead of time in order to improve loading times?
Avatar
how do i make this work in pyscript? cv2.VideoCapture(cam_port)
08:44
the camera feed is blank
Avatar
Avatar
tuxprint
is it possible to do any of the things that pyscript does during load ahead of time in order to improve loading times?
Andrea Giammarchi 15/05/2023 10:51
if you know AOT what are you going to download you can see if rel=preload helps or you can use a Service Worker that caches any request so that next time everything will be loaded from the disk instead. However, I've currently measured the whole bootstrap dance/time and unfortunately the biggest impact is pyodide initialization which is around 1.5 seconds so that even if everything else bypasses the network, there will still be some bottleneck on bootstrap, which is not super problematic on long sessions, but we need to figure out how to improve such bootstrap for repeated / new pages cases.
The preload value of the element's rel attribute lets you declare fetch requests in the HTML's , specifying resources that your page will need very soon, which you want to start loading early in the page lifecycle, before browsers' main rendering machinery kicks in. This ensures they are available earlier and are less likely to block the pa...
Avatar
Avatar
Andrea Giammarchi
if you know AOT what are you going to download you can see if rel=preload helps or you can use a Service Worker that caches any request so that next time everything will be loaded from the disk instead. However, I've currently measured the whole bootstrap dance/time and unfortunately the biggest impact is pyodide initialization which is around 1.5 seconds so that even if everything else bypasses the network, there will still be some bottleneck on bootstrap, which is not super problematic on long sessions, but we need to figure out how to improve such bootstrap for repeated / new pages cases.
thank you for the response. I am using a sw to cache everything. the pyodide initialization is also where I have found the bottleneck. This load time can be upwards of 40 seconds on very low end/old mobile devices. It's probably not a big deal since most users will have better hardware, and I have not gotten any complaints from users for loading times. But ideally, this time would be reduced.
13:26
it would be cool if we could like pickle a session or something. idk
Jeff Glass started a thread. 15/05/2023 13:34
Avatar
Avatar
tuxprint
thank you for the response. I am using a sw to cache everything. the pyodide initialization is also where I have found the bottleneck. This load time can be upwards of 40 seconds on very low end/old mobile devices. It's probably not a big deal since most users will have better hardware, and I have not gotten any complaints from users for loading times. But ideally, this time would be reduced.
Andrea Giammarchi 15/05/2023 14:00
yes, on mobile we know pyodide can be heavy, specially on cheap and old android devices ๐Ÿ˜ฆ
Avatar
@tuxprint well Pyodide does load a lot of stuff that isn't really needed just in case someone happens to use it...
Avatar
Avatar
hood
@tuxprint well Pyodide does load a lot of stuff that isn't really needed just in case someone happens to use it...
interesting. could you possibly direct me towards relevant documentation?
Avatar
quick question
09:49
how do i import local modules into the main pyscript file?
09:50
<py-script src="./run_browser.py"></py-script> #run_browser.py import ball_mgmt
09:51
i tried googling up but im pretty stupid to wrap my head around it
Avatar
Avatar
Shashwat
how do i import local modules into the main pyscript file?
Andrea Giammarchi 16/05/2023 11:29
have you tried to fetch into the pyodide FS via the config? https://docs.pyscript.net/latest/tutorials/py-config-fetch.html
Avatar
Avatar
Andrea Giammarchi
have you tried to fetch into the pyodide FS via the config? https://docs.pyscript.net/latest/tutorials/py-config-fetch.html
everywhere on the internet is says to use <py-env> which doesnt work fetch works, thanks
Avatar
Avatar
Shashwat
everywhere on the internet is says to use <py-env> which doesnt work fetch works, thanks
Andrea Giammarchi 16/05/2023 13:43
this is interesting ... AFAIK there is no py-env but the word env is something I am exploring and still you'll need a config there to bootstrap such "env" ๐Ÿ™‚
Avatar
@Shashwat py-env was an old API and was deprecated, the new thing in town is fetch section within py-config
๐Ÿ‘ 1
Avatar
Avatar
madhurt
@Shashwat py-env was an old API and was deprecated, the new thing in town is fetch section within py-config
Andrea Giammarchi 16/05/2023 15:18
that explains my surprise and that's also probably something I need to dig more about within my exploration!
Avatar
Jeff Glass 16/05/2023 17:11
This is something I've been thinking about... I think we should introduce re-introduce the warning that py-env is removed, and to use py-config and see the docs (edited)
๐Ÿ‘ 1
17:12
There's a ton of examples out there on the web from PyScript alpha, this would be an easy nudge to let folks know that there are newer versions/syntax
Avatar
Few more questions, 1. Is there anyway to optimise loading times further? 2. Is there anyway to run pyscript entirely offline on browser?
Avatar
bugzpodder 18/05/2023 17:10
Hello, I am trying to figure out the best way to pass in some data into my python script without hard coding a different variable name in js global scope for every py-script tag. eg like a function call. I've sort of settled on putting a "getPyScriptContext" function in js global and then inject some kind of id into source of every py-script by adding two lines in the beginning, or use some templating approach (in the code below, instead of getPyScriptId() which doesn't exist, I would just replace it with some generated static id). I'd appreciate it if someone has a better idea: something like: from js import getPyScriptContext, getPyScriptId context = getPyScriptContext(getPyScriptId()) (edited)
Avatar
Avatar
bugzpodder
Hello, I am trying to figure out the best way to pass in some data into my python script without hard coding a different variable name in js global scope for every py-script tag. eg like a function call. I've sort of settled on putting a "getPyScriptContext" function in js global and then inject some kind of id into source of every py-script by adding two lines in the beginning, or use some templating approach (in the code below, instead of getPyScriptId() which doesn't exist, I would just replace it with some generated static id). I'd appreciate it if someone has a better idea: something like: from js import getPyScriptContext, getPyScriptId context = getPyScriptContext(getPyScriptId()) (edited)
Jeff Glass 18/05/2023 17:20
Can you expand a little bit more on "pass some data in"? Is that data coming from HTML, user input, JS...?
Avatar
Avatar
Jeff Glass
Can you expand a little bit more on "pass some data in"? Is that data coming from HTML, user input, JS...?
bugzpodder 18/05/2023 17:22
all my py-script is dynamically generated instead of a static html. (in a react app). so i don't know how many ahead of time. for the purposes of this discussion they all have the same source code, for example draw a circle of radius n. I want a page where i showcase ten of these scripts with n = 1, 2, ..., 10
17:24
I can do <py-script id="foo-{i}">...</py-script> and it would be nice to get access the id inside the python code. for output divs we can pass in some kind of id for the py-script tag, it'd be nice if i can do something similar for input
Avatar
Avatar
bugzpodder
I can do <py-script id="foo-{i}">...</py-script> and it would be nice to get access the id inside the python code. for output divs we can pass in some kind of id for the py-script tag, it'd be nice if i can do something similar for input
Jeff Glass 18/05/2023 19:11
Ah interesting - perhaps just having a reference to the Element/tag that's current "executing" the Python code? That's not currently something that's in the PyScript API... don't see why it couldn't be in the future... there might be some odd edge cases I'm not thinking of
19:14
You might be able to do something hacky for now with the display-target value though...
19:16
<script defer src="https://pyscript.net/releases/2023.03.1/pyscript.js"></script> <py-script id="foo" src="demo.py"> </py-script> <py-script id="bar" src="demo.py"> </py-script> <py-script id="baz" src="demo.py"> </py-script> #demo.py from pyscript import get_current_display_target print(get_current_display_target._id)
19:16
Prints foo bar baz
Avatar
Avatar
Jeff Glass
Ah interesting - perhaps just having a reference to the Element/tag that's current "executing" the Python code? That's not currently something that's in the PyScript API... don't see why it couldn't be in the future... there might be some odd edge cases I'm not thinking of
bugzpodder 18/05/2023 19:16
oh nice, i did not know that thing existed, will take a look thanks. that would be more than sufficient
Avatar
Jeff Glass 18/05/2023 19:17
For sure, though I really have to emphasize that that's a hack based on knowledge of the internals. ๐Ÿ˜‡ So pin your version of pyscript, cause it's gonna get broken/changed in the next release (the whole Python source got reorganized recently) (edited)
19:19
For instance, the equivalent currently on the main (unstable) branch would be: from pyscript._internal import DISPLAY_TARGET print(DISPLAY_TARGET) (edited)
Avatar
antoinechv 19/05/2023 06:45
hi guys, i'm new on this discord. I'm a physics master student and in order to complete a class project, i'm trying to use both react Js and PyScript. I'm ok with python but very new to JS. Is there any pro who can tell me if i can generate a json file and give it to react in order to plot data ?
Avatar
DasSalamibrot 19/05/2023 17:54
Hello. ๐Ÿ™‚ Im currently working on a project with pyscript. In the python-file i wrote a function that writes something into a csv. If I run just the Pythonfile it works. But if I try it when the code is connected with the html, it does nothing. No error, just nothing. The csv-file stays untouched. Even if I try to write smth in a textfile like "Hello World" nothing happens. What am I missing? Where is the mistake? I appreciate any help ๐Ÿ™
Avatar
DasSalamibrot 19/05/2023 18:06
def save_data(data): with open("Log.csv", 'a') as file: line = ';'.join([str(value) for value in data.values()]) file.write(line + '\n') (edited)
Avatar
Jeff Glass 19/05/2023 18:23
When Python accesses files within PyScript, it is accessing files within the virtual, in-memory filesystem in the Browser window that is created when the page loads. To move the contents of files on your harddrive into the filesystem, you can use the <py-config> tag's [[fetch]] syntax: https://docs.pyscript.net/latest/reference/elements/py-config.html#a-name-fetch-fetch-a
18:24
For this to work locally, you will probably need to run a simple development server, to allow code running in the browser to access the contents of files on your hard disk. Here's a recent blog post explaining why that's necessary:
18:24
Solving the "Access to local files" error that plagues new PyScript developers
18:26
And finally, it's important to note that what you'll be loading is a copy of the contents of your file, not a reference to the local file itself. So you'll need an additional way to make those changes persistant, like allowed the user to re-download that file to disk
Avatar
DasSalamibrot 19/05/2023 18:30
Okay. I will try that. Thank you for the quick response. โค๏ธ
Avatar
Avatar
antoinechv
hi guys, i'm new on this discord. I'm a physics master student and in order to complete a class project, i'm trying to use both react Js and PyScript. I'm ok with python but very new to JS. Is there any pro who can tell me if i can generate a json file and give it to react in order to plot data ?
bugzpodder 19/05/2023 18:41
Hi, I am actually working on react integration myself and it's a lot more tricky than I expected. I haven't dealt with your case specifically but I believe it is possible to access stuff python generated https://docs.pyscript.net/latest/guides/passing-objects.html#exporting-individual-python-objects you'll probably need to access global from react to retrieve the output. there is also https://github.com/Py4Js/PyScript-React that may help setup pyscript in your react project finally you may also consider calling pyodide directly if you just want to run some simple python? unsure. (edited)
Avatar
Avatar
Jeff Glass
And finally, it's important to note that what you'll be loading is a copy of the contents of your file, not a reference to the local file itself. So you'll need an additional way to make those changes persistant, like allowed the user to re-download that file to disk
DasSalamibrot 19/05/2023 22:48
I now have the file successfully read. But I dont know how to download it back like you said (cant find anything usable in the web). Any advise? ๐Ÿ™ƒ
Avatar
bugzpodder 20/05/2023 02:58
I had a lot of issues integrating with react in the last week. there were issues with double rendering, vdom fighting with shadow dom, and state management. I finally decided to throw out all the py-script tags and replaced with essentially a single line of code: window.pyscript.pyScript.interpreter.run(code, outputId); and everything I had worked flawlessly!! (edited)
03:01
This was needed for state management anyway. When react props changed I needed to re-execute the py-script, I had to either rip out the dom via innerHTML = '' or use the call above..
Jeff Glass started a thread. 20/05/2023 15:29
Avatar
bugzpodder 20/05/2023 19:43
It isn't very clear how to render stuff using display outside of examples. I was able to get pyecharts working as follows: from pyecharts.charts import Bar from pyscript._html import HTML bar = Bar() bar.add_xaxis(["่กฌ่กซ", "็พŠๆฏ›่กซ", "้›ช็บบ่กซ", "่ฃคๅญ", "้ซ˜่ทŸ้ž‹", "่ขœๅญ"]) bar.add_yaxis("ๅ•†ๅฎถA", [5, 20, 36, 10, 75, 90]) # render ไผš็”Ÿๆˆๆœฌๅœฐ HTML ๆ–‡ไปถ๏ผŒ้ป˜่ฎคไผšๅœจๅฝ“ๅ‰็›ฎๅฝ•็”Ÿๆˆ render.html ๆ–‡ไปถ # ไนŸๅฏไปฅไผ ๅ…ฅ่ทฏๅพ„ๅ‚ๆ•ฐ๏ผŒๅฆ‚ bar.render("mycharts.html") display(HTML(bar.render_embed())) should I be importing pyscript._html import HTML or something else? It would be great if we have some examples of how to encode different MIME types. display(plt) and display(fig) is just too magical. I guess there are three ways of rendering: an image, HTML, or some json for client side JS to render (like THREE or plotly, bokeh)? (edited)
Avatar
bugzpodder 20/05/2023 20:07
Ok looks like pyscript automatically imports HTML, display, js for me in run_pyscript so I don't need that import, fair enough.
Avatar
bugzpodder 20/05/2023 21:00
render_embed() in pyecharts actually outputs an entire html doc with <html> so it's broken on pyscript.com: https://b412faf3-21af-4adc-894d-6174cf4f38a6.pyscriptapps.com/4155c492-8180-4bc2-a77f-603f9c7ca83e/latest/ and such an error will break all other py-scripts on the page panel.extension('echarts') had the same issue.. eventually figured out that I needed to add: <script type="text/javascript" src="https://assets.pyecharts.org/assets/v5/echarts.min.js"></script> (edited)
Avatar
bugzpodder 20/05/2023 22:29
I found performance a big laggy when I add 10+ py-scripts on a page. The main thread locks and the UI is unresponsive. Curious what's the plan in terms of getting display and Element /DOM in general to work in web-workers? (edited)
Avatar
Avatar
bugzpodder
I found performance a big laggy when I add 10+ py-scripts on a page. The main thread locks and the UI is unresponsive. Curious what's the plan in terms of getting display and Element /DOM in general to work in web-workers? (edited)
Jeff Glass 22/05/2023 19:20
Would you say it's more laggy than just running the code from all 10 in a single py-script tag? There's a little overhead for each tag, but I wouldn't have expected it to be that much (edited)
Avatar
Avatar
Jeff Glass
Would you say it's more laggy than just running the code from all 10 in a single py-script tag? There's a little overhead for each tag, but I wouldn't have expected it to be that much (edited)
bugzpodder 23/05/2023 03:44
fair enough. I tried on pyscript.com and 10 script tags with just print worked fine. It's mostly downloading and running different packages, which is probably not a real world use case anyway (at least the different packages part, I would have expected a couple of common packages to begin with)
Avatar
Avatar
Jeff Glass
Would you say it's more laggy than just running the code from all 10 in a single py-script tag? There's a little overhead for each tag, but I wouldn't have expected it to be that much (edited)
bugzpodder 23/05/2023 17:17
Actually I am just curious about the webworkers implementation for DOM in https://github.com/pyscript/pyscript/issues/1380. I know most existing implementations require some kind of proxy and probably isn't ideal. i guess i should just ask in the issue instead.
Home Page: https://pyscript.net Examples: https://pyscript.net/examples - Issues ยท pyscript/pyscript
Avatar
Doorless microwave 24/05/2023 08:26
Hello, i need help ! How do i import modules in my html code with pyscript please ? I have to use openpyxl for excel manipulation with python. Is it supported ?
Avatar
Avatar
Doorless microwave
Hello, i need help ! How do i import modules in my html code with pyscript please ? I have to use openpyxl for excel manipulation with python. Is it supported ?
Jeff Glass 25/05/2023 03:17
To add an additional package from PyPI to your page, include it in the packages key in <py-config>, like so: <py-config> packages = ['openpyxl'] </py-config>
03:18
openpyxl does appear to work in PyScript, in that at least trying some of the sample code from their homepage works.
Avatar
Doorless microwave 25/05/2023 07:18
Thank you so much !
Avatar
bugzpodder 30/05/2023 21:55
is there a recipe for declaring a top level module similar to pyscript into the runtime? like here: https://github.com/pyscript/pyscript/blob/932756c7a0a4d219f4c3f6bcc9c1ad5332df667e/pyscriptjs/src/remote_interpreter.ts#L133 (edited)
Jeff Glass started a thread. 30/05/2023 22:18
Avatar
Deleted User 06/06/2023 15:47
I am trying to import a python library, but it runs in 64bit system. When I run the followin lines in a <py-repl> import platform architecture = platform.architecture()[0] print(architecture) I can confirm pyscript is running in 32 bits, is there away to change it to 64 bits?
Avatar
Avatar
Deleted User
I am trying to import a python library, but it runs in 64bit system. When I run the followin lines in a <py-repl> import platform architecture = platform.architecture()[0] print(architecture) I can confirm pyscript is running in 32 bits, is there away to change it to 64 bits?
wasm is 32 bits so I don't think you'll get 64 bit pyscript until we get wasm64
Avatar
Avatar
Deleted User
I am trying to import a python library, but it runs in 64bit system. When I run the followin lines in a <py-repl> import platform architecture = platform.architecture()[0] print(architecture) I can confirm pyscript is running in 32 bits, is there away to change it to 64 bits?
Jeff Glass 06/06/2023 18:18
Out of curiousity, what is the library?
Avatar
There are some python packages that assume a 64 bit os. If you want to run them in wasm you'll have to fix that assumption. Most of them only make it by mistake though.
Avatar
Avatar
Jeff Glass
Out of curiousity, what is the library?
Deleted User 07/06/2023 11:32
It is openseespy
Avatar
bugzpodder 08/06/2023 06:03
I am getting: "[plugin] Error while calling afterSetup hook of plugin nl DOMException: Failed to execute 'define' on 'CustomElementRegistry': the name "py-terminal"/"py-script" has already been used with this registry" should I try/catch this in pyscript?
Avatar
Is it ok to use PyScript on live web server?
Avatar
Avatar
Dave
Is it ok to use PyScript on live web server?
bugzpodder 08/06/2023 18:17
pyscript is run on the client's browser (eg Chrome) so you shouldn't have too much issues. it's just html web components
Avatar
That's not what I meant, I meant is there a version that can be used for public projects?
Avatar
Avatar
Dave
That's not what I meant, I meant is there a version that can be used for public projects?
Jeff Glass 08/06/2023 21:42
Do you mean from a licensing perspective? A security perspective?
Avatar
bugzpodder 08/06/2023 22:24
@Andrea Giammarchi What is the status of running pyscript in a webworker? Is the pyscript-next branch what it is for? Right now I've seen adding pyscript just make the user's browser unresponsive for 20 seconds as it runs on the main thread. So I'll either need to test on webworkers and accept some js dom functionalities won't work, or do a serverside pyodide calls in nodejs. Also, if we have a bunch of workers (one for each py-script) does that mean the pyodide is loaded repeatedly within each worker thread?
Avatar
Avatar
Jeff Glass
Do you mean from a licensing perspective? A security perspective?
Yes!
Avatar
Avatar
bugzpodder
@Andrea Giammarchi What is the status of running pyscript in a webworker? Is the pyscript-next branch what it is for? Right now I've seen adding pyscript just make the user's browser unresponsive for 20 seconds as it runs on the main thread. So I'll either need to test on webworkers and accept some js dom functionalities won't work, or do a serverside pyodide calls in nodejs. Also, if we have a bunch of workers (one for each py-script) does that mean the pyodide is loaded repeatedly within each worker thread?
Andrea Giammarchi 09/06/2023 08:52
we know pyodide takes some time to bootstrap but you can already use pyodide in a Web worker but it's not super fun to interact with Workers in general. pyscript-next enables Web <-> Worker communication in a pretty simple way so that you can use just JS on the DOM (or pyodide, or something else too ...) and bootstrap a pyodide (or JS, or something else too ...) worker with ease, exposing some callback that can be easily invoked by the worker so that nothing blocks. The choice of having one worker VS many workers is yours but as a worker is a sandbox, each worker inevitably will need to bootstrap its own pyodide runtime (or something else too ...) but there are many ways to avoid spinning more than a worker so this should be some good news. We're trying hard to have soon a technical preview for next so please following this space to have updates in the very near future. (edited)
Avatar
Hi Folks, I know the web worker implementation is experimental, but I want to try it out to see what I need to do on my end to get it working in my app. I have downloaded pyscript and included it on my test site (along with implementing the correct headers, etc.) . When execution_thread = โ€œworkerโ€, Iโ€™m getting this error: ImportError: cannot import name โ€˜updateBootstrapโ€™ from โ€˜jsโ€™ (unknown location) This is coming from this line: from js import updateBootstrap, populateColForm, paintAfterBootstrap, paintAfterEst This is how Iโ€™ve been sending data back to the JS side from the python side (i.e. each of those are JS functions). With the worker implementation, how do we communicate to the JS side from python?
Avatar
Andrea Giammarchi 21/06/2023 16:42
are those callbacks defined in the main / page thread?
Avatar
They are in a javascript file that is run on the main page.
16:50
For reference, this is the test site: https://project-assessment-test-site.uc.r.appspot.com where I've enabled workers. The public app is at https://projectassessment.app (running without workers).
Avatar
Hello, sorry if this is a stupid question, or I missed it in the docs. Is it possible to fetch a file from a private github repo? Can I provide an access token? (edited)
Avatar
Avatar
Ben Smith
For reference, this is the test site: https://project-assessment-test-site.uc.r.appspot.com where I've enabled workers. The public app is at https://projectassessment.app (running without workers).
Andrea Giammarchi 21/06/2023 17:37
AFAIK no way workers, as implemented now, can access global variables in the main thread ,,, the current worker lacks access to document and anything else in the main thread, I suggest you to not use the execution_thread flag until we can grant/offer a better alternative ... which I'm working on, and it would fix your issue, but it's not part of current offer, sorry.
Avatar
@Andrea Giammarchi Perfect! Thank you for your help.
Avatar
Avatar
FightMilk
Hello, sorry if this is a stupid question, or I missed it in the docs. Is it possible to fetch a file from a private github repo? Can I provide an access token? (edited)
Andrea Giammarchi 21/06/2023 17:40
you never want to provide an access token to anything publicly available and readable, especially a py-config element ... you can though orchestrate through a build step behind the scene the fetching of such file and be aware that file, once reachable from your public site, won't be private anymore.
Avatar
Well the site would be a confluence space, so it's only 'publicly available' to people who have access to the github repo. So I guess the root question is really how to embed a py-script page with dependencies into a managed product like confluence.
Avatar
Avatar
FightMilk
Well the site would be a confluence space, so it's only 'publicly available' to people who have access to the github repo. So I guess the root question is really how to embed a py-script page with dependencies into a managed product like confluence.
Andrea Giammarchi 21/06/2023 17:44
you fetch that file on your private laptop/env/infrastructure and you point to that file in a config ... you never want to put an access token in the wild so I think we should never want to fix this particular issue, from a security perspective, becase your case is "sandboxed" behind confluence, the Web is bigger though and we should never encourage sharing access tokens in a config.
Avatar
Yeah I'm not asking you to fix it. I realize the security implications. Unfortunately my problem is more of a confluence problem than a pyscript problem. Thanks.
Avatar
Avatar
FightMilk
Yeah I'm not asking you to fix it. I realize the security implications. Unfortunately my problem is more of a confluence problem than a pyscript problem. Thanks.
Andrea Giammarchi 21/06/2023 17:49
you have some IT behind that confluence (I hope) you can ask them to provide a config.toml file that points at the file you are after, automating the process, still never making that access token readable to anyone, including people behind that confluence page ... ask IT would be my answer, you have security policies in place and it shouldn't be PyScript responsibility (ever?) to bypass such security policies. I hope this makes sense to you, I am just trying to help the best I can. (edited)
Avatar
Yeah I could have some separate operation retrieve the file and store it somewhere confluence can access it, which maybe confluence supports. I may need to stand up a server just to provide those files, which is a maintenance headache. I was interested in pointing to the original file because the app is a progressive caching algorithm visualization; so the real sweet feature pyscript is providing is the ability to use the same, unit tested back end code that our production software is using to actually generate the visualization as well. Sounds like it's not automatable in a generically secure fashion though I'll probably write a script to jam the whole mess into a single html file. That process is a one step drop in generated from original source instead of a rube goldberg machine of dependency fetching. Like I said, pyscript is great for me so far, the problem is confluence idiosyncracies.
Avatar
Avatar
FightMilk
Yeah I could have some separate operation retrieve the file and store it somewhere confluence can access it, which maybe confluence supports. I may need to stand up a server just to provide those files, which is a maintenance headache. I was interested in pointing to the original file because the app is a progressive caching algorithm visualization; so the real sweet feature pyscript is providing is the ability to use the same, unit tested back end code that our production software is using to actually generate the visualization as well. Sounds like it's not automatable in a generically secure fashion though I'll probably write a script to jam the whole mess into a single html file. That process is a one step drop in generated from original source instead of a rube goldberg machine of dependency fetching. Like I said, pyscript is great for me so far, the problem is confluence idiosyncracies.
Andrea Giammarchi 21/06/2023 17:58
security is never too easy, I can relate with your frustration, but there's a reason to not encourage ever sharing access tokens in the wild ... all services require a remote backend or some special file convention .env to expose these tokens, bu we don't get to decide when these files or env variables are available from the Web and trust me, not only we don't want to know, it's a good security feature we can't know too (imagine an accidental error stack where involved code which includes that private token is revealed in the wild). I hope you'll manage to provide that file behind your confluence curtains, if there's anything else I can try to help with though, just let me know ๐Ÿ‘‹ (edited)
Avatar
Hello,
Avatar
I wrote a simple guessing numbers python game and I want it to be available on a web browser. I created an html file inside the same folder when i pasted the code to my html inside the <py-script> tags nothing showed up. I also tried adding <py-env> tags in the head to show path. I tried all the suggestions online and on youtube but I cant fix it. Please help !
Avatar
Avatar
Nafiva
I wrote a simple guessing numbers python game and I want it to be available on a web browser. I created an html file inside the same folder when i pasted the code to my html inside the <py-script> tags nothing showed up. I also tried adding <py-env> tags in the head to show path. I tried all the suggestions online and on youtube but I cant fix it. Please help !
Jeff Glass 25/06/2023 01:25
Hello! Any chance you could copy/paste your full HTML code here so we can have a look? If you put it between sets of 3 backticks ``` Like this ``` It will even show up nicely formatted (edited)
Avatar
Avatar
Jeff Glass
Hello! Any chance you could copy/paste your full HTML code here so we can have a look? If you put it between sets of 3 backticks ``` Like this ``` It will even show up nicely formatted (edited)
Here is my full HTML code, thank you for your help. I tried it with indentation and without but something is off.
05:20
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css"/> <script defer src="https://pyscript.net/latest/pyscript.js"></script> <title>Document</title> </head> <body> <py-script> import random def guess_the_number(): secret_number = random.randint(1, 250) attempts = 0 while True: attempts += 1 guess = int(input("Guess a number between 1 and 250: ")) if guess < secret_number: print("Too low, Try Again!") elif guess > secret_number: print("Too high!") else: print(f"Congratulations! You guessed the number in {attempts} attempts.") break guess_the_number() </py-script> </body> </html>
Avatar
How can I import python lib that are not listed on pyscript white-list lib. Like working with reverse-int.
Jeff Glass started a thread. 26/06/2023 00:59
Jeff Glass started a thread. 26/06/2023 01:28
Avatar
Can we use Python library called TTS with pyscript??
19:32
Plz reply
Avatar
TyroneSlothrop 01/07/2023 19:10
hi team, after watching Jeff's tour of the new release I was incredibly excited by xterm, tried it and now trying to figure out how to edit some of the terminal interfaces - turns out the py-terminal docs page already has the info regarding this, really great to see the care being put into the package... really exciting times (edited)
Avatar
Avatar
sudip555
Can we use Python library called TTS with pyscript??
TyroneSlothrop 01/07/2023 19:18
@sudip550 did you try following this recipe https://pyscript.recipes/basic/installing-packages/ ?
Avatar
Jeff Glass 02/07/2023 19:43
Thanks @TyroneSlothrop ! So glad youโ€™re finding both the Whatโ€™s New stream and the xterm option useful ๐Ÿฅณ
Avatar
Avatar
Enes
Thanks mate for your time ๐Ÿ™‚ I really appreciate it!!!
Jeff Glass 05/07/2023 04:36
Not to revive a looooong dead chain @Enes , but REPL execution hooks were included in the 2023.05.1 release
Avatar
Anyone know whatsup with the AttributeError on the d3 example ?
12:12
I'm also suddenly having this error on my project even tho it worked fine a few days ago
Avatar
Does anyone have an example of a plugin which uses beforePyReplExec and afterPyReplExec and some of its arguments such as src. My code below throws errors when I use it. from pyscript import Plugin from js import console class PyReplTestLogger(Plugin): def beforePyReplExec(self, interpreter, outEl, src, pyReplTag): console.log(f'beforePyReplExec called') console.log(f'before_src:{src}') console.log(f'before_id:{pyReplTag.id}') def afterPyReplExec(self, interpreter, src, outEl, pyReplTag, result): console.log(f'afterPyReplExec called') console.log(f'after_src:{src}') console.log(f'after_id:{pyReplTag.id}') console.log(f'result:{result}') plugin = PyReplTestLogger() I get the following browser console error: Uncaught (in promise) PythonError: Traceback (most recent call last): File "/home/pyodide/myplugin.py", line 8, in beforePyReplExec console.log(f'before_src:{src}') ^^^^^^^^^^^^^^^^^^^ TypeError: __str__ returned non-string (type pyodide.ffi.JsProxy) (edited)
Avatar
Avatar
Jase
Does anyone have an example of a plugin which uses beforePyReplExec and afterPyReplExec and some of its arguments such as src. My code below throws errors when I use it. from pyscript import Plugin from js import console class PyReplTestLogger(Plugin): def beforePyReplExec(self, interpreter, outEl, src, pyReplTag): console.log(f'beforePyReplExec called') console.log(f'before_src:{src}') console.log(f'before_id:{pyReplTag.id}') def afterPyReplExec(self, interpreter, src, outEl, pyReplTag, result): console.log(f'afterPyReplExec called') console.log(f'after_src:{src}') console.log(f'after_id:{pyReplTag.id}') console.log(f'result:{result}') plugin = PyReplTestLogger() I get the following browser console error: Uncaught (in promise) PythonError: Traceback (most recent call last): File "/home/pyodide/myplugin.py", line 8, in beforePyReplExec console.log(f'before_src:{src}') ^^^^^^^^^^^^^^^^^^^ TypeError: __str__ returned non-string (type pyodide.ffi.JsProxy) (edited)
Jeff Glass 08/07/2023 04:35
@madhurt the errors that Jase is mentioning are exactly the kind I was seeing after PR1490 landed. I thought PR1529 fixed all that, but perhaps not? (edited)
04:37
@Jase are you running on latest / 2023.05.1?
Avatar
Yeah, apparently not. It was for fixing โ€œglobalsโ€ but I can take a look for these. These are again related to โ€œsynclinkโ€ stuff I guess.
04:38
Aka requiring awaits etc.
Avatar
I'm using <script defer src="https://pyscript.net/latest/pyscript.js"></script> , but I can see from the browser console that the corresponding versions are PyScript 2022.12.1.dev with Pyodide pyodide-0.23.2 @Jeff Glass (edited)
Avatar
Is there a CDN link for the 2023.05.1 version available anywhere? or is it planned to be updated on https://pyscript.net/latest/pyscript.js. Possibly something could be broken on the CICD as the latest code on GitHub appears to be newer? (edited)
Avatar
Avatar
Jase
I'm using <script defer src="https://pyscript.net/latest/pyscript.js"></script> , but I can see from the browser console that the corresponding versions are PyScript 2022.12.1.dev with Pyodide pyodide-0.23.2 @Jeff Glass (edited)
Jeff Glass 08/07/2023 13:36
If youโ€™re linking to latest, thatโ€™s the right one - the baked-in version number didnโ€™t get updated before the release ๐Ÿ˜ฐ, but was fixed in prod the day after https://github.com/pyscript/pyscript/pull/1552
๐Ÿ‘ 1
Avatar
I was wondering if there is an easy way to set a py-repl to have the dark theme?
Avatar
Is there any way to execute python from javascript?
Avatar
Avatar
soy.david
Is there any way to execute python from javascript?
Andrea Giammarchi 12/07/2023 20:50
that's the whole point of the project, more or less, so the answer is yes but I am not sure I understand the question ... everything that works in PyScript (out of Pyodide) is JavaScript executing Python.
Avatar
Sorry, I'm trying to execute the python within a javascript function, rather then specify it in the dom
Avatar
Avatar
soy.david
Sorry, I'm trying to execute the python within a javascript function, rather then specify it in the dom
Andrea Giammarchi 13/07/2023 12:20
if you are using pyscript there's a pyscript.interpreter.runPython available once it bootstrap, if not I think using "raw pyodide" would work too.
Avatar
thanks
Avatar
Avatar
2_.
Anyone know whatsup with the AttributeError on the d3 example ?
bugzpodder 14/07/2023 18:10
seems fine when i just copied over to pyscript.com: https://b412faf3-21af-4adc-894d-6174cf4f38a6.pyscriptapps.com/287fe75f-c894-425d-a68a-a9bf20c982e4/latest/ i think there is some race condition where js.d3 isn't being found because d3 isn't loaded?
Avatar
I think too, as mine code did work sometimes and sometimes not (edited)
18:19
well I put it on hold anyways until the next core update (edited)
18:20
I'm hoping that will fix it somehow
Avatar
Avatar
2_.
I'm hoping that will fix it somehow
bugzpodder 14/07/2023 18:40
actually i. see the real error: The resource at โ€œhttps://d3js.org/d3.v7.min.jsโ€ was blocked due to its Cross-Origin-Resource-Policy header (or lack thereof). See https://developer.mozilla.org/docs/Web/HTTP/Cross-Origin_Resource_Policy_(CORP)# in Chrome: Failed to load resource: net::ERR_BLOCKED_BY_RESPONSE.NotSameOriginAfterDefaultedToSameOriginByCoep (edited)
Cross-Origin Resource Policy is a policy set by the Cross-Origin-Resource-Policy HTTP header that lets websites and applications opt in to protection against certain requests from other origins (such as those issued with elements like and <img>), to mitigate speculative side-channel attacks, like Spectre, as well as Cross-Site Script Inclusion ...
18:44
i don't totally understand why it works on pyscript.com but not pyscript.net but maybe switching to jsdelivr helps (edited)
Avatar
I have the same with highlight.js when using jsdelivr
19:14
and doesn't really explain why it sometimes works and sometimes doesn't
19:14
well atleast If I'm having the same issue
Avatar
Avatar
2_.
and doesn't really explain why it sometimes works and sometimes doesn't
bugzpodder 15/07/2023 00:15
i've never seen it work. actually just took a closer look, on pyscript.net it as COEP: require-corp and on pyscript.com it's credentialess https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cross-Origin-Embedder-Policy#syntax (edited)
The HTTP Cross-Origin-Embedder-Policy (COEP) response header configures embedding cross-origin resources into the document.
Avatar
yeah I meant on my own project where I had the same error
00:27
but it might be caused by something totally different
00:27
because the js script actually gets loaded and works when I call it from a button callback
00:28
it just doesn't work on any py-scripts that gets executed at page loading
Avatar
bugzpodder 15/07/2023 00:57
hmm probably need an example to see. you can use pyscript.com and create one and post it? don't forget to defer loading pyscript script
01:16
the thing above the error is a textinput
01:16
note how when you make a change on it after the page is loaded the call to js.hljs.highlightAll() works fine
Avatar
Avatar
2_.
note how when you make a change on it after the page is loaded the call to js.hljs.highlightAll() works fine
bugzpodder 15/07/2023 04:48
so i recommend that instead of add_highlightjs() in a python plugin just do it directly in the html file, add a script tag. if it doesn't work split into two plugins one for loading highlightjs and that should come before simplecodeinput. the reason is if you inject a script it doesnt' get loaded automatically/immediately so there is some race condition here..
Avatar
same problems keeps happening even when I add a script tag to the page (edited)
11:10
the thing I find odd is that the js loading I actually copied from this plugin
11:10
which seems to work fine
Avatar
Loading_Eternal 15/07/2023 23:14
do you think pyscript can do a shopping cart for website? and how?
Avatar
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css"/> <script defer src="https://pyscript.net/latest/pyscript.js"></script> </head> <body> <py-script> print('hello world!') </py-script> <div> <center><h2>code your python</h2></center> <div><center><h3>pro tip: press <b>shift + enter</b> to execute the code</h3></center></div> <py-repl id="my-repl" auto-generate="true"> </py-repl> </div> </body> </html> why is the hello world output is behind in a seperate box like terminal
07:12
Avatar
Avatar
2_.
same problems keeps happening even when I add a script tag to the page (edited)
bugzpodder 16/07/2023 07:12
i copied your code and add a single line for the script tag and it worked: https://b412faf3-21af-4adc-894d-6174cf4f38a6.pyscriptapps.com/e317aba6-87de-4a4c-9987-73b1a47b8703/latest/ did you add a defer in script or something?
Avatar
Avatar
2_.
the thing I find odd is that the js loading I actually copied from this plugin
bugzpodder 16/07/2023 07:15
i dont see pytutor adding a pyscript though.
Avatar
Avatar
bugzpodder
i copied your code and add a single line for the script tag and it worked: https://b412faf3-21af-4adc-894d-6174cf4f38a6.pyscriptapps.com/e317aba6-87de-4a4c-9987-73b1a47b8703/latest/ did you add a defer in script or something?
Ye but having to manually add internal dependencies in ur html of a plugin is kinda annoying, I was hoping to get it working like py-tutor
Avatar
Avatar
bugzpodder
i dont see pytutor adding a pyscript though.
changing it to a js script instead of pyscript gives the same error
Jeff Glass started a thread. 16/07/2023 16:32
Avatar
Avatar
2_.
Ye but having to manually add internal dependencies in ur html of a plugin is kinda annoying, I was hoping to get it working like py-tutor
bugzpodder 16/07/2023 22:51
can you check your connect to async def connect(self):
Avatar
im not sure what u mean with that
Avatar
Can someone point me to a link to an example where py-click(say) is doing three commands chained together. I've seen it somewhere but I can't find it again ๐Ÿ˜ฆ
Avatar
Avatar
Neon22
Can someone point me to a link to an example where py-click(say) is doing three commands chained together. I've seen it somewhere but I can't find it again ๐Ÿ˜ฆ
Jeff Glass 18/07/2023 06:28
Hereโ€™s a really basic example; currently py-event execs its provided string as code https://3748111e-bd36-4293-8467-08ca7d447289.pyscriptapps.com/499b2a08-fa90-489a-b68f-4eb7de971c77/latest/
06:28
That is likely to change in a future release
Avatar
Avatar
Yoriichi
Click to see attachment ๐Ÿ–ผ๏ธ
use this version instead of latest version its working fine
Avatar
bugzpodder 21/07/2023 01:00
was able to integrate polyscript into my codebase (replacing pyscript). most annoying thing was that I am not sure how to access interpreter directly. ended up using hooks.onInterpreterReady and stuff it in globalThis... I kept around a few pyscript specific things mainly display and HTML..
Avatar
Avatar
bugzpodder
was able to integrate polyscript into my codebase (replacing pyscript). most annoying thing was that I am not sure how to access interpreter directly. ended up using hooks.onInterpreterReady and stuff it in globalThis... I kept around a few pyscript specific things mainly display and HTML..
Andrea Giammarchi 25/07/2023 20:53
import { env } from 'polyscript'; const interpreter = await env.pyodide; // or env.micropython // or env.wasmoon
20:55
polyscript is a module that exports a few things, including an env Proxy that resolves whenever a script with type xyz is bootstrapped on the page, or whenever a specific env is used ... if you don't specify any specific env, grabbing the interpreter is just env.pyodide and you don't need any hook ... and BTW, hooks are not shared globally, so I am not sure I understood your solution, but glad it worked anyway ๐Ÿ˜„
Avatar
Avatar
Andrea Giammarchi
polyscript is a module that exports a few things, including an env Proxy that resolves whenever a script with type xyz is bootstrapped on the page, or whenever a specific env is used ... if you don't specify any specific env, grabbing the interpreter is just env.pyodide and you don't need any hook ... and BTW, hooks are not shared globally, so I am not sure I understood your solution, but glad it worked anyway ๐Ÿ˜„
Andrea Giammarchi 27/07/2023 17:31
FYI, and apologies for late reply, this works for me: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script type="micropython"></script> <script type="module"> import { env } from 'https://esm.sh/polyscript'; const { runPython } = await env.micropython; runPython(`from js import document;document.body.textContent = "Hello There"`); </script> </head> </html> (edited)
Avatar
Marco Sarti 28/07/2023 10:43
Hi everyone, I'm very noob on pyscript and I'm stuck on a very stupid issue. So, I hope to receive an help here about my stupid point. I created a basic project following the guidelines and after I had read all documentation. I have an index.html with basic structure: <!DOCTYPE html> <html lang="en"> <head> <title>Test Prodigio v0.1</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width,initial-scale=1"> <link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" /> <script defer src="https://pyscript.net/latest/pyscript.js"></script> </head> <body> <div id="container"> Loading... </div> <py-config src="./pyscript.json" type="json"></py-config> <py-script src="./main.py"></py-script> </body> </html> and this is my main.py: from pyscript import Element Element("container").write("It's working") Unfortunately I receive this error. How to solve it? The same code works perfectly on pyscript.com as you can find and see at this link: https://12bbb25e-4e45-499b-8cab-e628d34b80e0.pyscriptapps.com/b2dd9786-5716-4539-bf06-0ba0eeaab8d3/latest/ Thanks if you can help me to understand what is wrong. (edited)
Avatar
Marco Sarti 28/07/2023 12:16
Avatar
Jeff Glass 28/07/2023 12:22
Your linked PyScriptApp is working for me:
12:23
Itโ€™s curious that you had a 500 error, which is an internal server error (the fetch error codes mirror the HTTP error codes https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#server_error_responses)
Avatar
Marco Sarti 28/07/2023 12:23
yes it seems to be a problem of permission on the files. In normal hosting area is not possible show files with .py extensions. The engine of pyscript try to assemble all using a fetch and I suppose that this is the problem. There is a way to solve it?
12:24
in pyscript.com if I digit main.py as continuation of the url I show all my code
Avatar
Jeff Glass 28/07/2023 12:28
Hmmm interesting - trying your demo code, I got a PY0001 error (โ€œAccess to local filesโ€ฆโ€) exactly once but it worked the 9 other times I reloadedโ€ฆ @FabioRosado do you know of anything odd with PSDC this morning?
12:29
Marco do you get the same error every time?
Avatar
Marco Sarti 28/07/2023 12:29
yes every time
12:30
I'm using IONOS hosting area to test my solution, and obviously pyscript.com to check
Avatar
Avatar
Jeff Glass
Hmmm interesting - trying your demo code, I got a PY0001 error (โ€œAccess to local filesโ€ฆโ€) exactly once but it worked the 9 other times I reloadedโ€ฆ @FabioRosado do you know of anything odd with PSDC this morning?
FabioRosado 28/07/2023 12:58
Just checked our server side logs and havent seen any error reported today
๐Ÿ‘ 1
Avatar
Marco Sarti 28/07/2023 13:26
I created a simple url to check the IONOS area https://test-pyscript.prodigioai.net/
13:30
the console
Avatar
Avatar
Marco Sarti
yes it seems to be a problem of permission on the files. In normal hosting area is not possible show files with .py extensions. The engine of pyscript try to assemble all using a fetch and I suppose that this is the problem. There is a way to solve it?
Andrea Giammarchi 28/07/2023 16:12
In normal hosting area is not possible show files with .py extensions
even GitHub pages is just fine with .py files and AFAIK any other "normal hosting area" ... why would your server block serving python? I understand maybe the mime type can be just text instead of the proper one, but blocking it feels weird ... as if there's anything able to execute it in the server if accessed by a browser (which is never the case)
16:12
you can, however, try to rename main.py to main.txt and see what happens there, as AFAIK (and IIRC) we don't check extensions there, we just fetch these files (edited)
Avatar
Marco Sarti 28/07/2023 16:15
Generally the code is always hide except js where I put only visual. I'll try immediately with your suggestion and I'll give you a feedback about it, but for business project I'm little confused about this point. There is some way to hide python code?
Avatar
Andrea Giammarchi 28/07/2023 16:17
what do you mean hide python code? what you publish as static file is accessible to anyone, unless your server is behind security measurements that protect those files ... just avoiding serving python files for static content makes literally no sense to me, and no other hosting service I know / used does that ... never store sensitive data in those files though, like you wouldn't in any JS or TXT or HTML or CSS file (or any other, for what it matters). (edited)
Avatar
Marco Sarti 28/07/2023 16:18
ok so I understood that you see python script as a normal js script only in a different language and I have to manage it as javascript hiding the logic in a API layer
Avatar
Andrea Giammarchi 28/07/2023 16:19
I am not following ... you can't hide any static file served from your site ... if those can be read by the browser, those are not hidden.
16:20
we're just fetching the file content to execute it as Python, it's not me seeing the project as something, it's how the Web works in general. You can fetch php and sql files too, if reachable and served as static content and not interpreter by the server, as example.
Avatar
Marco Sarti 28/07/2023 16:21
for instance I want to generate a rating from some data. I can do this in js, or pyscript but a normal user can understand my logic, or put in an API the logic passing the data and receive the payload with the result and use this in the normal static file. You are saying me that I have to manage pyscript as a static file
Avatar
Andrea Giammarchi 28/07/2023 16:21
now that I think about it, I think that's the culprit: your server tries to execute that python file when requested and it fails miserably as it doesn't know the pyscript module?!? (edited)
16:22
pyscript works with python source code, not with python executed on the server ... it execute python in the browser
Avatar
Marco Sarti 28/07/2023 16:24
ok so this is the point, I cannot use normal hosting but a dedicate area with a web server set to use pyscript properly, thanks now I can manage better all
Avatar
Avatar
Marco Sarti
ok so this is the point, I cannot use normal hosting but a dedicate area with a web server set to use pyscript properly, thanks now I can manage better all
Andrea Giammarchi 28/07/2023 16:26
whatever solution you are after, there must be a way to serve static files as content without interpreting any of these ... as if they are all .txt somehow, except the right mime-type should be provided. (edited)
Avatar
Marco Sarti 28/07/2023 16:29
I tried as you suggested and It works well and fast, thank you. I have only to think how to configure the project in a perfect way following this rule. Thank you for your support
๐Ÿ‘ 1
Avatar
What's the current state of things when it comes to websockets? Is there a way to do two way communication for something like a browser game where you want to get events from the server?
Avatar
Marco Sarti 29/07/2023 22:20
Hi guys, what is the best way to change only the value of a input of type hidden? <!-- original --> <input id="htest" type="hidden" value="0" /> <!-- new value --> <input id="htest" type="hidden" value="1" /> I tried to use this code in pyscript: from pyscript import Element Element("htest").write("1") but I obtained only a version like this: <input id="htest" type="hidden" value="0">1</input> I searched in the manual a solution but without success. Any idea? Thanks
Avatar
Marco Sarti 29/07/2023 22:39
I solved with: from pyscript import Element el = Element("htest") el.element.value = "1" Thank anyway
Avatar
Andrea Giammarchi 31/07/2023 09:30
<input id="htest" type="hidden" value="0">1</input>
09:30
ouch ... that's invalid HTML
09:31
but yes, your second solution works and it is correct
Avatar
bugzpodder 02/08/2023 07:14
I got webR to work in my app. It runs in the web worker thread but I only saw the interpreter and R packages install once. Last time I tried pyscript in a worker thread (a while back) I recall seeing it loads the same package multiple times. I currently have all the pyscript stuff run in the main thread but I'll revisit polyscript/pyscript to see if I can move things to worker thread
Avatar
Avatar
bugzpodder
I got webR to work in my app. It runs in the web worker thread but I only saw the interpreter and R packages install once. Last time I tried pyscript in a worker thread (a while back) I recall seeing it loads the same package multiple times. I currently have all the pyscript stuff run in the main thread but I'll revisit polyscript/pyscript to see if I can move things to worker thread
Andrea Giammarchi 02/08/2023 09:13
the worker approach in "classic PyScript" and polyscript is completely different so if you tried a while ago, please try it again ... but a Worker is an environment a part, it cannot consume main thread pyodide (if you bootstrapped pyodide there) so that if you want your page to load pyodide only once, you can use JavaScript to bootstrap the pyodide worker, and you'll load pyodide only once in the Worker.
09:16
the difference with webR is that it's always in a worker so of course you don't see it bootstrapping more than once, it's based on comlink ... with polyscript though, you cna do this: <script type="module"> import { XWorker } from 'https://esm.sh/polyscript'; new XWorker('./your-project.py', { type: 'pyodide', config: { fetch: ['./a.py', './b.py'], packages: ['numpy'] } }); </script> this will create a worker that botstraps pyodide once and the main difference with main thread is that to reach the document you need to from polyscript import xworker; document = xworker.window.document
09:17
On the other hand, we're working on PyScript Next in a way that in the future it will be possible to automate the "python in a worker" story and provide better DX around reaching the document, likely exposing that xworker.window.document reference in the global so that code using js.document would still work without changes.
Avatar
Is there any way to get pyscript to wait for an async js function to resolve before trying to get the value?
Avatar
Can polyscript be imported in TypeScript? Looks like there is a "types/" directory in the NPM package, but the package.json points to "./types/esm/index.d.ts" which does not exist. Maybe it's supposed to point to "types/polyscript/esm/index.d.ts" which does exist?
17:04
I am also starting some XWorkers from javascript, but can't seem to figure out how to access their interpreter's options
17:07
The docs mentions things like wrappers and hooks. Are those available when starting a worker in javascript via XWorker('worker.py', {...})?
Avatar
Avatar
FightMilk
What's the current state of things when it comes to websockets? Is there a way to do two way communication for something like a browser game where you want to get events from the server?
Anyone?
Avatar
Avatar
FightMilk
Anyone?
I don't think it would be possible to use websockets directly, but I am useing websockets in my pyscript webapp. I am using socket.io.js. pretty much I have a couple of js functions that handle the connections. and then using the js python interoperability I am calling the functions to init the connections from python, then when a message comes in js passes it aloge to be processed in python. You would need to have functions to send messages and to connect and disconnect.
20:30
something like that I think is your best bet.
Avatar
I'm using peerjs
Avatar
Same basic idea as socket.io.js? Do it in js and then python interop?
Avatar
yee, you could use pretty much any js library
Avatar
Gotcha. Thank you both.
Avatar
Avatar
BwbDwlan
Can polyscript be imported in TypeScript? Looks like there is a "types/" directory in the NPM package, but the package.json points to "./types/esm/index.d.ts" which does not exist. Maybe it's supposed to point to "types/polyscript/esm/index.d.ts" which does exist?
Andrea Giammarchi 05/08/2023 12:57
you are right, there's a typo in package.json ... I will fix it ASAP
Avatar
Avatar
BwbDwlan
I am also starting some XWorkers from javascript, but can't seem to figure out how to access their interpreter's options
Andrea Giammarchi 05/08/2023 12:58
access the interpreter from where? if from JS you start a micropython or pyodide worker the interpreter is within the worker realm (thread/environment) not in the main thread ... what am I missing?
Avatar
@Andrea Giammarchi Probably I'm the one missing something :D. I am trying to redirect a pyodide worker's stdout to my own function in JS
21:13
but maybe it's not possible and all has to go through onmessage/postmessage
21:15
by default I think stdout is written to the dev console, I was hoping to be able to write it to an HTML element
Avatar
yes, but I can't figure out how to get a reference to pyodide for the webworker
22:42
I have a sort of a workaround like this: class MyWriter(io.StringIO): def write(self, text): xworker.sync.display(text) sys.stdout = MyWriter() sys.stderr = MyWriter() (edited)
Avatar
Avatar
BwbDwlan
by default I think stdout is written to the dev console, I was hoping to be able to write it to an HTML element
Andrea Giammarchi 07/08/2023 09:44
with polyscript / PyScript next you can set worker.onerror = ({error}) => { do what you want }; after you create XWorker('./file.py') and the whole dance behind the scene is done automatically. That allows you, and us, to show errors from both main and worker in the page ... otherwise, in general, yes, Workers on the Web works only out of postMessage orchestration (optionally + Atomics + Proxy as we do here). (edited)
Avatar
Avatar
BwbDwlan
I have a sort of a workaround like this: class MyWriter(io.StringIO): def write(self, text): xworker.sync.display(text) sys.stdout = MyWriter() sys.stderr = MyWriter() (edited)
Inside of Pyodide you can use from pyodide_js import setStdout
07:12
To get a reference
Avatar
Avatar
hood
Inside of Pyodide you can use from pyodide_js import setStdout
Andrea Giammarchi 08/08/2023 15:43
that won't forward to the main thread though ... no DOM access in there if not orchestrated ... right? ๐Ÿค” my hint was about having both the worker and errors landing on the main thread
15:44
but then again, if the handler uses xworker.sync.showError(...) and there is a counter part, that'd work for errors or generic logs
Avatar
bugzpodder 08/08/2023 20:47
I was able to make good progress with xworkers. but didn't seem to do anything.
  • still trying to figure out how to associate exceptions (eg importing from a wrong library) with the execution context (when I am executing more than one script) and an error occurs, I can hook into onerror but may not know where the error block comes from. if the previous function runAsync throws that would be great but thats not how worker works.
(edited)
Avatar
Avatar
bugzpodder
I was able to make good progress with xworkers. but didn't seem to do anything.
  • still trying to figure out how to associate exceptions (eg importing from a wrong library) with the execution context (when I am executing more than one script) and an error occurs, I can hook into onerror but may not know where the error block comes from. if the previous function runAsync throws that would be great but thats not how worker works.
(edited)
Andrea Giammarchi 08/08/2023 22:25
you can't postMessage out of the blue ... you need sync there, otherwise the listener ignores any request (security reasons) .... but I am not sure I follow the multiple scripts question ... you mean multiple workers? @ntoll did a demo of dozen pompom landing on main out of dozen workers, I am sure the details are maybe blocking, but it should be already possible, whatever you are doing. P.S. to run async stuff, just pass the async option as true when creating XWorker ... it's _async in pyodide, in case you spin workers from pyodide from the main thread, instead of JS
22:26
also please keep in mind every worker is a different scope/env so they will all bootstrap a different pyodide, inevitably
22:27
that's how Workers work on the Web, even JS stuff doesn't survive cross worker, every worker is a sandbox a part
Avatar
Avatar
Andrea Giammarchi
you can't postMessage out of the blue ... you need sync there, otherwise the listener ignores any request (security reasons) .... but I am not sure I follow the multiple scripts question ... you mean multiple workers? @ntoll did a demo of dozen pompom landing on main out of dozen workers, I am sure the details are maybe blocking, but it should be already possible, whatever you are doing. P.S. to run async stuff, just pass the async option as true when creating XWorker ... it's _async in pyodide, in case you spin workers from pyodide from the main thread, instead of JS
bugzpodder 09/08/2023 00:40
I don't mean multiple workers. For example, I have two python blocks in a database that I want to execute on the same view (two charts on a dashboard). They may take a while to run so I'd like to take advantage of XWorker if possible in order not to block the main thread. And I want to execute using the same worker such that they bootstrap the same pyodide, and if any of the code block errors out I would display an error message. Something like this: const pyWorker = XWorker("./empty.py", { type: "pyodide" }); export function execPyCode(code, divId) { try { await pyWorker.execAsync(code, { context: { divId } }); } catch (e) { showError(divId, e); } I got this to work with webR (and pyscript on main-thread), just for fun here is the code: const webR: WebR = new WebR(); // This will throw an error if code execution failed! export async function webRExec(code: string, id: string, options: WebRScriptOptions) { // bootstraps webR interpreter only when this is called, which is nice. for pyscript it always loads pyodide on page load unfortunately. not sure about workers. await webR.init(); const elem = document.getElementById(id); await webR.installPackages(options.packages); const env = await new webR.REnvironment({ context: options.context }); await webR.evalRVoid(code, { env }); const msgs = await webR.flush(); ... handle canvasImage and canvasNewPage messages ... } (edited)
Avatar
bugzpodder 09/08/2023 10:26
@Andrea Giammarchi looks like once: trueis set to true here so i can only exec one code block for each worker https://github.com/pyscript/polyscript/blob/0bb0a5cb0a311cdff2939a92be096dd0d6feb2d3/esm/worker/_template.js#L40. it'd be nice to support multiple executions (edited)
Avatar
Is there a way to get this import recognised and have some intellisense for js module in VSCode? I was looking into source code and all imports from js import ... are wrapped like at attached screenshot. Does anyone have any tips? ๐Ÿ™‚
Avatar
Avatar
bugzpodder
@Andrea Giammarchi looks like once: trueis set to true here so i can only exec one code block for each worker https://github.com/pyscript/polyscript/blob/0bb0a5cb0a311cdff2939a92be096dd0d6feb2d3/esm/worker/_template.js#L40. it'd be nice to support multiple executions (edited)
Andrea Giammarchi 09/08/2023 11:06
that's only for the initial message, it has nothing to do with execution, it's the Worker engine bootstrap ... however, you need to export from the worker a function in sync and from the main thread you can await xworker.sync.execute_thing({some, data}) ... the sync.execute_thing in the worker must be a callback (sync or async) and it will receive thta object with some data fields ...
11:07
the sync utility allows dual way exchanges but in the worker it can be sync (unless you explicitly return a Promise from main) while in the main you inevitably need to await results, if needed, because main can't block the thread.
11:08
I hope this helps ... there are more examples in polyscript but pyscript should bring that feature in too
๐Ÿ‘ 1
Avatar
bugzpodder 09/08/2023 11:11
the worker is in python and execute_thing is a python function right? If we have static python code that will work. but i want to execute arbitrary python code as a string (loaded from a database), would that work? In the worker engine bootstrap it seems to run the python code here. I'd want to do this as well via a call: https://github.com/pyscript/polyscript/blob/0bb0a5cb0a311cdff2939a92be096dd0d6feb2d3/esm/worker/_template.js#L111 (edited)
Avatar
Avatar
bugzpodder
the worker is in python and execute_thing is a python function right? If we have static python code that will work. but i want to execute arbitrary python code as a string (loaded from a database), would that work? In the worker engine bootstrap it seems to run the python code here. I'd want to do this as well via a call: https://github.com/pyscript/polyscript/blob/0bb0a5cb0a311cdff2939a92be096dd0d6feb2d3/esm/worker/_template.js#L111 (edited)
Andrea Giammarchi 09/08/2023 11:28
the python running in the worker can import pyodide which offers runPythonAsync (can't remember if that's the exact name) so your execute_thing python function would accept data or any string from the main or from where you fetch such data and pass it to the worker that can evaluate python ... the worker is just a "regular" pyodide env and the xworker is there to facilitate these kind of use cases
๐Ÿ‘ 1
Avatar
bugzpodder 09/08/2023 15:19
thanks I did get it to work finally end to end, but the micropip.install is so flaky (just doesn't respond back and the whole thing hangs) that I decided to hold off on implementing: from polyscript import xworker import pyodide import micropip document = xworker.window.document async def run_async(code): return await pyodide.code.eval_code_async(code) async def micropip_install(packages): return await micropip.install(packages) xworker.sync.runAsync = run_async xworker.sync.micropipInstall = micropip_install
Avatar
Avatar
bugzpodder
thanks I did get it to work finally end to end, but the micropip.install is so flaky (just doesn't respond back and the whole thing hangs) that I decided to hold off on implementing: from polyscript import xworker import pyodide import micropip document = xworker.window.document async def run_async(code): return await pyodide.code.eval_code_async(code) async def micropip_install(packages): return await micropip.install(packages) xworker.sync.runAsync = run_async xworker.sync.micropipInstall = micropip_install
Andrea Giammarchi 09/08/2023 15:57
I am not sure you can install packages on demand ... or better, even if that's possible, it's likely that you need to remove previous installed packages first then install from scratch, but that might lead to errors ... this is the reason we offer a config.toml, or a config.json, where you can declare packages you want/need, and we bootstrap the interpreter with it ... if you do these things manually you are a bit on your own (and @hood here probably could help better than me) (edited)
15:59
also worth mentioning we're using pyodide.runPythonAsync in both polyscript and pyscript next, maybe that works better?
16:00
although that's from JS, maybe the Python API is different
16:01
P.S. if these functions come from the worker, you don't need async declaration, neither await in return ... from main, those functions will be always async, and the returned promise will fulfill out of the box: def run_async(code): return pyodide.code.eval_code_async(code) def micropip_install(packages): return micropip.install(packages)
16:02
this should be it and that's how returned promises make any function async anyway in JS
16:03
the difference though is where you await results, so maybe your code is just fine, as it requires less back and forward from the two worlds.
Avatar
bugzpodder 09/08/2023 21:14
I currently have on-demand package installations setup automatically via https://github.com/pyscript/pyscript/pull/1099 and it works fine. usually I do have multiple active code blocks that the user entered so I have no control over it other than best-effort installing of all required packages. but usually users behave nicely and require same packages in different code blocks .
Avatar
Nikita Churikov 11/08/2023 15:51
Hi! Could someone please help me with this issue https://github.com/pyscript/pyscript/discussions/1632 The problem is roughly the following I want to make a reveal-md presentation with pyscript in it. PyScript it's self works there, but I have problems with py-repl tag. It seems like some reveal-js styles are being applied to py-repl tag and that results in input window looking bad.
Hi! I'd like to ask, how to fix indentation for reveal.js presentation. On this screenshot you can see that I've selected the whole line, but this is not correct Also cursor is not working ...
Avatar
If I specify a .gz file to be loaded in the config, it gets automatically ungziped. Is there a way to prevent this?
Avatar
>https://discord.com/channels/972017612454232116/1024755729820811334/1139555785588875347 Thank you for the start of the documentation ๐Ÿ‘. Is there an example of how to use micropython instead of pyodide, if I wanted to eliminate the long loading time? (Hope I didn't miss it in the readme).
Avatar
Avatar
b48g55m
>https://discord.com/channels/972017612454232116/1024755729820811334/1139555785588875347 Thank you for the start of the documentation ๐Ÿ‘. Is there an example of how to use micropython instead of pyodide, if I wanted to eliminate the long loading time? (Hope I didn't miss it in the readme).
Oh, I found it in the polyscript attributes, good ๐Ÿ‘
Avatar
bugzpodder 14/08/2023 17:35
how do I use onWorkerReady when construction a XWorker in js? const options = { type: 'pyodide', config: {}, onWorkerReady: function(interpreter, worker) { console.log(interpreter, worker);}, }; new XWorker('./worker.py', options); gives error core.js:1 Uncaught (in promise) DOMException: Failed to execute 'postMessage' on 'Worker': function(interpreter, worker) { console.log(interpreter, worker); } could not be cloned. Edit: I added a xworker.sync.onWorkerReady and got unblocked here.. (edited)
Avatar
bugzpodder 14/08/2023 21:18
Also xworker.sync functions (eg run_async below) doesn't return errors as far as I know, so I can't await it in js land and catch errors. I got it to work using run_async_catch_error: def run_async(code): return pyodide.code.eval_code_async(code) async def run_async_catch_error(code): try: return await pyodide.code.eval_code_async(code) catch Exception as e: xworker.sync.onError(str(e)) (edited)
Avatar
haidar kahla 15/08/2023 09:57
hello guys , i have problem with pyscript , i couldn't import external module written in python and javascript to my pyscript application this is the code , any idea please , or anyone have an idea how to import an external package in pyscript ?
Avatar
haidar kahla 15/08/2023 10:05
I'm using python 3.11.0 and chrome
Avatar
Avatar
haidar kahla
hello guys , i have problem with pyscript , i couldn't import external module written in python and javascript to my pyscript application this is the code , any idea please , or anyone have an idea how to import an external package in pyscript ?
Nikita Churikov 15/08/2023 10:31
1. I think you are trying to use old api for dependencies. https://docs.pyscript.net/latest/reference/elements/py-config.html#dependencies-and-packages Try this: <py-config type="toml"> packages = ["no-api-sdk"] </py-config> I think that also you don't really need if __name__ == "__main__": statement (edited)
Nikita Churikov started a thread. 15/08/2023 10:35
Nikita Churikov started a thread. 15/08/2023 10:45
Avatar
Avatar
Nikita Churikov
1. I think you are trying to use old api for dependencies. https://docs.pyscript.net/latest/reference/elements/py-config.html#dependencies-and-packages Try this: <py-config type="toml"> packages = ["no-api-sdk"] </py-config> I think that also you don't really need if __name__ == "__main__": statement (edited)
haidar kahla 15/08/2023 12:43
do you think i can do the samething with brython ?
Nikita Churikov started a thread. 15/08/2023 13:18
Avatar
Hello guys, was wondering - can you do something like this in pyscript? <button py-click="on_click()">Click me..</button> if show_h1: <h1>Hello World</h1> <py-script> def on_click(): show_h1 = True </py-script> Comming from the Blazor world this is normal, is it like this in pyscript as well?
Avatar
haidar kahla 16/08/2023 09:50
hi guys , is there a way to call javascript functions from pyscript ?
Nikita Churikov started a thread. 16/08/2023 12:04
Nikita Churikov started a thread. 16/08/2023 12:18
Avatar
bugzpodder 17/08/2023 17:16
@Andrea Giammarchi @hood here is an example of polyscript + worker. Things starts to hang in definitely when it gets to micropip.install in the worker thread for some reason. https://b412faf3-21af-4adc-894d-6174cf4f38a6.pyscriptapps.com/9c92b15a-2412-4d76-a09a-f39912726b5a/latest/ submitted issue https://github.com/pyscript/polyscript/issues/14 (edited)
Avatar
Hello! How do I export async python functions in such a way that I can use await on the JavaScript side? E.g. // python import asyncio async def hello(): return 1 // export hello to JavaScript.. console.log(await hello())
Avatar
Avatar
YingTIG
Hello! How do I export async python functions in such a way that I can use await on the JavaScript side? E.g. // python import asyncio async def hello(): return 1 // export hello to JavaScript.. console.log(await hello())
Jeff Glass 23/08/2023 04:08
You can export object names directly to the JavaScript global namespace by assigning to them to โ€œjsโ€ module: import js async def hello(): return 1 js.hello_func = hello console.log(await hello_func())
๐Ÿ‘ 1
Avatar
Avatar
Jeff Glass
You can export object names directly to the JavaScript global namespace by assigning to them to โ€œjsโ€ module: import js async def hello(): return 1 js.hello_func = hello console.log(await hello_func())
I must of made a mistake somewhere when playing around... I was doing exactly that and the await returned a pyproxy object Trying again now, it works fine.. ๐Ÿคฃ
๐Ÿฅณ 1
Avatar
Jeff Glass 23/08/2023 04:18
I think they might both be trueโ€ฆ when you assign to js that way, the object is a pyproxy, but that pyproxy is awaitable/thenable. Under the hood, thereโ€™s a bunch of feature detection that happens (is the python object callable, enumerable, iterable, etc) and a JavaScript PyProxy object is created with โ€œmatchingโ€ characteristics
Avatar
Another one... why does this hang? <py-script> import js import asyncio import time class Test: def __init__(self): self.val = 1 async def run(self): while True: self.val += 1 time.sleep(1) js.test = Test() </py-script> <script> (function runTest(){ if(window.test){ console.log("Starting"); test.run(); console.log("Started"); setInterval(() => console.log(test.val), 1000); // this never logs?? <---------- }else{ setTimeout(runTest, 100); } })() </script> My console freezes running this, and the tab uses 100% of CPU? Should I be using an alternative to time.sleep? (edited)
Avatar
Avatar
YingTIG
Another one... why does this hang? <py-script> import js import asyncio import time class Test: def __init__(self): self.val = 1 async def run(self): while True: self.val += 1 time.sleep(1) js.test = Test() </py-script> <script> (function runTest(){ if(window.test){ console.log("Starting"); test.run(); console.log("Started"); setInterval(() => console.log(test.val), 1000); // this never logs?? <---------- }else{ setTimeout(runTest, 100); } })() </script> My console freezes running this, and the tab uses 100% of CPU? Should I be using an alternative to time.sleep? (edited)
Jeff Glass 23/08/2023 04:41
await asyncio.sleep()
04:42
time.sleep in CPython inherently relies on a multi-thread environment; in the browser, thereโ€™s no โ€œother threadโ€ to come back and resume the current thread after the timeout.
Avatar
I love easy fixes ๐Ÿ˜„
๐Ÿคฉ 1
04:43
thanks!
Avatar
Jeff Glass 23/08/2023 04:43
No sweat!
Avatar
I'm guessing that there's some funny timing things going on with pyodide and async.sleep. It seems to take more than 1s on average
Avatar
Avatar
YingTIG
I'm guessing that there's some funny timing things going on with pyodide and async.sleep. It seems to take more than 1s on average
Jeff Glass 23/08/2023 12:29
Hmmm havenโ€™t seen that before. Can you share your full code again?
Avatar
Emscripten: An LLVM-to-WebAssembly Compiler. Contribute to emscripten-core/emscripten development by creating an account on GitHub.
Avatar
Avatar
Jeff Glass
Hmmm havenโ€™t seen that before. Can you share your full code again?
its just the same as above but with asyncio.sleep <py-script> import js import asyncio import time class Test: def __init__(self): self.val = 1 async def run(self): while True: self.val += 1 await asyncio.sleep(1) js.test = Test() </py-script> <script> (function runTest(){ if(window.test){ console.log("Starting"); test.run(); console.log("Started"); setInterval(() => console.log(test.val), 1000); }else{ setTimeout(runTest, 100); } })() </script> (edited)
Avatar
Avatar
YingTIG
its just the same as above but with asyncio.sleep <py-script> import js import asyncio import time class Test: def __init__(self): self.val = 1 async def run(self): while True: self.val += 1 await asyncio.sleep(1) js.test = Test() </py-script> <script> (function runTest(){ if(window.test){ console.log("Starting"); test.run(); console.log("Started"); setInterval(() => console.log(test.val), 1000); }else{ setTimeout(runTest, 100); } })() </script> (edited)
Jeff Glass 23/08/2023 18:10
Huh, I"m seeing it count up at one second per second:
18:10
18:12
What platform/os/browser? My quick test was Windows 10/Chrome 115.
18:12
Any memory limitations? (edited)
Avatar
no memory limitations. I just tried again, and it starts lagging behind once I change tabs to something like youtube (edited)
18:14
or minimise chrome into the background for some other app
18:15
maybe whatever thread is running pyodide/wasm gets put on low priority when not focused?
18:18
Windows 10/Chrome 116 btw
Avatar
pyfetch doesnt seem to be sending the cookie ive set earlier with document.cookie
Jeff Glass started a thread. 24/08/2023 12:43
Avatar
Trying to get three.js working as a module (AGAIN; last time was a year ago and it took days...). This <script type="importmap"> { "imports": { "three": "https://cdn.jsdelivr.net/npm/three@0.154.0/build/three.module.js" } } </script> "works" (exactly as it should, and used to), to the extent that I see [plugins/importmap] Registering JS module three in the browser console, but there is absolutely no hint as to how to actually import it into Python. The previous from three import (AmbientLight, etc... does NOT work (nor does any other obvious syntax). Note that simply sourcing three works perfectly, but three are planning to deprecate everything but modules Real Soon Now.
Avatar
beniroquai 26/08/2023 08:48
Hey all, first of all: great project! I'd love to use python more for our embedded stuff that streams images into a browser from an ESP32-based camera. I would like to do image processing "on the fly", where new images are handed over to the pyscript processor and then displayed in the HTML again. I guess I had something working, but apparently it doesn't work anymore. (https://github.com/Matchboxscope/matchboxscope-simplecamera/blob/matchboxscope/main/index_holo.h#L29) Anybody knows a good template where there is a callback on a new image in an img-tag that then does something on the image before displaying it again? Sounds straightforward, I just don't know the right syntax.. Sorry ๐Ÿ˜• Any help very much appreciated ๐Ÿ™‚
Expanded version of the Espressif ESP webcam. Contribute to Matchboxscope/matchboxscope-simplecamera development by creating an account on GitHub.
Avatar
hey so im running into two things which are probably directly related. the first is when I add the <py-terminal></py-terminal> to the dom via javascript, pyscript doesnt insert a terminal there even after running a print("Hello World"), which would go to the terminal if im reading the docs correctly. the second is that when i have anything in <py-config> the loader splash never gets removed. It feels like something is not loading correctly, but nothing in the webdev console tells me what.
Avatar
Avatar
hosler
hey so im running into two things which are probably directly related. the first is when I add the <py-terminal></py-terminal> to the dom via javascript, pyscript doesnt insert a terminal there even after running a print("Hello World"), which would go to the terminal if im reading the docs correctly. the second is that when i have anything in <py-config> the loader splash never gets removed. It feels like something is not loading correctly, but nothing in the webdev console tells me what.
Jeff Glass 27/08/2023 00:43
Are you using the current release, or the PyScript Next preview?
Avatar
Avatar
hosler
hey so im running into two things which are probably directly related. the first is when I add the <py-terminal></py-terminal> to the dom via javascript, pyscript doesnt insert a terminal there even after running a print("Hello World"), which would go to the terminal if im reading the docs correctly. the second is that when i have anything in <py-config> the loader splash never gets removed. It feels like something is not loading correctly, but nothing in the webdev console tells me what.
Jeff Glass 27/08/2023 00:51
Are you using the current release, or the PyScript Next preview?
Avatar
Jeff Glass 27/08/2023 00:53
Oh gotcha, py-terminal wasnโ€™t in the alpha release at all, you may want to move to a more recent version. The terminal was added in 2022.12.1; the most recent release is 2023.05.1. https://pyscript.net/releases/2023.05.1/pyscript.js
Avatar
rofl didnt even realize i was on such an old version
Avatar
beniroquai 27/08/2023 06:46
On newer versions I had issues with asyncio-related stuff. @Jeff Glass I studied your examples very carefully ๐Ÿ˜‰ I guess the closest would be the Emoji Example, where A onChange listener waits for changes in the image tag, or?
Avatar
Avatar
YingTIG
no memory limitations. I just tried again, and it starts lagging behind once I change tabs to something like youtube (edited)
Andrea Giammarchi 28/08/2023 09:14
that's browsers prioritizing tasks ... I imagine behind the scene there's some timer scheduling in the JS world and that inevitably depends on visibility of the page ... there are APIs to deal with that but we can't enforce timers accuracy when a browser looks elsewhere, if I understand the issue here.
Avatar
Avatar
quaestor
Trying to get three.js working as a module (AGAIN; last time was a year ago and it took days...). This <script type="importmap"> { "imports": { "three": "https://cdn.jsdelivr.net/npm/three@0.154.0/build/three.module.js" } } </script> "works" (exactly as it should, and used to), to the extent that I see [plugins/importmap] Registering JS module three in the browser console, but there is absolutely no hint as to how to actually import it into Python. The previous from three import (AmbientLight, etc... does NOT work (nor does any other obvious syntax). Note that simply sourcing three works perfectly, but three are planning to deprecate everything but modules Real Soon Now.
Anyone? Maybe even a hint using another similar library as an example?
Avatar
Avatar
quaestor
Anyone? Maybe even a hint using another similar library as an example?
Andrea Giammarchi 29/08/2023 07:41
apologies I've missed that ... so there's no way to map JS imports to Python out of the box, but because you have access to the global JS context (in main) you can land three.js there and use it from Python <script type="importmap"> {"imports": {"three": "https://cdn.jsdelivr.net/npm/three@0.154.0/build/three.module.js"}} </script> <script type="module"> import * as three from 'three'; globalThis.three = three; </script> <py-script> from js import three print(three.AmbientLight) </py-script>
Avatar
bugzpodder 29/08/2023 15:29
pyscriptapps is being blocked by Cisco umbrella. I've reported the issue but not sure if reports are taken seriously
๐Ÿ‘€ 1
Avatar
Avatar
Andrea Giammarchi
apologies I've missed that ... so there's no way to map JS imports to Python out of the box, but because you have access to the global JS context (in main) you can land three.js there and use it from Python <script type="importmap"> {"imports": {"three": "https://cdn.jsdelivr.net/npm/three@0.154.0/build/three.module.js"}} </script> <script type="module"> import * as three from 'three'; globalThis.three = three; </script> <py-script> from js import three print(three.AmbientLight) </py-script>
globalThis.three = three; !!! OMG... TYSM, that's what I was missing!
15:37
Out of curiosity, what exactly does [plugins/importmap] Registering JS module three do?
Avatar
Avatar
quaestor
Out of curiosity, what exactly does [plugins/importmap] Registering JS module three do?
Andrea Giammarchi 29/08/2023 16:19
nothing relevant for your issue but basically it instruments the browser to use the related field value (the CDN url) when you import 'three' in your JS code (not in Workers, because Web development gotta suck somehow anyway)
๐Ÿ‘ 1
Avatar
Having issues sending a request to an api with the requests library, giving the No 'Access-Control-Allow-Origin' header is present on the requested resource. error. Is there any way around it?
Nikita Churikov started a thread. 06/09/2023 12:03
Avatar
Avatar
Andrea Giammarchi
apologies I've missed that ... so there's no way to map JS imports to Python out of the box, but because you have access to the global JS context (in main) you can land three.js there and use it from Python <script type="importmap"> {"imports": {"three": "https://cdn.jsdelivr.net/npm/three@0.154.0/build/three.module.js"}} </script> <script type="module"> import * as three from 'three'; globalThis.three = three; </script> <py-script> from js import three print(three.AmbientLight) </py-script>
NaturalAliens 07/09/2023 16:43
Hello @Andrea Giammarchi I tried your snippet from 29/08/2023 and I get: JsException(PythonError: Traceback (most recent call last): File "/lib/python3.10/site-packages/_pyodide/_base.py", line 429, in eval_code .run(globals, locals) File "/lib/python3.10/site-packages/_pyodide/_base.py", line 300, in run coroutine = eval(self.code, globals, locals) File "", line 1, in File "", line 1075, in _handle_fromlist JsException: TypeError: Cannot read properties of undefined (reading 'name') ) I have in <head>: <script defer src="https://pyscript.net/alpha/pyscript.js"onerror=scriptLoadFailure('pyscript.js')></script> and your exact example code in <body>, and nothing else. What am I doing wrong? looks like to import more recent Three.js modules like the model loaders, importmap is a must (edited)
Avatar
Avatar
NaturalAliens
Hello @Andrea Giammarchi I tried your snippet from 29/08/2023 and I get: JsException(PythonError: Traceback (most recent call last): File "/lib/python3.10/site-packages/_pyodide/_base.py", line 429, in eval_code .run(globals, locals) File "/lib/python3.10/site-packages/_pyodide/_base.py", line 300, in run coroutine = eval(self.code, globals, locals) File "", line 1, in File "", line 1075, in _handle_fromlist JsException: TypeError: Cannot read properties of undefined (reading 'name') ) I have in <head>: <script defer src="https://pyscript.net/alpha/pyscript.js"onerror=scriptLoadFailure('pyscript.js')></script> and your exact example code in <body>, and nothing else. What am I doing wrong? looks like to import more recent Three.js modules like the model loaders, importmap is a must (edited)
Andrea Giammarchi 07/09/2023 17:30
I am afraid I understand nothing from this message ... can you create a (private?) gist with the whole page and code you're running, or the minimal thing that reproduces the issue?
Avatar
Avatar
NaturalAliens
Hello @Andrea Giammarchi I tried your snippet from 29/08/2023 and I get: JsException(PythonError: Traceback (most recent call last): File "/lib/python3.10/site-packages/_pyodide/_base.py", line 429, in eval_code .run(globals, locals) File "/lib/python3.10/site-packages/_pyodide/_base.py", line 300, in run coroutine = eval(self.code, globals, locals) File "", line 1, in File "", line 1075, in _handle_fromlist JsException: TypeError: Cannot read properties of undefined (reading 'name') ) I have in <head>: <script defer src="https://pyscript.net/alpha/pyscript.js"onerror=scriptLoadFailure('pyscript.js')></script> and your exact example code in <body>, and nothing else. What am I doing wrong? looks like to import more recent Three.js modules like the model loaders, importmap is a must (edited)
Jeff Glass 07/09/2023 18:12
I've got an example working. Here's a sample HTML document; the three.js code is adapted/ripped from the three.js documentation here: https://threejs.org/docs/index.html#manual/en/introduction/Creating-a-scene: <!doctype html> <html lang="en"> <head><!-- Use one of the examples below--></head> <body> <script type="module"> import * as three from 'three'; globalThis.three = three; </script> <py-script> from js import three, document, Object, requestAnimationFrame from pyodide.ffi import to_js, create_once_callable scene = three.Scene.new() camera = three.PerspectiveCamera.new(75, 1, 0.1, 1000) renderer = three.WebGLRenderer.new() renderer.setSize(400,400) document.body.appendChild(renderer.domElement) geometry = three.BoxGeometry.new(1,1,1) material = three.MeshBasicMaterial.new(to_js({'color': 0x00ff00}, dict_converter=Object.fromEntries)) cube = three.Mesh.new(geometry, material) scene.add(cube) camera.position.z = 5 def animate(*args): requestAnimationFrame(create_once_callable(animate)) cube.rotation.x += 0.01 cube.rotation.y += 0.01 renderer.render(scene, camera) animate() </py-script> </body> </html>
18:13
For the <head> you can use either the PyScript Next core current release (in beta): <head> <script type="importmap"> {"imports": {"three": "https://cdn.jsdelivr.net/npm/three@0.154.0/build/three.module.js"}} </script> <script type="module" src="https://cdn.jsdelivr.net/npm/@pyscript/core@0.1.10/dist/core.js"></script> </head> or PyScript Classic: <head> <script type="importmap"> {"imports": {"three": "https://cdn.jsdelivr.net/npm/three@0.154.0/build/three.module.js"}} </script> <script defer src="https://pyscript.net/releases/2023.05.1/pyscript.js"></script> <link rel="stylesheet" href="https://pyscript.net/releases/2023.05.1/pyscript.css"> </head>
Avatar
Avatar
Jeff Glass
For the <head> you can use either the PyScript Next core current release (in beta): <head> <script type="importmap"> {"imports": {"three": "https://cdn.jsdelivr.net/npm/three@0.154.0/build/three.module.js"}} </script> <script type="module" src="https://cdn.jsdelivr.net/npm/@pyscript/core@0.1.10/dist/core.js"></script> </head> or PyScript Classic: <head> <script type="importmap"> {"imports": {"three": "https://cdn.jsdelivr.net/npm/three@0.154.0/build/three.module.js"}} </script> <script defer src="https://pyscript.net/releases/2023.05.1/pyscript.js"></script> <link rel="stylesheet" href="https://pyscript.net/releases/2023.05.1/pyscript.css"> </head>
NaturalAliens 07/09/2023 22:18
Thanks a lot Jeff this is helping me move forward. If I may ask a followup, my importmap is this: {"imports": {"three": "https://cdn.jsdelivr.net/npm/three@0.154.0/build/three.module.js", "OrbitControls": "https://cdn.jsdelivr.net/npm/three@0.154.0/examples/jsm/controls/OrbitControls.js"}} previously without modules I could use controls = THREE.OrbitControls.new(...) without having to declare anything, I tried the naive: import * as OrbitControls from 'OrbitControls'; globalThis.OrbitControls = OrbitControls; ...but then I have to do the slightly weird: controls = OrbitControls.OrbitControls.new(...) is this right? also, my code then fails on: my_task = PyScript.loop.create_task(app.initialize()) with error: NameError: name 'PyScript' is not defined I am using the beta: <script type="module" src="https://cdn.jsdelivr.net/npm/@pyscript/core@0.1.10/dist/core.js"></script> (edited)
Avatar
Jeff Glass 07/09/2023 22:28
For the first, you should be able to use import { OrbitControls } from 'OrbitControls'; (with the {} object destructuring)
Avatar
Avatar
NaturalAliens
Thanks a lot Jeff this is helping me move forward. If I may ask a followup, my importmap is this: {"imports": {"three": "https://cdn.jsdelivr.net/npm/three@0.154.0/build/three.module.js", "OrbitControls": "https://cdn.jsdelivr.net/npm/three@0.154.0/examples/jsm/controls/OrbitControls.js"}} previously without modules I could use controls = THREE.OrbitControls.new(...) without having to declare anything, I tried the naive: import * as OrbitControls from 'OrbitControls'; globalThis.OrbitControls = OrbitControls; ...but then I have to do the slightly weird: controls = OrbitControls.OrbitControls.new(...) is this right? also, my code then fails on: my_task = PyScript.loop.create_task(app.initialize()) with error: NameError: name 'PyScript' is not defined I am using the beta: <script type="module" src="https://cdn.jsdelivr.net/npm/@pyscript/core@0.1.10/dist/core.js"></script> (edited)
Jeff Glass 07/09/2023 22:29
<script type="module"> import * as three from 'three'; globalThis.three = three; import { OrbitControls } from 'OrbitControls'; globalThis.OrbitControls = OrbitControls; </script>
Avatar
Avatar
Jeff Glass
<script type="module"> import * as three from 'three'; globalThis.three = three; import { OrbitControls } from 'OrbitControls'; globalThis.OrbitControls = OrbitControls; </script>
NaturalAliens 07/09/2023 22:30
Thank you so much, for the second I found it: asyncio.create_task(app.initialize()) replaces PyScript.loop.create_task(app.initialize())
๐Ÿฅณ 1
Avatar
ShalolaAuthor 08/09/2023 03:39
so i have 4 .py files
03:39
it gives me this kind of error
03:40
i read a file using open on the first one. and on the 2nd and 3rd one i import from requesting_stuffs
03:43
i am scratching my head why is it not working? and from all the youtube video ive seen i think they used a deprecated tag called py-env
Avatar
ShalolaAuthor 08/09/2023 05:22
nvm i found a workaround
Avatar
ShalolaAuthor 08/09/2023 07:28
is there a way to cache pyodide?
07:28
seems downloading it everytime and installing it took a while
Avatar
@ShalolaAuthor you need to use py-config tag to fetch the files then you can import them <py-config> [[fetch]] files = ["/request.py"] </py-config> (edited)
11:06
That is not a good example link. Dang. Idk, someone can tell you how to use that, I cant find a link and I don't have time to explain it myself
Avatar
ShalolaAuthor 09/09/2023 23:38
no problem thanks for trying anyways! appreciate it
Avatar
ShalolaAuthor 09/09/2023 23:43
i think this will solve the issue of my first question.
Avatar
Avatar
NaturalAliens
Thank you so much, for the second I found it: asyncio.create_task(app.initialize()) replaces PyScript.loop.create_task(app.initialize())
@Jeff Glass a Three.js recipe would be great to add to your todo list for https://pyscript.recipes/ ๐Ÿ™‚
โค๏ธ 1
Avatar
hey folks! I've got a pyscript setup that I'd love to convert to pure wasm so I can run it on an edge runtime hosted by vercel, what's the recommended method for going for a pure wasm bundle from python? (edited)
Avatar
Avatar
Bryan
hey folks! I've got a pyscript setup that I'd love to convert to pure wasm so I can run it on an edge runtime hosted by vercel, what's the recommended method for going for a pure wasm bundle from python? (edited)
bugzpodder 12/09/2023 19:07
pyscript has direct access to DOM, if you don't need it you can run pyodide directly in nodejs? https://pyodide.org/en/stable/usage/index.html#node-js
Avatar
Avatar
bugzpodder
pyscript has direct access to DOM, if you don't need it you can run pyodide directly in nodejs? https://pyodide.org/en/stable/usage/index.html#node-js
Jeff Glass 12/09/2023 19:31
Pyodide also has DOM access, unless Iโ€™m misunderstanding
Avatar
Avatar
bugzpodder
pyscript has direct access to DOM, if you don't need it you can run pyodide directly in nodejs? https://pyodide.org/en/stable/usage/index.html#node-js
Oh neat, didnโ€™t realize I could run pyiodide from node itself! Very cool
Avatar
Avatar
Jeff Glass
Pyodide also has DOM access, unless Iโ€™m misunderstanding
bugzpodder 12/09/2023 21:11
pyodide passes jsglobals, which defaults to globalThis and you have document for free if you run in browser but pyodide of course doesn't really do anything with it. (it does require some stuff like fetch/setInterval/setTimeout/and maybe Object). I was recently playing around with restricting js.document in polyscript and this is what i found out.
Avatar
Eruvanos [UTC+1] 13/09/2023 20:28
Hi, I am looking into a way to use npm packages within pyscript. My idea was to use esbuild and a index.ts, which imports the modules and exports them to some global scope, but I cant get it to work. Anybody was able to use npm packages and has an example?
Eruvanos [UTC+1] started a thread. 13/09/2023 21:16
Avatar
Avatar
Eruvanos [UTC+1]
Hi, I am looking into a way to use npm packages within pyscript. My idea was to use esbuild and a index.ts, which imports the modules and exports them to some global scope, but I cant get it to work. Anybody was able to use npm packages and has an example?
bugzpodder 14/09/2023 05:45
@antocuni saw your thread regarding calling worker to run python code from js (could not reply to it), I have an existing example available: https://pyscript.com/@bugzpodder/polyscript-worker/latest I think the key is that you'll need your worker to be ready before you can call xworker.sync.runAsync. So I had to hack together a onWorkerReady function since I don't believe polyscript.XWorker exposes such a hook. related https://github.com/pyscript/polyscript/issues/20 (edited)
Avatar
Avatar
bugzpodder
@antocuni saw your thread regarding calling worker to run python code from js (could not reply to it), I have an existing example available: https://pyscript.com/@bugzpodder/polyscript-worker/latest I think the key is that you'll need your worker to be ready before you can call xworker.sync.runAsync. So I had to hack together a onWorkerReady function since I don't believe polyscript.XWorker exposes such a hook. related https://github.com/pyscript/polyscript/issues/20 (edited)
thank you, this is useful. In my experiments I'm doing the opposite: the JS side does a xworker.postMessage('init') and the python side listen to the message. But now I wonder whether this is guaranteed to work or if there is a possible error condition
Avatar
Hi! I have a question. Can I use my own library in pyscript? For example: ยด<html> <head> <title>Ice Cream Picker</title> <meta charset="utf-8"> <link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" /> <script defer src="https://pyscript.net/latest/pyscript.js"></script> </head> <body> <py-config>
packages = ["matplotlib", "pandas", "MY OWN LIBRARY"]
</py-config> </body> </html>ยด
(edited)
Jeff Glass started a thread. 14/09/2023 14:00
Avatar
Avatar
antocuni
thank you, this is useful. In my experiments I'm doing the opposite: the JS side does a xworker.postMessage('init') and the python side listen to the message. But now I wonder whether this is guaranteed to work or if there is a possible error condition
bugzpodder 14/09/2023 17:27
I have tested error (exception) condition handlling from xworker.sync and here is a code snippet: https://github.com/pyscript/polyscript/issues/15#issuecomment-1716380394 (edited)
Avatar
Pyodide isn't support tkinter, does anyone know how to fix it? For example, finding an alternative library or something?
Avatar
Avatar
Maolin
Pyodide isn't support tkinter, does anyone know how to fix it? For example, finding an alternative library or something?
Jeff Glass 14/09/2023 20:02
Since you're building on the web, you could pretty much use any web-based UI library... but if you're looking for more a more Python-centric option you might check out htag, which has an export-to-pyscript option: https://github.com/manatlan/htag
Python3 GUI toolkit for building &quot;beautiful&quot; applications for mobile, web, and desktop from a single codebase - GitHub - manatlan/htag: Python3 GUI toolkit for building &quot;...
Avatar
Avatar
Maolin
Pyodide isn't support tkinter, does anyone know how to fix it? For example, finding an alternative library or something?
Jeff Glass 14/09/2023 20:05
As far as tkinter support, it would require either creating a new backend for tkinter itself, or replicating its API with browser-based functionality, both of which would be a significant lift and I don't know of anyone working on it. See Gyeongjae's (ryanking'13) comment from last year https://github.com/pyodide/pyodide/issues/2172#issuecomment-1038749714
Avatar
Is pygame feasible? (edited)
Avatar
Avatar
Maolin
Is pygame feasible? (edited)
Jeff Glass 14/09/2023 23:34
I believe itโ€™s plausible but also not implemented. @pmp-p may have more recent news or alternatives
23:34
Pyxel is supported (if experimental) (edited)
Avatar
Avatar
Maolin
Is pygame feasible? (edited)
pygame on pyodide/pyscript is still not very practical to implement because game loops and filesystems internal requirements are high. It's getting there but meanwhile you can use something close pygame script or github embedding pygame canvas
Run python and pygame code in your html
06:08
original pygame issue was there https://github.com/pyodide/pyodide/issues/289 but now it's up to pygame-ce ( community edition ) and pyodide
Avatar
also there's that new wheel to test https://github.com/pygame-web/archives/blob/main/repo/pkg/pygame_static-1.0-cp311-cp311-wasm32_bi_emscripten.whl . It may just work it's so fresh i did not test it yet on pyodide
06:16
nb the entry point is "import pygame_static" not pygame and you must manage to download the python part of pygame-ce <= (note the -ce) in the filesystem + sys.path (edited)
Avatar
disregard that, i just tried it and it doesn't work yet. if you want pygame now just use pygbag until we solve full sdl2 integration in pyodide
Avatar
Avatar
Maolin
Pyodide isn't support tkinter, does anyone know how to fix it? For example, finding an alternative library or something?
as for tkinter the closest thing i found is that https://www.androwish.org/home/wiki?name=undroidwish which is tkinter over SDL2 but it was very hard to build for wasm last time i tried (edited)
Avatar
Since the source code is related to tkinter, I'm considering switching to pyxel, hopefully it's feasible.
Avatar
note that pyxel / pygame-ce don't make the same results https://itch.io/games/tag-pyxel https://itch.io/c/2563651/pygame-wasm pyxel is a virtual console while pygame gives you full power and 100% portability of the game
Avatar
the source code is related to pacman game, i think pyxel is enough, right?
Avatar
yeah for retro game better choose pyxel you willl finish quicker because it's more focused (edited)
Avatar
Avatar
antocuni
thank you, this is useful. In my experiments I'm doing the opposite: the JS side does a xworker.postMessage('init') and the python side listen to the message. But now I wonder whether this is guaranteed to work or if there is a possible error condition
Andrea Giammarchi 15/09/2023 12:47
FYI we have a py:ready event now that is triggered on the main thread for whenever main scripts or workers are ready to run/execute the code ... the event is triggered right before so that using a queueMicrotask(callback) to use anything from the worker would do ... and btw, sync uses an async guard so in general on main you can always use it to define whatever you like but I haven't tried the other way around ... although I think, by design, it should work.
12:48
in polyscript that's pyodide:ready or micropython:ready or in short whatever interpreter or custom type name is defined.
12:49
this was brought in to simplify any sort of bootstrap / attach things / do whatever so that the postMessage('init') from @antocuni shouldn't be needed at all anymore
๐Ÿ™ 1
Avatar
Avatar
pmp-p
yeah for retro game better choose pyxel you willl finish quicker because it's more focused (edited)
thank you, i am trying it.
Avatar
When pyscript fetches files based on py-config, does it put them in a file system? I essentially want to know if i can provide those fetched files as something a user can edit. Once edited I want to be able to use them just as before. I mean import from them and all the other things.
Avatar
Avatar
sai
When pyscript fetches files based on py-config, does it put them in a file system? I essentially want to know if i can provide those fetched files as something a user can edit. Once edited I want to be able to use them just as before. I mean import from them and all the other things.
Jeff Glass 17/09/2023 14:37
It does! It sticks them inside a โ€œvirtualโ€ in-memory filesystem that exists for the lifetime of the page. You can read more about the filesystem in the Emscripten docs - PyScript/Pyodide uses a โ€œMEMFSโ€ type filesystem (in-memory) by default: https://emscripten.org/docs/api_reference/Filesystem-API.html
๐Ÿ‘ 1
14:40
All the familiar Python functions for interacting with this filesystem (open, file.read(), file.write, os.listdir/cwd/chdir etc) work normally with this filesystem. Just remember that nothing in this filesystem is persistent - it all goes away when the page unloads, so if you need to persist data, you may need to use a more persistent filesystem like IndexDB, or have the user download the file(s), or use a more web ish solution like a database backing your application.
๐Ÿคฏ 1
Avatar
takacsjanos 18/09/2023 08:14
Is there a way to cache the state to local storage after the python runtime is initialized and packages are installed?
Avatar
@takacsjanos there has been some discussion of this. AFAICT it would involve (perhaps via a service like PyScript.com) running the app and setting everything up so the filesystem has all the requirements, then... since the filesystem in Emscripten (which is what we use for this) is just a JavaScript byte array, taking a dump of this blob... then when the app is next called for the blob is served and the filesystem re-hydrated. But this needs lots more investigation and what I've just written is very off-the-top-of-my-head hand-wavey in nature.
bugzpodder started a thread. 18/09/2023 16:58
Avatar
Original message was deleted or could not be loaded.
Jeff Glass 22/09/2023 18:24
Avatar
Hi. The documentation has changed and it has become incomplete. I am interested in a question. How to make get, post requests to the api?
Avatar
Original message was deleted or could not be loaded.
Jeff Glass 22/09/2023 18:31
Yes! But in that release, I'd recommend using from pyscript import document to make you're threads worker-friendly. Basically, in the main thread, document is js.document, and in a worker, it's a proxy for the main thread/page's document.
18:31
Right??
18:32
I think @tedpatrick 's changelog says it best: ---- Changelog: We re-wrote everything. ๐Ÿš€
Avatar
Avatar
Shram
Hi. The documentation has changed and it has become incomplete. I am interested in a question. How to make get, post requests to the api?
Jeff Glass 22/09/2023 18:36
Here's the guide on patching the requests library using pyodide-http to make get, post requests: https://github.com/pyscript/pyscript/blob/2023.05.1/docs/tutorials/requests.md
18:38
Oh yeah, it's meant to emulate exactly what requests does on desktop, which is block. If you want non-blocking, I'd recommend using pyfetch from Pyodide directly: https://github.com/pyscript/pyscript/blob/2023.05.1/docs/tutorials/requests.md https://pyodide.org/en/stable/usage/api/python-api/http.html#pyodide.http.pyfetch
18:39
(The rest of the tutorial on that first link about creating your own request function is a little much IMHO, and the name collision with requests has been troublesome. I'd just patch requests or use pyfetch, depending on whether you want blocking or non-blocking.
Avatar
I apologize if this is documented somewhere, but I can't find it. In the new 2023.09.1 RC1, how do we send an object from the JavaScript side to the Python side? In the last release, we used pyscript.interpreter.globals to grab a function from Python in Javascript then send it an object. However, "pyscript" doesn't appear to be defined in JavaScript anymore.
Avatar
Avatar
Ben Smith
I apologize if this is documented somewhere, but I can't find it. In the new 2023.09.1 RC1, how do we send an object from the JavaScript side to the Python side? In the last release, we used pyscript.interpreter.globals to grab a function from Python in Javascript then send it an object. However, "pyscript" doesn't appear to be defined in JavaScript anymore.
Andrea Giammarchi 25/09/2023 11:13
we don't leak globals anymore because pyscript is now a standard module. You can grab its env export and await env['py-script'] when you have pyodide there and you can do whatever you want with it, as that's the interpreter you were after before. Please note with workers the story is a bit different as there is a sync utility instead but that's very new and probably not what you are looking for at this point.
11:15
alternatively you can simply add a window.addEventListener('py:ready', clalback) event which receives the node that bootstrapped ... or you can import hooks from the module, add any onInterpreterReady or onBeforeRun callback which will be invoked with the node and the wrapper with all the utilities, including the interpreter.
11:15
the latter way is what we use too and it's likely the best way to do so.
11:16
import { hooks } form '@pyscript/core'; hooks.onInterpreterReady.add((wrap, element) => { console.log(wrap); }); as example. (edited)
11:19
the wrap.interpreter will be pyodide with its own API https://pyscript.github.io/polyscript/#custom-scripts-wrappers
Polyscript - PyScript single core to rule them all
Avatar
Avatar
Ben Smith
I apologize if this is documented somewhere, but I can't find it. In the new 2023.09.1 RC1, how do we send an object from the JavaScript side to the Python side? In the last release, we used pyscript.interpreter.globals to grab a function from Python in Javascript then send it an object. However, "pyscript" doesn't appear to be defined in JavaScript anymore.
Jeff Glass 25/09/2023 11:28
To add onto what Andrea said - if what you need is access to a specific Python object/function on the JS side (as opposed to the whole interpreter), you can assign directly to the js module: import js def foo(arg): print(arg) js.fooFunc = foo Will create a JS object in the global scope called fooFunc that is a proxy for the Python function foo
๐Ÿ‘ 2
11:30
In theory, you could also use from pyscript import window to always grab a reference to the main threadโ€™s global scope, even if your python code is running in a worker, but I havenโ€™t tested if assigned to that works the same as assigning to js
Avatar
Thanks folks. I think I'm going with Jeff's method as it seems simpler for my use case.
Avatar
Andrea Giammarchi 25/09/2023 11:39
if it works, great ... you wrote "how do we send an object from the JavaScript side to the Python side?" though while Jeff told you how to do the other way around unless, of course, you want to use that passed value from JS into Python, to which I agree it's a simpler approach. Thanks Jeff!
๐Ÿ‘ 1
Avatar
Yes, I have a total of five Python functions that my JavaScript code calls. I also have four Javascript functions my Python code calls. This is the extent of the communication between Python and Javascript in my app. In all cases, they are sending blobs of data or results back and forth.
Avatar
Avatar
Jeff Glass
To add onto what Andrea said - if what you need is access to a specific Python object/function on the JS side (as opposed to the whole interpreter), you can assign directly to the js module: import js def foo(arg): print(arg) js.fooFunc = foo Will create a JS object in the global scope called fooFunc that is a proxy for the Python function foo
This looks like it will be my solution to this issue. I have not gotten this far in porting my app to next, but I am using the exact convention that was described by op
Avatar
Avatar
Jeff Glass
In theory, you could also use from pyscript import window to always grab a reference to the main threadโ€™s global scope, even if your python code is running in a worker, but I havenโ€™t tested if assigned to that works the same as assigning to js
Am I correct in assuming the "js.fooFunc = foo" method only works when the python script is NOT running in a worker? I was able to get everything to work when I did not enable the worker flag (i.e. in the script tag calling the .py file). However, my Python exported functions are not available when I enable the worker flag.
Avatar
Andrea Giammarchi 25/09/2023 13:20
with PyScript and workers when you want to reach the main thread global context you use from pyscript import window instead of import js ... js is still there but it refers to the global context in the worker, unreachable by main out of the box.
Avatar
Avatar
Ben Smith
Am I correct in assuming the "js.fooFunc = foo" method only works when the python script is NOT running in a worker? I was able to get everything to work when I did not enable the worker flag (i.e. in the script tag calling the .py file). However, my Python exported functions are not available when I enable the worker flag.
Jeff Glass 25/09/2023 13:20
Exactly the js module is always a reference to the current global scope. When run in a worker, that means that worker's global scope, which isn't available to js in the main thread
13:20
Oh lol, Andrea beat me to it ๐Ÿ˜…
Avatar
Andrea Giammarchi 25/09/2023 13:21
if you from pyscript import window in main, you get the equivalent of js ... so that when the main world is desired, always use from pyscript import window instead, and you'll have portable code for both workers and main thread.
๐Ÿ’ฏ 1
Avatar
Jeff Glass 25/09/2023 13:22
Similarly, from pyscript import document always reference's the main thread's document. A lot of older PyScript (and Pyodide) examples import document, so this is a handy "drop in" fix to make them work in workers
๐Ÿ‘ 1
Avatar
Got it. Thanks folks!
Avatar
Hi, I'm interesting in three.js on PyScript (or Pyodide?), and now I'm try it. I saw WebGL demo ๐Ÿ™‚ I have a question. Can I find the source code of js.THREE module somewhere? I think it will most likely the wrapper code of js library.
Avatar
Avatar
lyouta
Hi, I'm interesting in three.js on PyScript (or Pyodide?), and now I'm try it. I saw WebGL demo ๐Ÿ™‚ I have a question. Can I find the source code of js.THREE module somewhere? I think it will most likely the wrapper code of js library.
hi, isn't that just a proxy to normal three.js module ?
05:42
"js" represents the javascript namespace python side
Avatar
Avatar
pmp-p
hi, isn't that just a proxy to normal three.js module ?
I don't understand meaning of word "proxy", because Iโ€™m not familiar with JavaScript. If my understanding is correct, the huge module named "js" including e.g.THREE, Math, Object, ... and so on is connecting to JavaScript through such as Pyodide, then execute three.js library on JavaScript, right? I don't accurately know the mechanism which is existing between Python running on Wasm and JavaScript on browser... if so, I think the wrapper library of three.js called THREE written by Python is existing. I want to look up following from source code. For example, correct type information of object related to THREE being executed in Python side. Complete stub files of THREE, if exists. And, can I access all classes and functions of three.js from Python? Which version of three.js is supported by THREE? and so on.
Avatar
the huge module named "js" is not "huge" and is not really a "module", it is some sort of bridge beetween javascript environnement and python env. it's called a proxy because it is a "man in the middle" translating requests from one side to another on the fly
08:52
@lyouta if I were describing things in the style of a science fiction novel, the js module is just a "wormhole" in the space/time (browser) continuum that connects the Python world with the parallel reality that is JavaScript (and vice versa). Stuff is sent to/from the wormhole between the two worlds. But the js module in Python itself is only the wormhole layer, the actual work is happening over in the JavaScript world (it works both ways).
08:54
So that's why it's a "proxy". From Python you're actually talking to the js wormhole (and the js wormhole is passing stuff to/from the JavaScript world behind the scenes).
Avatar
Avatar
ntoll
Click to see attachment ๐Ÿ–ผ๏ธ
hey here's a python playable one (panda3d) ๐Ÿ˜„ (edited)
Avatar
yeah... talking of "wormholes" (tubes?) https://langa.pl/downthedrain/
Avatar
Avatar
pmp-p
hey here's a python playable one (panda3d) ๐Ÿ˜„ (edited)
Oh my, that's awesome
08:59
08:59
Very classic "Elite"
09:00
@pmp-p but that's PyGame for WASM right..?
Avatar
above link is panda3d wasm wheel
Avatar
I notice there are several "in the browser" PyWeek entries (this is a good thing!): https://pyweek.org/36/diaries/
09:01
Aha... nice.
Avatar
it loads on pyodide but only on firefox it get some trouble with getting wasm compilation on v8
Avatar
Last time I did PyWeek much of the judging involved trying to figure out how to get some module or other working on Linux (and failing). In the browser โ„ข๏ธ - it's a thing!
09:02
@pmp-p lucky I'm a firefox user ๐Ÿ˜‰
Avatar
is there a way to install directly a .whl with micropip ?
Avatar
In PyScript? Yeah...
09:04
in your config just have the line (off the top of my head): packages = ["https://github.com/pygame-web/archives/blob/main/repo/pkg/panda3d-1.11.0-cp311-cp311-wasm32_bi_emscripten.whl" ] (Lemme just try)
๐Ÿ‘ 1
09:05
Hmm... that's a nope: (PY1001): Unable to install package(s) 'https://github.com/pygame-web/archives/blob/main/repo/pkg/panda3d-1.11.0-cp311-cp311-wasm32_bi_emscripten.whl'. Reason: Traceback (most recent call last): File "/lib/python3.11/site-packages/micropip/_micropip.py", line 576, in install await transaction.gather_requirements(requirements) File "/lib/python3.11/site-packages/micropip/_micropip.py", line 342, in gather_requirements await gather(*requirement_promises) File "/lib/python3.11/site-packages/micropip/_micropip.py", line 353, in add_requirement wheel.check_compatible() File "/lib/python3.11/site-packages/micropip/_micropip.py", line 134, in check_compatible raise ValueError( ValueError: Wheel was built with Emscripten vwasm32.bi.emscripten but Pyodide was built with Emscripten v3.1.32 . Please open an issue at https://github.com/pyscript/pyscript/issues/new if you require help or you think it's a bug.
09:05
Looks like they need to be compiled with the same Emscripten
Avatar
seems like an artificial restriction
09:06
that wasm is mvp+bigint it should just load anywhere
09:07
on firefox i could loadit with await (await pyfetch(whl)).unpack_archive()
09:08
must go, but i wish there's a simple trick to load arbitrary .so / wheels
๐Ÿ‘ 1
Avatar
Avatar
pmp-p
the huge module named "js" is not "huge" and is not really a "module", it is some sort of bridge beetween javascript environnement and python env. it's called a proxy because it is a "man in the middle" translating requests from one side to another on the fly
You mean... the layer like wrapper library that I said is not exists? interesting ๐Ÿ˜„ I misunderstood and thought PyScript (or Pyodide) developper comunity rewrite such python code every time version up of JavaScript library. The one exception is stub files for type hint? I found "js.pyi" in Pyodide module. or a part of module that I can get from pip or github is only existing for type hint? because I felt like there were many unimplemented functions.
Avatar
I'm testing my app using the new release candidate (09.01 RC1). I've found a problem that I'm trying to determine if it is a bug in PyScript, PyOdide, or maybe my code. This only happens in Firefox (always works in Safari and Chrome), and most confusingly, only happens intermittently in Firefox. On load, my test site (https://project-assessment-test-site.uc.r.appspot.com; which is running the RC version of PyScript) will produce the following error in the console: "Source map error: Error: unsupported protocol for sourcemap request pyodide.mjs.map Resource URL: Source Map URL: pyodide.mjs.map". If this happens, Python functions are not accessible to JavaScript code (in fact, I'm pretty sure Python is just not available). However, sometimes when I reload the page, this map warning goes away. When this happens, everything starts to work. I can't figure out a pattern on when this reload fixes the issue. Info that might be relevant: 1. I'm loading Pyscript in the normal way: "<script type="module" src="https://pyscript.net/snapshots/2023.09.1.RC1/core.js"></script>" 2. I'm running my python code in a Worker: "<script type="py" src="./startup.py?ref=20230925" config="./pyscript.toml" worker></script>" 3. I am sending all of the special headers needed when running in a worker. Any thoughts? (edited)
Avatar
Avatar
Ben Smith
I'm testing my app using the new release candidate (09.01 RC1). I've found a problem that I'm trying to determine if it is a bug in PyScript, PyOdide, or maybe my code. This only happens in Firefox (always works in Safari and Chrome), and most confusingly, only happens intermittently in Firefox. On load, my test site (https://project-assessment-test-site.uc.r.appspot.com; which is running the RC version of PyScript) will produce the following error in the console: "Source map error: Error: unsupported protocol for sourcemap request pyodide.mjs.map Resource URL: Source Map URL: pyodide.mjs.map". If this happens, Python functions are not accessible to JavaScript code (in fact, I'm pretty sure Python is just not available). However, sometimes when I reload the page, this map warning goes away. When this happens, everything starts to work. I can't figure out a pattern on when this reload fixes the issue. Info that might be relevant: 1. I'm loading Pyscript in the normal way: "<script type="module" src="https://pyscript.net/snapshots/2023.09.1.RC1/core.js"></script>" 2. I'm running my python code in a Worker: "<script type="py" src="./startup.py?ref=20230925" config="./pyscript.toml" worker></script>" 3. I am sending all of the special headers needed when running in a worker. Any thoughts? (edited)
Andrea Giammarchi 26/09/2023 11:02
this looks very Firefox related ... source map files are a developer only thing and these should not even be fetched if your devtools console is closed ... these are comments in the optimized source file that point at something useful only for debuging purpose. The fact the file misses in pyodide CDN is probably causing your issue but it's still strictly not a Pyodide issue, rather Firefox that bails out when source maps are not available which is completely unexpected and surely not aligned with what other browsers do. There are tons of bugs around FF sourcemap https://bugzilla.mozilla.org/buglist.cgi?quicksearch=sourcemap so I am afraid you should test things without devtools enabled or explicitly disable sourcemap in your Firefox and see if you have consistently working code.
Avatar
Avatar
Andrea Giammarchi
this looks very Firefox related ... source map files are a developer only thing and these should not even be fetched if your devtools console is closed ... these are comments in the optimized source file that point at something useful only for debuging purpose. The fact the file misses in pyodide CDN is probably causing your issue but it's still strictly not a Pyodide issue, rather Firefox that bails out when source maps are not available which is completely unexpected and surely not aligned with what other browsers do. There are tons of bugs around FF sourcemap https://bugzilla.mozilla.org/buglist.cgi?quicksearch=sourcemap so I am afraid you should test things without devtools enabled or explicitly disable sourcemap in your Firefox and see if you have consistently working code.
Unfortunately, I've encountered it not working when dev tools are closed. Of course, without dev tools, I really don't know what might be going on. Maybe the fact that it has a CDN miss for the map file also indicates some other part of pyodide is also being loaded incorrectly?
Avatar
Avatar
Ben Smith
Unfortunately, I've encountered it not working when dev tools are closed. Of course, without dev tools, I really don't know what might be going on. Maybe the fact that it has a CDN miss for the map file also indicates some other part of pyodide is also being loaded incorrectly?
Andrea Giammarchi 26/09/2023 11:11
to be honest, I've just tried your https://project-assessment-test-site.uc.r.appspot.com/ test page with Firefox and never had an issue or even an Network error ... maybe you have FF configured in a custom/weird way that gives you trouble or you have some extension that is failing? I have all 200 there without issues whatsoever.
Avatar
Avatar
Andrea Giammarchi
to be honest, I've just tried your https://project-assessment-test-site.uc.r.appspot.com/ test page with Firefox and never had an issue or even an Network error ... maybe you have FF configured in a custom/weird way that gives you trouble or you have some extension that is failing? I have all 200 there without issues whatsoever.
Ok, I'll try a clean install of Firefox and see if that fixes anything.
Avatar
FWIW I just tried it too on latest Firefox and "it works for me" ๐Ÿคทโ€โ™‚๏ธ - in the past I've been bitten because I've had plugins enabled that interfere with the web page or settings not-quite-as-default and I spent several hours thinking it was one thing, only to realise I had a Firefox hiccup. I think your idea of testing it with a clean install of Firefox is a good idea ๐Ÿ‘ - please let us know how you get on.
Avatar
Josh (EduBlocks) 27/09/2023 23:57
Playing around with PyScript Asyncio which is wrapped inside a custom module (test.py). When i run this locally i get the expected outcome of each command waiting until the previous is completed. However in PyScript they all run together. Is there something i'm missing that I need to enable? https://pyscript.com/@joshualowe1002/asyncio-module-test/latest
Avatar
Avatar
Josh (EduBlocks)
Playing around with PyScript Asyncio which is wrapped inside a custom module (test.py). When i run this locally i get the expected outcome of each command waiting until the previous is completed. However in PyScript they all run together. Is there something i'm missing that I need to enable? https://pyscript.com/@joshualowe1002/asyncio-module-test/latest
Jeff Glass 28/09/2023 00:06
In Pyodide's event loop, run_until_complete is a synonym for asyncio.ensure_future. There's some notes in their source about why and possible alternatives: https://github.com/pyodide/pyodide/blob/5dbb91a769862c79d42835614df70ecf4041752e/src/py/pyodide/webloop.py#L248-L264 (edited)
Pyodide is a Python distribution for the browser and Node.js based on WebAssembly - pyodide/pyodide
Avatar
DarthWarpath 28/09/2023 01:50
Hey guys
01:50
I need help
01:51
What is wrong with this
01:51
<!DOCTYPE html> <head> <link rel=โ€stylesheetโ€ href=โ€:https//pyscript.net/alpha/pyscript.cssโ€ /> <script defer src=โ€:https//pyscript.net/alpha/pyscript.jsโ€></script> </head> <body> <p id=โ€textโ€></p> <py-script> pass=input(โ€œWhat is the password: โ€œ) if pass == โ€œpasswordโ€ : print(โ€œYou got the Correct Password.) </py-script> </body> </html>
01:56
@Jeff Glass
Avatar
Jeff Glass 28/09/2023 02:57
Hey Darth, a couple things - first, the alpha release is almost 18 months old, the most recent release is https://pyscript.net/releases/2023.05/1/pyscript.js (edited)
02:57
Second, input is tricky in the browser - it relies on blocking the python thread, waiting for the terminal to pass along input from another thread... but the browser only has one thread*, so things lock up
Avatar
Avatar
Jeff Glass
Second, input is tricky in the browser - it relies on blocking the python thread, waiting for the terminal to pass along input from another thread... but the browser only has one thread*, so things lock up
DarthWarpath 28/09/2023 11:14
Oh so it wonโ€™t work?
Avatar
Avatar
Jeff Glass
Second, input is tricky in the browser - it relies on blocking the python thread, waiting for the terminal to pass along input from another thread... but the browser only has one thread*, so things lock up
DarthWarpath 28/09/2023 11:52
Whatโ€™s the 2023.09.01 version
Avatar
Avatar
DarthWarpath
Whatโ€™s the 2023.09.01 version
Jeff Glass 28/09/2023 12:17
That's the version that currently has a release candidate out - 2023.09.1.RC1. Essentially a beta release. That one's actually a from-the-ground-up rewrite of the entire core of pyscript.
Avatar
Avatar
Jeff Glass
In Pyodide's event loop, run_until_complete is a synonym for asyncio.ensure_future. There's some notes in their source about why and possible alternatives: https://github.com/pyodide/pyodide/blob/5dbb91a769862c79d42835614df70ecf4041752e/src/py/pyodide/webloop.py#L248-L264 (edited)
Josh (EduBlocks) 28/09/2023 12:23
ahh i see, interesting. Tried many different combinations with no success, do you know of any docs/guidance to get this specific thing to work?
Jeff Glass started a thread. 28/09/2023 12:52
Avatar
Avatar
DarthWarpath
<!DOCTYPE html> <head> <link rel=โ€stylesheetโ€ href=โ€:https//pyscript.net/alpha/pyscript.cssโ€ /> <script defer src=โ€:https//pyscript.net/alpha/pyscript.jsโ€></script> </head> <body> <p id=โ€textโ€></p> <py-script> pass=input(โ€œWhat is the password: โ€œ) if pass == โ€œpasswordโ€ : print(โ€œYou got the Correct Password.) </py-script> </body> </html>
Andrea Giammarchi 28/09/2023 13:01
<script defer src=โ€:https//pyscript.net/alpha/pyscript.jsโ€></script> I am not sure where you copied the example code but all quotes are broken as these should be " and not โ€ or โ€œ plus your URLs start with a : colon ... nothing in that HTML is strictly valid so maybe the issue is your IDE?
Avatar
Avatar
Andrea Giammarchi
<script defer src=โ€:https//pyscript.net/alpha/pyscript.jsโ€></script> I am not sure where you copied the example code but all quotes are broken as these should be " and not โ€ or โ€œ plus your URLs start with a : colon ... nothing in that HTML is strictly valid so maybe the issue is your IDE?
DarthWarpath 28/09/2023 14:10
Ok thanks
Avatar
Hi folks: https://twitter.com/pyscript_dev/status/1702330058676019370 TL;DR: PyScript FUN "show and tell": Time: 4pm UTC every other Thursday Call Link: https://tinyurl.com/pyscriptfun STARTING IN 15mins
๐Ÿ“ข It's PyScript FUN Thursday! Cool hacks, unexpected projects & funky tricks! ๐Ÿ“ท A call to share and celebrate your fun #PyScript code with the world, our community & maintainers Time: 4pm UTC every other Thursday Call Link: https://t.co/TGxu9s29kp See you there!
Zoom is the leader in modern enterprise video communications, with an easy, reliable cloud platform for video and audio conferencing, chat, and webinars across mobile, desktop, and room systems. Zoom Rooms is the original software-based conference room solution used around the world in board, conference, huddle, and training rooms, as well as ex...
Avatar
Phelsong (Josh) 29/09/2023 20:09
Is there are way to actually import pydom in RC1/RC2 atm? nvm, fuzzy find for the win... from pyweb import pydom, in case anyone else has the same question before the docs get updated (edited)
Avatar
Eruvanos [UTC+1] 29/09/2023 20:59
Error: pyodide.ffi has no attribute wrappers (edited)
Eruvanos [UTC+1] started a thread. 29/09/2023 21:47
Avatar
hi. i am unable to access old pyscript docs. The new docs does not seem to be complete. can someone help ?
Avatar
Answered over in the #โ•ฐโ•ธpyscript-docs chan
Avatar
Hey everyone, I'm working on a little project that included some browser-side Python execution. I was wondering if the pyscript.com UI is open source? It says free to use but I can't find the repo for the (very beautiful) UI. I'd like to use it for personal use and pyodide development/testing.
Avatar
!KubiFiลŸko 03/10/2023 07:40
how i add loop in pyscript?
Avatar
!KubiFiลŸko 03/10/2023 08:17
while True not working
Avatar
!KubiFiลŸko 03/10/2023 08:29
how i can do this loop from pyscript import document import time def printTutorial(event): # input label #input id tag name # get value from input input_text.value input_text = document.querySelector("#input") # this is for output id name #output div tag output_div = document.querySelector("#output") output_div.innerText += " " + input_text.value + "\n" if input_text.value == "yes": output_div = document.querySelector("#output") output_div.innerText += "no this is not yes" + "\n" input_text.value = "" if __name__ == "__main__": currentTime = time.localtime() #Hour/Minute/Second clock = document.querySelector("#time") clock.innerText = time.strftime("%H:%M:%S", currentTime) # day/month/Year day = document.querySelector("#cal") day.innerText = time.strftime("%d.%m.%Y", currentTime)
Avatar
Eruvanos [UTC+1] 04/10/2023 21:48
Is py-repl removed from the RC2? Or do I need to add some script import? <link rel="stylesheet" href="https://pyscript.net/snapshots/2023.09.1.RC2/core.css" /> <script type="module" src="https://pyscript.net/snapshots/2023.09.1.RC2/core.js"></script> (edited)
Avatar
Avatar
Eruvanos [UTC+1]
Is py-repl removed from the RC2? Or do I need to add some script import? <link rel="stylesheet" href="https://pyscript.net/snapshots/2023.09.1.RC2/core.css" /> <script type="module" src="https://pyscript.net/snapshots/2023.09.1.RC2/core.js"></script> (edited)
Jeff Glass 04/10/2023 22:56
No py-repl in RC2, no. I think itโ€™s reimagined form is planned for this release though.
๐Ÿ‘ 1
Avatar
Yes, throughout the RC process we'll be aiming to get the new version of PyScript back to parity with the "classic" in its most important features... the <py-repl> being one of them. It's coming... (and it'll be much closer to a "normal" Python REPL than the previous version).
โค๏ธ 1
Avatar
DrPlanecraft173 06/10/2023 08:27
so I have this problem with PyScript, I am trying to set it up on bubble.io with an example python script, and i have duplicated outputs so I was wondering what is the root cause of the issue Input Source: <py-script output="pytest"> from datetime import datetime now = datetime.now() print("Hello world!\nThis is the current date and time, as computed by Python:") print(now.strftime("%m/%d/%Y, %H:%M:%S")) </py-script>
08:31
This is the pyscript output
Avatar
Avatar
DrPlanecraft173
so I have this problem with PyScript, I am trying to set it up on bubble.io with an example python script, and i have duplicated outputs so I was wondering what is the root cause of the issue Input Source: <py-script output="pytest"> from datetime import datetime now = datetime.now() print("Hello world!\nThis is the current date and time, as computed by Python:") print(now.strftime("%m/%d/%Y, %H:%M:%S")) </py-script>
Andrea Giammarchi 06/10/2023 08:45
which version of PyScript are you using? current RC doesn't support output but you can always use the display utility
Avatar
Avatar
Andrea Giammarchi
which version of PyScript are you using? current RC doesn't support output but you can always use the display utility
DrPlanecraft173 06/10/2023 08:45
what is the difference?
Avatar
Avatar
DrPlanecraft173
what is the difference?
Andrea Giammarchi 06/10/2023 08:46
<py-script> from pyscript import display from datetime import datetime now = datetime.now() display("Hello world!\nThis is the current date and time, as computed by Python:", target="pytest") display(now.strftime("%m/%d/%Y, %H:%M:%S"), target="pytest") </py-script>
Avatar
Avatar
Andrea Giammarchi
which version of PyScript are you using? current RC doesn't support output but you can always use the display utility
DrPlanecraft173 06/10/2023 08:47
As for pyscript version I have no idea, the plugin I have is from: https://bubble.io/plugin/pyscript-python-in-bubble-1651841707680x506028031273599000
Pyscript Python in Bubble Plugin page on Bubble, by Zeroqode. Add Pyscript Python in Bubble in one click to your app. Bubble lets you build web apps without any code.
08:48
the output I believe is specific to the plugin's own implementation of pyscript
Avatar
Avatar
Andrea Giammarchi
<py-script> from pyscript import display from datetime import datetime now = datetime.now() display("Hello world!\nThis is the current date and time, as computed by Python:", target="pytest") display(now.strftime("%m/%d/%Y, %H:%M:%S"), target="pytest") </py-script>
DrPlanecraft173 06/10/2023 08:48
I actually tried that, it never loaded and it gave me the error for not having IPython
Avatar
Avatar
DrPlanecraft173
As for pyscript version I have no idea, the plugin I have is from: https://bubble.io/plugin/pyscript-python-in-bubble-1651841707680x506028031273599000
Andrea Giammarchi 06/10/2023 08:48
IIRC PyScript "classic" handles already the output attribute but for your use case I think display would do
08:49
yeah, if you are in an older version (classic) then display is there out of the box, you don't need to import it also because there is no pyscript module AFAIK.
08:50
all I am saying is that we're working on the next RC so stuff affecting 3rd party plugins and PyScript classic won't likely have much priority these days
Avatar
DrPlanecraft173 06/10/2023 08:50
RC as in?
08:51
never mind, its just the version Facepalm
Avatar
Avatar
DrPlanecraft173
RC as in?
Andrea Giammarchi 06/10/2023 08:55
Release Candidate ... we're at version 2 now ๐Ÿ™‚
Avatar
DrPlanecraft173 06/10/2023 08:56
I see, the headers are the standard ones thats provided on the site, so im assuming im at RC2?
Avatar
Avatar
Andrea Giammarchi
Release Candidate ... we're at version 2 now ๐Ÿ™‚
DrPlanecraft173 06/10/2023 08:57
Start building plugins for Bubble! You can now extend the platform with API, actions and elements
Avatar
Avatar
DrPlanecraft173
Andrea Giammarchi 06/10/2023 09:05
like I've said, we're focusing on the RC3 these days so I hope somebody else might help you there as I don't even know this service (yet?).
Avatar
DrPlanecraft173 06/10/2023 09:05
I see, thanks!
Avatar
!KubiFiลŸko 06/10/2023 12:20
Traceback (most recent call last): File "/lib/python311.zip/_pyodide/_base.py", line 499, in eval_code .run(globals, locals) ^^^^^^^^^^^^^^^^^^^^ File "/lib/python311.zip/_pyodide/_base.py", line 340, in run coroutine = eval(self.code, globals, locals) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "<exec>", line 11, in <module> NameError: name 'pyscript' is not defined
Avatar
Avatar
DrPlanecraft173
Jeff Glass 06/10/2023 13:34
If thatโ€™s how theyโ€™re setting up the <head>, theyโ€™re linking to the alpha release from last Mayโ€ฆ but in theory you can use the latest release candidate by changing those tags to point at https://pyscript.net/releases/2022.05.1/pyscriptโ€™js (and pyscript.css, or to the new west release candidate at https://pyscript.bet/releases/2023.09.1.RC2/core.js (and core.css)
Avatar
Avatar
Jeff Glass
If thatโ€™s how theyโ€™re setting up the <head>, theyโ€™re linking to the alpha release from last Mayโ€ฆ but in theory you can use the latest release candidate by changing those tags to point at https://pyscript.net/releases/2022.05.1/pyscriptโ€™js (and pyscript.css, or to the new west release candidate at https://pyscript.bet/releases/2023.09.1.RC2/core.js (and core.css)
DrPlanecraft173 06/10/2023 13:36
Thanks bro! Imma try and make. My own clone of their plugin and see how it goes
๐Ÿฅณ 1
Avatar
Avatar
!KubiFiลŸko
Traceback (most recent call last): File "/lib/python311.zip/_pyodide/_base.py", line 499, in eval_code .run(globals, locals) ^^^^^^^^^^^^^^^^^^^^ File "/lib/python311.zip/_pyodide/_base.py", line 340, in run coroutine = eval(self.code, globals, locals) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "<exec>", line 11, in <module> NameError: name 'pyscript' is not defined
Jeff Glass 06/10/2023 13:39
Looks like you need to import pyscript
Avatar
!KubiFiลŸko 06/10/2023 13:57
Traceback (most recent call last): File "/lib/python311.zip/_pyodide/_base.py", line 499, in eval_code .run(globals, locals) ^^^^^^^^^^^^^^^^^^^^ File "/lib/python311.zip/_pyodide/_base.py", line 340, in run coroutine = eval(self.code, globals, locals) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "<exec>", line 12, in <module> AttributeError: module 'pyscript' has no attribute 'run_until_complete'
13:57
Traceback (most recent call last): File "/lib/python311.zip/_pyodide/_base.py", line 491, in eval_code CodeRunner( File "/lib/python311.zip/_pyodide/_base.py", line 268, in __init__ self.ast = next(self._gen) ^^^^^^^^^^^^^^^ File "/lib/python311.zip/_pyodide/_base.py", line 145, in _parse_and_compile_gen mod = compile(source, filename, mode, flags | ast.PyCF_ONLY_AST) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "<exec>", line 46 pass IndentationError: unexpected indent
Avatar
Avatar
!KubiFiลŸko
Traceback (most recent call last): File "/lib/python311.zip/_pyodide/_base.py", line 491, in eval_code CodeRunner( File "/lib/python311.zip/_pyodide/_base.py", line 268, in __init__ self.ast = next(self._gen) ^^^^^^^^^^^^^^^ File "/lib/python311.zip/_pyodide/_base.py", line 145, in _parse_and_compile_gen mod = compile(source, filename, mode, flags | ast.PyCF_ONLY_AST) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "<exec>", line 46 pass IndentationError: unexpected indent
Jeff Glass 06/10/2023 14:23
Which version of pyscript are you using?
Avatar
Jeff Glass 06/10/2023 14:50
Ah well 2 things then. The 2022.09 release doesnโ€™t have a custom event loop like previous releases, so it doesnโ€™t have its own run_until_complete method.
14:51
Second, run_until_compete doesnโ€™t really work in Pyodide, since you canโ€™t block the main execution thread while waiting for a coroutine. In Pyodide itโ€™s just a proxy for ensure_future
Avatar
Avatar
Jeff Glass
Second, run_until_compete doesnโ€™t really work in Pyodide, since you canโ€™t block the main execution thread while waiting for a coroutine. In Pyodide itโ€™s just a proxy for ensure_future
Andrea Giammarchi 09/10/2023 09:18
we have also async attribute to allow top level async code in Pyodide so be sure your script/tag has an async when the code is going to run is async too.
Avatar
Avatar
Andrea Giammarchi
we have also async attribute to allow top level async code in Pyodide so be sure your script/tag has an async when the code is going to run is async too.
Jeff Glass 09/10/2023 16:07
Just to expand on this for future readers - you don't need an async tag to run code that makes use of asyncio, just if you want to use top level await. Adding the async tag is essentially: <script type="py" async> # your code here import asyncio await asyncio.sleep(1) # 'await' outside an async function == top level await print("done") </script> <!-- is equivalent to--> <script type="py"> async def _temp(): # your code here import asyncio await asyncio.sleep(1) print("done") asyncio.ensure_future(_temp()) </script> So depending on what you're doing, using an async tag is a really convenient way to run coroutines on the page. But if you're running tasks/coroutines manually in some way anyway, it doesn't add anything on top of that.
๐Ÿ‘ 1
Avatar
Avatar
Jeff Glass
Just to expand on this for future readers - you don't need an async tag to run code that makes use of asyncio, just if you want to use top level await. Adding the async tag is essentially: <script type="py" async> # your code here import asyncio await asyncio.sleep(1) # 'await' outside an async function == top level await print("done") </script> <!-- is equivalent to--> <script type="py"> async def _temp(): # your code here import asyncio await asyncio.sleep(1) print("done") asyncio.ensure_future(_temp()) </script> So depending on what you're doing, using an async tag is a really convenient way to run coroutines on the page. But if you're running tasks/coroutines manually in some way anyway, it doesn't add anything on top of that.
Andrea Giammarchi 09/10/2023 16:14
thank you Jeff, I am indeed a bit cryptic when it comes to Pythonic hints, as I know little about all the ways to do the same thing (or the best way in general) and your examples help me too better understanding the whole stack. (edited)
๐Ÿ‘ 1
Avatar
Avatar
Jeff Glass
Just to expand on this for future readers - you don't need an async tag to run code that makes use of asyncio, just if you want to use top level await. Adding the async tag is essentially: <script type="py" async> # your code here import asyncio await asyncio.sleep(1) # 'await' outside an async function == top level await print("done") </script> <!-- is equivalent to--> <script type="py"> async def _temp(): # your code here import asyncio await asyncio.sleep(1) print("done") asyncio.ensure_future(_temp()) </script> So depending on what you're doing, using an async tag is a really convenient way to run coroutines on the page. But if you're running tasks/coroutines manually in some way anyway, it doesn't add anything on top of that.
bugzpodder 09/10/2023 22:14
I just tried in pyodide and it doesn't seem to be the case. [edit] it seems to not work in pyodide console here https://pyodide.org/en/stable/console.html but i plugged into pyscript and it works perfectly in RC2.... (edited)
Avatar
Avatar
bugzpodder
I just tried in pyodide and it doesn't seem to be the case. [edit] it seems to not work in pyodide console here https://pyodide.org/en/stable/console.html but i plugged into pyscript and it works perfectly in RC2.... (edited)
Jeff Glass 09/10/2023 22:51
Not sure what you mean... if you enter: >>> import asyncio await asyncio.sleep(2) print("FOO") Into the Pyodide console, it will "sleep" asynchronously for 2 seconds then print. That's not possible unless you have top level await available (edited)
22:53
What were you trying that wasn't working? Surely possible there are bugs there. Talking to the Pyodide core devs, the console/REPL are the reason they added the ability to run with top-level await in the first place, so if something is broken I'm sure they'd love to know
Avatar
Avatar
Jeff Glass
What were you trying that wasn't working? Surely possible there are bugs there. Talking to the Pyodide core devs, the console/REPL are the reason they added the ability to run with top-level await in the first place, so if something is broken I'm sure they'd love to know
bugzpodder 09/10/2023 22:53
async def _temp(): # your code here import asyncio await asyncio.sleep(30) print("done") import asyncio asyncio.ensure_future(_temp()) <PyodideTask pending name='Task-15' coro=<_temp() running at <console>:4> wait_for=<PyodideFuture pending cb =[WebLoop._decrement_in_progress(), PyodideTask.task_wakeup()]> cb=[WebLoop._decrement_in_progress()]>
Avatar
Jeff Glass 09/10/2023 22:57
Ohhhh I see. The fact that it doesn't show up in the console is a byproduct of how that console doesn't handle output from webloop coroutines very well. I do see "done" being printed in the dev console though (edited)
22:58
One error in my example though, is I should have said fut = asyncio.ensure_future(_temp()) since Futures/Tasks that don't have a referenced held to them can be cleaned up by GC basically whenever, even if they're still running
22:58
(Don't think it affects this example necessarily, but worth mentioning)
Avatar
Avatar
Jeff Glass
One error in my example though, is I should have said fut = asyncio.ensure_future(_temp()) since Futures/Tasks that don't have a referenced held to them can be cleaned up by GC basically whenever, even if they're still running
bugzpodder 09/10/2023 22:58
interesting... await _temp() prints fine so it threw me off, but makes sense (edited)
๐Ÿ‘ 1
Avatar
Avatar
Jeff Glass
Ohhhh I see. The fact that it doesn't show up in the console is a byproduct of how that console doesn't handle output from webloop coroutines very well. I do see "done" being printed in the dev console though (edited)
bugzpodder 09/10/2023 23:03
actually i was right. what I meant to say is that asyncio.ensure_future(_temp()) is not equilvalent to await _temp(). ensure_future just schedules the task and await blocks.. demo: https://bugzpodder.pyscriptapps.com/piratical-copy-copy/latest/. 'done3' is printed before 'done' (edited)
Avatar
Jeff Glass 09/10/2023 23:05
Well sure, await waits on the promise to resolve, and ensure_future just schedules it in to the event loop and doesn't block... let me look at your demo...
23:05
You're not using an async attribute in your tags...
Avatar
Avatar
Jeff Glass
Well sure, await waits on the promise to resolve, and ensure_future just schedules it in to the event loop and doesn't block... let me look at your demo...
bugzpodder 09/10/2023 23:06
yeah i was just going off your example above. you said they were equilvalent, so i thought await was same as ensure_future. but i guess they are slightly different. one blocks and the other just schedules. (edited)
Avatar
Jeff Glass 09/10/2023 23:07
The example above is only valid if they're in separate tags - the one with top level await in a tag with the async attribute, the other in a separate tag
23:07
You've combined them both into one tag, it seems?
23:08
Let me try another example and see if this makes it more clear, perhaps that earlier example wasn't the cleanest
Avatar
Avatar
Jeff Glass
You've combined them both into one tag, it seems?
bugzpodder 09/10/2023 23:10
i just have a non-async tag that does asyncio.ensure_future(_temp()) followed by print('done3'). i wanted print to happen after _temp() is completed by i guess this is not possible unless i create another async function and run it in main. like: async main():\n await _temp()\n print('done3') and use asyncio.ensure_future(main())
Avatar
Jeff Glass 09/10/2023 23:12
Yes, in general the only way for something to act "synchronously" with respect to an async thing (i.e. wait until the async thing is complete) is to either await your awaitable (where possible), or wrap them in another awaitable like you say. (edited)
๐Ÿ‘ 1
23:12
That said (and I know this is a microexample of whatever real thing you're doing is)... why not just do all that in an async tag?
23:13
Or do you have other synchronous things that rely on it not being an async tag?
Avatar
Avatar
Jeff Glass
That said (and I know this is a microexample of whatever real thing you're doing is)... why not just do all that in an async tag?
bugzpodder 09/10/2023 23:14
it came up because i was just putting a tag in (without realizing there is an async vs non-async tag) and you happen to discuss this previously so i thought i'd give it a try
Avatar
Jeff Glass 09/10/2023 23:15
Ahhh gotcha ๐Ÿ‘
Avatar
Fabiano.py 10/10/2023 23:42
@ntoll @Jeff Glass @Andrea Giammarchi Help The code is working, but when I update to the recent documentation it stops working. To make it easier to understand, I commented on the changes that cause it to stop working. How to import pydom? <html> <head> <!-- Recommended meta tags --> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width,initial-scale=1.0"> <link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" /> <script defer src="https://pyscript.net/latest/pyscript.js"></script> <!-- PyScript CSS --> <!--<link rel="stylesheet" href="https://pyscript.net/snapshots/2023.09.1.RC2/core.css">--> <!-- This script tag bootstraps PyScript --> <!--<script type="module" src="https://pyscript.net/snapshots/2023.09.1.RC2/core.js"></script>--> </head> <body> <p> ADD 2 NUMBERS </p> <label for="input1">Number:1</label> <input type="" id="input1" name="input1"><br><br> <label for="input2">Number:2</label> <input type="" id="input2" name="input2"><br><br> <button type="button" id="btn">Clique-me</button> <div id="output"></div> <py-config> [splashscreen] enabled = false packages = [""] </py-config> <py-script> #from pyscript.pydom import Element #from pydom import Element from pyscript import when @when('click','#btn') def add(): number1 = int(Element("input1").value) number2 = int(Element("input2").value) keep = number1 + number2 display(keep, target="output") </py-script> </body> </html> (edited)
Avatar
Avatar
Fabiano.py
@ntoll @Jeff Glass @Andrea Giammarchi Help The code is working, but when I update to the recent documentation it stops working. To make it easier to understand, I commented on the changes that cause it to stop working. How to import pydom? <html> <head> <!-- Recommended meta tags --> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width,initial-scale=1.0"> <link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" /> <script defer src="https://pyscript.net/latest/pyscript.js"></script> <!-- PyScript CSS --> <!--<link rel="stylesheet" href="https://pyscript.net/snapshots/2023.09.1.RC2/core.css">--> <!-- This script tag bootstraps PyScript --> <!--<script type="module" src="https://pyscript.net/snapshots/2023.09.1.RC2/core.js"></script>--> </head> <body> <p> ADD 2 NUMBERS </p> <label for="input1">Number:1</label> <input type="" id="input1" name="input1"><br><br> <label for="input2">Number:2</label> <input type="" id="input2" name="input2"><br><br> <button type="button" id="btn">Clique-me</button> <div id="output"></div> <py-config> [splashscreen] enabled = false packages = [""] </py-config> <py-script> #from pyscript.pydom import Element #from pydom import Element from pyscript import when @when('click','#btn') def add(): number1 = int(Element("input1").value) number2 = int(Element("input2").value) keep = number1 + number2 display(keep, target="output") </py-script> </body> </html> (edited)
Jeff Glass 11/10/2023 01:34
It looks like you have two script tags, linking to both /latest and the RC2 candidate. You should only use one or the other - if you want to use pydom, it should be the release candidate
Avatar
Avatar
Jeff Glass
It looks like you have two script tags, linking to both /latest and the RC2 candidate. You should only use one or the other - if you want to use pydom, it should be the release candidate
Fabiano.py 11/10/2023 01:58
It is not a duplicate, I commented to make it easier for anyone who will help, to see what I have already done in a simple way. and not waste time searching through documentation. I could put one before after, two versions of code, but I think that would only complicate things. When I switch to RC2 it will break everything I've done and learned if pydom doesn't work (edited)
Avatar
Jeff Glass 11/10/2023 02:05
I think it will be clearer if you post a version that is just the thing that is breaking, the commented things only make it more confusing ๐Ÿค”
02:09
@Fabio is the mastermind behind pydom, so he would know better than I, but I think directly calling the Element constructor isnโ€™t the way anymore. Rather, use pydom.ids.some_id to get the element with that if https://fpliger.pyscriptapps.com/pyweb/latest/pydom.html
Avatar
Avatar
Jeff Glass
@Fabio is the mastermind behind pydom, so he would know better than I, but I think directly calling the Element constructor isnโ€™t the way anymore. Rather, use pydom.ids.some_id to get the element with that if https://fpliger.pyscriptapps.com/pyweb/latest/pydom.html
Fabiano.py 11/10/2023 02:10
Can you make it work? <html> <head> <!-- Recommended meta tags --> <!-- PyScript CSS --> <link rel="stylesheet" href="https://pyscript.net/snapshots/2023.09.1.RC2/core.css"> <!-- This script tag bootstraps PyScript --> <script type="module" src="https://pyscript.net/snapshots/2023.09.1.RC2/core.js"></script> </head> <body> <p> ADD 2 NUMBERS </p> <label for="input1">Number:1</label> <input type="" id="input1" name="input1"><br><br> <label for="input2">Number:2</label> <input type="" id="input2" name="input2"><br><br> <button type="button" id="btn">Clique-me</button> <div id="output"></div> <py-config> [splashscreen] enabled = false packages = [""] </py-config> <py-script> from pydom import Element from pyscript import when @when('click','#btn') def add(): number1 = int(Element("input1").value) number2 = int(Element("input2").value) keep = number1 + number2 display(keep, target="output") </py-script> </body> </html>
Avatar
Avatar
Jeff Glass
@Fabio is the mastermind behind pydom, so he would know better than I, but I think directly calling the Element constructor isnโ€™t the way anymore. Rather, use pydom.ids.some_id to get the element with that if https://fpliger.pyscriptapps.com/pyweb/latest/pydom.html
Fabiano.py 11/10/2023 02:12
I already tried too from pyscript.pydom import Element as you can see the latest version works without importing pydon ,If I have to use js writing it will break everything I've already done and learned. the RC2 candidate it will complicate our lives if pydom stops working, it was so good working in peace now it doesn't work in a natural standard way and doesn't even matter. it will break everything. ๐Ÿ™‚ (edited)
Avatar
Fabiano.py 11/10/2023 02:32
In short: how can I update my codes using the documentation and importantly, Pydom no longer works by default, that's all.
Avatar
Yes, with the new release you should use pydom. Basically, instead of Element("input1") you should use pydom["#input1"] I'm out right now but can check it out later.
Avatar
Avatar
Fabio
Yes, with the new release you should use pydom. Basically, instead of Element("input1") you should use pydom["#input1"] I'm out right now but can check it out later.
Fabiano.py 11/10/2023 03:43
Thank you very much for your attention Fรกbio. hugs from Brazil ๐Ÿ‡ง๐Ÿ‡ท ๐Ÿ™‚
Avatar
Avatar
Fabiano.py
Thank you very much for your attention Fรกbio. hugs from Brazil ๐Ÿ‡ง๐Ÿ‡ท ๐Ÿ™‚
Boa @Fabiano.py ! Aqui vai uma versรฃo funcionante... https://pyscript.com/@fpliger/add-numbers/latest (switching to english ๐Ÿ˜‰ ) In the example above I've fixed some of the issues but I realized we should add the value attribute directly to the returned collection (and Element) in pydom. I'll put a PR to improve things tomorrow. Obrigado!!
Avatar
How do I run arbitrary Python code from within JavaScript? I built a JavaScript ide and I would like to run Python in the terminal. How can I make this work? How can I retrieve prints and all writes to stdout and run the repl?
Avatar
Hi , May I ask a question regarding to possibility of our student project If we want to learn & use PyScript in this project? We have a group project and we want to make Student <> Teacher communication platform. So, there'll be several features including registration , communication between students & teachers , feedback system, etc. When we learnt about Flask or Django framework , it's absolutely doable but we need to be familiar with Javascript. So, May I know our project idea is possible to do with PyScript alone ?
Avatar
Avatar
Fabio
Boa @Fabiano.py ! Aqui vai uma versรฃo funcionante... https://pyscript.com/@fpliger/add-numbers/latest (switching to english ๐Ÿ˜‰ ) In the example above I've fixed some of the issues but I realized we should add the value attribute directly to the returned collection (and Element) in pydom. I'll put a PR to improve things tomorrow. Obrigado!!
Fabiano.py 11/10/2023 20:35
@Fabio thanks for the help, yes it worked, although there is no simple way to use pydom and get the same result with Element, I created this. if you want to see the code (รด_รด) https://sites.google.com/view/pagtestes/inรญcio
ADD 2 NUMBER Number 1:
Avatar
Avatar
Fabiano.py
@Fabio thanks for the help, yes it worked, although there is no simple way to use pydom and get the same result with Element, I created this. if you want to see the code (รด_รด) https://sites.google.com/view/pagtestes/inรญcio
@Fabiano.py we had not mapped value to pydom.Element yet (we'll be adding attributes gradually because we want to be careful, keeping the API small and focused on the part of the HTML API that is most use). Element in your example is strictly focused on input elements [value attribute] and, yes, it's not the same use case but once we add value to pydom.Element you'll be able to simply do pydom["#input1"].value.
๐Ÿ™‚ 1
Avatar
Avatar
Hudson
How do I run arbitrary Python code from within JavaScript? I built a JavaScript ide and I would like to run Python in the terminal. How can I make this work? How can I retrieve prints and all writes to stdout and run the repl?
Can you give more details around your use case? We've are rethinking the repl in the upcoming release and we'd love to understand the many possible use cases are... It's likely that we'll have a simpler repl (probably being renamed to terminal and we'll add another type for more complex use cases)
Avatar
Avatar
paing
Hi , May I ask a question regarding to possibility of our student project If we want to learn & use PyScript in this project? We have a group project and we want to make Student <> Teacher communication platform. So, there'll be several features including registration , communication between students & teachers , feedback system, etc. When we learnt about Flask or Django framework , it's absolutely doable but we need to be familiar with Javascript. So, May I know our project idea is possible to do with PyScript alone ?
@paing can you elaborate on what is your idea and what is the use case for PyScript? From what you said, my understanding is that you'd use PyScript mainly to replace Javascript and you'd still use a Python framework to deal with the logic on the backend, right?
Avatar
Avatar
Fabio
Can you give more details around your use case? We've are rethinking the repl in the upcoming release and we'd love to understand the many possible use cases are... It's likely that we'll have a simpler repl (probably being renamed to terminal and we'll add another type for more complex use cases)
I have an xterm with custom split-if based commands built in JS with a virtual localStorage filestystem. Here's a link: https://adkcode.programit.repl.co I would like to add full Python support. The ability to run Python files and run the python repl and use pip.
Avatar
I see. I think the options right now would be 1. writing a plugin to intercept the main interpreter and then using it to explicit run the code fro your terminal or 2. run commands dynamically adding <script type="py>... (maybe @Andrea Giammarchi has other ideas...) This type of need is something we were looking at anyway but it hasn't been prioritized in this next release. Probably something we'll be making it easier to support in the future.
Avatar
Avatar
Hudson
I have an xterm with custom split-if based commands built in JS with a virtual localStorage filestystem. Here's a link: https://adkcode.programit.repl.co I would like to add full Python support. The ability to run Python files and run the python repl and use pip.
Andrea Giammarchi 11/10/2023 22:57
you can hook as plugin to grab pyodide stdio and redirect the log or error elsewhere but I am working as we speak into py-terminal so I'll have better answers as soon as I fully understand what is it that doesn't work. The main goal is to enable that from a worker too which is where current core "shines" but I am not there yet, sorry.
Avatar
Avatar
Andrea Giammarchi
you can hook as plugin to grab pyodide stdio and redirect the log or error elsewhere but I am working as we speak into py-terminal so I'll have better answers as soon as I fully understand what is it that doesn't work. The main goal is to enable that from a worker too which is where current core "shines" but I am not there yet, sorry.
What's your ETA for that?
Avatar
Avatar
Hudson
What's your ETA for that?
Andrea Giammarchi 11/10/2023 23:03
hopefully end of this week to at least pin down what's missing, worst case scenario it'll be beginning of next week after possible discussions around it too.
Avatar
Avatar
Andrea Giammarchi
hopefully end of this week to at least pin down what's missing, worst case scenario it'll be beginning of next week after possible discussions around it too.
Okay. When do you think you may have a good solution? Just a rough estimate would be fine.
Avatar
Avatar
Hudson
Okay. When do you think you may have a good solution? Just a rough estimate would be fine.
Andrea Giammarchi 11/10/2023 23:06
I don't know, sorry, that's why I am investigating, to have even an idea of how a good solution would look like.
Avatar
Okay, well thanks.
Avatar
Avatar
Hudson
I have an xterm with custom split-if based commands built in JS with a virtual localStorage filestystem. Here's a link: https://adkcode.programit.repl.co I would like to add full Python support. The ability to run Python files and run the python repl and use pip.
Avatar
Avatar
Fabio
@paing can you elaborate on what is your idea and what is the use case for PyScript? From what you said, my understanding is that you'd use PyScript mainly to replace Javascript and you'd still use a Python framework to deal with the logic on the backend, right?
Yes, it's correct. @Fabio . We're student group that started learning Python a few months ago , so thinking to stick with Flask which might be more easier than Django to learn. So, our tech stack want to be Flask + HTML/CSS + Pyscript to build the whole system that we want to build. (edited)
Avatar
Avatar
paing
Yes, it's correct. @Fabio . We're student group that started learning Python a few months ago , so thinking to stick with Flask which might be more easier than Django to learn. So, our tech stack want to be Flask + HTML/CSS + Pyscript to build the whole system that we want to build. (edited)
Andrea Giammarchi 13/10/2023 13:04
I remember Harvard CS50 course also uses Flask to teach Python, it's a great way to start, imho.
Avatar
Avatar
paing
Yes, it's correct. @Fabio . We're student group that started learning Python a few months ago , so thinking to stick with Flask which might be more easier than Django to learn. So, our tech stack want to be Flask + HTML/CSS + Pyscript to build the whole system that we want to build. (edited)
Agreed. Think it's a great way to learn and that you probably can use PyScript for a lot of if. Maybe not use JavaScript directly at all. Actually, could be a great way to try out if there are still any rough edges that we can work on and improve. For instance, I'm pretty sure you'll find limitations (that we know) on the pydom interface. Having that feedback will help us move in the right direction.
Avatar
Avatar
paing
Yes, it's correct. @Fabio . We're student group that started learning Python a few months ago , so thinking to stick with Flask which might be more easier than Django to learn. So, our tech stack want to be Flask + HTML/CSS + Pyscript to build the whole system that we want to build. (edited)
bugzpodder 13/10/2023 15:45
fastAPI is the industry stnadard for building python web apps nowadays if you are interested in something new
Avatar
How do I un-install pyscript
Avatar
Avatar
Smallgaps
How do I un-install pyscript
Jeff Glass 15/10/2023 14:27
How did you install it?
Avatar
I didn't I knew something was up with my phone and started digging and found just a little piece of what is a major problem
14:41
Please help me my privacy is being invaded majorly and
Avatar
Avatar
Smallgaps
Please help me my privacy is being invaded majorly and
Andrea Giammarchi 16/10/2023 08:50
pyscript runs in your browser which takes care of all security related things for you as it does for any other regular website you visit. One does not install pyscript, it's just like any other regular JavaScript file, in terms of security, so it's hard to help you but also hard to understand what is exactly your concern or issue. Any extra detail might help though.
Avatar
i keep gettin this error while trying to run my translator script with pyscript (already tried 3 different libraries). any idea what could be a problem?
Avatar
Avatar
Shaman
i keep gettin this error while trying to run my translator script with pyscript (already tried 3 different libraries). any idea what could be a problem?
Andrea Giammarchi 16/10/2023 15:39
it looks like goslate package is not part of the running code so you should likely add that to a py-config tag or literal object, depending on how you bootstrap pyscript ... none of that looks like a security concern and be assured the erros you see in your devtool console are yours to read ... we don't know, store, read, anything happining locally in your browser/site/env.
15:40
oops, I've combined two topics at once ... well, maybe I've replied to both topics (pyscript security/"installed" and just missing packages).
Avatar
I've got "<py-env> - goslate </py-env>" in my html head, isnt that it?
Avatar
Avatar
Shaman
I've got "<py-env> - goslate </py-env>" in my html head, isnt that it?
It looks like that library is not compatible with pyodide. It will either need a pure python wheel, or it will need to be built for wasm
15:58
Oh, that requires socket, that is probably going to be a no-go in the browser. I may be wrong though.
Avatar
Avatar
tuxprint
It looks like that library is not compatible with pyodide. It will either need a pure python wheel, or it will need to be built for wasm
so i guess any translator library will result like this one
16:02
because concept is simmilar
Avatar
Idk, that just uses Google translate. It's also only like 600 lines. I bet it could be ported without too much trouble.
16:09
Just use like pyfetch to make the network calls.
16:10
I'm just a user fyi
Avatar
so far i tried 3 libraries (googletrans, goslate and translate from libre) and all three work separetly in .py or repl.it, but when i use it with pyscript i get the same error
Avatar
That is because pyscript runs directly in your browser, and some things will not work one of which is things to do with sockets.
Avatar
Avatar
tuxprint
Just use like pyfetch to make the network calls.
wish i knew what that is ๐Ÿ˜„ im beginner btw and i work on simple projects (these days i experiment with different libraries and trying to implement them with pyscript to my html)
Avatar
Hmm
Avatar
Rust-Ninja-Sabi 16/10/2023 17:37
Does PyScript work with a virtual DOM?
Avatar
Avatar
Rust-Ninja-Sabi
Does PyScript work with a virtual DOM?
bugzpodder 16/10/2023 17:40
you mean React? it works alongside virtual DOM but not directly.
Avatar
Rust-Ninja-Sabi 16/10/2023 18:21
As far as I understand PyScript works as a web component and web components can have virtual doms
Avatar
Avatar
Shaman
wish i knew what that is ๐Ÿ˜„ im beginner btw and i work on simple projects (these days i experiment with different libraries and trying to implement them with pyscript to my html)
ok, so I made some good progress getting goslate working with pyscript. Unfortunately there are a few issues. one is the rate limit, it is very low. probably since the official api is paid, so translate.google.com get limited hard. but the real killer is the fact that google does not send CORS headers. without cors headers, your browser will fail to load the content. so yeah, any of the libraries that use that link will not work in a browser ๐Ÿ˜ฆ
18:36
maybe there is a better way to do what you're trying to do.
Avatar
ahh i see. i guess i'll skip this one then ๐Ÿ™‚ thanks for update!
Avatar
Avatar
Shaman
ahh i see. i guess i'll skip this one then ๐Ÿ™‚ thanks for update!
what exactly are you trying to do? There may be something that could be done. I think I have something that might work for you
18:58
its nothing special, but im working on translator like this
19:00
i dont expect it to translate the moment input is typed, but i hoped it would translate it by pressing the bottom button
Avatar
Avatar
Shaman
its nothing special, but im working on translator like this
yeah, no problem. that can be done
Avatar
in rc2, I have a button with py-click on it, and no mater what I try to get it to run, it gives this error "Cannot read properties of undefined (reading 'call')" is there something I should do different?
Avatar
Avatar
tuxprint
in rc2, I have a button with py-click on it, and no mater what I try to get it to run, it gives this error "Cannot read properties of undefined (reading 'call')" is there something I should do different?
Jeff Glass 16/10/2023 19:59
Try it on the unstable build and see what happens: https://pyscript.net/unstable/core.js
20:00
Are you trying to use the method of an object? I have run into some issues there as well
Avatar
still the same. even just print(1) gives the error
20:02
TypeError: Cannot read properties of undefined (reading 'call') at Object.ct [as runEvent] (_python.js:36:22) at HTMLButtonElement.Ht (listeners.js:42:17)
20:03
oh, wait
20:03
we give it the function, but not with ()
20:03
hmm
20:09
ok, so async functions will just work again?
20:09
nice
Avatar
Jeff Glass 16/10/2023 20:45
Yeah itโ€™s now just the name of a function in the global namespace, and the handler gets passed the event object
20:45
Well, a Callable in the global namespace
Avatar
Awesome!
Avatar
Hi, i really like the info on the splash screen showing what is happening while a user is waiting for packages to load. Anyway i can customize that? Ideally use my own component, but still access the logs?
Avatar
Avatar
Rust-Ninja-Sabi
As far as I understand PyScript works as a web component and web components can have virtual doms
bugzpodder 17/10/2023 01:29
do you mean shadow-dom?
Avatar
Avatar
bugzpodder
do you mean shadow-dom?
Rust-Ninja-Sabi 17/10/2023 14:39
The series aims to show what Web Components are, put them in parallel for comparison with Frameworks to understand the differences and the needs each of them answer. Youโ€™re looking at part 1 here, in which I talk about Native support vs Virtual DOM.
Real DOMs and virtual DOMs and shadow DOMs, oh my! Let's take a dive to see how they all work...
Avatar
bugzpodder 17/10/2023 16:58
javascript web component doesn't have virtual dom. elm/vue/react does
๐Ÿ‘ 1
Avatar
Avatar
bugzpodder
javascript web component doesn't have virtual dom. elm/vue/react does
Andrea Giammarchi 17/10/2023 18:23
on top of that, it's not clear what is it that we are after with this Shadow/virtual DOM question ... what is it that is missing or how can it anyhow improve anything in here? Thanks! (edited)
Avatar
Jeff Glass 17/10/2023 20:13
As a little aside @Rust-Ninja-Sabi , the old (and maybe returning/getting renamed) <py-repl> in pyscript classic used a shadow DOM to wrap up the CodeMirror editor, output section, etc, if you wanted to look at an example of what that looked like. https://github.com/pyscript/pyscript/blob/2023.05.1/pyscriptjs/src/components/pyrepl.ts In the Next release, weโ€™re not doing that much DOM manipulation, so not sure how maintaining a virtual DOM would speed anything up, and thereโ€™s not much to โ€œhideโ€ with a shadow DOMโ€ฆ
Avatar
Rust-Ninja-Sabi 17/10/2023 22:05
Thanks to @Jeff Glass and all the others for the answers. I was only interested to know the manipulation of DOM in PyScript works. I wrote a game in Elm only with SVG and it was really fast. I will implement it in PyScript to see the differences in speed. Should I use MicroPython? What are the differences to Pyodide?
Avatar
Avatar
Rust-Ninja-Sabi
Thanks to @Jeff Glass and all the others for the answers. I was only interested to know the manipulation of DOM in PyScript works. I wrote a game in Elm only with SVG and it was really fast. I will implement it in PyScript to see the differences in speed. Should I use MicroPython? What are the differences to Pyodide?
bugzpodder 17/10/2023 22:38
main difference is that MicroPython doesn't allow external packages so you are stuck with python stdlib. if you need some external packages pyodide is your best bet. Also see https://docs.micropython.org/en/latest/genrst/index.html (edited)
Avatar
Avatar
bugzpodder
main difference is that MicroPython doesn't allow external packages so you are stuck with python stdlib. if you need some external packages pyodide is your best bet. Also see https://docs.micropython.org/en/latest/genrst/index.html (edited)
Rust-Ninja-Sabi 18/10/2023 14:31
Thanks. But it is possible in MicroPython to use JavaScript packages?
Avatar
Avatar
Rust-Ninja-Sabi
Thanks. But it is possible in MicroPython to use JavaScript packages?
bugzpodder 18/10/2023 16:31
depends on how you use it. if you wantt o pass values between micropython and js you might run into some difficulties. if you just want to call a function without worrying about arguments and return value
Avatar
Andrea Giammarchi 18/10/2023 17:42
I'm afraid with micropython we're stuck here around modules that can be imported via mip https://github.com/pyscript/polyscript/pull/63
In theory, everything should work ... in practice: ImportError: no module named 'mip' I've asked in MicroPython discord channel about this, let's see how that goes: https://discord...
Avatar
Hi I am trying to run a python package in browser using vscode live server project structure (not posting all the files as that will be too long) . |-- aksharamukha | |-- Convert.py | |-- transliterate.py |-- demo.py |-- requirements.txt |-- test-server | |-- __init__.py | |-- config.json | |-- index.html | `-- main.py test-server/index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Script Transcription</title> <!-- PyScript CSS --> <link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css"> <!-- This script tag bootstraps PyScript --> <script defer src="https://pyscript.net/latest/pyscript.js"></script> </head> <body> <div id="root"></div> <div id="jsroot"></div> <textarea id="hi">เคงเคฐเฅเคฎ เคญเคพเคฐเคค เค•เฅ€ </textarea> <textarea id="kn"></textarea> <button id="transliterate">Transliterate</button> <py-config type="json" src="config.json"></py-config> <script type="py" src="main.py" config="config.json"></script> <script> window.show = (fromTxt, translated, map) => { const root = document.getElementById('jsroot') console.info({fromTxt, translated, map}) } </script> <py-repl>import os print(os.listdir()) print(os.listdir('./pyscript'))</py-repl> </body> </html> test-server/config.josn { "packages":[ "regex", "Flask>=2.0.3", "Flask-CORS>=3.0.6", "Requests>=2.20.1", "pyyaml>=5.4.1", "regex>=2021.8.3", "fonttools[unicode]>=4.27", "lxml" ], "fetch": [{ "files": [ "../test-server/__init__.py", "../test-server/main.py", "../aksharamukha/Convert.py", "../aksharamukha/transliterate.py", ...rest of the files... ] }] } I am getting No module named 'aksharamukha' Error
17:16
ooh and main.py from js import window, document from pyodide.ffi import to_js, create_proxy import os print(os.listdir()) print(os.listdir('./pyscript')) from aksharamukha import transliterate def handler(e): fromTxt = document.getElementById('hi').value translated = transliterate.process('hindi', 'kannada', fromTxt, param="lang_name") # translated = "HI" print(fromTxt, translated) window.show(fromTxt, translated, []) document.getElementById('transliterate').addEventListener('click', create_proxy(handler))
17:22
I was using https://pyscript.net/snapshots/2023.09.1.RC2/core.js before as per the docs and I was able to load these local files into the env but I was not able to access createproxy now I am able to access create proxy but running import os print(os.listdir()) print(os.listdir('./pyscript')) gives ```['pyscript'] ['html.py', '__init.py', '_plugin.py', '_mime.py', '_deprecated_globals.py', '_event_loop.py', '_internal.py', '_event_handling.py']``` meaning noting is getting loaded into browser's python env even though I am getting log of these files loading, am I setting this incorrectly?
Avatar
switched back to https://pyscript.net/snapshots/2023.09.1.RC2/core.js again and used @when('click', "#transliterate") now everything is working, surprisingly even importing create_proxy is working, I guess I was importing it from a wrong place before, sorry for confusion.
๐Ÿ‘ 2
Avatar
maybe this is more of a pyodide issue but anyone else find that they can't install the wordcloud package even though the pyodide website says it should be available?
Avatar
Avatar
alexb
maybe this is more of a pyodide issue but anyone else find that they can't install the wordcloud package even though the pyodide website says it should be available?
Jeff Glass 20/10/2023 19:38
What version of pyscript/Pyodide are you using?
Avatar
Avatar
Jeff Glass
What version of pyscript/Pyodide are you using?
whatever is provided at https://pyscript.net/latest/pyscript.js which I believe you guys call "classic" now
19:43
pyodide 0.23.2
Avatar
I just checked and pyodide latest is on 0.24 so maybe thats why
Avatar
Jeff Glass 20/10/2023 20:33
Ah yeah wordcloud specifically had some issues in Pyodide 0.23. Though the changelog says it was restored in 0.23.4, it wasn't actually re-included in the release until 0.24.0
Avatar
sadly trying to upgrade pyscript versions is non-trivial either but I guess thats to be expected for such a young project
Avatar
Jeff Glass 20/10/2023 21:10
For sure - the current release candidate is a total rewrite from โ€œclassicโ€. In classic though, thereโ€™s a setting for py-config called interpreters that allows you to specify a Pyodide version https://github.com/pyscript/pyscript/blob/2023.05.1/docs/reference/elements/py-config.md#interpreter
21:11
Running classic with Pyodide 0.24.1 might break a few things, but worth a try
Avatar
Avatar
Jeff Glass
For sure - the current release candidate is a total rewrite from โ€œclassicโ€. In classic though, thereโ€™s a setting for py-config called interpreters that allows you to specify a Pyodide version https://github.com/pyscript/pyscript/blob/2023.05.1/docs/reference/elements/py-config.md#interpreter
amazing! that worked! thanks so much!
Avatar
Rust-Ninja-Sabi 23/10/2023 10:41
I want to use Python functions in JavaScript. Is that possible? Any Examples? Thanks
Avatar
Avatar
Rust-Ninja-Sabi
I want to use Python functions in JavaScript. Is that possible? Any Examples? Thanks
Jeff Glass 23/10/2023 13:47
Check out https://pyscript.recipes/2023.09.1.RC2/js-integration/export-js/. Essentially: from pyscript import window def hello(name): print(f"Hello {name}") window.helloInJS = hello <script> helloInJs("Rust-Ninja-Sabi") </script>
๐Ÿ‘ 1
Avatar
Avatar
Jeff Glass
Check out https://pyscript.recipes/2023.09.1.RC2/js-integration/export-js/. Essentially: from pyscript import window def hello(name): print(f"Hello {name}") window.helloInJS = hello <script> helloInJs("Rust-Ninja-Sabi") </script>
Rust-Ninja-Sabi 23/10/2023 14:17
@Jeff Glass thank you ๐Ÿ‘๐Ÿ˜Ž
Avatar
Avatar
Jeff Glass
Check out https://pyscript.recipes/2023.09.1.RC2/js-integration/export-js/. Essentially: from pyscript import window def hello(name): print(f"Hello {name}") window.helloInJS = hello <script> helloInJs("Rust-Ninja-Sabi") </script>
Rust-Ninja-Sabi 23/10/2023 15:24
Normally JavaScript starts before Python is loaded. Is there an event to wait until interpreter is loaded? What is the best way to guarantee that the Python function is defined before JavaScript use it?
Avatar
Avatar
Rust-Ninja-Sabi
Normally JavaScript starts before Python is loaded. Is there an event to wait until interpreter is loaded? What is the best way to guarantee that the Python function is defined before JavaScript use it?
Jeff Glass 23/10/2023 15:26
If you're using 2023.09.1.RC2 (the current release candidate), the simplest thing to do is add an event listener for the py:all-done event (edited)
๐Ÿ‘ 1
Avatar
Andrea Giammarchi 23/10/2023 17:05
that's a bit upside-down to me ... the listener is for the main thread to allow JS listeners to eventually do something or react to it, but in Python code, the moment your code executes is already the moment the interpreter is ready, for logical reasons ๐Ÿ˜… (edited)
17:07
the hook for onInterpreterReady is currently the way to know when everything is going to be executed for that specific script or tag node, the all:done is for when all things have finished so it might be too late to the pipe, but I am not 100% sure that was the question about lifecycle
๐Ÿ‘ 1
17:08
alternatively, there is a py:ready event dispatched per each <script type="py"> or <py-script> content, so that's where you want to do more before the code gets executed, if that makes sense. (edited)
Avatar
FirstLast | Ping reply please 24/10/2023 05:52
Hello, I am trying to use pyscript. I downloaded core.css and core.js files from https://pyscript.net/snapshots/2023.09.1.RC2/ and have included them in my HTML using the <link> and the <script> tag. In Chrome's browser dev tools, I get the following errors: plugins.js:3 GET http://localhost:8000/static/pyscript.core2023.09.1.RC2/error-e4fe78fd.js net::ERR_ABORTED 404 (Not Found) error @ plugins.js:3 (anonymous) @ config.js:117 login/:1 Uncaught (in promise) TypeError: Failed to fetch dynamically imported module: http://localhost:8000/static/pyscript.core2023.09.1.RC2/error-e4fe78fd.js core.js:223 Uncaught (in promise) TypeError: Failed to fetch dynamically imported module: http://localhost:8000/static/pyscript.core2023.09.1.RC2/error-e4fe78fd.js await (async) (anonymous) @ custom.js:151 Promise.then (async) zt @ custom.js:83 Vt @ custom.js:232 (anonymous) @ core.js:129 Show 3 more frames Show less (edited)
Avatar
FirstLast | Ping reply please 24/10/2023 07:06
Okay. I figured out the issue. This is because these dependencies on the error javascripts is resolved only if you use the JS files live at pyscript server.
Avatar
Avatar
FirstLast | Ping reply please
Okay. I figured out the issue. This is because these dependencies on the error javascripts is resolved only if you use the JS files live at pyscript server.
Andrea Giammarchi 24/10/2023 08:45
the best way to use PyScript is via CDN as explained in all examples or readme. This is because we have a dist folder where anything that can be lazy loaded will be lazy loaded. To fully have PyScript offline you need to fork the project and point at that dist folder. However, maybe for simplicity sake, we could also publish, together with the npm module a pyscript.zip file that contains the dist folder. @Jeff Glass @ntoll @antocuni anything against that? It's just another artifact to land at build time ... and it could also be just dist.zip for simplicity sake.
๐Ÿ‘ 3
Avatar
FirstLast | Ping reply please 24/10/2023 08:46
Oh, Andrea, while you are here, could you please help me one other thing?
Avatar
Avatar
Andrea Giammarchi
the best way to use PyScript is via CDN as explained in all examples or readme. This is because we have a dist folder where anything that can be lazy loaded will be lazy loaded. To fully have PyScript offline you need to fork the project and point at that dist folder. However, maybe for simplicity sake, we could also publish, together with the npm module a pyscript.zip file that contains the dist folder. @Jeff Glass @ntoll @antocuni anything against that? It's just another artifact to land at build time ... and it could also be just dist.zip for simplicity sake.
it seems like a good idea to me
Avatar
FirstLast | Ping reply please 24/10/2023 08:47
I am trying to explore the cryptography package (https://github.com/pyca/cryptography/tree/main). But some of it is in Rust. You think Pyodide would be able to handle it?
Avatar
Avatar
FirstLast | Ping reply please
I am trying to explore the cryptography package (https://github.com/pyca/cryptography/tree/main). But some of it is in Rust. You think Pyodide would be able to handle it?
cryptography is listed among the officially supported pyodide packages, so it should just work out of the box: https://pyodide.org/en/stable/usage/packages-in-pyodide.html
Avatar
Avatar
antocuni
cryptography is listed among the officially supported pyodide packages, so it should just work out of the box: https://pyodide.org/en/stable/usage/packages-in-pyodide.html
FirstLast | Ping reply please 24/10/2023 08:49
Yeah. But the versions seem different.
08:50
Cryptography github is at version 41-ish (https://github.com/pyca/cryptography/tags)
08:51
But in Pyodide changelogs, it's cryptography v3.3.2 (https://pyodide.org/en/stable/project/changelog.html#id21)
Avatar
Avatar
FirstLast | Ping reply please
Cryptography github is at version 41-ish (https://github.com/pyca/cryptography/tags)
Andrea Giammarchi 24/10/2023 09:08
I think that's the pypy version, not the module available in pyodide ... you need to pre-build-and-adjust (Emscripten and/or WASI) native packages to have these available, and as that package looks available in pyodide, I strongly recommend to use it from there.
๐Ÿ‘ 1
Avatar
Avatar
antocuni
it seems like a good idea to me
Andrea Giammarchi 24/10/2023 09:18
Description This MR simply addresses this discord comment. Changes added an explicit release build step that builds and zip the dist folder excluded the dist.zip file from GitHub Checklist All...
๐Ÿ‘ 2
Avatar
While we're at it, can someone please check out this: https://github.com/pyscript/pyscript/pull/1793
Description Our top level Makefile was complicated and contained cruft. This PR simplifies things so a new maintainers set-up process is simple. After discord based discussion we've dropped con...
Avatar
Avatar
ntoll
While we're at it, can someone please check out this: https://github.com/pyscript/pyscript/pull/1793
Andrea Giammarchi 24/10/2023 09:23
This branch is out-of-date with the base branch
Avatar
Hmm... I rebased yesterday... 2 mins and I'll do it again (again). ๐Ÿ˜‰
Avatar
@Andrea Giammarchi fixed.
Avatar
Avatar
ntoll
@Andrea Giammarchi fixed.
Andrea Giammarchi 24/10/2023 09:51
approved
Avatar
Thank you!
Avatar
FirstLast | Ping reply please 24/10/2023 15:05
I am implementing a feature to encrypt user passwords in my login form using the cryptography package. I am using django as my backend framework. In django, the code to generate the key-pairs is as follows: from cryptography.hazmat.primitives.asymmetric import rsa from cryptography.hazmat.primitives import serialization # 1. Generate private key. private_key = rsa.generate_private_key( public_exponent=65537, key_size=2048, ) # 2. Generate public key from the private key public_key = private_key.public_key() # 3. Serialize the public key into session data so as to be able to access it in template file. self.request.session["loginPublicKey"] = public_key.public_bytes( encoding=serialization.Encoding.PEM, format=serialization.PublicFormat.SubjectPublicKeyInfo ).decode()
15:08
Then, in my template file, I was thinking of code as follows: <script type="py" src="encryption.py" config="pyscript.json"></script> <py-script> def on_click_of_submit_button(event): public_key = {{request.session.loginPublicKey|safe}} encrypt(public_key) </py-script>
15:10
With the encrypt(public_key) function being defined in encryption.py as follows: from pyscript import document from cryptography.hazmat.primitives import hashes, serialization from cryptography.hazmat.primitives.asymmetric import padding import base64 def encrypt(public_key): # 1. Get the plain-text password. plainTextPassword = document.querySelector("#id_password").value # 2. Deserialize the public key into cyrptography library's public_key object public_key_object = serialization.load_pem_public_key(public_key.encode()) # 3. Encrypt the plain-text password using the deserialized public key. encryptedPasswordInBytes = public_key_object.encrypt( plainTextPassword, padding.OAEP( mgf=padding.MGF1(algorithm=hashes.SHA256()), algorithm=hashes.SHA256(), label=None ) ) # 4. Change the encrypted password's byte-encoding to base 64 so as to be able to serialize it into UTF-8 text. encryptedPasswordInUTF8Text = base64.b64encode(encryptedPasswordInBytes).decode('utf-8') document.querySelector("#id_password").value = encryptedPasswordInUTF8Text
15:13
The serialized public key, as stored in self.request.session["loginPublicKey"] is of the form: '-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA6xFbS2+0frRUFoFYGXla\nfONlN+3vvGtoyQN/h/uxgIo2HJnlJ4dlRhRgyN8FBUPADgeKz7rApaezeVnHAkRG\nYt6A2GgWGfqqWNLwobGadvTcxGx9kgOiur0+6MKvIUahfy3E+FkMb/2hCtHNKoIf\nm3fV1hLIxqUkf8B6GsuAi3oMBfD8M6wMPiKM4bds2mM/Pb8jnvnNI+tolmO+GjwO\nuhEOy+gBt/Of7L5t2AmVFRh1ZmVTKz3xC3oNlvsF6oFsiiDcPRWwK4HZGZ84Kyha\n4Pk95+3Oq5pMhzGLyvWB7JrAiO97udL43YiELsVrcc6pXKc2R3sGIcGzJl/3319H\nswIDAQAB\n-----END PUBLIC KEY-----\n'
15:16
However, the newline characters don't show up in the output inside the <py-script> tag: <py-script> public_key = -----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyAWBZHwC33EBnhY0GHM9 TEwflddhf3ZQIx8uF0uB95ihkegXSBsqorKp11MzPZqVAYS8ByHAV7TF6lH5i6FH ACZOHjxsJcWfNmwk5RHyOxFBc8JmcHjY8uLsw/en6r3FeAzES8tZhyahWSVLK2wn WBA3b6gCPDXVqcU6mO3LEur2aAbZY8nsdFr9PmKW5D3PchaFWCCKJZQzZ8ozUdIl vAnkVZt123UzVcLAEk2XYwbuns+Bz67CTdDKFUPd6b5bGG1xI/CfgFgMJB8HNVrN +T8qpLptk3EP2yHCGpuMh51d4Zk6/HTG8UExS98bUMEhrmVrb3YKeg68eDTQXmHw xQIDAQAB -----END PUBLIC KEY----- </py-script> (edited)
15:17
and I get the following errors in chrome's dev tools on page load: error.js:15 PythonError: Traceback (most recent call last): File "/lib/python311.zip/_pyodide/_base.py", line 491, in eval_code CodeRunner( File "/lib/python311.zip/_pyodide/_base.py", line 268, in __init__ self.ast = next(self._gen) ^^^^^^^^^^^^^^^ File "/lib/python311.zip/_pyodide/_base.py", line 145, in _parse_and_compile_gen mod = compile(source, filename, mode, flags | ast.PyCF_ONLY_AST) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "<exec>", line 2 public_key = -----BEGIN PUBLIC KEY----- ^^^^^^ SyntaxError: invalid syntax (at pyodide.asm.js:9:12519) at new_error (pyodide.asm.js:9:12519) at pyodide.asm.wasm:0x158827 at pyodide.asm.wasm:0x15892c at Module._pythonexc2js (pyodide.asm.js:9:640895) at Module.callPyObjectKwargs (pyodide.asm.js:9:81856) at Proxy.callKwargs (pyodide.asm.js:9:97507) at Object.runPython (pyodide.asm.js:9:118859) at Object.it [as run] (_python.js:12:28) at a.<computed> [as run] (custom.js:110:51) at HTMLElement.connectedCallback (core.js:259:17) e.onInterpreterReady.add.t.io.stderr @ error.js:15 it @ _python.js:15 a.<computed> @ custom.js:110 connectedCallback @ core.js:259 await in connectedCallback (async) (anonymous) @ core.js:266 Show 2 more frames Show less (edited)
15:20
So, my question is, how to hande such multiline strings in pyscript?
Avatar
FirstLast | Ping reply please 24/10/2023 15:51
Okay, so I changed the code inside <py-script> to the following: {% autoescape off %} <py-script> def on_click_of_submit_button(event): encrypt("""{{request.session.loginPublicKey|safe}}""") </py-script> {% endautoescape %} Although the output didn't change (it still doesn't show the newline (\n) characters, but all the errors on page load are gone. (edited)
Avatar
Avatar
FirstLast | Ping reply please
However, the newline characters don't show up in the output inside the <py-script> tag: <py-script> public_key = -----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyAWBZHwC33EBnhY0GHM9 TEwflddhf3ZQIx8uF0uB95ihkegXSBsqorKp11MzPZqVAYS8ByHAV7TF6lH5i6FH ACZOHjxsJcWfNmwk5RHyOxFBc8JmcHjY8uLsw/en6r3FeAzES8tZhyahWSVLK2wn WBA3b6gCPDXVqcU6mO3LEur2aAbZY8nsdFr9PmKW5D3PchaFWCCKJZQzZ8ozUdIl vAnkVZt123UzVcLAEk2XYwbuns+Bz67CTdDKFUPd6b5bGG1xI/CfgFgMJB8HNVrN +T8qpLptk3EP2yHCGpuMh51d4Zk6/HTG8UExS98bUMEhrmVrb3YKeg68eDTQXmHw xQIDAQAB -----END PUBLIC KEY----- </py-script> (edited)
Andrea Giammarchi 24/10/2023 15:59
this is not valid python ... you write only valid python in <py-script> or, even better, <script type="py"> ... the "even better" is that <script> never suffers from DOM parsing, while custom elements do.
Avatar
Avatar
FirstLast | Ping reply please
Okay, so I changed the code inside <py-script> to the following: {% autoescape off %} <py-script> def on_click_of_submit_button(event): encrypt("""{{request.session.loginPublicKey|safe}}""") </py-script> {% endautoescape %} Although the output didn't change (it still doesn't show the newline (\n) characters, but all the errors on page load are gone. (edited)
Andrea Giammarchi 24/10/2023 15:59
have you tried using <script type="py"> instead?
Avatar
Avatar
Andrea Giammarchi
have you tried using <script type="py"> instead?
FirstLast | Ping reply please 24/10/2023 15:59
on it.
Avatar
Andrea Giammarchi 24/10/2023 16:01
<script type="py"> def on_click_of_submit_button(event): encrypt("""{{request.session.loginPublicKey|safe}}""") </script>
Avatar
FirstLast | Ping reply please 24/10/2023 16:01
Yeah. I tried this: {% autoescape off %} <script type="py"> def on_click_of_submit_button(event): encrypt("""{{request.session.loginPublicKey|safe}}""") <script type="py"> {% endautoescape %}
Avatar
Andrea Giammarchi 24/10/2023 16:01
one thing though ... watch out leaking sensitive details on your page, within or without pyscript around
๐Ÿ‘ 1
Avatar
Avatar
FirstLast | Ping reply please
Yeah. I tried this: {% autoescape off %} <script type="py"> def on_click_of_submit_button(event): encrypt("""{{request.session.loginPublicKey|safe}}""") <script type="py"> {% endautoescape %}
Andrea Giammarchi 24/10/2023 16:02
that's not what I wrote
Avatar
FirstLast | Ping reply please 24/10/2023 16:02
Okay. I'll change it.
Avatar
Avatar
Andrea Giammarchi
<script type="py"> def on_click_of_submit_button(event): encrypt("""{{request.session.loginPublicKey|safe}}""") </script>
FirstLast | Ping reply please 24/10/2023 16:03
The errors on page load are gone.
Avatar
Andrea Giammarchi 24/10/2023 16:03
if you right click the source of the page what do you see?
Avatar
FirstLast | Ping reply please 24/10/2023 16:04
But, when I click submit, I get the following errors in Chrome's dev tools: error.js:15 PythonError: Traceback (most recent call last): File "<exec>", line 3, in on_click_of_submit_button NameError: name 'encrypt' is not defined at new_error (pyodide.asm.js:9:12519) at pyodide.asm.wasm:0x158827 at pyodide.asm.wasm:0x15892c at Module._pythonexc2js (pyodide.asm.js:9:640895) at Module.callPyObjectKwargs (pyodide.asm.js:9:81856) at Module.callPyObject (pyodide.asm.js:9:82066) at Proxy.call (pyodide.asm.js:9:97213) at Object.ct [as runEvent] (_python.js:36:22) at HTMLInputElement.Dt (listeners.js:42:17) e.onInterpreterReady.add.t.io.stderr @ error.js:15 ct @ _python.js:39 Dt @ listeners.js:42 Show 2 more frames Show less
Avatar
Avatar
Andrea Giammarchi
if you right click the source of the page what do you see?
FirstLast | Ping reply please 24/10/2023 16:04
<script type="py"> def on_click_of_submit_button(event): encrypt("""-----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsgN0/gotEGvYkgKK2LNw Zas+ys9P86JNfE0T2iqjpllzLYc0e0VKZdlS9vTHvdkSlEw5uP5VAHAPLryWvVws zch74NfSNBjd25IJNnAcMXZhwTnFGajN1qVwWczC3EoCaoKsrRog+pkHmPxZ2o4M 7jp74kLuL5x90aGfAIFGOc+Cu+2oRoaUqnU8u2bYMv9oPxa9GcMVUkX+81H/T0xm nwKeutfI85ZcsCIr8Ghn1MCxEPrLtamZxeay/BFdHvs8pnLp07B10J6FOEsQ146S OBa1GpyOWlw3dKC2YaAMC4lIIosBX4h5MZV+ITRhdnuTbwPoP1b8RzNlVu7W9udL VQIDAQAB -----END PUBLIC KEY----- """) </script>
Avatar
Andrea Giammarchi 24/10/2023 16:05
OK, the new line is there, great ... now, where does encrypt come from?
16:05
you can ignore 2/3rd of the error message, just focus on the initial part, which is pretty clear to me
16:06
NameError: name 'encrypt' is not defined
Avatar
FirstLast | Ping reply please 24/10/2023 16:07
encrypt() is there in encryption.py <script type="py" src="encryption.py" config="pyscript.json"></script>
Avatar
Andrea Giammarchi 24/10/2023 16:07
to me it looks like you should put encryption.py in the py-config as file to fetch, then in the page you need to from encryption import encrypt then you should be good when you invoke that
Avatar
FirstLast | Ping reply please 24/10/2023 16:07
Ah! I see. I commented that line.
Avatar
Andrea Giammarchi 24/10/2023 16:08
watch out this just execute the file as python, it doesn't leak globally the encrypt function (or if it does, it's not the suggested approach here) (edited)
16:08
<script type="py" src="encryption.py" config="pyscript.json"></script>
16:09
you want that file to be part of the filesystem and import encrypt from it, then you should be OK
Avatar
Avatar
Andrea Giammarchi
you want that file to be part of the filesystem and import encrypt from it, then you should be OK
FirstLast | Ping reply please 24/10/2023 16:09
Um. Please elaborate, Andrea.
16:10
Like, should I, instead of mentioning encryption.py as src, import it in <script type="py">
16:10
?
Avatar
Andrea Giammarchi 24/10/2023 16:10
you want to import encrypt from encryption.py file ... you don't want to execute 2 script type py, you want a single script with a config that also brings in the encryption.py file in the filesystem so you can import encrypt from it.
Avatar
FirstLast | Ping reply please 24/10/2023 16:11
I get the not executing 2 py type scripts part. But not the part after that. (edited)
Avatar
Andrea Giammarchi 24/10/2023 16:12
in your pyscript.json you need to add a fetch that points at encryption.py so you can then from encryption import encrypt and call it a day ... so that encrypt is available.
Avatar
FirstLast | Ping reply please 24/10/2023 16:12
Oh. Lemme see that.
Avatar
Andrea Giammarchi 24/10/2023 16:13
<script type="py" config="pyscript.json"> from encryption import encrypt def on_click_of_submit_button(event): encrypt("""{{request.session.loginPublicKey|safe}}""") </script> (edited)
16:13
this is the script you land, the file needs to be available in Python though, so you need to add a "fetch": ["./encryption.py"] in the pyscript.json config (edited)
๐Ÿ‘ 1
Avatar
FirstLast | Ping reply please 24/10/2023 16:19
Andrea, I don't see "fetch" in the user guide here https://docs.pyscript.net/2023.09.1.RC2/user-guide/
16:19
Are you referring to the "files" directive? (edited)
Avatar
Yes, use "files".
๐Ÿ‘ 1
Avatar
Andrea Giammarchi 24/10/2023 16:32
uhm ... we do have fetch too though, either ways, that's the solution ๐Ÿ˜„
16:32
apologies for the confusion
Avatar
Avatar
Andrea Giammarchi
apologies for the confusion
FirstLast | Ping reply please 24/10/2023 16:33
I am not able to make "files" work. (edited)
16:37
I created the following pyscript.json: { "packages": ["cryptography"], // "files": { // // "AdminApp/static/AdminApp/py/encryption.py" : "encryption.py" // } "fetch" : [ "AdminApp/static/AdminApp/py/encryption.py" ] } (edited)
16:40
With fetch, I don't get any errors... neither on page load, nor on click of the submit button. However, on click of the submit button, I don't see the text being encrypted. It's being trasmitted in plain text format.
Avatar
FirstLast | Ping reply please 24/10/2023 17:31
I got the "files" working for the script and import statement. ๐Ÿฅณ (edited)
17:32
But, cryptography library is throwing error in chrome's dev tools: error.js:15 PythonError: Traceback (most recent call last): File "<exec>", line 4, in on_click_of_submit_button File "/home/pyodide/encryption.py", line 14, in encrypt encryptedPasswordInBytes = public_key_object.encrypt( ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/lib/python3.11/site-packages/cryptography/hazmat/backends/openssl/rsa.py", line 539, in encrypt return _enc_dec_rsa(self._backend, self, plaintext, padding) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/lib/python3.11/site-packages/cryptography/hazmat/backends/openssl/rsa.py", line 95, in _enc_dec_rsa return _enc_dec_rsa_pkey_ctx(backend, key, data, padding_enum, padding) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/lib/python3.11/site-packages/cryptography/hazmat/backends/openssl/rsa.py", line 157, in _enc_dec_rsa_pkey_ctx res = crypt(pkey_ctx, buf, outlen, data, len(data)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: initializer for ctype 'unsigned char *' must be a bytes or list or tuple, not str at new_error (pyodide.asm.js:9:12519) at pyodide.asm.wasm:0x158827 at pyodide.asm.wasm:0x15892c at Module._pythonexc2js (pyodide.asm.js:9:640895) at Module.callPyObjectKwargs (pyodide.asm.js:9:81856) at Module.callPyObject (pyodide.asm.js:9:82066) at Proxy.call (pyodide.asm.js:9:97213) at Object.ct [as runEvent] (_python.js:36:22) at HTMLInputElement.Dt (listeners.js:42:17)
Avatar
Andrea Giammarchi 24/10/2023 17:32
I am afraid you are in pure Python and library territory here, I can't help much further as I don't know that module myself
๐Ÿ‘ 1
17:32
everything else seems to work as expected though
๐Ÿ‘ 1
Avatar
FirstLast | Ping reply please 24/10/2023 17:33
Thanks, Andrea! Big help!
17:33
Um.. say..
Avatar
Andrea Giammarchi 24/10/2023 17:33
IIRC pyodide has a handy utility to convert text into buffer ... or maybe it's just Python offering that somehow
Avatar
FirstLast | Ping reply please 24/10/2023 17:33
could it be the issue with newline characters not showing up?
Avatar
Andrea Giammarchi 24/10/2023 17:34
nope, the error is clear
17:34
TypeError: initializer for ctype 'unsigned char *' must be a bytes or list or tuple, not str
17:34
the new line is there, you can see it yourself ๐Ÿ˜‰
Avatar
FirstLast | Ping reply please 24/10/2023 17:34
Because the output is this: <script type="py" config="/static/InfrastructureApp/pyscript.core2023.09.1.RC2/pyscript.json"></script> <script type="py"> from encryption import encrypt def on_click_of_submit_button(event): encrypt("""-----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsAD64DBNQdk5ZNlfI0cf SV6fMs5t2/Oq7kXuFrIh/UZqVhLbt5f6MIznQWJiqkipQaV7DItzsSa3oz0E6EQ1 m7Ds+1W2emjJUwq+Nc9jWhojlMEtvb7fm5MY13uUriiDdLjkptQJuO5O6/1jtBEY n5eeqH+jIBnTM8B6bCCoQop4PTLWgjoz/n1y3HVPqMIVH+kf3DZeUzFAFv3wxeTQ UApdJ08Mvairmys1k5YS5vtuLGSU7buV6IuU8uM8YXNpViGuXCeaSQkYaEsvx/4d GbBOFsJQaIQgc5sBYwvFeCSPw2ncEEPsUI4masii+eFA8vs89nSU9AHIk3sutsqr EwIDAQAB -----END PUBLIC KEY----- """) </script>
Avatar
Andrea Giammarchi 24/10/2023 17:34
that's a string with a new line at the end
17:35
do you need to remove it? if that's the case, trim the string but I don't think that's the error
17:35
this one is: TypeError: initializer for ctype 'unsigned char *' must be a bytes or list or tuple, not str
๐Ÿ‘ 1
Avatar
FirstLast | Ping reply please 24/10/2023 17:35
No. I don't need it removed.
Avatar
Andrea Giammarchi 24/10/2023 17:35
not str
17:35
so it's there
17:35
you can literally see it
17:35
-----END PUBLIC KEY----- """
๐Ÿ‘ 1
17:35
that's a new line
17:36
you can trim and add "\n" manually though, if that's your concern, but the error is clearly about you passing a string, the new line eventually comes after as issue
๐Ÿ‘ 1
Avatar
FirstLast | Ping reply please 24/10/2023 17:40
I made a minor change to the code: <script type="py" config="pyscript.json"></script> <script type="py"> from encryption import encrypt def on_click_of_submit_button(event): public_key = """{{request.session.loginPublicKey|safe}}""" encrypt(public_key) </script>
17:41
Then, in encryption.py, I have: from pyscript import document from cryptography.hazmat.primitives import hashes, serialization from cryptography.hazmat.primitives.asymmetric import padding import base64 def encrypt(public_key): # 1. Get the plain-text password. plainTextPassword = document.querySelector("#id_password").value # 2. Deserialize the public key into cyrptography library's public_key object public_key_object = serialization.load_pem_public_key(public_key.encode()) # 3. Encrypt the plain-text password using the deserialized public key. encryptedPasswordInBytes = public_key_object.encrypt( plainTextPassword, padding.OAEP( mgf=padding.MGF1(algorithm=hashes.SHA256()), algorithm=hashes.SHA256(), label=None ) ) # 4. Change the encrypted password's byte-encoding to base 64 so as to be able to serialize it into UTF-8 text. encryptedPasswordInUTF8Text = base64.b64encode(encryptedPasswordInBytes).decode('utf-8') document.querySelector("#id_password").value = encryptedPasswordInUTF8Text
Avatar
Andrea Giammarchi 24/10/2023 17:41
try bytes(string, 'utf-8') or something, I am also out for today ... btw, it's: <script type="py" config="pyscript.json"> from encryption import encrypt def on_click_of_submit_button(event): public_key = """{{request.session.loginPublicKey|safe}}""" encrypt(public_key) </script> the cases you want or need two pyscript tags on the same page are really close to zero ... this is not one of them
17:41
see if using encrypt(bytes(public_key)) changes anything in there
17:42
public_key_object = serialization.load_pem_public_key(public_key.encode())
17:42
I kinda bet that's the issue there
Avatar
Avatar
Andrea Giammarchi
try bytes(string, 'utf-8') or something, I am also out for today ... btw, it's: <script type="py" config="pyscript.json"> from encryption import encrypt def on_click_of_submit_button(event): public_key = """{{request.session.loginPublicKey|safe}}""" encrypt(public_key) </script> the cases you want or need two pyscript tags on the same page are really close to zero ... this is not one of them
FirstLast | Ping reply please 24/10/2023 17:42
Ah! shucks. Sorry.
17:43
<script type="py" config="pyscript.json"> from encryption import encrypt def on_click_of_submit_button(event): public_key = """{{request.session.loginPublicKey|safe}}""" encrypt(public_key) </script>
Avatar
Andrea Giammarchi 24/10/2023 17:43
or the fact you pass plainTextPassword as first argument ... then again, I am closing for today, maybe others can help ... I don't know this API, sorry.
๐Ÿ‘ 1
Avatar
FirstLast | Ping reply please 24/10/2023 17:54
Thank you so much, Andrea! The problem was indeed with the encrypt() method. It required the plaintextpassword to be converted to bytes-type object.
๐Ÿ‘ 1
Avatar
I'm having an issue where trying to run a Matplotlib plot on pyscript is telling me that plt.Rectangle has no get_corners attribute, however this works completely fine on my local environment, and the plt.Rectangle object definitely does have a get_corners attribute I just started using pyscript and theres a good chance it's just a dumb mistake, but any help would be greatly appreciated!
Avatar
I printed the dir of the object and yeah, the pyscript one doesn't have get_corners but the local environment one does, version mismatch I guess?
Avatar
Avatar
Caera
I printed the dir of the object and yeah, the pyscript one doesn't have get_corners but the local environment one does, version mismatch I guess?
Jeff Glass 24/10/2023 21:09
Which version of PyScript are you using, and how are you including Matplotlib in your project (in py-config)
Avatar
I have packages = ["matplotlib", "scipy",] name = "Matplotlib" as my pyconfig and I'm using latest pyscript (I believe)
Avatar
Avatar
Jeff Glass
Which version of PyScript are you using, and how are you including Matplotlib in your project (in py-config)
<link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" /> <script defer src="https://pyscript.net/latest/pyscript.js"></script>
21:12
This is all on pyscript.com, not sure if that's impacting it as well
Avatar
Jeff Glass 24/10/2023 21:12
So /latest uses Pyodide 0.23.2, which pins Matplotlib 3.5.2. Is it possible the get_corners function was introduced in a newer version?
Avatar
Yeah definitely possible
21:14
Is there any work-around or do I just have to wait for an update
21:15
Not really in a rush to make this so it's no big deal, just a fun side project
Avatar
Jeff Glass 24/10/2023 21:17
In that release, thereโ€™s a setting for py-config called interpreters that allows you to specify a Pyodide version https://github.com/pyscript/pyscript/blob/2023.05.1/docs/reference/elements/py-config.md#interpreter
21:17
PyScirpt only guarantees compatibility with a single Pyodide version, but youโ€™d be welcome to try, say, Pyodide 0.24.1 and see if that works, at your own risk ๐Ÿ˜…
Avatar
I appreciate the help I'll give it a shot
21:18
Its definitely not an important project or anything, just a personal website, so not much risk for me
๐Ÿ‘ 1
Avatar
seems like even pyodide 0.24.1 has matplotlib 3.5.2
21:31
The wait begins
Avatar
Hey! I have been using pyscript classic until now for a POC and am working on switching to pyscript NEXT and start using workers. I was using pyscript.interpreter.runPythonAsync(code) to run some code on demand, is that still valid in NEXT? My existing code doesn't work and I am wondering if pyscript is still available in the window or if I have to do something else to access it. (edited)
Avatar
Avatar
sai
Hey! I have been using pyscript classic until now for a POC and am working on switching to pyscript NEXT and start using workers. I was using pyscript.interpreter.runPythonAsync(code) to run some code on demand, is that still valid in NEXT? My existing code doesn't work and I am wondering if pyscript is still available in the window or if I have to do something else to access it. (edited)
Jeff Glass 24/10/2023 23:39
The way to grab a reference to the interpreter, if you need one, is using a Hook, probably onInterpreterReady. Sorry Iโ€™m on mobile and canโ€™t prep a demo, but check out the PolyScript documentation on Hooks for some pointers
Avatar
Avatar
Jeff Glass
The way to grab a reference to the interpreter, if you need one, is using a Hook, probably onInterpreterReady. Sorry Iโ€™m on mobile and canโ€™t prep a demo, but check out the PolyScript documentation on Hooks for some pointers
Thanks for pointing out the hooks! Iโ€™ll try that!
Avatar
สŸแดœแดแด€ 25/10/2023 19:23
Hello ! I have a website running using pyscript for the past couple months with no trouble. However, since yesterday, for no reason, this error pops up: Traceback (most recent call last): File "/lib/python3.10/site-packages/_pyodide/_base.py", line 435, in eval_code .run(globals, locals) File "/lib/python3.10/site-packages/_pyodide/_base.py", line 304, in run coroutine = eval(self.code, globals, locals) File "<exec>", line 3, in <module> File "/lib/python3.10/site-packages/panel/__init__.py", line 48, in <module> from . import layout # noqa File "/lib/python3.10/site-packages/panel/layout/__init__.py", line 31, in <module> from .accordion import Accordion # noqa File "/lib/python3.10/site-packages/panel/layout/accordion.py", line 5, in <module> from .base import NamedListPanel File "/lib/python3.10/site-packages/panel/layout/base.py", line 13, in <module> from ..reactive import Reactive File "/lib/python3.10/site-packages/panel/reactive.py", line 33, in <module> from .viewable import Layoutable, Renderable, Viewable File "/lib/python3.10/site-packages/panel/viewable.py", line 25, in <module> from .config import config, panel_extension File "/lib/python3.10/site-packages/panel/config.py", line 417, in <module> config = _config(**{k: None if p.allow_None else getattr(_config, k) File "/lib/python3.10/site-packages/panel/config.py", line 216, in __init__ super().__init__(**params) File "/lib/python3.10/site-packages/param/parameterized.py", line 4139, in __init__ if not isinstance(self._param__private, _InstancePrivate): File "/lib/python3.10/site-packages/panel/config.py", line 289, in __getattribute__ init = super().__getattribute__('initialized') AttributeError: '_config' object has no attribute 'initialized' Did pyscript receive an update lately that couldve broke panel ?
19:25
In fact, im using a specific release of pyscript since any versions beyond that one broke panel
Avatar
Avatar
สŸแดœแดแด€
Hello ! I have a website running using pyscript for the past couple months with no trouble. However, since yesterday, for no reason, this error pops up: Traceback (most recent call last): File "/lib/python3.10/site-packages/_pyodide/_base.py", line 435, in eval_code .run(globals, locals) File "/lib/python3.10/site-packages/_pyodide/_base.py", line 304, in run coroutine = eval(self.code, globals, locals) File "<exec>", line 3, in <module> File "/lib/python3.10/site-packages/panel/__init__.py", line 48, in <module> from . import layout # noqa File "/lib/python3.10/site-packages/panel/layout/__init__.py", line 31, in <module> from .accordion import Accordion # noqa File "/lib/python3.10/site-packages/panel/layout/accordion.py", line 5, in <module> from .base import NamedListPanel File "/lib/python3.10/site-packages/panel/layout/base.py", line 13, in <module> from ..reactive import Reactive File "/lib/python3.10/site-packages/panel/reactive.py", line 33, in <module> from .viewable import Layoutable, Renderable, Viewable File "/lib/python3.10/site-packages/panel/viewable.py", line 25, in <module> from .config import config, panel_extension File "/lib/python3.10/site-packages/panel/config.py", line 417, in <module> config = _config(**{k: None if p.allow_None else getattr(_config, k) File "/lib/python3.10/site-packages/panel/config.py", line 216, in __init__ super().__init__(**params) File "/lib/python3.10/site-packages/param/parameterized.py", line 4139, in __init__ if not isinstance(self._param__private, _InstancePrivate): File "/lib/python3.10/site-packages/panel/config.py", line 289, in __getattribute__ init = super().__getattribute__('initialized') AttributeError: '_config' object has no attribute 'initialized' Did pyscript receive an update lately that couldve broke panel ?
Andrea Giammarchi 25/10/2023 19:34
none of these errors look related to pyscript ... which pinned version are you on? it could be that pyodide version is different, so it's the package ... we haven't changed anything around that yesterday in particular, but not knowing which PyScript version you are running, with all these Release Candidate changes that also land in separate URLs, is not super helpful, thanks!
Avatar
สŸแดœแดแด€ 25/10/2023 19:34
let me grab that
Avatar
Avatar
sai
Thanks for pointing out the hooks! Iโ€™ll try that!
Andrea Giammarchi 25/10/2023 19:35
watch out hooks have been fully refactored, but if you get it to work on RC hooks, the change for you should be minimal in the next RC.
Avatar
สŸแดœแดแด€ 25/10/2023 19:36
<script defer src="https://pyscript.net/releases/2022.12.1/pyscript.js" ></script> panel is using 0.13
Avatar
Avatar
สŸแดœแดแด€
<script defer src="https://pyscript.net/releases/2022.12.1/pyscript.js" ></script> panel is using 0.13
Andrea Giammarchi 25/10/2023 19:36
ouch ... that's a pretty old pinned version, and I assure you we have never changed a single byte in that code-base yesterday ... not that I am aware of anyway
Avatar
สŸแดœแดแด€ 25/10/2023 19:37
yeaah
19:37
i havent touched anything since its basically a static web page
19:38
and at that point, I went through enough trouble making panel and file uploads work fine where it went like "push to repo, launch website, everybody's happy" (edited)
Avatar
Andrea Giammarchi 25/10/2023 19:38
I hear you, and we promote pinned versions for stability too ... it looks like upstream (read pyodide) deployed a package that might have changed out of the blue ... we don't control pyodide dependencies or packages graph in PyScript, so it's likely something to dig more in there. (edited)
Avatar
สŸแดœแดแด€ 25/10/2023 19:38
hmm
19:39
Could be pyodide, its the only one that doesnt exactly have a package import or script import
19:39
(outside of the typical import pyodide)
Avatar
Andrea Giammarchi 25/10/2023 19:42
I am afraid without a minimal reproducible example with your config and code it's hard to help but even then, it's really an old PyScript version we haven't changed or touched since ... well, 2022 I suppose ๐Ÿ˜ฆ
Avatar
สŸแดœแดแด€ 25/10/2023 19:42
hmmm
19:43
<py-config type="toml"> packages = ["numpy", "pandas", "panel==0.13.1a2", "activejson", "./crcmod-1.7-py3-none-any.whl", "ndspy", "more_itertools", "ordered_set"] terminal = false [[fetch]] files = ["./randomizer_py.zip"] </py-config>
19:43
not a whole lot going on
19:44
I can link the github repo, but it might a job of "get up and update your dependencies"
Avatar
Andrea Giammarchi 25/10/2023 19:45
so you have pyodide v0.21.3 there ... have you looked for issues with your specific library? ./crcmod-1.7-py3-none-any.whl doens't fully help here neither, sorry.
19:45
although panel==0.13.1a2 should guarantee at least that bit is sealed in time
Avatar
Avatar
สŸแดœแดแด€
I can link the github repo, but it might a job of "get up and update your dependencies"
Andrea Giammarchi 25/10/2023 19:46
not necessarily if underlying stack changed too ... minor releases on semver starting with 0 might mean breaking changes
Avatar
สŸแดœแดแด€ 25/10/2023 19:47
crcmod is a standalone wheel, no worries on that side
Avatar
Andrea Giammarchi 25/10/2023 19:47
pyodide is beyond 0.24 now, so things might be different ... I'd rather pin all packages to versions you know that worked instead, if you're not willing to rewrite or change the thing using latest
19:48
have you checked which of your dependencies got updated yesterday or few days before that?
Avatar
สŸแดœแดแด€ 25/10/2023 19:50
hmmm checking the console doesnt reveal any possible deps that updated; however, the exception happens only when the page has been fully initialized
19:50
might lead to somewhere
Avatar
Avatar
สŸแดœแดแด€
hmmm checking the console doesnt reveal any possible deps that updated; however, the exception happens only when the page has been fully initialized
Jeff Glass 25/10/2023 21:43
Just trying to get a minimal example, i see the same error with just this: <script defer src="https://pyscript.net/releases/2022.12.1/pyscript.js"></script> <py-config> packages=['panel==0.13.1a2'] </py-config> <py-script> import panel </py-script>
Avatar
สŸแดœแดแด€ 25/10/2023 21:44
so one of those three
21:44
but none has changed since, theyre all frozen in time
Avatar
Jeff Glass 25/10/2023 21:45
Network tab in the dev tools does imply it's fetching the corret wheel
21:45
For panel 0.13.1a2
21:45
Well, could be some not-fully-pinned dependency in that panel version?
Avatar
สŸแดœแดแด€ 25/10/2023 21:45
hmmmm
21:45
Most likely, didnt consider that
Avatar
Avatar
สŸแดœแดแด€
Most likely, didnt consider that
Jeff Glass 25/10/2023 23:12
param had a 2.0.0 release yesterday... that seems suspicious, given that the error message mentioned param/pameterized.py...
Avatar
Avatar
Jeff Glass
param had a 2.0.0 release yesterday... that seems suspicious, given that the error message mentioned param/pameterized.py...
สŸแดœแดแด€ 25/10/2023 23:38
ooooh
23:38
interesting
23:38
Think I could force a param version from the package config ?
Avatar
Jeff Glass 26/10/2023 00:02
I would think so? List it before panel and see what happens?
Avatar
FirstLast | Ping reply please 26/10/2023 09:47
Hi, I have created the following pyscript: <script type="py" config="{% static 'InfrastructureApp/pyscript.core2023.09.1.RC2/pyscript.json' %}"> from pyscript import document from encryption import encrypt def on_click_of_submit_button(event): if (document.getElementById('id_username').value.strip() != "") and (document.getElementById('id_password').value.strip() != ""): public_key = """{{request.session.loginPublicKey|safe}}""" encrypt(public_key) return True else: return False </script> The intent is that:
  • if the form's username and password fields, after being trimmed, don't represent and empty string, then encrypt them;
  • else (i.e., when their trimmed version is empty), prevent form submission.
09:49
How should I go about doing this? In javascript, as shown here (https://phpgurukul.com/how-to-encrypt-password-on-client-side/), they prevent form submission by return false from the function.
Avatar
Andrea Giammarchi 26/10/2023 09:55
else: event.preventDefault()
09:55
the return value is a DOM Level 0 thing but the correct way to prevent default is via event ... you can omit the return True or return False entirely.
๐Ÿ‘ 1
Avatar
FirstLast | Ping reply please 26/10/2023 13:33
I have a python file titled encryption.py with the following two functions: def formValidation(): pass def encrypt(public_key): pass
13:35
I have included it in my pyscript script's json's configuration using the "files" attribute as follows: { "files": { "{DOMAIN}": "http://localhost:8000", "{DOMAIN}/static/py/encryption.py" : "./encryption.py" }, "packages": ["cryptography"] }
13:36
Then, I am trying to import those two functions in my pyscript script as follows: <script type="py" config="pyscript.json"> from pyscript import document, window from encryption import formValidation, encrypt </script>
13:37
With this, I get the error: PythonError: Traceback (most recent call last): File "/lib/python311.zip/_pyodide/_base.py", line 499, in eval_code .run(globals, locals) ^^^^^^^^^^^^^^^^^^^^ File "/lib/python311.zip/_pyodide/_base.py", line 340, in run coroutine = eval(self.code, globals, locals) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "<exec>", line 3, in <module> ImportError: cannot import name 'formValidation' from 'encryption' (/home/pyodide/encryption.py) (edited)
13:39
However, with the same encryption.py, if I change my pyscript script to only import the encrypt function, everythin works fine.
13:39
I mean there's nothing in those functions except pass statements.
13:43
And just now, I also observed that if I do those two function's import in two separate lines, it works. So, the following works: <script type="py" config="pyscript.json"> from pyscript import document, window from encryption import formValidation from encryption import encrypt </script>
13:44
Is this some known issue?
Avatar
Avatar
FirstLast | Ping reply please
Is this some known issue?
Jeff Glass 26/10/2023 13:56
Not a known issue, and trying your example locally works fine for me, either importing both in one line or in separate lines...
13:57
It sounds like you have a caching issue maybe, where your first try was using a cached version of encryption.py, but your changes in the second version busted the cache and caused things to work. Just a guess
13:57
What are you using to using to view/load these example? A live server/dev server? PyScript.com?
Avatar
Avatar
Jeff Glass
What are you using to using to view/load these example? A live server/dev server? PyScript.com?
FirstLast | Ping reply please 26/10/2023 16:15
Hi Jeff, I am using my local machine, referencing the pyscript JS and CSS from https://pyscript.net/snapshots/2023.09.1.RC2/
Avatar
Avatar
FirstLast | Ping reply please
Hi Jeff, I am using my local machine, referencing the pyscript JS and CSS from https://pyscript.net/snapshots/2023.09.1.RC2/
Jeff Glass 26/10/2023 16:20
And what are you using to actually view your files? Are you just doing File > Open in a web browser, using a local dev server... ?
Avatar
Avatar
Jeff Glass
And what are you using to actually view your files? Are you just doing File > Open in a web browser, using a local dev server... ?
FirstLast | Ping reply please 26/10/2023 16:27
So, my web server is actually django's in-built server.
16:27
My browser is Google Chrome, I use the incognito mode for development purposes to do away with the caching issue.
16:28
But it's quite possible that some cache may have built into my tab's environment.
16:28
I'll look into it by closing all browser instances and restarting the incognito mode afresh.
16:29
Till now, I would resetart django's web server and do Ctrl+F5 in the incognito browser window. (edited)
Avatar
Avatar
FirstLast | Ping reply please
So, my web server is actually django's in-built server.
Jeff Glass 26/10/2023 16:35
I would be curious if you changed your PyScript code back to the original 'non-working' version if it continued working
Avatar
Avatar
Jeff Glass
I would be curious if you changed your PyScript code back to the original 'non-working' version if it continued working
FirstLast | Ping reply please 26/10/2023 16:35
'non-working' as in the one where I was importing both the functions in the same line?
16:36
I too am intrigued by this issue, btw, because if you look at the line above, i am importing 2 objects in a single line from pyscript. (edited)
16:37
Right now, I have both the imports from encryption.py in different lines to be able to work on other stuff.
16:44
On a side-note, Jeff, do you know if it's possible to debug pyscript using breakpoints and stuff within the browser? Like Chrome's dev tools allow you to add breakpoints to javascript in real time to help with debugging. Is the same possible with pyscript?
Avatar
Avatar
FirstLast | Ping reply please
Right now, I have both the imports from encryption.py in different lines to be able to work on other stuff.
Jeff Glass 26/10/2023 16:47
Yes it's very odd - what I'm saying is, if you change that line back to importing both objects on the same line... it breaks again?
16:47
Not quite possible to debug in PyScript with IDE breakpoints... but check out these early-stage experiements from @antocuni https://github.com/pyscript/pyscript/discussions/1744#discussioncomment-7074227
๐Ÿ‘ 1
Avatar
Avatar
Jeff Glass
Yes it's very odd - what I'm saying is, if you change that line back to importing both objects on the same line... it breaks again?
FirstLast | Ping reply please 26/10/2023 16:48
It does. Trully in ntoll's way of saying in the other channel, "why on earth doesn't this work?". ๐Ÿ˜†
๐Ÿซ  1
Avatar
Jeff Glass 26/10/2023 16:49
That is... bizarre. Whitespace issue of some kind??
16:50
If you add a third smybol to encryption.py, do all three have to be imported on separate lines? (edited)
Avatar
FirstLast | Ping reply please 26/10/2023 16:51
Lemme add a 3rd function.
16:53
from encryption import formValidation, encrypt, thirdfunction doesn't break.
16:55
Now, from encryption import formValidation, encrypt doesn't break either. ๐Ÿคฃ
๐Ÿ˜… 1
Avatar
Jeff Glass 26/10/2023 16:55
Well, problem solved then!
Avatar
FirstLast | Ping reply please 26/10/2023 16:56
In true dev style as I usually do. ๐Ÿ˜†
16:57
Thanks, Jeff! And special thanks for introducing that thread to me. I'll be sure to keep up with it.
Avatar
Avatar
FirstLast | Ping reply please
Thanks, Jeff! And special thanks for introducing that thread to me. I'll be sure to keep up with it.
Andrea Giammarchi 27/10/2023 09:01
usually Ctrl+Shift+R helps busting the browser cache
๐Ÿ‘ 1
Avatar
FirstLast | Ping reply please 27/10/2023 10:41
Say I'm have the following pyscript code to select a password input field: passwordInputElement = document.getElementById('id_password') When the HTML doesn't have any id_password field, javascript returns a null. In case of pyscript, passwordInputElement would be None?
10:42
And I would be checking if passwordInputElement is None, right?
Avatar
Avatar
FirstLast | Ping reply please
And I would be checking if passwordInputElement is None, right?
Andrea Giammarchi 27/10/2023 11:01
if not passwordInputElement should work too
๐Ÿ‘ 1
Avatar
Hi all. Quick question. I am trying to add an event listener to an <img id="test_image" src=....> tag (is just a 'click' so I can test how it works), that runs a python function that brings up an alert (I just want to see it work and expand from there). def tst(): js.alert('I work') persistent_fct = js.create_proxy(tst) my_image = grab_element_by_id("test_image") my_image.addEventListener('click', persistent_fct) I imported (all) js and grab_element_by_id() is just a custom made, a bit fancier function on top of the js.document.querySelector, that returns the selected tag object. However I am getting this traceback: AttributeError: create_proxy. Is there another built in function I should be using to create a persistent proxy? (edited)
Avatar
Jeff Glass 27/10/2023 17:47
create_proxy isn't a JS function, it's something that Pyodide adds on the Python side. You'll want to add from pyodide.ffi import create_proxy
17:48
Well, the cleanest would actually be to do: import js from pyodide.ffi.wrappers import add_event_listener def tst(): js.alert('I work') my_image = grab_element_by_id("test_image") add_event_listener(my_image, 'click', tst)
Avatar
Avatar
Jeff Glass
create_proxy isn't a JS function, it's something that Pyodide adds on the Python side. You'll want to add from pyodide.ffi import create_proxy
Great, it works! Thank you!
๐Ÿฅณ 1
Avatar
Avatar
Mikimic
Great, it works! Thank you!
Jeff Glass 27/10/2023 17:48
The pyodide add_event_listener is a thin wrapper around JS's add_event_listener and does the proxy management for you
Avatar
Avatar
Jeff Glass
The pyodide add_event_listener is a thin wrapper around JS's add_event_listener and does the proxy management for you
Thank you so much! ๐ŸŽ‰
Avatar
Avatar
Jeff Glass
Well, the cleanest would actually be to do: import js from pyodide.ffi.wrappers import add_event_listener def tst(): js.alert('I work') my_image = grab_element_by_id("test_image") add_event_listener(my_image, 'click', tst)
I actually had to add the "event" as an argument to the tst() function, otherwise it throws back a traceback, but it works
Avatar
Avatar
Mikimic
I actually had to add the "event" as an argument to the tst() function, otherwise it throws back a traceback, but it works
Jeff Glass 27/10/2023 20:20
Ahhh yes good catch
Avatar
Josh (EduBlocks) 27/10/2023 20:42
Is it possible to access the dom inside of a hooks.worker.onReady hook?
20:42
or at least, pass something to the main thread to then do something with the dom
Avatar
Avatar
Josh (EduBlocks)
Is it possible to access the dom inside of a hooks.worker.onReady hook?
Jeff Glass 27/10/2023 21:28
You can use the XWorker that's passed to that hook to access the dom via its window property: <p id="foo">Hello!</p> <script type="module"> import { config, hooks } from "https://pyscript.net/unstable/core.js" hooks.worker.onReady.add((wrap, xw) => { const p = xw.window.document.getElementById("foo") console.log("Content is: ", p.textContent) }) </script> <script type="py" worker> # Just a tag to make the above hook run </script>
Avatar
Avatar
Jeff Glass
Josh (EduBlocks) 27/10/2023 21:29
excellent! thank you! knew there would be docs somewhere ๐Ÿ™‚
๐Ÿฅณ 1
Avatar
Jeff Glass 27/10/2023 21:30
For sure! The hooks docs further down that page are also useful, and already reflect the 'new style' hooks from the breaking changes from a PR earlier this week
๐Ÿ‘ 1
Avatar
Rust-Ninja-Sabi 28/10/2023 15:44
Is it possible to use PyTorch or transformers from huggingface with PyScript? Any examples?
Avatar
Rust-Ninja-Sabi 29/10/2023 12:08
Package enum does not work in PyScript. Should it work or is it not available with pyodide?
Avatar
Avatar
Rust-Ninja-Sabi
Package enum does not work in PyScript. Should it work or is it not available with pyodide?
Jeff Glass 29/10/2023 13:06
When you say โ€œdoes not workโ€, what are you doing and what error are you getting?
Avatar
Avatar
Jeff Glass
When you say โ€œdoes not workโ€, what are you doing and what error are you getting?
Rust-Ninja-Sabi 29/10/2023 16:19
The programm: <py-config> { "packages": ["enum"] } </py-config> <py-script> from enum import Enum class Color(Enum): RED = 1 GREEN = 2 BLUE = 3 </py-script> The error Unhandled Promise Rejection: PythonError: Traceback (most recent call last): File "/lib/python3.11/site-packages/micropip/_commands/install.py", line 142, in install await transaction.gather_requirements(requirements) Fil...
Avatar
Avatar
Rust-Ninja-Sabi
The programm: <py-config> { "packages": ["enum"] } </py-config> <py-script> from enum import Enum class Color(Enum): RED = 1 GREEN = 2 BLUE = 3 </py-script> The error Unhandled Promise Rejection: PythonError: Traceback (most recent call last): File "/lib/python3.11/site-packages/micropip/_commands/install.py", line 142, in install await transaction.gather_requirements(requirements) Fil...
Jeff Glass 29/10/2023 17:58
enum is in the python standard lib, and does not need to be included in packages
17:58
In your code, itโ€™s trying to install โ€œenumโ€ from PyPI and failing
Ferro1945 started a thread. 29/10/2023 19:09
Avatar
hello
22:47
Is it possible to have a user input directly on the web page and not through a dialog box ?
Avatar
Avatar
Vigon_E
Is it possible to have a user input directly on the web page and not through a dialog box ?
Jeff Glass 29/10/2023 23:15
How do you mean "directly on the web page"? You mean without the alert pop-up area?
Avatar
Avatar
Andrea Giammarchi
the best way to use PyScript is via CDN as explained in all examples or readme. This is because we have a dist folder where anything that can be lazy loaded will be lazy loaded. To fully have PyScript offline you need to fork the project and point at that dist folder. However, maybe for simplicity sake, we could also publish, together with the npm module a pyscript.zip file that contains the dist folder. @Jeff Glass @ntoll @antocuni anything against that? It's just another artifact to land at build time ... and it could also be just dist.zip for simplicity sake.
FirstLast | Ping reply please 30/10/2023 08:04
Hi, Is it possible to have a completely offline pyscript experience? Like not have any cdn.jsdelivr.net depencies?
Avatar
Avatar
FirstLast | Ping reply please
Hi, Is it possible to have a completely offline pyscript experience? Like not have any cdn.jsdelivr.net depencies?
Andrea Giammarchi 30/10/2023 10:12
the latest module on npm also provide a dist.zip folder with all files in there ... however, you are in charge of downloading your own pyodide and use that as interpreter in the py-config
Avatar
FirstLast | Ping reply please 30/10/2023 10:13
Hi Andrea!
Avatar
Andrea Giammarchi 30/10/2023 10:13
we should probably write a quick documentation about it ... I know @Fabio did it recently (I mean, getting it to work locally)
โค๏ธ 1
Avatar
FirstLast | Ping reply please 30/10/2023 10:15
Yeah. I downloaded the npm release. But it didn't offer a dist.zip. I did the following:
  • ran the npm i @pyscript/core on local windows terminal
  • copied the files from C:\Users\username\node_modules\@pyscript\core\dist folder
Avatar
Andrea Giammarchi 30/10/2023 10:16
maybe it's not published yet ... it should soon
Avatar
FirstLast | Ping reply please 30/10/2023 10:16
But the files I copied had dependencies on cdn.jsdelivr for pyodide etc. (edited)
Avatar
Avatar
Jeff Glass
enum is in the python standard lib, and does not need to be included in packages
Rust-Ninja-Sabi 30/10/2023 10:58
Thank you. Now it โ€žworksโ€œ.๐Ÿ‘๐Ÿ˜Ž
Avatar
Avatar
FirstLast | Ping reply please
But the files I copied had dependencies on cdn.jsdelivr for pyodide etc. (edited)
Andrea Giammarchi 30/10/2023 12:38
which one?
Avatar
Hello! Can I use AJAX in pyscript?
Avatar
Avatar
Mikimic
Hello! Can I use AJAX in pyscript?
Andrea Giammarchi 30/10/2023 21:01
Hello, yes!
Avatar
Avatar
Andrea Giammarchi
Hello, yes!
Sweet! How would I go about getting the fetch and the .then methods? This is what I am trying to translate to pyscript (in Django): <form id="my_form" method="post"> {% csrf_token %} <input id="my_data_field" type="text" name="my_field"> <input type="submit"> </form> <p id="my_answer_p">No data</p> <script> document.getElementById("my_form").addEventListener("submit", function(event) { event.preventDefault(); var my_data_in_field = document.getElementById("my_data_field").value; const formData = {'my_field': my_data_in_field}; fetch('{% url "quest_populate_log" %}', { method: 'POST', body: JSON.stringify(formData), headers: { 'X-Requested-With': 'XMLHttpRequest', 'X-CSRFToken': getCookie('csrftoken') } }) .then(response => response.json()) .then(data => { document.getElementById("my_answer_p").innerText = data.answer; }) .catch(error => console.error('Error:', error)); }); function getCookie(name) { let value = "; " + document.cookie; let parts = value.split("; " + name + "="); if (parts.length === 2) return parts.pop().split(";").shift(); } (edited)
Avatar
Avatar
Andrea Giammarchi
Hello, yes!
I think I can pretty much figure out by myself - I just don't know where to get the fetch, .then and .catch and how to implement them in pyscript. I only provided the code so you understand what I want to do. Thank you in advance!
Avatar
Avatar
Mikimic
I think I can pretty much figure out by myself - I just don't know where to get the fetch, .then and .catch and how to implement them in pyscript. I only provided the code so you understand what I want to do. Thank you in advance!
Jeff Glass 30/10/2023 22:12
For the fetch, I would use pyodide.http.pyfetch - itโ€™s a pretty thin wrapper around fetch, with a slightly more pythonic API https://pyodide.org/en/stable/usage/api/python-api/http.html#pyodide.http.pyfetch
22:14
You can await js Thenables directly in pyscript - see the section on calling Js functions in Python https://pyodide.org/en/stable/usage/type-conversions.html#calling-javascript-functions-from-python
22:15
So I think th strategy is - define an async function which does the fetch, then processes the results, then attach that function as an event listener
๐Ÿ‘ 2
Avatar
Avatar
Andrea Giammarchi
which one?
FirstLast | Ping reply please 31/10/2023 05:52
Pretty sure I saw core.js having dependencies on cdn.jsdeliver. Hold up.
05:56
Yeah. Can confirm. core.js definitely has them. Just need a CTRL+F cdn.jsdelivr.
Avatar
FirstLast | Ping reply please 31/10/2023 06:38
Those dependencies result in errors (in Chrome Dev Tools' Console) when working offline.
Avatar
Hi, I have my folder structure set up where I have a script.py in my root folder that I want to import in my main.py inside a subdirectory where the Pyscript files are stored. . โ”œโ”€โ”€ script.py โ”œโ”€โ”€ demo-website โ”œโ”€โ”€ index.html โ”œโ”€โ”€ main.js โ”œโ”€โ”€ main.py โ””โ”€โ”€ pyscript.toml In my main.py I import as follows from script import ... Assuming the web server runs from the directory demo-website/, is it possible to import script.py?
07:06
Idk what I did earlier but when I had pyscript.toml like this: [[fetch]] files = ["../script.py"] to_folder = "project" and when I ran the web server from the root dir, it works. But I'm hoping to deploy this to GitHub Pages where it runs the webserver from demo-website/ instead because that's where index.html is stored. But like earlier, it doesn't work. Any ideas?
Avatar
Avatar
kyoya
Idk what I did earlier but when I had pyscript.toml like this: [[fetch]] files = ["../script.py"] to_folder = "project" and when I ran the web server from the root dir, it works. But I'm hoping to deploy this to GitHub Pages where it runs the webserver from demo-website/ instead because that's where index.html is stored. But like earlier, it doesn't work. Any ideas?
FirstLast | Ping reply please 31/10/2023 08:27
I did the following (though, I used JSON; not TOML): { "files": { "https://domain.com/directory/script.py" : "./script.py" } } (edited)
Avatar
Avatar
FirstLast | Ping reply please
Yeah. Can confirm. core.js definitely has them. Just need a CTRL+F cdn.jsdelivr.
Andrea Giammarchi 31/10/2023 08:44
we don't ship all runtimes and all runtimes have their own CDN dependency too (i.e. any extra package you might want or need) so for those you need to download somewhere pyodide and packages and use the py-config interpreter to point at those ... the only foreign dependency is the lazy toml parser but has also been moved in the dist recently and XTerm will do the same unless we make that lazy dist import too ... which we could.
Avatar
Avatar
Andrea Giammarchi
we don't ship all runtimes and all runtimes have their own CDN dependency too (i.e. any extra package you might want or need) so for those you need to download somewhere pyodide and packages and use the py-config interpreter to point at those ... the only foreign dependency is the lazy toml parser but has also been moved in the dist recently and XTerm will do the same unless we make that lazy dist import too ... which we could.
FirstLast | Ping reply please 31/10/2023 08:57
Hey Andrea! Can you please share an example? Or point me to the documentation section I should refer to? I am not able to deduce how to make the browser load the interpreter using interpreter option in the user guid here (https://docs.pyscript.net/2023.09.1.RC1/user-guide). Are you referring to <script type = "mpy"...> and <script type = "py"...> ?
Avatar
Avatar
FirstLast | Ping reply please
Hey Andrea! Can you please share an example? Or point me to the documentation section I should refer to? I am not able to deduce how to make the browser load the interpreter using interpreter option in the user guid here (https://docs.pyscript.net/2023.09.1.RC1/user-guide). Are you referring to <script type = "mpy"...> and <script type = "py"...> ?
Andrea Giammarchi 31/10/2023 08:59
08:59
you should use RC2
๐Ÿ‘ 1
โค๏ธ 1
Avatar
FirstLast | Ping reply please 31/10/2023 09:18
Ah! Thanks!
Avatar
FirstLast | Ping reply please 31/10/2023 10:23
Hmm. I download pyodide's full package and put in at this location in my Django app: {Project_Root_Directory}/InfrastructureApp/static/InfrastructureApp/pyodide_full_0.24.1/ Then, I wrote my JSON as follows: { "interpreter": "http://localhost:8000/static/InfrastructureApp/pyodide_full_0.24.1/pyodide.mjs", "files": { "{DOMAIN}": "http://localhost:8000", "{DOMAIN}/static/ADSSAdminApp/py/encryption.py" : "./encryption.py" }, "packages": ["cryptography"] } Now, when I load my homepage (http://localhost:8000), Chrome's Dev Tools show these errors: pyodide.mjs:1 Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/plain". Strict MIME type checking is enforced for module scripts per HTML spec. login/:1 Uncaught (in promise) TypeError: Failed to fetch dynamically imported module: http://localhost:8000/static/InfrastructureApp/pyodide_full_0.24.1/pyodide.mjs login/:1 Uncaught (in promise) TypeError: Failed to fetch dynamically imported module: http://localhost:8000/static/InfrastructureApp/pyodide_full_0.24.1/pyodide.mjs However, when I right-click --> "Open in new tab" to that link related to pyodide.mjs, it does load that javascript file. (edited)
10:24
I am definitely missing something. ๐Ÿค”
Avatar
Avatar
Jeff Glass
For the fetch, I would use pyodide.http.pyfetch - itโ€™s a pretty thin wrapper around fetch, with a slightly more pythonic API https://pyodide.org/en/stable/usage/api/python-api/http.html#pyodide.http.pyfetch
Thank you kind sir, I shall try and make it work
Avatar
FirstLast | Ping reply please 31/10/2023 10:36
Oh. This is wierd. In the Network tab, pyodide.mjs got a 200 OK.
10:36
Avatar
Avatar
FirstLast | Ping reply please
Hmm. I download pyodide's full package and put in at this location in my Django app: {Project_Root_Directory}/InfrastructureApp/static/InfrastructureApp/pyodide_full_0.24.1/ Then, I wrote my JSON as follows: { "interpreter": "http://localhost:8000/static/InfrastructureApp/pyodide_full_0.24.1/pyodide.mjs", "files": { "{DOMAIN}": "http://localhost:8000", "{DOMAIN}/static/ADSSAdminApp/py/encryption.py" : "./encryption.py" }, "packages": ["cryptography"] } Now, when I load my homepage (http://localhost:8000), Chrome's Dev Tools show these errors: pyodide.mjs:1 Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/plain". Strict MIME type checking is enforced for module scripts per HTML spec. login/:1 Uncaught (in promise) TypeError: Failed to fetch dynamically imported module: http://localhost:8000/static/InfrastructureApp/pyodide_full_0.24.1/pyodide.mjs login/:1 Uncaught (in promise) TypeError: Failed to fetch dynamically imported module: http://localhost:8000/static/InfrastructureApp/pyodide_full_0.24.1/pyodide.mjs However, when I right-click --> "Open in new tab" to that link related to pyodide.mjs, it does load that javascript file. (edited)
Andrea Giammarchi 31/10/2023 10:36
you need to run a local server or the mime type will always be text content ... this is how localhost works for anything out there.
Avatar
FirstLast | Ping reply please 31/10/2023 10:37
You mean an additional local server, in addition to Django's built-in server? (edited)
Avatar
Avatar
FirstLast | Ping reply please
You mean an additional local server, in addition to Django's built-in server? (edited)
Andrea Giammarchi 31/10/2023 10:39
the error is clear: Strict MIME type checking is enforced for module scripts per HTML spec. so either you are not running your server in localhost:8000 or your Django version doesn't understand .mjs file extension as application/javascript which is required ... I've raised this concern ages ago when they proposed this extension and here we are with the issue I've raised myself. you can rename that file to .js instead and see if that works, overwriting eventually the other .js file if present, as it'll be useless.
Avatar
Avatar
Andrea Giammarchi
the error is clear: Strict MIME type checking is enforced for module scripts per HTML spec. so either you are not running your server in localhost:8000 or your Django version doesn't understand .mjs file extension as application/javascript which is required ... I've raised this concern ages ago when they proposed this extension and here we are with the issue I've raised myself. you can rename that file to .js instead and see if that works, overwriting eventually the other .js file if present, as it'll be useless.
FirstLast | Ping reply please 31/10/2023 10:42
Pyodide's distribution also provides a pyodide.js. Lemme try with that.
10:44
pyodide.js:17 Uncaught (in promise) TypeError: e is not a function at Object.engine (pyodide.js:17:13) at interpreters.js:41:24 engine @ pyodide.js:17 (anonymous) @ interpreters.js:41 Promise.then (async) sn @ custom.js:83 define @ custom.js:234 (anonymous) @ core.js:259 Promise.then (async) (anonymous) @ core.js:129 Show 4 more frames Show less pyodide.js:17 Uncaught (in promise) TypeError: e is not a function at Object.engine (pyodide.js:17:13) at interpreters.js:41:24 engine @ pyodide.js:17 (anonymous) @ interpreters.js:41 Promise.then (async) Qt @ listeners.js:66 define @ custom.js:233 (anonymous) @ core.js:259 Promise.then (async) (anonymous) @ core.js:129 Show 4 more frames Show less
10:44
This is what I get when I switched to JS from MJS.
Avatar
Avatar
FirstLast | Ping reply please
pyodide.js:17 Uncaught (in promise) TypeError: e is not a function at Object.engine (pyodide.js:17:13) at interpreters.js:41:24 engine @ pyodide.js:17 (anonymous) @ interpreters.js:41 Promise.then (async) sn @ custom.js:83 define @ custom.js:234 (anonymous) @ core.js:259 Promise.then (async) (anonymous) @ core.js:129 Show 4 more frames Show less pyodide.js:17 Uncaught (in promise) TypeError: e is not a function at Object.engine (pyodide.js:17:13) at interpreters.js:41:24 engine @ pyodide.js:17 (anonymous) @ interpreters.js:41 Promise.then (async) Qt @ listeners.js:66 define @ custom.js:233 (anonymous) @ core.js:259 Promise.then (async) (anonymous) @ core.js:129 Show 4 more frames Show less
Andrea Giammarchi 31/10/2023 10:45
that file is not a module ... you need to remove that file and rename .mjs into .js instead.
Avatar
FirstLast | Ping reply please 31/10/2023 10:45
oh.
Avatar
Andrea Giammarchi 31/10/2023 10:45
you can rename that file to .js instead and see if that works, overwriting eventually the other .js file if present, as it'll be useless.
I didn't write this by accident ๐Ÿ˜‰
10:46
you should also raise an issue in Django project as .mjs is standard IANA matter https://datatracker.ietf.org/doc/rfc9239/
This document describes the registration of media types for the ECMAScript and JavaScript programming languages and conformance requirements for implementations of these types. This document obsoletes RFC 4329 ("Scripting Media Types)", replacing the previous registrations with information and requirements aligned with common usage and implement...
10:47
otherwise just use an updated version of Django as .mjs files are pretty common these days, even if nobody technically needs these on the Web (and ultimately in NodeJS neither).
Avatar
FirstLast | Ping reply please 31/10/2023 10:51
Andrea, you said you had raised an issue. Was it in django community? If it was, maybe I can add a comment there. Otherwise, if I do raise an issue, it should be that Django's built-in server should serve MJS files with application/javascript MIME-type instead of being served with text/plainMIME-type, right? (edited)
Avatar
Avatar
FirstLast | Ping reply please
Andrea, you said you had raised an issue. Was it in django community? If it was, maybe I can add a comment there. Otherwise, if I do raise an issue, it should be that Django's built-in server should serve MJS files with application/javascript MIME-type instead of being served with text/plainMIME-type, right? (edited)
Andrea Giammarchi 31/10/2023 10:52
I've raised an issue with standard bodies, NodeJS TC, TC39, everyone on the Web, but not in Django which is just one out of thousand projects that needed to update their extensions -> mime database. (edited)
10:52
Django should handle .js and .mjs identically
10:53
that's it ... I am also surprised that's not the case already, because .mjs is surely used in the wild a lot but it cannot be served as text/plain because that's not standard so browsers complain (edited)
Avatar
FirstLast | Ping reply please 31/10/2023 10:55
So, is it that the world bodies haven't come to a conclusion whether MJS files should be served as application/javascript or as text/plain?
Avatar
Andrea Giammarchi 31/10/2023 10:56
Published specification: [ECMA-262] Applications that use this media type: Script interpreters as discussed in RFC 9239. Additional information: Deprecated alias names for this type: application/javascript, application/x-javascript, text/javascript1.0, text/ javascript1.1, text/javascript1.2, text/javascript1.3, text/ javascript1.4, text/javascript1.5, text/jscript, text/ livescript Magic number(s): N/A File extension(s): .js, .mjs Macintosh File Type Code(s): TEXT
10:56
this is the current standard ... it's your version of Django or Django that is ignoring the standard.
10:57
nothing else I can do here, sorry.
10:58
A media type (also known as a Multipurpose Internet Mail Extensions or MIME type) indicates the nature and format of a document, file, or assortment of bytes. MIME types are defined and standardized in IETF's RFC 6838.
10:58
actually, it's text/javascript the correct one ... but again, whatever Django does for .js should be the same for .mjs
Avatar
FirstLast | Ping reply please 31/10/2023 11:00
In addition, a new file extension of .mjs has been added to the list of file extensions with the restriction that contents should be parsed using the Module goal. Finally, the [HTML] specification uses "text/javascript" as the default media type of ECMAScript when preparing script tags; therefore, "text/javascript" intended usage has been moved from OBSOLETE to COMMON.
Avatar
Andrea Giammarchi 31/10/2023 11:00
Media Type Database. Contribute to jshttp/mime-db development by creating an account on GitHub.
Avatar
Avatar
FirstLast | Ping reply please
In addition, a new file extension of .mjs has been added to the list of file extensions with the restriction that contents should be parsed using the Module goal. Finally, the [HTML] specification uses "text/javascript" as the default media type of ECMAScript when preparing script tags; therefore, "text/javascript" intended usage has been moved from OBSOLETE to COMMON.
FirstLast | Ping reply please 31/10/2023 11:00
This is what you are pointing to, right? (edited)
Avatar
Andrea Giammarchi 31/10/2023 11:01
mate ... honestly, I don't know how to help further. .mjs is a standard extension that should be served as text/javascript. All servers do that. The whole Web does that. If your Django or Django doesn't do that it's a Django problem, not a PyScript one. Have you fixed your issue? We have an RC to release soon and I can't really help further here, sorry.
Avatar
Avatar
Andrea Giammarchi
mate ... honestly, I don't know how to help further. .mjs is a standard extension that should be served as text/javascript. All servers do that. The whole Web does that. If your Django or Django doesn't do that it's a Django problem, not a PyScript one. Have you fixed your issue? We have an RC to release soon and I can't really help further here, sorry.
FirstLast | Ping reply please 31/10/2023 12:58
Thanks Andrea! That helped. I asked in the Django community. It was a simple import statement at the top in app's settings.py: import mimetypes mimetypes.add_type("text/javascript", ".mjs", True)
12:58
They said that it wasn't a Django issue either. Because Django's built-in server is simply wrapping Python's built-in mimetype module. (edited)
13:01
So, if python's built-in mimetype module doesn't recognize MJS file extension, then Django's built-in server isn't going to either. So, I would need to add this mimetype manually in the django's settings.py. (edited)
Avatar
Avatar
FirstLast | Ping reply please
They said that it wasn't a Django issue either. Because Django's built-in server is simply wrapping Python's built-in mimetype module. (edited)
Andrea Giammarchi 31/10/2023 13:07
Thanks for the follow up ... interesting ... so Python lacks .mjs in its built-in mimetype module?
13:09
>>> import mimetypes >>> mimetypes.guess_type('test.mjs') ('text/javascript', None)
13:10
this works out of the box to me ... Python 3.11
Avatar
FirstLast | Ping reply please 31/10/2023 16:26
Hmm. Interesting.
Avatar
it uses mimetypes from /etc or whatever for OS so anything is possible, hence i asked to add (edited)
๐Ÿ‘ 1
Avatar
Deleted User 01/11/2023 10:23
hey im new to the server and just started experementing with py-script , as far as im concered py-script should work with the python native libraries so i tried to create a simple client-server code in which py-script runs the server nut i always encounter the same problem while tring to establish the connection and gather the client address. line: clientsocket, clientaddress = server_socket.accept(). traceback: Traceback (most recent call last): File "/home/pyodide/pyscript/_internal.py", line 104, in run_pyscript result = eval_code(code, globals=__main.dict) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/lib/python311.zip/_pyodide/_base.py", line 468, in eval_code .run(globals, locals) ^^^^^^^^^^^^^^^^^^^^ File "/lib/python311.zip/_pyodide/_base.py", line 310, in run coroutine = eval(self.code, globals, locals) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "<exec>", line 20, in <module> File "/lib/python311.zip/socket.py", line 294, in accept fd, addr = self._accept() ^^^^^^^^^^^^^^ OSError: [Errno 28] Invalid argument. can anyone help me figure it out?
Avatar
Avatar
Deleted User
hey im new to the server and just started experementing with py-script , as far as im concered py-script should work with the python native libraries so i tried to create a simple client-server code in which py-script runs the server nut i always encounter the same problem while tring to establish the connection and gather the client address. line: clientsocket, clientaddress = server_socket.accept(). traceback: Traceback (most recent call last): File "/home/pyodide/pyscript/_internal.py", line 104, in run_pyscript result = eval_code(code, globals=__main.dict) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/lib/python311.zip/_pyodide/_base.py", line 468, in eval_code .run(globals, locals) ^^^^^^^^^^^^^^^^^^^^ File "/lib/python311.zip/_pyodide/_base.py", line 310, in run coroutine = eval(self.code, globals, locals) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "<exec>", line 20, in <module> File "/lib/python311.zip/socket.py", line 294, in accept fd, addr = self._accept() ^^^^^^^^^^^^^^ OSError: [Errno 28] Invalid argument. can anyone help me figure it out?
Jeff Glass 01/11/2023 13:24
There are natural limitations to what the browser itself (and the WASM engine inside it) will allow you to do - running a webserver from inside a browser tab is one of them. The error you're seeing from socket.py is because that module is specifically a minimal stub in Pyodide (PyScript's primary Python runtime). See https://pyodide.org/en/stable/usage/wasm-constraints.html#included-but-not-working-modules for other modules that fall into this category, and other limitations. PyScript is a frontend framework for writing Python into your HTML/page. For the backend, you'd be better off with simply Python, in some form.
Avatar
Avatar
Jeff Glass
There are natural limitations to what the browser itself (and the WASM engine inside it) will allow you to do - running a webserver from inside a browser tab is one of them. The error you're seeing from socket.py is because that module is specifically a minimal stub in Pyodide (PyScript's primary Python runtime). See https://pyodide.org/en/stable/usage/wasm-constraints.html#included-but-not-working-modules for other modules that fall into this category, and other limitations. PyScript is a frontend framework for writing Python into your HTML/page. For the backend, you'd be better off with simply Python, in some form.
Deleted User 01/11/2023 13:38
thanks for clarifying that nethertheless this tool is amazing
Avatar
Rust-Ninja-Sabi 01/11/2023 15:47
I use in config [files] โ€žhttp://127.0.0.1:8000/images/โ€ž =โ€ž./Images/โ€ž That works well on a local server. But if I transfer it to other servers like itch.o I donโ€™t know ip ore port. What should I do?
Avatar
Avatar
Rust-Ninja-Sabi
I use in config [files] โ€žhttp://127.0.0.1:8000/images/โ€ž =โ€ž./Images/โ€ž That works well on a local server. But if I transfer it to other servers like itch.o I donโ€™t know ip ore port. What should I do?
Jeff Glass 01/11/2023 16:32
You can use a relative URL in files
Avatar
Is there a pyscript reference doc? I find it incredihbly difficult to know what objects I have access to.
14:55
I basically had to create a repl in a different tab to dir() my way through
Avatar
Avatar
CK
Is there a pyscript reference doc? I find it incredihbly difficult to know what objects I have access to.
Jeff Glass 02/11/2023 15:03
The current edition of the docs is here: https://docs.pyscript.net/2023.11.1.RC3/
๐Ÿ‘ 1
15:03
It sounds like the builtin helpers may be the page you most want? https://docs.pyscript.net/2023.11.1.RC3/user-guide/builtins/
Avatar
Avatar
Jeff Glass
It sounds like the builtin helpers may be the page you most want? https://docs.pyscript.net/2023.11.1.RC3/user-guide/builtins/
I just saw the #โ•ฐโ•ธpyscript-docs channel and came across this https://github.com/pyscript/pyscript/blob/2023.05.1/docs/reference/API/element.md
15:05
this would be more inline with what I was looking for
Avatar
Jeff Glass 02/11/2023 15:12
Ah gotcha - so that level of granular API docs I don't think exists one a cohesive place for the current release/RC. The pages on DOM https://docs.pyscript.net/2023.11.1.RC3/user-guide/dom/ and the external PyDom reference https://fpliger.pyscriptapps.com/pyweb/latest/pydom.html may be helpful... but I don't think there's a singular place where, say, all of the pyscript package is laid out. Maybe we need a "cover page" describing the layout of this package, and making users aware that they can access the Pyodide ffi as well https://pyodide.org/en/stable/usage/api/python-api.html. Going to ping @ntoll here who's been heading up a lot of the docs, though he's out of touch until next week.
Avatar
yeah that would help a lot with adoption
15:28
like pyscript is great but its hard to use the pyscript features if you don't know enough about them
Avatar
First I want to thank the team for all of your prior help, not to mention building such a useful tool. However, I want to revisit an item from a few weeks ago in this channel: Firefox weird behavior. Specifically, intermittent failure. At the time, I was testing with the RC2 version. This new round I'm testing with the new RC3 version. I have tested this with fresh installs with Firefox and with new profiles (at the time some of you thought it might be caused by an extension; at the time I also hadn't nailed down exactly what line could cause the issue). I've also tried the beta version of Firefox thinking it might be a bug they are addressing. Ok, so the issue is that sometimes when a Javascript function is called from the Python side (running in a worker), it fails. For instance, this is an example error: PythonError: Traceback (most recent call last): File "<exec>", line 89, in buildTableWrapper File "<exec>", line 81, in buildTable File ":9", line 20583, in hiwire_call_bound pyodide.ffi.JsException: TypeError: func.apply is not a function
16:03
buildTableWrapper and buildTable are my functions that end with a call to a JavaScript function (it passes a JSON object to paint the screen with the results). If I don't make that final call to the JavaScript function, this won't event happen. Now to emphasize the intermittent part of all of this, I had to run this function 14 times to get it to fail the first time. I'd also note that the situation is way, way better than it was with RC2. With RC2, once this error was encountered, the page was toast (I think the python worker crashed). With RC3, the user just tries again, and more likely than not it will work. This behavior occurs in no other browser (tested in Edge, Safari, Chrome, ARC, and Opera). I've also tested versions of Firefox on both Windows and Mac with the same result. I'd describe this as slightly annoying, but on balance, not a huge deal. Firefox accounts for 3-ish percent of the market and in my particular application, the typical user is going to be more than willing to download a copy of Chrome. It is just so weird given it is intermittent. Thank you again so much for your work. If this is something that can be fixed, great. If not, I'm not going to lose any sleep over it. I just wanted to let you know that it is still happening. I'm happy to provide any additional information that might be helpful.
Avatar
Avatar
Ben Smith
buildTableWrapper and buildTable are my functions that end with a call to a JavaScript function (it passes a JSON object to paint the screen with the results). If I don't make that final call to the JavaScript function, this won't event happen. Now to emphasize the intermittent part of all of this, I had to run this function 14 times to get it to fail the first time. I'd also note that the situation is way, way better than it was with RC2. With RC2, once this error was encountered, the page was toast (I think the python worker crashed). With RC3, the user just tries again, and more likely than not it will work. This behavior occurs in no other browser (tested in Edge, Safari, Chrome, ARC, and Opera). I've also tested versions of Firefox on both Windows and Mac with the same result. I'd describe this as slightly annoying, but on balance, not a huge deal. Firefox accounts for 3-ish percent of the market and in my particular application, the typical user is going to be more than willing to download a copy of Chrome. It is just so weird given it is intermittent. Thank you again so much for your work. If this is something that can be fixed, great. If not, I'm not going to lose any sleep over it. I just wanted to let you know that it is still happening. I'm happy to provide any additional information that might be helpful.
Jeff Glass 02/11/2023 16:18
Thank you for reporting it! I think the thing that would help most is a GitHub issue (unless there already is one?) with a reproducable example that we can run ourselves and test. The error message is interesting, but doesn't really tell us that much on its own
๐Ÿ‘ 1
Avatar
hmm I appear to be stuck with an app I was writing: The app would use pyodide.http.open_url to open a remote page and then parse that page with beautifulsoup to obtain some links
22:16
I thought I read that gitpages does not have CORS set for public pages but I could be wrong
Avatar
ุงุณุงู…ุฉ 03/11/2023 08:50
how can i get the input text from the html
Avatar
Jeff Glass 03/11/2023 12:52
From your use of defer in the script tag, I'm guessing you're using PyScript classic. In that case, you can grab the input element directly and read it's value: <script defer src="https://pyscript.net/releases/2023.05.1/pyscript.js"></script> <link rel="stylesheet" href="https://pyscript.net/releases/2023.05.1/pyscript.css"> <input id="inputword" type="text" placeholder="Enter a word" > <button py-click="handle_word()">Submit</button> <script type="py"> import js def handle_word(): value = js.document.getElementById("inputword").value print(f"The value is {value}") </script>
12:53
The upcoming release includes the pydom package, with is somewhat more Pythonic. Though the above syntax will still work. Note too that the py-*event syntax is a little different in this case. Once we fix our CI and actually get the unstable package built, you can use pydom like so: <script type="module" src="https://pyscript.net/unstable/core.js"></script> <link rel="stylesheet" href="https://pyscript.net/unstable/core.css"> <input id="inputword" type="text" placeholder="Enter a word" > <button py-click="handle_word">Submit</button> <script type="py"> from pyweb import pydom def handle_word(event): input_el = pydom['#inputword'][0] print(f"The value is {input_el.value}") </script> (edited)
12:54
Important: The value attribute of pydom was only added two days ago and we haven't successfully published an unstable release since then, so this will be broken for a couple days at least https://github.com/pyscript/pyscript/actions/runs/6721728371/job/18268073117
Avatar
Rust-Ninja-Sabi 03/11/2023 15:32
What is the difference between js.document and pyscript.document?
Avatar
Avatar
Rust-Ninja-Sabi
What is the difference between js.document and pyscript.document?
js.document exists only when you run pyscript in the main thread; pyscript.document exists only when you run in a worker (e.g. using <script type="py" worker>)
16:23
in short: it's preferrable to use pyscript.document
Avatar
Avatar
antocuni
js.document exists only when you run pyscript in the main thread; pyscript.document exists only when you run in a worker (e.g. using <script type="py" worker>)
Andrea Giammarchi 03/11/2023 17:11
pyscript.document exists always .. in the main, it'd be the equivalent of js.document, in workers, it'd be the proxied equivalent of js.document on main
Avatar
FirstLast | Ping reply please 04/11/2023 05:05
My understanding is that this one can probably be solved best once we have some sort of debugger available for pyscript.
Avatar
Rust-Ninja-Sabi 04/11/2023 09:00
Now we have RC3. Is there already a release date for version 1.0?
Avatar
i want to use pyscript in react+JS project
12:10
how can i parse variables from js to pyscript and viceversa?
Avatar
Rust-Ninja-Sabi 04/11/2023 12:57
I am using touch in PyScript. In event.touches is a list of touches TouchList. When I want the first element with event.touches[0] there is an error : An exception occurred: 'pyodide.ffi.JsProxy' object is not subscriptable. Whatโ€™s wrong?
Avatar
Avatar
Kobalt
i want to use pyscript in react+JS project
bugzpodder 04/11/2023 13:14
depends on how you use it. If you are looking for a pure js solution you can try react-py or my package https://bugzpodder.github.io/pysandbox/. if you want pure pyscript then pyscript-react might work. for passing variable just have them in globalThis (ideally they should be functions) (edited)
Get started incorporating python code to your app.
Avatar
Avatar
bugzpodder
depends on how you use it. If you are looking for a pure js solution you can try react-py or my package https://bugzpodder.github.io/pysandbox/. if you want pure pyscript then pyscript-react might work. for passing variable just have them in globalThis (ideally they should be functions) (edited)
thx gonna checkout both of those
Avatar
Avatar
bugzpodder
depends on how you use it. If you are looking for a pure js solution you can try react-py or my package https://bugzpodder.github.io/pysandbox/. if you want pure pyscript then pyscript-react might work. for passing variable just have them in globalThis (ideally they should be functions) (edited)
ok i included your package (in head of index.html) but idk how to insert python code in react component files
13:28
please help
Avatar
Avatar
Kobalt
ok i included your package (in head of index.html) but idk how to insert python code in react component files
bugzpodder 04/11/2023 13:35
there is no need to modify index.html, it's for a different use case ok so my package isnt react specific so you need to write your own react component with a code prop, and useEffect would execute code when it changes in addition to pass variables. i'll update my docs this week to show how it can work with react. if you want something simple react-py might do what you need out of box. (edited)
Avatar
Avatar
Rust-Ninja-Sabi
I am using touch in PyScript. In event.touches is a list of touches TouchList. When I want the first element with event.touches[0] there is an error : An exception occurred: 'pyodide.ffi.JsProxy' object is not subscriptable. Whatโ€™s wrong?
Jeff Glass 04/11/2023 13:57
Try event.touches.item(0)
Avatar
Avatar
Jeff Glass
Try event.touches.item(0)
Rust-Ninja-Sabi 04/11/2023 14:16
๐Ÿ‘ thanks. Thatโ€™s working.
Avatar
Avatar
Kobalt
ok i included your package (in head of index.html) but idk how to insert python code in react component files
bugzpodder 05/11/2023 06:03
I added a section on react integration here: https://bugzpodder.github.io/pysandbox/guides/react/
Use PySandbox in React.
Avatar
Rust-Ninja-Sabi 05/11/2023 16:18
I created my first game with PyScript. https://github.com/Rust-Ninja-Sabi/pyscript-johnny Thanks for your help
Avatar
Avatar
Rust-Ninja-Sabi
I created my first game with PyScript. https://github.com/Rust-Ninja-Sabi/pyscript-johnny Thanks for your help
bugzpodder 05/11/2023 16:38
link doesnt work
Avatar
Avatar
bugzpodder
link doesnt work
Rust-Ninja-Sabi 05/11/2023 18:02
Now it should be available
Avatar
Avatar
Rust-Ninja-Sabi
Now it should be available
bugzpodder 05/11/2023 18:03
your up/down arrow keys scrolls the page
Avatar
Avatar
bugzpodder
your up/down arrow keys scrolls the page
Rust-Ninja-Sabi 05/11/2023 18:30
Yes, sometimes. Have you any idea to change it?
Avatar
Avatar
bugzpodder
your up/down arrow keys scrolls the page
Rust-Ninja-Sabi 05/11/2023 18:34
Perhaps it has something to do with event bubbling. I will try event.stopPropagation().
Avatar
Avatar
Jeff Glass
Ah gotcha - so that level of granular API docs I don't think exists one a cohesive place for the current release/RC. The pages on DOM https://docs.pyscript.net/2023.11.1.RC3/user-guide/dom/ and the external PyDom reference https://fpliger.pyscriptapps.com/pyweb/latest/pydom.html may be helpful... but I don't think there's a singular place where, say, all of the pyscript package is laid out. Maybe we need a "cover page" describing the layout of this package, and making users aware that they can access the Pyodide ffi as well https://pyodide.org/en/stable/usage/api/python-api.html. Going to ping @ntoll here who's been heading up a lot of the docs, though he's out of touch until next week.
So this section: https://docs.pyscript.net/2023.11.1.RC3/user-guide/builtins/ contains the description of the current "pyscript" builtin module that will contain such capabilities.
Avatar
Hello, Iโ€™m in a project where Iโ€™ve made some dataframes and plots. I could display all the plots, but Iโ€™m looking for a way to display my dataframe/tabular data and, if itโ€™s possible, to add a download button for the dataframes (Iโ€™m still deciding if to provide it as a zip or put a button for each dataframe). Idk how to program it, I saw that you can use Panel, but it is slowly and, pyscript examples that I visited are not working. I could save the dataframes to a json and then manipulate them with JavaScript, but also, idk how to do it lol and I'm not as good at JavaScript as I am at Python. Any thoughts? You can ping me if u have an answer (edited)
Avatar
RobertHana 09/11/2023 02:21
hi, does anyone know where i can find documentation on calling a pyscript python function from javascript or typescript and handling the result?
Avatar
Avatar
RobertHana
hi, does anyone know where i can find documentation on calling a pyscript python function from javascript or typescript and handling the result?
Jeff Glass 09/11/2023 03:12
Thereโ€™s a couple of ways of handling this, but Iโ€™d recommend:
  • In python, from pyscript import window. Window, in this case, is the JavaScript global namespace in the main window. You can assign objects to it, and they will be accessible in JavaScript.
See here for a recipe: https://pyscript.recipes/latest/js-integration/export-js/
Avatar
Avatar
Dystrom
Hello, Iโ€™m in a project where Iโ€™ve made some dataframes and plots. I could display all the plots, but Iโ€™m looking for a way to display my dataframe/tabular data and, if itโ€™s possible, to add a download button for the dataframes (Iโ€™m still deciding if to provide it as a zip or put a button for each dataframe). Idk how to program it, I saw that you can use Panel, but it is slowly and, pyscript examples that I visited are not working. I could save the dataframes to a json and then manipulate them with JavaScript, but also, idk how to do it lol and I'm not as good at JavaScript as I am at Python. Any thoughts? You can ping me if u have an answer (edited)
bugzpodder 09/11/2023 03:15
you can convert df to json using df.to_json() go from there. you can either inspect the result of runPythonAsync, or do ython -> js call I personally prefer runPythonAsync, but you'll need to hook into one of the polyscript hooks like main.onReady such as this i also built the exact same thing so feel free to ask any questions (edited)
Avatar
good morning - can anyone please explain how you can use multiple py files? I have a Django project and I would like to import functions from file a.py into file b.py. Is there a way I can do that?
Avatar
Avatar
Mikimic
good morning - can anyone please explain how you can use multiple py files? I have a Django project and I would like to import functions from file a.py into file b.py. Is there a way I can do that?
bugzpodder 09/11/2023 18:19
see Files section here
๐Ÿ‘ 1
Avatar
Hi all, has anyone succeeded in using ipywidgets,ipydatawidget in pyscript. From searching the web it looks like there are people who have succeeded, but I couldn't find any instructions I can follow. I am getting stuck importing termios when I try to import either iphwidgets or ipydatagrid.
01:59
Btw thanks for the help
02:00
If there are other widget library that would do the job, I am open to it. I mainly need a fileinput widget and a datagrid widget that can handle multi index dataframes
Avatar
Avatar
mp2kd
Hi all, has anyone succeeded in using ipywidgets,ipydatawidget in pyscript. From searching the web it looks like there are people who have succeeded, but I couldn't find any instructions I can follow. I am getting stuck importing termios when I try to import either iphwidgets or ipydatagrid.
bugzpodder 10/11/2023 03:03
its definitely supported in jupyterlite which uses pyodide under the hood: https://ipywidgets.readthedocs.io/en/latest/_static/lab/index.html that being said jupyterlite does a lot of customizations on top of pyodide so ymmv
Avatar
Avatar
mp2kd
Hi all, has anyone succeeded in using ipywidgets,ipydatawidget in pyscript. From searching the web it looks like there are people who have succeeded, but I couldn't find any instructions I can follow. I am getting stuck importing termios when I try to import either iphwidgets or ipydatagrid.
bugzpodder 10/11/2023 03:14
Avatar
Thanks for the help, let me give that a try tomorrow
Avatar
Avatar
mp2kd
Thanks for the help, let me give that a try tomorrow
bugzpodder 10/11/2023 04:22
just to be clear i dont think it will work in pyscript
Avatar
Oh ic..
Avatar
I used to be able to get the pyscript version with pyscript.__version__ Is there a way to get it now ?
Avatar
Hello when PyScript just came out I toyed around a bit with it, and used to be able to do something like this: from pyodide import create_proxy enable_wallet_btn = document.querySelector('#enable-wallet-btn') enable_wallet_btn.addEventListener('click', create_proxy(enable_nami)) if await nami.isEnabled(): enable_wallet_btn.click() And also have await in the global scope. Now it throws these 2 errors: SyntaxError: 'await' outside function ImportError: cannot import name 'create_proxy' from 'pyodide' (/lib/python311.zip/pyodide/__init__.py) Anyone know a fix for this?
Avatar
I fixed the create_proxy issue, new import is from pyiodide.ffi import create_proxy
Avatar
Has pyscript.write() been deprecated? Error in console: AttributeError: module 'pyscript' has no attribute 'write'
Avatar
Avatar
Zaheer
Hello when PyScript just came out I toyed around a bit with it, and used to be able to do something like this: from pyodide import create_proxy enable_wallet_btn = document.querySelector('#enable-wallet-btn') enable_wallet_btn.addEventListener('click', create_proxy(enable_nami)) if await nami.isEnabled(): enable_wallet_btn.click() And also have await in the global scope. Now it throws these 2 errors: SyntaxError: 'await' outside function ImportError: cannot import name 'create_proxy' from 'pyodide' (/lib/python311.zip/pyodide/__init__.py) Anyone know a fix for this?
Josh (EduBlocks) 10/11/2023 20:11
For the await error, which PyScript version are you now using? If 2023.11.1, then youโ€™ll need to add an async attribute to the script type=โ€œpyโ€ tag
Avatar
Avatar
Josh (EduBlocks)
For the await error, which PyScript version are you now using? If 2023.11.1, then youโ€™ll need to add an async attribute to the script type=โ€œpyโ€ tag
Yes 2023.11.1. Thanks that fixed it
Avatar
Avatar
Zaheer
Has pyscript.write() been deprecated? Error in console: AttributeError: module 'pyscript' has no attribute 'write'
Josh (EduBlocks) 10/11/2023 20:12
Iโ€™m unsure, but I think pyscript.display might do what youโ€™re looking for here
Avatar
Avatar
Josh (EduBlocks)
Iโ€™m unsure, but I think pyscript.display might do what youโ€™re looking for here
WIth display, what would be the equivalent to old style write?: pyscript.write('wait-txs', '<h4>Fetching transactions...</h4>') (edited)
Avatar
Josh (EduBlocks) 10/11/2023 20:40
Ahh maybe not then! My PyScript knowledge doesnโ€™t expand back that far, so I wasnโ€™t entirely sure what write did
Avatar
Avatar
Zaheer
WIth display, what would be the equivalent to old style write?: pyscript.write('wait-txs', '<h4>Fetching transactions...</h4>') (edited)
bugzpodder 10/11/2023 22:34
Avatar
Avatar
Zaheer
WIth display, what would be the equivalent to old style write?: pyscript.write('wait-txs', '<h4>Fetching transactions...</h4>') (edited)
Jeff Glass 10/11/2023 22:54
You can handle this with display() and the HTML helper: <div id="wait-txs"></div> <script type="py"> from pyscript import display, HTML display(HTML("<h4> This is an h4 tag! </h4>"), target="#wait-txs") </script>
22:55
(By default, display tries to escape what you give it so it appears as written. HTML bypasses this, so you can write tags in your string)
Avatar
takacsjanos 11/11/2023 17:49
Is it possible to start pyscript after I have everything else loaded in my webapp? I'd like to display the menus, put together a THREE.js scene, and then display pyscript initialization in a small modal dialog, instead of covering the whole screen. Also, is it possible to save a snapshot of the python environment on the first load, and start it from localstorage on the next loads?
Avatar
Hi everyone. With pyscript, is it possible to import a โ€œ.pyโ€ file into the <py-script> tag of a โ€œ.htmlโ€ file which however is not on the same level as the file?
Avatar
Avatar
Novecento
Hi everyone. With pyscript, is it possible to import a โ€œ.pyโ€ file into the <py-script> tag of a โ€œ.htmlโ€ file which however is not on the same level as the file?
takacsjanos 11/11/2023 18:47
I'm not sure what you mean by level. You can fetch files from different paths as your index.html. And you can put them into folders in the python environment, After that, you can import from that folder. "fetch": [{ "from": "webresources/almanac", "to_folder": "almanacfiles", "files": [ "__init__.py", "Hipparcos.csv", "finals2000A.all", "de421.bsp", "almanac.py" ] }], (edited)
18:48
and in the py-script tag: from almanacfiles.almanac import * (edited)
18:55
I renamed here the folder names to make it a bit more understandable what is what.
Avatar
Avatar
takacsjanos
I renamed here the folder names to make it a bit more understandable what is what.
19:46
thanks, I'll try this tonight!
Avatar
Avatar
takacsjanos
Is it possible to start pyscript after I have everything else loaded in my webapp? I'd like to display the menus, put together a THREE.js scene, and then display pyscript initialization in a small modal dialog, instead of covering the whole screen. Also, is it possible to save a snapshot of the python environment on the first load, and start it from localstorage on the next loads?
bugzpodder 12/11/2023 06:06
I know you can load polyscript after the fact (which is not the same as pyscript). to see an example, this is how i do it in my project. As for loading environment snapshot, it depends on what you want to snapshot. for example, if you know all the code you ran from beginning, you can just rerun the same code (and store this code in localstorage) to get to the same state.
Avatar
Avatar
takacsjanos
Is it possible to start pyscript after I have everything else loaded in my webapp? I'd like to display the menus, put together a THREE.js scene, and then display pyscript initialization in a small modal dialog, instead of covering the whole screen. Also, is it possible to save a snapshot of the python environment on the first load, and start it from localstorage on the next loads?
Jeff Glass 12/11/2023 13:30
You may also just want to not include the script tag that points to PyScript, and then inject it at the end of your JS, or with some callback after your three.Js scene has loaded
Avatar
I'm on pyscript.com and I want to use pydom but can't import it yet. Using <script type="py" src="main.py" config="pyscript.toml"></script> as my main script but import pydom not available. What have I forgotten to do ? when wil lit be enabled ?
๐Ÿ‘€ 1
09:56
I meant to say <script type="module" src="https://pyscript.net/releases/2023.11.1/core.js"></script>
Avatar
Avatar
Neon22
I'm on pyscript.com and I want to use pydom but can't import it yet. Using <script type="py" src="main.py" config="pyscript.toml"></script> as my main script but import pydom not available. What have I forgotten to do ? when wil lit be enabled ?
Jeff Glass 13/11/2023 11:37
I believe itโ€™s from pyweb import pydom
Avatar
Does MicroPython have an equivalent to create_proxy() from pyodide.ffi?
Avatar
Avatar
mcintyre
Does MicroPython have an equivalent to create_proxy() from pyodide.ffi?
Jeff Glass 13/11/2023 13:54
It does, the Micropython API mimics the Pyodide one in this case. You can do from pyodide.ffi import create_proxy in Micropython as well.
๐Ÿ”ฅ 1
Avatar
That is truly awesome
Avatar
hi, I start for first time an project and I try to use the example with "To Do List" , but I get this error : ModuleNotFoundError: No module named 'utils'
19:34
I created a pyscript.json and I try to add this package : { "packages": ["utils"] } , the project comes with this file pyscript.toml , any idea or maybe a good video tutorial to see how to make settings ?
Avatar
Avatar
catafest
hi, I start for first time an project and I try to use the example with "To Do List" , but I get this error : ModuleNotFoundError: No module named 'utils'
Jeff Glass 13/11/2023 19:55
Do you have utils.py loaded via py-config, using either [[fetch]] or [files]? Those are the two keys that allow for fetching resources from URLs and moving them into the virtual filesystem where Python can use them
19:56
For example, the todo-pylist example has this a a py-config: <py-config> [[fetch]] files = ["./utils.py"] </py-config>
19:56
packages is for installing full Python packages from elsewhere - think of it as the equivalent of pip install
19:58
The documentation on [files] is here - honestly, that'd be recommended over [[fetch]] for new installs - it's a much simpler interface https://docs.pyscript.net/2023.11.1/user-guide/configuration/#files
Avatar
I have an online app That I converted from Flask to PyScript and it finally works great under 2023.05.1. I had some problems trying to figure out how to download a PDF file, but that is working now. So, I tried using 2023.11.1, changing nothing but the <SCRIPT... reference, and I get this error message when I click the py-button class button that invokes the code through 'py-click="process_xlsx()" ' -- "Cannot read properties of undefined (reading 'call')." Since there is no module reference or line reference, I assume that it is something in my configuration. Any ideas like, "Oh yeah, everybody knows you've got to..."?
Avatar
Avatar
Dana
I have an online app That I converted from Flask to PyScript and it finally works great under 2023.05.1. I had some problems trying to figure out how to download a PDF file, but that is working now. So, I tried using 2023.11.1, changing nothing but the <SCRIPT... reference, and I get this error message when I click the py-button class button that invokes the code through 'py-click="process_xlsx()" ' -- "Cannot read properties of undefined (reading 'call')." Since there is no module reference or line reference, I assume that it is something in my configuration. Any ideas like, "Oh yeah, everybody knows you've got to..."?
Jeff Glass 13/11/2023 20:02
The syntax for py-*events changed slightly - instead of taking a string of Python code, the value is just the name of a Callable in the global namespace, which gets passed the event object: <!-- 2023.05.1 --> <button py-click="foo()">Click me</button> <py-script> def foo(): print("HI!") </py-script> <!-- 2023.11.1 --> <button py-click="foo">Click me</button> <script type="py"> def foo(evt): print("HI!") </script>
Avatar
Aha! I read your "What's New in PyScript Next (2023.11.1)" but somehow missed the application of that point. (]
๐Ÿ˜Š 1
20:11
That's all it took, @Jeff Glass, thanks!
Avatar
Jeff Glass 13/11/2023 20:14
Excellent!
Avatar
Jeff Glass 13/11/2023 20:58
Your config and your py-script tag shouldn't live inside main.py - they should be in your index.html document.
Avatar
@Jeff Glass I update with some changes , but I got this: File "<exec>", line 8, in <module> File "/home/pyodide/pyscript/_html.py", line 99, in select el = el.content ^^^^^^^^^^ AttributeError: 'NoneType' object has no attribute 'content'
Avatar
Jeff Glass 13/11/2023 21:20
You should take a look at the HTML content of that example again - you'll need to bring in the appropriate HTML input and button. That error message is complaining that it can't find that button and input on your page
Avatar
I understand , I try to create this example : https://pyscript.net/examples/todo.html
Avatar
now I get this after I remove py_script.json and toml file : Traceback (most recent call last): File "/lib/python311.zip/_pyodide/_base.py", line 499, in eval_code .run(globals, locals) ^^^^^^^^^^^^^^^^^^^^ File "/lib/python311.zip/_pyodide/_base.py", line 340, in run coroutine = eval(self.code, globals, locals) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "<exec>", line 34, in <module> NameError: name 'Element' is not defined
Avatar
I find the project on GitHub : https://github.com/pyscript/pyscript/blob/main/examples/todo.html I don't know why the input is not add after I typed and I press the add button ?!
21:51
It is a difference between files = ["utils.py"] and files = ["./utils.py"] ?
Avatar
@Jeff Glass It's working , I need to read more ... and learn , thank you for support !
๐ŸŽ‰ 1
Avatar
Trying to use pydom.ids.foo when I have a <div id="foo"></div> above my script in index.html but not working for me. Domscope defined like this: class DomScope: def __getattr__(self, __name: str) -> Any: element = document[f"#{__name}"] if element: return element[0]` Tried interrogating pydom.ids directly but can't find anything. What am I misunderstanding ?
Avatar
MicroPython question: I hit RuntimeError: index out of bounds for a Python module with over a thousand lines and believe it's this issue: https://github.com/micropython/micropython-esp32/issues/205 I tried print(1) at the top and it didn't work. Can we configure MicroPython to allow more memory for compilation? Does mpy-cross work for PyScript/WebAssembly? Is this the right fork/branch to start from? https://github.com/dpgeorge/micropython/tree/webassembly-add-modjs
I config the stack and heap of mp_task like this: #define MP_TASK_STACK_SIZE (8 * 1024) #define MP_TASK_STACK_LEN (MP_TASK_STACK_SIZE / sizeof(StackType_t)) #define MP_TASK_HEAP_SIZE (60 * 1024) Th...
The MicroPython project. Contribute to dpgeorge/micropython development by creating an account on GitHub.
Avatar
Getting this error while set up the new version, I don't know what I'm doing wrong
Neon22 started a thread. 14/11/2023 03:24
Avatar
Avatar
mcintyre
MicroPython question: I hit RuntimeError: index out of bounds for a Python module with over a thousand lines and believe it's this issue: https://github.com/micropython/micropython-esp32/issues/205 I tried print(1) at the top and it didn't work. Can we configure MicroPython to allow more memory for compilation? Does mpy-cross work for PyScript/WebAssembly? Is this the right fork/branch to start from? https://github.com/dpgeorge/micropython/tree/webassembly-add-modjs
@mcintyre good spot - yes that's thr right branch from which to work. At some point @dpgeorge will fold his work for PyScript into MicroPython main.
Avatar
Avatar
ntoll
@mcintyre good spot - yes that's thr right branch from which to work. At some point @dpgeorge will fold his work for PyScript into MicroPython main.
Cool, no big rush on my end. MicroPython's FFI works perfectly with a small subset of the p5.js API I used to test. Amazing work @dpgeorge! I'll focus on restructuring proceso into an รผber module in the meantime and will document the process.
Avatar
ๅ“Ÿ ่œ้€ผ 16/11/2023 12:07
Hi I m getting this error while plotting a bar chart in my html Pls help
Avatar
Avatar
ๅ“Ÿ ่œ้€ผ
Hi I m getting this error while plotting a bar chart in my html Pls help
Jeff Glass 16/11/2023 12:15
โ€œUnexpected indentโ€ means you have some line thatโ€™s more indented than its neighbors that shouldnโ€™t be. Worth a double-check?
Avatar
Avatar
Jeff Glass
โ€œUnexpected indentโ€ means you have some line thatโ€™s more indented than its neighbors that shouldnโ€™t be. Worth a double-check?
ๅ“Ÿ ่œ้€ผ 16/11/2023 12:20
Oh I see Thx for helping Manage to solve the issue
Avatar
Rust-Ninja-Sabi 18/11/2023 08:27
I wanted to use my own python package. But pyscript do not find it. <py-config> [[fetch]] files = ['wonderworld3d.py'] from = '/' to_folder = '/' </py-config> <py-script> from wonderworld3d import * (edited)
Avatar
Avatar
Rust-Ninja-Sabi
I wanted to use my own python package. But pyscript do not find it. <py-config> [[fetch]] files = ['wonderworld3d.py'] from = '/' to_folder = '/' </py-config> <py-script> from wonderworld3d import * (edited)
is there "/" in sys.path ?
Avatar
Avatar
pmp-p
is there "/" in sys.path ?
Rust-Ninja-Sabi 18/11/2023 09:17
import sys sys.path sys.path.append('/') #thanks now it works๐Ÿ‘
๐ŸŽ‰ 1
Avatar
chris.laffra 18/11/2023 14:13
I am using the new "worker" keyword from PyScript 2023.11.01. I have another script that I load that is "normal". How do I communicate between the two? I am trying to using "window.postMessage" and "window.onMessage", but my main script and the worker do not seem to see each other that way. From both the main script and the worker I am using pyscript.window.onMessage and postMessage.
Avatar
Avatar
chris.laffra
I am using the new "worker" keyword from PyScript 2023.11.01. I have another script that I load that is "normal". How do I communicate between the two? I am trying to using "window.postMessage" and "window.onMessage", but my main script and the worker do not seem to see each other that way. From both the main script and the worker I am using pyscript.window.onMessage and postMessage.
bugzpodder 18/11/2023 19:20
you can try worker.sync. for onMessage/postMessage, I believe you can postMessage from worker to main thread but not vice versa. something like this (edited)
๐Ÿ‘ 1
Avatar
Hi all. I'm playing with PyScript - trying to build some fun things with my son. PyScript looks really promising. We'd like to use radio buttons. I've built a small test example - see code at https://pastebin.com/h5MXvaQZ
19:55
19:55
This works, but I wonder if it could be improved.
19:55
One particular thing that bothers me a little: here, each radio button calls a Python function when it is clicked (via mpy_change). But this means that the default value is encoded both in the HTML (as the radio button with the checked attribute), and in the Python (as the default value of favourite_song_id). I usually try to avoid repetition like this (https://en.wikipedia.org/wiki/Don't_repeat_yourself).
19:55
I tried instead to write code in announce_favourite (called when the button is pressed) that reads the current state of the radio buttons, but couldn't quickly work out how to do this. I see various approaches at https://stackoverflow.com/questions/9618504/how-to-get-the-selected-radio-button-s-value, but I don't know how best to do this in PyScript. Ideally, I'd prefer something Pythonic, because that's the language my son is studying.
Avatar
Avatar
39AndABit
I tried instead to write code in announce_favourite (called when the button is pressed) that reads the current state of the radio buttons, but couldn't quickly work out how to do this. I see various approaches at https://stackoverflow.com/questions/9618504/how-to-get-the-selected-radio-button-s-value, but I don't know how best to do this in PyScript. Ideally, I'd prefer something Pythonic, because that's the language my son is studying.
Jeff Glass 20/11/2023 16:09
One thing you might do is look up which of the radio buttons is checked at the moment the button is clicked: def announce_favourite(event): #Find the first radio button with type "radio" that is checked: selected = document.querySelector('input[type="radio"]:checked') favourite_song_id = selected.id announce_text = f"Oh yes I really like {favourite_song_id} \u2764\uFE0F\n" outputElement.innerText += announce_text
16:10
With that, you can do away with the song_radio_change function entirely
Avatar
anthony.pandiani 20/11/2023 16:10
getting an error trying to get the time with micropython. what is wrong with this code: from pyscript import display import time display("The current time is:") t = time.localtime() display(t) error-text is: Traceback (most recent call last): File "<stdin>", line 32, in <module> AttributeError: 'module' object has no attribute 'localtime' thank you for your help!
Avatar
Avatar
anthony.pandiani
getting an error trying to get the time with micropython. what is wrong with this code: from pyscript import display import time display("The current time is:") t = time.localtime() display(t) error-text is: Traceback (most recent call last): File "<stdin>", line 32, in <module> AttributeError: 'module' object has no attribute 'localtime' thank you for your help!
Jeff Glass 20/11/2023 16:18
It's not currently enabled for the webassembly build of micropython; none of the time_ns-based methods currenly are.
16:18
The currently-available methods are: ['sleep', 'sleep_ms', 'sleep_us', 'ticks_add', 'ticks_cpu', 'ticks_diff', 'ticks_ms', 'ticks_us'] (edited)
Avatar
Jeff Glass 20/11/2023 16:27
That's not an impossible add, but not currently implemented
Avatar
Avatar
Jeff Glass
That's not an impossible add, but not currently implemented
anthony.pandiani 20/11/2023 17:27
that's fine, thanks a lot for the response!
Avatar
๐Ÿ‘‹ So we want to start working with Damien to fill in such gaps in the API. Hopefully we'll have good news soon.
Avatar
chris.laffra 21/11/2023 11:40
@anthony.pandiani @ntoll I just ran into the same issue. I monkey-path the time module to handle time.time() missing in MicroPython. Otherwise, it breaks logging.info(...), for instance.
Avatar
@chris.laffra ๐Ÿ‘‹ yeah. This is on an ever increasing list of things with which we need to engage with Damien to ensure it's covered.
Avatar
chris.laffra 21/11/2023 12:30
@39AndABit This is how I handle radiobuttons (and other input widgets) in LTK, See: https://laffra.github.io/ltk/?tab=1
Avatar
Fireflexor 21/11/2023 19:09
hey, i'm not really too familiar with this stuff, so 2 questions firstly, are there any limitations to what python libraries i can import? secondly, where can i get information on how this works, mainly the python part, rather than the HTML part
Avatar
Avatar
Fireflexor
hey, i'm not really too familiar with this stuff, so 2 questions firstly, are there any limitations to what python libraries i can import? secondly, where can i get information on how this works, mainly the python part, rather than the HTML part
Jeff Glass 21/11/2023 19:31
Both really good questions: For the python libraries, if you're using Pyodide/CPython, you can basically load:
  • Any pure-python package on PyPI, or that you build yourself
These can be loaded using the <py-config> packages key: https://docs.pyscript.net/2023.11.1/user-guide/configuration/#packages Almost all of the standard library is usable, with some notable exceptions due to the restrictions that the browser has around networking (i.e. you can't open a raw socket) and UI access (i.e. turtle/tkinter try to grab low-level windowing optoins which aren't available in the browser.) For more on the standard lib, see here: https://pyodide.org/en/stable/usage/wasm-constraints.html
19:32
For the second question... I'm not sure I understand what you mean ๐Ÿ˜‡
Avatar
Avatar
Jeff Glass
Both really good questions: For the python libraries, if you're using Pyodide/CPython, you can basically load:
  • Any pure-python package on PyPI, or that you build yourself
These can be loaded using the <py-config> packages key: https://docs.pyscript.net/2023.11.1/user-guide/configuration/#packages Almost all of the standard library is usable, with some notable exceptions due to the restrictions that the browser has around networking (i.e. you can't open a raw socket) and UI access (i.e. turtle/tkinter try to grab low-level windowing optoins which aren't available in the browser.) For more on the standard lib, see here: https://pyodide.org/en/stable/usage/wasm-constraints.html
Fireflexor 21/11/2023 19:32
how can i tell if a library is pure-python
Avatar
Avatar
Jeff Glass
For the second question... I'm not sure I understand what you mean ๐Ÿ˜‡
Fireflexor 21/11/2023 19:34
and for that, mean like in the example thing on the website where you translate stuff to pirate talk it says to use the line input_text = document.querySelector("#english") my question is is there a place i can see all of the stuff like querySelector, and what they're used for and all that
Avatar
Avatar
Fireflexor
how can i tell if a library is pure-python
Fireflexor 21/11/2023 19:36
ok well for this i checked the library's github and it says it's 100% python. not sure if you mean fully made in python by pure-python or it's something different tho
Avatar
Jeff Glass 21/11/2023 19:44
So being 100% implemented in Python is a good start - in this case, a 'Pure Python Package' also means that the package publishes a 'pure-Python' wheel with the suffix -none-any.whl on PyPI. The none means no particular reliance on a C ABI, and the any means any platform. You can check the published files on PyPI to see if there's a wheel like that that. (edited)
19:45
If you try to load a package using packages and you get an error about "couldn't find a pure-Python wheel", it means a wheel file like that couldn't be found on PyPI
Avatar
Avatar
Fireflexor
and for that, mean like in the example thing on the website where you translate stuff to pirate talk it says to use the line input_text = document.querySelector("#english") my question is is there a place i can see all of the stuff like querySelector, and what they're used for and all that
Jeff Glass 21/11/2023 19:51
Ahhhh you've hit on something really cool with your question about document.querySelector. Earlier in that example, you'll see from pyscript import document. That 'document' is a transparent proxy for the Browser's document object https://developer.mozilla.org/en-US/docs/Web/API/Document. PyScript (and under the hood, Pyodide and Micropython+WASM) allow you to import objects directly from JavaScript and use them like normal Python objects
19:52
So you get access to all of the existing Web API's for free https://developer.mozilla.org/en-US/docs/Web/API.
19:58
Some additional info on PyScript's proxying here https://docs.pyscript.net/2023.11.1/user-guide/builtins/, And on Pyodide's proxying in depth here https://pyodide.org/en/stable/usage/type-conversions.html
Avatar
Avatar
Jeff Glass
So being 100% implemented in Python is a good start - in this case, a 'Pure Python Package' also means that the package publishes a 'pure-Python' wheel with the suffix -none-any.whl on PyPI. The none means no particular reliance on a C ABI, and the any means any platform. You can check the published files on PyPI to see if there's a wheel like that that. (edited)
Fireflexor 21/11/2023 20:05
yup there's a wheel like that, so i guess it is one, thankfully
Avatar
Avatar
Jeff Glass
Ahhhh you've hit on something really cool with your question about document.querySelector. Earlier in that example, you'll see from pyscript import document. That 'document' is a transparent proxy for the Browser's document object https://developer.mozilla.org/en-US/docs/Web/API/Document. PyScript (and under the hood, Pyodide and Micropython+WASM) allow you to import objects directly from JavaScript and use them like normal Python objects
Fireflexor 21/11/2023 20:06
also i'll be perfectly honest i don't really understand this but i'll read through it a bit more, at least i know where to look now
20:08
one question however, later on in the program it says output_div.innerText when i search for innerText on the web docs page you sent it seems to be from something called HTMLElement, but i never imported this at the start, only document (edited)
20:09
so how do i know what i need to import?
Avatar
chris.laffra 21/11/2023 21:23
Once you call something like document.x you enter JavaScript land. From that moment on the APIs are not Python, but JS DOM apis. PyScript offer a thin layer on top, but anythin g you want to do to the DOM goes through JavaScript APIs, such as "getElementById", etc. You can also third-party libraries that abstract some of that away, such as jQuery. Finally, there are efforts by people to make the whole API more Pythonic, such as the "pydom" project: https://fpliger.pyscriptapps.com/pyweb/latest/pydom.html
21:24
If you want to see another project that tries to hide some of the web API in a more Pythonic facade, check out LTK: https://laffra.github.io/ltk/
21:27
In this example, the Python code creates a UI by constructing DOM elements, sets styling using CSS, and registers for events that are handled in Python again.
21:29
All the styling done in that example is optional and is better done in an external stylesheet. In the example, we demonstrate the APIs though. The TicTacToe example does that, by injecting a style sheet and have no CSS in the code (other than creating feedback during mousemove events, which also can be done using classes of course).
Avatar
chris.laffra 21/11/2023 21:39
In fact, I changed the tictactoe example just now to only use CSS classes.
Avatar
Last version of pyscript is awesome! Thank you! I have questions too now. In browser it works great. But i wanna try use it in plugins for Obsidian (note taking app) which is bundled in electron and also utilises wasm. Have anyone tried pyscript in such way?
Avatar
Avatar
Jeff Glass
One thing you might do is look up which of the radio buttons is checked at the moment the button is clicked: def announce_favourite(event): #Find the first radio button with type "radio" that is checked: selected = document.querySelector('input[type="radio"]:checked') favourite_song_id = selected.id announce_text = f"Oh yes I really like {favourite_song_id} \u2764\uFE0F\n" outputElement.innerText += announce_text
Thank you!
Avatar
Avatar
chris.laffra
@39AndABit This is how I handle radiobuttons (and other input widgets) in LTK, See: https://laffra.github.io/ltk/?tab=1
Thank you ๐Ÿ™‚ We have something working now, but this looks interesting
Avatar
Avatar
Ridensium
Last version of pyscript is awesome! Thank you! I have questions too now. In browser it works great. But i wanna try use it in plugins for Obsidian (note taking app) which is bundled in electron and also utilises wasm. Have anyone tried pyscript in such way?
bugzpodder 22/11/2023 19:53
for plugins use extism
Avatar
Avatar
bugzpodder
for plugins use extism
Oh thats beyond my abilities i guess. I am just an writer and make software to fulfil personal and some other writers needs. Thanks anyway. (edited)
Avatar
Fireflexor 23/11/2023 13:01
Hey, is there a way to get the element that called a function? For example if I have an <input> with py-clickโ€subprogramNameโ€ is there an easy way of getting the <input> element in the subprogram it calls without itโ€™s ID?
Avatar
Avatar
Fireflexor
Hey, is there a way to get the element that called a function? For example if I have an <input> with py-clickโ€subprogramNameโ€ is there an easy way of getting the <input> element in the subprogram it calls without itโ€™s ID?
Jeff Glass 23/11/2023 13:52
The handler function for py-*events are automatically passed the Event Object (https://developer.mozilla.org/en-US/docs/Web/API/Event) for that event. event.target is the element that originally dispatched that event. Try this: <button py-click="foo">A Button</button> <button py-click="foo">Another Button</button> <script type="py"> def foo(evt): print(f"The target's text is {evt.target.innerText}") </script>
Avatar
Still trying to workout the new pydom. Any help please: I need this to get a handle to add a new element: from pyweb import pydom from pyscript import display, document, window, HTML, when docbody = pydom["body"][0] # Element ('body' can be any selector) I can create a new element: baz = pydom.create("div", classes=["baz"]) docbody.append(baz) I have to add an id inorder to use display or to assign using baz.content= ..
  • but no id=.. option in create.
so: baz.id = 'baz' I want to add a pyclick="mycallback" but no options at create to do so. I expect I have to assign the innerHTML directly or craft something using HTML(..) What's an actual working (pyscript code) example where I can create a div or a button with a py-click event on it and maybe a css style please...
(edited)
Avatar
I'm trying to use web workers with PyScript 2023.11.1 on pyscript.com, but I'm running into an error. I thought I'd check to see if I'm missing something obvious. My test project: https://pyscript.com/@ryaowe/wispy-bonus/latest I copied and pasted the PyWorker sync example from the docs. It doesn't raise an exception, but "s is undefined" is logged to the terminal with Firefox, and "Cannot read properties of undefined (reading 'module')" in Chrome. The worker code never seems to run. I've also tried using the polyscript.XWorker example, but I get similar results. I'm able to get a worker to run if I use a py-script tag in index.html with the "worker" attribute, but I'm not sure how to communicate with the worker from the main thread in that case. I saw a post earlier about using onMessage and postMessage, but that only seems to work from worker to main and not vice versa. What I'm trying to accomplish is to send keydown events from the main thread to a worker thread. Any advice on how to do something like that?
Avatar
I can't work it out either. I've seen some usecases like this from xworker import XWorker sync = XWorker("worker.py", config="pyscript.toml", type="micropython") But pretty sure you don't need what's in your .toml file.. But as I say - can't work it out either.
19:35
In the worker.py you might need from xworker import xworker document = xworker.window.document (edited)
Avatar
Thanks for looking at it! I managed to get this to work: from polyscript import XWorker sync = XWorker("worker.py", config="pyscript.toml", type="pyodide").sync def hello(arg): print(arg) sync.hello = hello and from polyscript import xworker xworker.sync.hello(xworker.window.document.title) (edited)
20:25
which is pretty similar to the polyscript xworker example I tried initially... maybe adding the config= and type= parameters did the trick?
Avatar
cool. glad it worked. well done. its so hard to know if should be importing xworker from pyworker, polyscript, xworker and Xworker or xworker.
Avatar
christy heaton 26/11/2023 19:10
Hello! I've just switched over to the latest PyScript version (2023.11.1) and I no longer see the startup log messages that I was seeing in the previous version. I know I can see some logs in the developer tools console but, is a plain white screen to be expected while loading in this new version?
Avatar
Avatar
christy heaton
Hello! I've just switched over to the latest PyScript version (2023.11.1) and I no longer see the startup log messages that I was seeing in the previous version. I know I can see some logs in the developer tools console but, is a plain white screen to be expected while loading in this new version?
Josh (EduBlocks) 26/11/2023 19:16
hi @christy heaton! That's correct, there's no splashscreen in the latest PyScript version. I'm not sure if it's on the roadmap to bring back into the rewritten PyScript (others will have a better idea than I do), but it's definitely possible to create your own "loading" screen by using the hooks if you want something similar (rather than a blank page) (edited)
๐Ÿ‘ 1
Avatar
PyScript probably won't have a splash screen, but (as you know Josh), we have the mechanism built in for folks to implement their own, and we should probably document how to do this (care to share this?), so it's left to the person creating things with PyScript to have the choice.
Avatar
Hi, i have a question on pyscript.window : Why can pyscript access "var" but not "let" and "const" Thanks you
Avatar
Avatar
ntoll
PyScript probably won't have a splash screen, but (as you know Josh), we have the mechanism built in for folks to implement their own, and we should probably document how to do this (care to share this?), so it's left to the person creating things with PyScript to have the choice.
Josh (EduBlocks) 27/11/2023 12:50
of course! this is how i'm doing it in EduBlocks, hopefully this helps @christy heaton (and others who want to achieve something similar to the old splashscreen) ๐Ÿ™‚ https://pyscript.com/@joshualowe1002/pyscript-next-splashscreen/v1
๐Ÿคฉ 1
Avatar
Avatar
Josh (EduBlocks)
of course! this is how i'm doing it in EduBlocks, hopefully this helps @christy heaton (and others who want to achieve something similar to the old splashscreen) ๐Ÿ™‚ https://pyscript.com/@joshualowe1002/pyscript-next-splashscreen/v1
Andrea Giammarchi 27/11/2023 14:14
may I suggest position fixed instead? ๐Ÿ˜‰ also remember that py:ready is an event globally dispatched (no need to hook into ... hooks :P) and that the <dialog> is probably the best element to use, as it doesn't require CSS hackery around it ... overall, it's a great demo on why we don't have the splash screen by default: with workers, the main thread is never blocked, there's no need to pause the world until py-script runs + with MicroPython instead of pyodide, the delay is irrelevant, and flushing a huge splash screen makes little sense, it'd be actually just disturbing. I hope this explain the current reason we don't py-splash anymore.
๐Ÿ‘ 1
Avatar
I think the important thing is we give folks choice. ๐Ÿ™‚
Jeff Glass started a thread. 27/11/2023 14:32
Avatar
Andrea Giammarchi 27/11/2023 14:57
@Josh (EduBlocks) here my splash screen simplified version with dialog: https://pyscript.com/@agiammarchi/pyscript-next-splashscreen-copy/latest
๐ŸŽ‰ 4
Avatar
Avatar
Andrea Giammarchi
@Josh (EduBlocks) here my splash screen simplified version with dialog: https://pyscript.com/@agiammarchi/pyscript-next-splashscreen-copy/latest
Josh (EduBlocks) 27/11/2023 19:35
Well, you're called Web Yoda for a reason ๐Ÿ˜‰ Much better... and I learnt something (py:ready via event listeners ๐Ÿ™‚) (edited)
Avatar
Avatar
Neon22
Still trying to workout the new pydom. Any help please: I need this to get a handle to add a new element: from pyweb import pydom from pyscript import display, document, window, HTML, when docbody = pydom["body"][0] # Element ('body' can be any selector) I can create a new element: baz = pydom.create("div", classes=["baz"]) docbody.append(baz) I have to add an id inorder to use display or to assign using baz.content= ..
  • but no id=.. option in create.
so: baz.id = 'baz' I want to add a pyclick="mycallback" but no options at create to do so. I expect I have to assign the innerHTML directly or craft something using HTML(..) What's an actual working (pyscript code) example where I can create a div or a button with a py-click event on it and maybe a css style please...
(edited)
Phelsong (Josh) 28/11/2023 15:39
I have some stuff that would help, but I'm away till the afternoon, poke me tonight if I don't get back to you
15:41
https://github.com/Phelsong/sigil-script/tree/main is the start of what I have in my actual project. Maybe helpful
Contribute to Phelsong/sigil-script development by creating an account on GitHub.
Avatar
Avatar
Phelsong (Josh)
https://github.com/Phelsong/sigil-script/tree/main is the start of what I have in my actual project. Maybe helpful
Thanks - looking into it.
Avatar
Avatar
Phelsong (Josh)
https://github.com/Phelsong/sigil-script/tree/main is the start of what I have in my actual project. Maybe helpful
@Phelsong (Josh) you may be interested in @ntoll svg.py here: - https://pyscript.com/@ntoll/turtle-workers/latest
Avatar
Avatar
Neon22
Thanks - looking into it.
Phelsong (Josh) 28/11/2023 21:25
I updated the repo with what I could quickly sanitize.
21:28
Essentially what ive found works for me is having a main/app div in the origin HTML doc, doing... main: Div = Div(pydom["body"][0], id="main") in the top of my app file, then everything plays nice after that (edited)
Avatar
Yes - me too. but having trouble attaching py-events to elements as not same api as before and wanting to use pydom...
22:38
I should probably just go back to using the js module and wiring it all all up using Javascript events but was hoping this would work (also doing a lot of events in svg in my UIs)
22:38
I.e. pydom to create the UI elements and attach events
Avatar
chris.laffra 29/11/2023 10:53
@Phelsong (Josh) You can also use LTK to render SVG, style things, and handle events: https://laffra.github.io/ltk/?runtime=mpy&tab=9 (edited)
Avatar
Phelsong (Josh) 30/11/2023 03:27
I'll take a look at both, thx
Avatar
I have a py file whose output is tkinter, can anyone help me how can I use this in pyscript in the website
Avatar
Avatar
zain
I have a py file whose output is tkinter, can anyone help me how can I use this in pyscript in the website
consider chris's ltk which is similar in many ways to ctk or tkinter.
Avatar
Fireflexor 30/11/2023 17:05
what editor is reccomended for doing HTML and CSS and all that?
17:06
since with notepad++ the pyscript stuff isn't being highlighted properly, which is annoying me
Avatar
Josh (EduBlocks) 30/11/2023 17:25
There's this, which might do what you're looking for, in VSCode. https://marketplace.visualstudio.com/items?itemName=HardeepSingh.pyscript
Extension for Visual Studio Code - Code Highlighter for PyScript
17:26
Alternatively, using a <script type="py" src="main.py"> tag and separating your python code from your HTML should give you the experience you're looking for with proper syntax highlighting
Avatar
Avatar
Fireflexor
since with notepad++ the pyscript stuff isn't being highlighted properly, which is annoying me
Jeff Glass 30/11/2023 17:27
If youโ€™re writing Python directly inside a <script> tag, I donโ€™t know of an editor/IDE that fully handles it. (Maybe PyCharm?). But Iโ€™d recommend using the src attribute and put your source in a separate Python file where it will be properly highlighted
17:27
Oops, @Josh (EduBlocks) beat me to it!
๐Ÿ˜‚ 1
Avatar
Avatar
Jeff Glass
If youโ€™re writing Python directly inside a <script> tag, I donโ€™t know of an editor/IDE that fully handles it. (Maybe PyCharm?). But Iโ€™d recommend using the src attribute and put your source in a separate Python file where it will be properly highlighted
Fireflexor 30/11/2023 17:28
yeah i am, i more meant stuff like py-click='name' not being highlighted right
17:30
i am using the SRC attribute and having my python code in a seperate file i mean by that
Avatar
Avatar
Josh (EduBlocks)
There's this, which might do what you're looking for, in VSCode. https://marketplace.visualstudio.com/items?itemName=HardeepSingh.pyscript
Andrea Giammarchi 30/11/2023 17:37
this is both awesome and somehow misleading as the preferred way to deal with Python code on any HTML page is <script type="py"> or <script type="mpy"> tag ... not <py-script> as that's full of hidden issues ... even if it kinda works as it used to ... I will try to check that entry source code and help improving it but I wanted to say: please use <script type="py"> in the present/future instead and be sure no surprises ever happen.
17:39
about Jeff's suggestion, the src works well and has no issues in general with either approaches, but that also doesn't need any special VS Code/ium plugin ๐Ÿ‘
Avatar
Avatar
Fireflexor
yeah i am, i more meant stuff like py-click='name' not being highlighted right
Jeff Glass 30/11/2023 18:02
Interesting that things like py-click arenโ€™t being highlightedโ€ฆ can you post a screenshot of what you see?
Avatar
Avatar
Jeff Glass
Interesting that things like py-click arenโ€™t being highlightedโ€ฆ can you post a screenshot of what you see?
Fireflexor 30/11/2023 18:07
stuff like type and id are being highlighted
18:07
py-click just isn't
18:08
also ignore the names, it's all for testing stuff
18:08
in actual programs i don't have such terrible names
๐Ÿคฃ 1
Avatar
Avatar
Josh (EduBlocks)
of course! this is how i'm doing it in EduBlocks, hopefully this helps @christy heaton (and others who want to achieve something similar to the old splashscreen) ๐Ÿ™‚ https://pyscript.com/@joshualowe1002/pyscript-next-splashscreen/v1
christy heaton 02/12/2023 22:03
Ah this is great! Thank you so much!
Avatar
Fireflexor 04/12/2023 19:57
hey, what would be the best way to call a function on keypress?
19:58
i want to make it so that users can either click enter when typing in a textbox, or can click a button next to it
Avatar
Avatar
Fireflexor
i want to make it so that users can either click enter when typing in a textbox, or can click a button next to it
Jeff Glass 04/12/2023 20:44
<textarea id="area" py-keydown="keyhandle"></textarea> <script type="py"> def keyhandle(evt): if evt.key == "Enter": print("Hey it's the enter key!") </script>
20:45
or: <textarea id="area"></textarea> <script type="py"> from pyscript import when @when('keydown', '#area') def keyhandle(evt): if evt.key == "Enter": print("Hey it's the enter key!") </script>
Avatar
Avatar
Jeff Glass
<textarea id="area" py-keydown="keyhandle"></textarea> <script type="py"> def keyhandle(evt): if evt.key == "Enter": print("Hey it's the enter key!") </script>
Fireflexor 04/12/2023 20:46
does the textarea work with an <input type="text"> instead? (edited)
Avatar
Jeff Glass 04/12/2023 20:50
Sure, same idea. Here's a slightly more involved example, which prints the contents of the <input> when you hit enter, then clears that input box: <input type="text" id="area"> <script type="py"> from pyscript import when @when('keydown', '#area') def keyhandle(evt): if evt.key == "Enter": print(f'You hit enter with "{evt.target.value}"" in the text input') evt.target.value = "" </script>
20:50
(Would also work with <input type="text" py-keydown="keyhandle">, like in the first example)
Avatar
Fireflexor 04/12/2023 21:44
great, much appreciated
Avatar
@ntoll your old project https://pyscript.com/@ntoll/turtle-workers/latest does not work anymore. Any ideas ? I'm cribbing svg.py successfully but would like to see how his works in a worker
Avatar
Does pyscript include pyodide?
Avatar
Avatar
Simanelix
Does pyscript include pyodide?
Jeff Glass 06/12/2023 16:27
Yes - if you use a <script type="py"> (or the old style <py-script> tag), you're running code with Pyodide.
16:28
You can also use a <script type="mpy"> tag to run code with Micropython-compiled-to-webassembly
16:28
All of the Pyodide FFI is available in a type="py" tag, and a limited subset of it is available in Micropython (very much a work-in-progress there)
Avatar
Hi, I've recently started using PyScript and encountered an issue. I'm trying to install some libraries, so I created a JSON file to include them in my main Python program. However, I'm getting an error: 'Can't find a pure Python 3 Wheel for package(s).' Is there a way to resolve this? Thank you.
Avatar
Avatar
Jorlord45
Hi, I've recently started using PyScript and encountered an issue. I'm trying to install some libraries, so I created a JSON file to include them in my main Python program. However, I'm getting an error: 'Can't find a pure Python 3 Wheel for package(s).' Is there a way to resolve this? Thank you.
Jeff Glass 06/12/2023 22:27
A 'pure python Wheel' is a wheel file that has built explicitly to be compatible with an architechture and Python ABI. You can check the files on PyPI for your desired package - a Pure Python wheel is one that ends in -none-any.whl. If one doesn't exist, you can try: (edited)
Avatar
iยดm trying to install pypdf, openai, langchain, and llama-index
Avatar
Avatar
Jeff Glass
A 'pure python Wheel' is a wheel file that has built explicitly to be compatible with an architechture and Python ABI. You can check the files on PyPI for your desired package - a Pure Python wheel is one that ends in -none-any.whl. If one doesn't exist, you can try: (edited)
Oh, iยดll check, thanks
Avatar
This is a fantastic project. PyScript 2023.11.2 and micropython don't support "when." How would one go about assigning the a python function to the window.onload event?
Avatar
Avatar
Lucas
This is a fantastic project. PyScript 2023.11.2 and micropython don't support "when." How would one go about assigning the a python function to the window.onload event?
Andrea Giammarchi 07/12/2023 08:54
form pyscript import window window.addEventListener('load', print)
08:54
although it's very likely that when MicroPython loads the onload event already happened, unless you have really heavy images down the page.
Avatar
@Lucas PyScript is definitely a work in progress... I hope you can see our direction of travel, and we have lots of work, refinement and fixing to do before we get close to where we want to be. But the important thing is, thanks to all the work done so far, we have a solid foundation from which to build. ALL FEEDBACK IS WELCOME, don't hesitate to ask questions, and if something doesn't look right, please mention it. It all goes into the melting pot, and we're thankful for such contributions.
Avatar
Andrea Giammarchi 07/12/2023 11:14
if anyone is interested, I have written down a detailed "step by step" documentation on how to run PyScript offline https://github.com/pyscript/pyscript/issues/581#issuecomment-1845137741 (edited)
๐ŸŽ‰ 1
Avatar
Avatar
ntoll
@Lucas PyScript is definitely a work in progress... I hope you can see our direction of travel, and we have lots of work, refinement and fixing to do before we get close to where we want to be. But the important thing is, thanks to all the work done so far, we have a solid foundation from which to build. ALL FEEDBACK IS WELCOME, don't hesitate to ask questions, and if something doesn't look right, please mention it. It all goes into the melting pot, and we're thankful for such contributions.
Thanks. I've been following it from a distance for a bit; the November release lured me in.
Avatar
Avatar
Andrea Giammarchi
form pyscript import window window.addEventListener('load', print)
Andrea, thanks for the info. Is there an elegant way to execute a function after the <mpy> tags have been loaded/handled? Jeff Glass has an example at https://pyscript.recipes/2023.11.1/basic/when-finished/ using pyodide libraries, but it doesn't look like that will work with Micropython. (edited)
Avatar
Avatar
Lucas
Andrea, thanks for the info. Is there an elegant way to execute a function after the <mpy> tags have been loaded/handled? Jeff Glass has an example at https://pyscript.recipes/2023.11.1/basic/when-finished/ using pyodide libraries, but it doesn't look like that will work with Micropython. (edited)
Jeff Glass 07/12/2023 15:35
The mpy equivalent would be: <script type="mpy"> from pyscript import display, window def callback(event): display("This will run when all mpy script tags are complete") display(f"{event.type=}") window.addEventListener("py:all-done", callback) </script>
15:35
To be honest, I thought it would be an mpy:all-done event, but I guess it's signalling completion of PYscript's tags, not the tags of a particular interpreter type?
15:36
I'll add it to the recipes for the upcoming release
Avatar
Josh (EduBlocks) 07/12/2023 21:25
Does anyone know of a text to speech library that works with PyScript?
Avatar
Avatar
Jeff Glass
The mpy equivalent would be: <script type="mpy"> from pyscript import display, window def callback(event): display("This will run when all mpy script tags are complete") display(f"{event.type=}") window.addEventListener("py:all-done", callback) </script>
Jeff, that's outstanding. Thank you very much!
๐Ÿค— 1
Avatar
Avatar
Josh (EduBlocks)
Does anyone know of a text to speech library that works with PyScript?
i would try vosk/espeak wasm and some js plumbing (edited)
Avatar
Avatar
Josh (EduBlocks)
Does anyone know of a text to speech library that works with PyScript?
Tinkering with the browser speech APIs... using only PyScript and JavaScript, no additional packages: https://sadukie.pyscriptapps.com/gentle-sound/latest/ (That was its generic name - I didn't name it!)
  • The SpeechSynthesisUtterance needs to be created with new in JavaScript, and I haven't dug into the code enough to see if this is possible with PyScript yet. So I left it to JavaScript to create the object.
  • This was a total guess of whether it would work. I was thrilled to see my theory work. I hope Jeff or Fabio or one of the others can chime in though. (I'm coding way later than I should, so it isn't pretty.)
(edited)
Avatar
Avatar
sadukie
Tinkering with the browser speech APIs... using only PyScript and JavaScript, no additional packages: https://sadukie.pyscriptapps.com/gentle-sound/latest/ (That was its generic name - I didn't name it!)
  • The SpeechSynthesisUtterance needs to be created with new in JavaScript, and I haven't dug into the code enough to see if this is possible with PyScript yet. So I left it to JavaScript to create the object.
  • This was a total guess of whether it would work. I was thrilled to see my theory work. I hope Jeff or Fabio or one of the others can chime in though. (I'm coding way later than I should, so it isn't pretty.)
(edited)
Jeff Glass 08/12/2023 03:41
(I too am up too lateโ€ฆ but Iโ€™m excited to try this out in the morning ๐Ÿคฉ)
Avatar
And now I'm trying it with emoji... yep... calling it a night here! ๐Ÿ˜„
Avatar
Avatar
sadukie
Tinkering with the browser speech APIs... using only PyScript and JavaScript, no additional packages: https://sadukie.pyscriptapps.com/gentle-sound/latest/ (That was its generic name - I didn't name it!)
  • The SpeechSynthesisUtterance needs to be created with new in JavaScript, and I haven't dug into the code enough to see if this is possible with PyScript yet. So I left it to JavaScript to create the object.
  • This was a total guess of whether it would work. I was thrilled to see my theory work. I hope Jeff or Fabio or one of the others can chime in though. (I'm coding way later than I should, so it isn't pretty.)
(edited)
Josh (EduBlocks) 08/12/2023 13:43
Wow, that is awesome. Thank you @sadukie!!
13:43
From some looking around, it seems like you can add .new() to the end of a JS class name
Avatar
Avatar
Josh (EduBlocks)
From some looking around, it seems like you can add .new() to the end of a JS class name
Jeff Glass 08/12/2023 13:44
๐Ÿ™๐Ÿป 1
Avatar
Avatar
Josh (EduBlocks)
From some looking around, it seems like you can add .new() to the end of a JS class name
See the end of this section in the docs: https://pyscript.github.io/docs/2023.12.1/user-guide/dom/#ffi (edited)
๐Ÿ™๐Ÿป 1
Avatar
Got it - this time without calling to js : https://pyscript.com/@sadukie/gentle-sound-no-js/latest
Avatar
So, in Chrome the voices sound great. In Firefox they sound like a Dalek with a cold. (Both on Linux). Great demo!!!!
14:23
That's more a browser TTS engine thing than anything... but good to know (use Chrome for demos). ๐Ÿ˜‰
Avatar
You can use Edge if you're doing Windows demos as well. Lots of Microsoft Online voices to choose from in addition to the default voices.
Avatar
Yeah it'll be an OS / browser thing.
Avatar
Josh (EduBlocks) 08/12/2023 21:01
Back with more questions ๐Ÿ™‚ Is it possible to add an importmap to a worker in PyScript, as this doesn't work when added to <head> (as it's adding it to the main thread) <script type="importmap"> { "imports": { "perf_hooks": "./perf_hooks.js" } } </script>
Avatar
if I use foo = parent.create('div') and I add a button with py-click by setting the foo.html to say: '<button py-click="btnrndm" class="rndbtn" btntype="foo1" >R</button>' Then btnrndm function is called with evt. Working fine. But the evt.target is not an element. Its an [object HTMLButtonElement] (which is presented as a list when printed but its not a list but that's another issue.) How do I add further elements under this object HTMLButtonElement using pydom. Or do I move wholesale over to js ? all the methods dir() seem to be javascript. Is there a pydom way to add the attributes like py-click="btnrndm" and btntype="foo1" to an exisiting element? (edited)
Avatar
Hi I was just wondering how you would make HTTP requests with pyscript. I'm currently using requests and everytime I run it it says [Errno 50] Protocol not available.
12:14
I've been trying to convert a Python app that I made into a webapp so that it can be easily used by others. Part of the app involves users entering their username and a request being sent to an api...
12:16
I've written up my code on stackoverflow so as to not flood this channel with code. Any help is appreciated. Thanks!
Jeff Glass started a thread. 09/12/2023 14:45
sadukie started a thread. 09/12/2023 22:32
Avatar
I can't use [[fetch]] at all, it seems. I have 3 files:
  • scripts/main.py:
import my_module my_module.my_f()
  • scripts/my_module.py:
def my_f(): print("Code works?")
  • index.html
<html> <head> <!-- this part just works; I simply have PyScript itself stored on my computer locally --> <link rel="stylesheet" href="pyscript/pyscript.css"/> <script defer src="pyscript/pyscript.js"></script> </head> <body> <!-- this is the part that does not work --> <py-config> [[fetch]] files = ['scripts/main.py', 'scripts/my_module.py'] to_folder = 'scripts' </py-config> <py-script src="scripts/main.py"></py-script> </body> </html> The 2 Python files are simply put in a folder called inner. My problem is simple. I am getting this error: ModuleNotFoundError: No module named 'my_module' Even though my_module should work, just like it would if I ran Python outside of PyScript.
Avatar
Avatar
Simanelix
I can't use [[fetch]] at all, it seems. I have 3 files:
  • scripts/main.py:
import my_module my_module.my_f()
  • scripts/my_module.py:
def my_f(): print("Code works?")
  • index.html
<html> <head> <!-- this part just works; I simply have PyScript itself stored on my computer locally --> <link rel="stylesheet" href="pyscript/pyscript.css"/> <script defer src="pyscript/pyscript.js"></script> </head> <body> <!-- this is the part that does not work --> <py-config> [[fetch]] files = ['scripts/main.py', 'scripts/my_module.py'] to_folder = 'scripts' </py-config> <py-script src="scripts/main.py"></py-script> </body> </html> The 2 Python files are simply put in a folder called inner. My problem is simple. I am getting this error: ModuleNotFoundError: No module named 'my_module' Even though my_module should work, just like it would if I ran Python outside of PyScript.
is "scripts" folder in sys.path ?
Avatar
Yes. It's just not working. I think the problem is the parameters I used in fetch.
Avatar
I solved it: from = 'scripts/' files = ['main.py', 'my_module.py']
Avatar
Avatar
Simanelix
I solved it: from = 'scripts/' files = ['main.py', 'my_module.py']
Jeff Glass 10/12/2023 17:27
In this case, you donโ€™t need to include main.py in your files. The src attribute of a PyScript tag loads the Python code from the given URL, not from the virtual filesystem where fetch puts them
Avatar
I might have forgotten this but: What was the easiest way to call a function with arguments using py-click? So, using py-click="foo(5, 2)" instead of py-click="foo" Is this even possible right now? Currently, for the latter, we define foo as: def foo(event): pass With arguments of course, the function definition would be something like: def foo(x, y): pass It was possible with the โ€œclassicโ€ version if I remember correctlyโ€ฆ
Avatar
Avatar
madhurt
I might have forgotten this but: What was the easiest way to call a function with arguments using py-click? So, using py-click="foo(5, 2)" instead of py-click="foo" Is this even possible right now? Currently, for the latter, we define foo as: def foo(event): pass With arguments of course, the function definition would be something like: def foo(x, y): pass It was possible with the โ€œclassicโ€ version if I remember correctlyโ€ฆ
Jeff Glass 10/12/2023 18:56
Not currently possible - py-events are simply the name of a callable in the global namespace of the main thread, that get passed the event argument
Avatar
Avatar
Jeff Glass
Not currently possible - py-events are simply the name of a callable in the global namespace of the main thread, that get passed the event argument
Any workarounds? I ask because something of this sort was being done in: https://pyscript.com/@6fd75ce3-df0a-4384-b255-195e41c4f02f/tic-tac-toe/latest -- but this was with the "classic" PyScript of course... (edited)
Avatar
Avatar
madhurt
Any workarounds? I ask because something of this sort was being done in: https://pyscript.com/@6fd75ce3-df0a-4384-b255-195e41c4f02f/tic-tac-toe/latest -- but this was with the "classic" PyScript of course... (edited)
Jeff Glass 10/12/2023 20:52
In that particular example, you could rework GAME.click to grab the ID of the tile and use that to generate the argumentsโ€ฆ although the more proper thing might be to give them attributes like data-row and data-column and use those
๐Ÿ‘๐Ÿผ 1
Avatar
Avatar
madhurt
Any workarounds? I ask because something of this sort was being done in: https://pyscript.com/@6fd75ce3-df0a-4384-b255-195e41c4f02f/tic-tac-toe/latest -- but this was with the "classic" PyScript of course... (edited)
Jeff has a post here which explains how to do it with an example: https://jeff.glass/post/whats-new-pyscript-2023-11-1/ <button data-color='red' py-click="select_color">Red</button> <button data-color='green' py-click="select_color">Green</button> <button data-color='blue' py-click="select_color">Blue</button> <script type="py"> def select_color(event): print(f"Setting color to: {event.target.getAttribute('data-color')}") </script>
What's new and what's changed in the newest PyScript release
๐Ÿ‘ 1
Avatar
Avatar
Neon22
Jeff has a post here which explains how to do it with an example: https://jeff.glass/post/whats-new-pyscript-2023-11-1/ <button data-color='red' py-click="select_color">Red</button> <button data-color='green' py-click="select_color">Green</button> <button data-color='blue' py-click="select_color">Blue</button> <script type="py"> def select_color(event): print(f"Setting color to: {event.target.getAttribute('data-color')}") </script>
Thanks @Neon22 for pointing me to it...
Avatar
Andrea Giammarchi 14/12/2023 15:45
remember int or float in Python or parseInt or parseFloat in JS, when it comes to number, might help too, as all attributes on the DOM are strings
Avatar
Rust-Ninja-Sabi 15/12/2023 16:12
Does anyone work with PyScript and llama-index?
Avatar
Rust-Ninja-Sabi 16/12/2023 13:37
I used PyScript with llama-index. What does the error mean? Unhandled Promise Rejection: PythonError: Traceback (most recent call last): File "/lib/python3.11/site-packages/micropip/_commands/install.py", line 142, in install await transaction.gather_requirements(requirements) Fil... No pure Python package? Or something else?
Avatar
Avatar
Rust-Ninja-Sabi
I used PyScript with llama-index. What does the error mean? Unhandled Promise Rejection: PythonError: Traceback (most recent call last): File "/lib/python3.11/site-packages/micropip/_commands/install.py", line 142, in install await transaction.gather_requirements(requirements) Fil... No pure Python package? Or something else?
Jeff Glass 16/12/2023 17:38
Interestingโ€ฆ. Does the error actually end in โ€œFilโ€ฆโ€ or is there more after that?
Avatar
Avatar
Jeff Glass
Interestingโ€ฆ. Does the error actually end in โ€œFilโ€ฆโ€ or is there more after that?
Rust-Ninja-Sabi 18/12/2023 08:30
Hello Jeff. The message Uncaught (in promise) PythonError: Traceback (most recent call last): File "/lib/python3.11/site-packages/micropip/_commands/install.py", line 142, in install await transaction.gather_requirements(requirements) File "/lib/python3.11/site-packages/micropip/transaction.py", line 204, in gather_requirements await asyncio.gather(requirement_promises) File "/lib/python3.11/site-packages/micropip/transaction.py", line 211, in add_requirement return await self.add_requirement_inner(Requirement(req)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/lib/python3.11/site-packages/micropip/transaction.py", line 300, in add_requirement_inner await self._add_requirement_from_package_index(req) File "/lib/python3.11/site-packages/micropip/transaction.py", line 347, in _add_requirement_from_package_index await self.add_wheel(wheel, req.extras, specifier=str(req.specifier)) File "/lib/python3.11/site-packages/micropip/transaction.py", line 385, in add_wheel await self.gather_requirements(wheel.requires(extras)) File "/lib/python3.11/site-packages/micropip/transaction.py", line 204, in gather_requirements await asyncio.gather(requirement_promises) File "/lib/python3.11/site-packages/micropip/transaction.py", line 208, in add_requirement return await self.add_requirement_inner(req) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/lib/python3.11/site-packages/micropip/transaction.py", line ^^^^^^^^^^^^^^^^^^^^^^^^^ File "/lib/python3.11/site-packages/micropip/transaction.py", line 431, in find_wheel raise ValueError( ValueError: Can't find a pure Python 3 wheel for 'aiohttp<4.0.0,>=3.8.6'. See: https://pyodide.org/en/stable/usage/faq.html#why-can-t-micropip-find-a-pure-python-wheel-for-a-package
Avatar
Avatar
Rust-Ninja-Sabi
Hello Jeff. The message Uncaught (in promise) PythonError: Traceback (most recent call last): File "/lib/python3.11/site-packages/micropip/_commands/install.py", line 142, in install await transaction.gather_requirements(requirements) File "/lib/python3.11/site-packages/micropip/transaction.py", line 204, in gather_requirements await asyncio.gather(requirement_promises) File "/lib/python3.11/site-packages/micropip/transaction.py", line 211, in add_requirement return await self.add_requirement_inner(Requirement(req)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/lib/python3.11/site-packages/micropip/transaction.py", line 300, in add_requirement_inner await self._add_requirement_from_package_index(req) File "/lib/python3.11/site-packages/micropip/transaction.py", line 347, in _add_requirement_from_package_index await self.add_wheel(wheel, req.extras, specifier=str(req.specifier)) File "/lib/python3.11/site-packages/micropip/transaction.py", line 385, in add_wheel await self.gather_requirements(wheel.requires(extras)) File "/lib/python3.11/site-packages/micropip/transaction.py", line 204, in gather_requirements await asyncio.gather(requirement_promises) File "/lib/python3.11/site-packages/micropip/transaction.py", line 208, in add_requirement return await self.add_requirement_inner(req) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/lib/python3.11/site-packages/micropip/transaction.py", line ^^^^^^^^^^^^^^^^^^^^^^^^^ File "/lib/python3.11/site-packages/micropip/transaction.py", line 431, in find_wheel raise ValueError( ValueError: Can't find a pure Python 3 wheel for 'aiohttp<4.0.0,>=3.8.6'. See: https://pyodide.org/en/stable/usage/faq.html#why-can-t-micropip-find-a-pure-python-wheel-for-a-package
Rust-Ninja-Sabi 18/12/2023 08:34
I think there is no version on aiohttp that works in the browser. Right? And so llamaindex that needs it can not work. Is there any solution?
Avatar
Fabiano.py 21/12/2023 19:07
I'm following the example given on the Pyscript main page, every day thousands of people come here, where are we going wrong? Now we can't anymore? Because it does not work? ๐Ÿซฃ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="https://pyscript.net/releases/2023.11.1/core.css" /> <script type="module" src="https://pyscript.net/releases/2023.11.1/core.js"></script> <title>Document</title> </head> <body> <py-script> print('Now you can!') </py-script> </body> </html>
๐Ÿ˜– 2
Avatar
The example above will still print "Now you can!" to the browser console. But unfortunately, I think the front-page example is a bit outdate as it references the 11.1 release. I think they're now on 12.1 now. Check out the docs for a more recent example: https://pyscript.github.io/docs/2023.12.1/beginning-pyscript/. I also found @sadukie 's recent talk at PyOhio to be helpful as well: https://www.youtube.com/watch?v=nfqYepL0ykE&list=PL2k6bbM_wgjvpaFWOp1Jao78LqMzlZ3Gl&index=4
๐Ÿ™๐Ÿป 1
Avatar
Avatar
Knillz
The example above will still print "Now you can!" to the browser console. But unfortunately, I think the front-page example is a bit outdate as it references the 11.1 release. I think they're now on 12.1 now. Check out the docs for a more recent example: https://pyscript.github.io/docs/2023.12.1/beginning-pyscript/. I also found @sadukie 's recent talk at PyOhio to be helpful as well: https://www.youtube.com/watch?v=nfqYepL0ykE&list=PL2k6bbM_wgjvpaFWOp1Jao78LqMzlZ3Gl&index=4
These are the links I shared with the PyOhio crew afterwards: โ€ผ๏ธUpdated link PyScript examples: https://pyscript.com/@examples (go here - not the pyscript.net URL I mentioned in the video... things changed after I submitted my recording to PyOhio)
๐ŸŽ‰ 1
๐Ÿ‘ 2
Avatar
Avatar
Fabiano.py
I'm following the example given on the Pyscript main page, every day thousands of people come here, where are we going wrong? Now we can't anymore? Because it does not work? ๐Ÿซฃ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="https://pyscript.net/releases/2023.11.1/core.css" /> <script type="module" src="https://pyscript.net/releases/2023.11.1/core.js"></script> <title>Document</title> </head> <body> <py-script> print('Now you can!') </py-script> </body> </html>
Fabiano.py 22/12/2023 04:33
@Jeff Glass took the example from your video to test and it's not working either. (Today I received a message from a Facebook friend in Cairo โ€œEgyptโ€ with the same error) everything we put (PRINT) is not working. No error message appears, the screen stays white we are using vscode plugin live server help!!!!!!!!!! ๐Ÿฅบ
Avatar
Avatar
Fabiano.py
@Jeff Glass took the example from your video to test and it's not working either. (Today I received a message from a Facebook friend in Cairo โ€œEgyptโ€ with the same error) everything we put (PRINT) is not working. No error message appears, the screen stays white we are using vscode plugin live server help!!!!!!!!!! ๐Ÿฅบ
Andrea Giammarchi 22/12/2023 08:53
we have tons of integration tests that block releases if anything ain't working as expected ... what do you mean by "it's not working", if I might ask?
Avatar
Fabiano.py 22/12/2023 09:05
@Andrea Giammarchi Hello, the world does not appear or show the error message on the screen, the screen is blank. (I'm on Linux deepin, vs code, live server)
Avatar
Avatar
Fabiano.py
@Andrea Giammarchi Hello, the world does not appear or show the error message on the screen, the screen is blank. (I'm on Linux deepin, vs code, live server)
Andrea Giammarchi 22/12/2023 11:13
I am not sure I understand ... this works out of the box for me (use py or mpy, it's the same) <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script type="module" src="https://pyscript.net/releases/2023.11.2/core.js"></script> </head> <body> <script type="mpy"> from pyscript import display print("Hello World") display("Hello World") </script> </body> </html>
Avatar
Avatar
Fabiano.py
@Jeff Glass took the example from your video to test and it's not working either. (Today I received a message from a Facebook friend in Cairo โ€œEgyptโ€ with the same error) everything we put (PRINT) is not working. No error message appears, the screen stays white we are using vscode plugin live server help!!!!!!!!!! ๐Ÿฅบ
With your code, the print() statement will do what it's supposed to but you're not seeing the output. You have: <script type="py"> # But this website can run Python print("Hello world") </script> What you need is a way to capture the output where you can see it. The terminal attribute on the <script> tag will help in this case: <script type="py" terminal> # But this website can run Python print("Hello world") </script> This will get you a terminal output like the attached image. (edited)
11:34
Note that my solution is using type="py". This terminal doesn't work with MicroPython (type="mpy") yet - that's called out in the docs page for terminal. (edited)
๐Ÿ˜€ 1
11:38
Andrea's solution also works because display() is another way of displaying output. There's more info on display() in the pyscript.display doc. (edited)
๐Ÿ‘ 1
Boni started a thread. 22/12/2023 11:41
Avatar
Ayman Moussa 27/12/2023 16:14
Hi everyone. I am discovering PyScript and I am wondering if it is possible to use it to produce an animation using animation.Funcanimation of Matplotlib. I found several examples using Matplotlib to produce one (single) plot but not one which actually animate a sequence. Ultimately, my goal is to translate a small python script that I have which produces an animation depending on some parameters into a web page with widgets allowing to fix those parameters ; the user could then (on the client) launch the computation and see the resulting animation. But as I am completely new to PyScript I don't even know if that's possible ! If you have any example which resembles to this I would be glad to know about it !
Avatar
Avatar
Ayman Moussa
Hi everyone. I am discovering PyScript and I am wondering if it is possible to use it to produce an animation using animation.Funcanimation of Matplotlib. I found several examples using Matplotlib to produce one (single) plot but not one which actually animate a sequence. Ultimately, my goal is to translate a small python script that I have which produces an animation depending on some parameters into a web page with widgets allowing to fix those parameters ; the user could then (on the client) launch the computation and see the resulting animation. But as I am completely new to PyScript I don't even know if that's possible ! If you have any example which resembles to this I would be glad to know about it !
I tried this sample animation.FuncAnimation out on PyScript.com and am running into issues with the TimerWasm in the Pyodide implementation of Matplotlib not having a _timer attribute. This is the error that comes across: AttributeError: 'TimerWasm' object has no attribute '_timer' Maybe someone here will have more insight for you. (edited)
Avatar
Avatar
sadukie
I tried this sample animation.FuncAnimation out on PyScript.com and am running into issues with the TimerWasm in the Pyodide implementation of Matplotlib not having a _timer attribute. This is the error that comes across: AttributeError: 'TimerWasm' object has no attribute '_timer' Maybe someone here will have more insight for you. (edited)
Ayman Moussa 27/12/2023 18:00
Thanks for the try ; I am running into the same issue. If anyone has a clue I'll be delighted to discover a possible solution !
๐Ÿ™๐Ÿป 1
Avatar
Avatar
Ayman Moussa
Thanks for the try ; I am running into the same issue. If anyone has a clue I'll be delighted to discover a possible solution !
Avatar
Ayman Moussa 29/12/2023 08:30
Thanks @sadukie !!
Avatar
Hello guys! I have a question. How can you use files other than index.html while using html? I mean I have a function in file main.py that just prints hello world, and want to use that function on index.html to to write it there. How to because I did it normal -python way and it doesnt work. What am I doing wrong?
Avatar
PyScript needs to know about the module somehow in order to download it first. Can you try adding a config attribute like in the following snippet? <script config='{"files":{"main.py":""}}' type="py"> import main main.printage() </script> If that works, consider using a TOML configuration file. Described in the documentation: https://pyscript.github.io/docs/2023.12.1/user-guide/configuration/.
12:40
Oh uh... Just noticed you are using a version 2022. Unsure if <script> was supported back then. If possible, try using 2023.12.1 as well.
12:42
The script path was changed. So you might need to change that too. See https://pyscript.github.io/docs/2023.12.1/beginning-pyscript/#indexhtml. <script type="module" src="https://pyscript.net/releases/2023.12.1/core.js"></script> Corresponding CSS can be dropped.
Avatar
okay, I will try to use following lines, but out of curiosity, what if I am using multiple files not only main.py?
14:46
oh and now I dont get any output
15:42
Check browser console for error messages. Or do some printf debugging before and after import.
Avatar
Is there anyway to clear the output from the PyScript terminal?
Avatar
If not, is there anyway to redirect the print output from Python to a different place instead of Terminal or the console?
Avatar
I know in xterm you can call terminal.clear() , but doesn't look like PyScript exposes the Terminal to call this method
Avatar
There is always for _ in range(1000): print("") ๐Ÿ™ƒ
Avatar
Ahh ok, I think the functionality I am looking for is maybe not included in PyScript, at least for what I am trying to build. I may just work with Pyodide and xterm directly (edited)
Avatar
Overwriting sys.stdout might work. Doesn't rely on Pyscript implementation details. https://stackoverflow.com/questions/63925727/how-to-enable-print-after-disabling-it
Avatar
Yeah, maybe. Honestly, I was trying to build an IDE in the browser, but the only way to get it to work with PyScript is a bunch of hacks or the REPL, which is not what I am looking for.
Avatar
Avatar
Zinx
Is there anyway to clear the output from the PyScript terminal?
Andrea Giammarchi 29/12/2023 20:31
as answered elsewhere too, you can always element.textContent = ''; but it's a good idea to expose the terminal reference that points at Xterm or at least some of its helpers, like .clear() to smooth out the experience. The inevitable question after would be ... did you mean to clear just the terminal or the whole env? In the first case, it's easy and cheap/fast, in the latter one, you need to await the bootstrap of a whole new pyodide or micropython interpreter, if that's what you're after, which is not 100% clear from your request. (edited)
20:33
in the latter case, you have better chances by replacing the current terminal with a new one and call it a day ... just sharing options as the topic is interesting and relevant for the terminal, but not entirely described as behavior/intent/expectation.
Avatar
Avatar
Andrea Giammarchi
as answered elsewhere too, you can always element.textContent = ''; but it's a good idea to expose the terminal reference that points at Xterm or at least some of its helpers, like .clear() to smooth out the experience. The inevitable question after would be ... did you mean to clear just the terminal or the whole env? In the first case, it's easy and cheap/fast, in the latter one, you need to await the bootstrap of a whole new pyodide or micropython interpreter, if that's what you're after, which is not 100% clear from your request. (edited)
I only wanted the first case, so literally just calling terminal.clear()
Avatar
How on onClick I call a function in another Python file, not an inline Python function in the index.html? Thanks in advance.
Avatar
Also, what's the best way to move forward is it button onClick or psy-onClick?
Avatar
I may have figured out how to call an external file from index.html with this. "<script type="py" src="main.py"></script>" but how to call a particular function in an external Python file from index.html?
Avatar
Avatar
Stanny
How on onClick I call a function in another Python file, not an inline Python function in the index.html? Thanks in advance.
chris.laffra 01/01/2024 12:27
I recommend you take a look at LTK, which makes the creation of more complex UIs a lot easier: https://github.com/laffra/ltk (edited)
LTK is a little toolkit for writing UIs in PyScript - GitHub - laffra/ltk: LTK is a little toolkit for writing UIs in PyScript
Avatar
You might need a publicly accessible place to show an example ๐Ÿ˜…
Avatar
Avatar
Boni
You might need a publicly accessible place to show an example ๐Ÿ˜…
chris.laffra 01/01/2024 16:17
I fixed the link, thanks.
Avatar
Avatar
chris.laffra
I fixed the link, thanks.
Chris, this is great work.
๐Ÿ‘ 2
sadukie started a thread. 02/01/2024 19:38
Avatar
Szabolcs Dombi 07/01/2024 13:43
Hello, Can anyone confirm if this one is working? https://szabolcsdombi.pyscriptapps.com/zengl-demo/latest/ it should render a rotating logo and the animation should be smooth
Avatar
Szabolcs Dombi 07/01/2024 13:52
It seems the default error message for a missing package is misleading: ModuleNotFoundError: The module 'numpy' is included in the Pyodide distribution, but it is not installed. You can install it by calling: await micropip.install("numpy") in Python, or await pyodide.loadPackage("numpy") in JavaScript See https://pyodide.org/en/stable/usage/loading-packages.html for more details. Actually none of the suggestions work. import micropip; await micropip.install("numpy") This one fails for await being outside of a function (this actually works in the pyodide console) adding a script tag with module type and calling await pyodide.loadPackage("numpy") fails for pyodide being undefined why is pyodide undefined, Is there another place to add this line?
Avatar
Avatar
Szabolcs Dombi
It seems the default error message for a missing package is misleading: ModuleNotFoundError: The module 'numpy' is included in the Pyodide distribution, but it is not installed. You can install it by calling: await micropip.install("numpy") in Python, or await pyodide.loadPackage("numpy") in JavaScript See https://pyodide.org/en/stable/usage/loading-packages.html for more details. Actually none of the suggestions work. import micropip; await micropip.install("numpy") This one fails for await being outside of a function (this actually works in the pyodide console) adding a script tag with module type and calling await pyodide.loadPackage("numpy") fails for pyodide being undefined why is pyodide undefined, Is there another place to add this line?
Jeff Glass 07/01/2024 15:30
Youโ€™ll want to add numpy to your list of pyconfig packages: <py-config> packages = [โ€˜numpy`] </py-config> We should maybe consider catching that error from Pyodide and subsituting a pyscript specific one
Avatar
Sabo Katana 07/01/2024 15:45
Hello, I'm pretty much at the beginning, I have installed Visual Code and want to connect it pyscript with visual code but it works.
Avatar
Phelsong (Josh) 07/01/2024 15:46
that print should be in the dev console
15:50
if you add a <script type="py" src="/pathto/py file" config="/pathto/config.toml"></script> to your body, imo it's a much easier way to work inside pyscript
Avatar
Avatar
Jeff Glass
Youโ€™ll want to add numpy to your list of pyconfig packages: <py-config> packages = [โ€˜numpy`] </py-config> We should maybe consider catching that error from Pyodide and subsituting a pyscript specific one
Szabolcs Dombi 07/01/2024 15:56
I just realize this message is not from PyScript but from pyodide. Thanks!
Avatar
Avatar
Phelsong (Josh)
if you add a <script type="py" src="/pathto/py file" config="/pathto/config.toml"></script> to your body, imo it's a much easier way to work inside pyscript
Sabo Katana 07/01/2024 15:58
okay i just don't understand why everyone goes this way that i have entered and it works for everyone and not for me. And that means that this script you have sent here must be under <body> ?
Avatar
Phelsong (Josh) 07/01/2024 16:00
it does the same thing you're trying do to, but allows you to work inside a normal .py file with syntax hightlighting/lsp support
Avatar
Sabo Katana 07/01/2024 16:32
so is it not possible to work with the python programming language in the index.html file?
Avatar
Avatar
Phelsong (Josh)
it does the same thing you're trying do to, but allows you to work inside a normal .py file with syntax hightlighting/lsp support
Sabo Katana 07/01/2024 16:36
is that right? and if I have entered this I can work in the demo.py what should be displayed in the html. Or have I misunderstood this?
Avatar
Avatar
Sabo Katana
so is it not possible to work with the python programming language in the index.html file?
Phelsong (Josh) 07/01/2024 16:39
you can, it's just going to be like working in notepad
Avatar
Avatar
Sabo Katana
is that right? and if I have entered this I can work in the demo.py what should be displayed in the html. Or have I misunderstood this?
Phelsong (Josh) 07/01/2024 16:40
Contribute to Phelsong/Senza development by creating an account on GitHub.
16:40
need a toml with imports like that
Avatar
And what's in demo.py? Might be easier testing with pyscript.display instead of print.
Avatar
Avatar
Boni
And what's in demo.py? Might be easier testing with pyscript.display instead of print.
Sabo Katana 07/01/2024 17:49
my py file
Avatar
Sabo Katana 07/01/2024 17:55
okay since i'm still a beginner this is still a step too far for me to solve it i'd better continue normally for now. i can't do it this way. thank you
Avatar
Hello, Sorry for the beginner's question, but how can a terminal worker access a javascript variable? var textreceive ="" //variable javascript <script type="py" terminal worker target="#terminal"> from pyscript import document term = document.querySelector('script[terminal]').terminal term.resize(40, 25) import code code.interact(local=globals()) </script> Thank you (edited)
Avatar
Avatar
pat_pat
Hello, Sorry for the beginner's question, but how can a terminal worker access a javascript variable? var textreceive ="" //variable javascript <script type="py" terminal worker target="#terminal"> from pyscript import document term = document.querySelector('script[terminal]').terminal term.resize(40, 25) import code code.interact(local=globals()) </script> Thank you (edited)
Jeff Glass 07/01/2024 22:23
If you import the window element of the pyscript module, thatโ€™s a proxy for the JS global namespace
Avatar
Jeremy Chen 07/01/2024 22:35
Hi, I like the fireworks included in the latest release note and would like to show it to my students. I am wondering if I can get the sound going and the sky (background) turn dark. This is my code so far. https://jeremychen886.pyscriptapps.com/new-year-fireworks/latest/ # if I pass in the options dict, I will get this error # Error: ownKeys not implemented # fireworks = Fireworks.new(container, {"opacity": True}) # It would be nice to get the sound working # https://github.com/crashmax-dev/fireworks-js/blob/eedee4020e9c458fde83b60dfac6f2502d4593bb/packages/fireworks-js/src/options.ts#L91-L102 Thanks. (edited)
Avatar
Avatar
Jeremy Chen
Hi, I like the fireworks included in the latest release note and would like to show it to my students. I am wondering if I can get the sound going and the sky (background) turn dark. This is my code so far. https://jeremychen886.pyscriptapps.com/new-year-fireworks/latest/ # if I pass in the options dict, I will get this error # Error: ownKeys not implemented # fireworks = Fireworks.new(container, {"opacity": True}) # It would be nice to get the sound working # https://github.com/crashmax-dev/fireworks-js/blob/eedee4020e9c458fde83b60dfac6f2502d4593bb/packages/fireworks-js/src/options.ts#L91-L102 Thanks. (edited)
Jeff Glass 08/01/2024 01:25
A couple of things are happening here - the options object that the Fireworks constructor takes is indeed an Object, not a dict - this is quite common in the JavaScript world, and it's an unfortunate coincidence that the syntax for a JS object looks so much like a Python dict.
01:27
There's something funky with the way object proxying works in Micropython in this case; but you can see this working using Pyodide's to_js conversion functionality: <canvas id="fw" style="background-color: black;"></canvas> <py-config> [js_modules.main] "https://cdn.jsdelivr.net/npm/fireworks-js@2.10.7/+esm" = "Fireworks_Module" </py-config> <script type="py"> from pyscript import document, window from pyscript.js_modules import Fireworks_Module from pyodide.ffi import to_js from js import Object # This line converts the Python dict to a JS object options = to_js({"decay": {"min": 0.005, "max": 0.020}}, dict_converter = Object.fromEntries) container = document.querySelector('#fw') fireworks = Fireworks_Module.Fireworks.new(container, options) fireworks.start() </script>
01:28
Unfortunately, I can't seem to get sound working either, even in Pure JS, not even on their demo sight. Maybe some odd browser interaction. I would have thought this would have worked: options = to_js({"sound": {"enabled": True, "volume" : {"min": 4, "max": 8}, "files" : ["https://raw.githubcontent.com/crashmax-dev/fireworks-js/website/public/sounds/explosion0.mp3"]}}, dict_converter = Object.fromEntries) (edited)
Avatar
Avatar
Jeff Glass
Unfortunately, I can't seem to get sound working either, even in Pure JS, not even on their demo sight. Maybe some odd browser interaction. I would have thought this would have worked: options = to_js({"sound": {"enabled": True, "volume" : {"min": 4, "max": 8}, "files" : ["https://raw.githubcontent.com/crashmax-dev/fireworks-js/website/public/sounds/explosion0.mp3"]}}, dict_converter = Object.fromEntries) (edited)
note some browsers don't like mp3 better use ogg when testing
๐Ÿ‘ 1
Avatar
elliot0x01 08/01/2024 08:23
Can anyone tell, what I am doing wrong here? It won't display and take input and blank black terminal occurs only
Avatar
elliot0x01 08/01/2024 08:46
Checklist I added a descriptive title I searched for other issues and couldn't find a solution or duplication I already searched in Google and didn't find any good information or help What ...
Avatar
elliot0x01 08/01/2024 09:03
Can anyone tell how do I get an element using pydom? I tried btn1=pydom.Element('#btn1') where #btn1 is id of button1 but it won't work
Avatar
elliot0x01 08/01/2024 09:11
I even tried pydom["#btn1"] but still no success
Avatar
Jeff Glass 08/01/2024 14:14
I think `pydom[โ€˜#btn1โ€™] returns a collection of elements, even if it matches a single element. Have you tried iterating over the result? Or what happens if you print out the elements of the result?
Avatar
Andrea Giammarchi 08/01/2024 20:13
honestly, I think PyDom should be smarter ... if it's a single selector (no comma in there) and it starts with a # hash, there's no way, Web standards speaking, that ID should ever return more than a single element.
20:14
cc-ing @Fabio here for ideas or counter arguments
Avatar
Avatar
Andrea Giammarchi
cc-ing @Fabio here for ideas or counter arguments
That's a good argument. I initially borrowed ideas from jquery and some python projects with know "pythonic" APIs. I think I had this chat with Antonio and maybe eve you @Jeff Glass ... and, iirc, the argument for how it behaves today is really just consistency. I'm not opposed to sploring the idea of returning THE object when users are querying with a "#... selector. The thing that sucks is that there are other cases when you "know" there's only one element, without specifying the id... An idea that I've been king of entertaining is to have an alias for "the first element" (something like _, first or something like that) so btn1 = pydom["#btn1"].first in the example above would point to the Element (I like _ better but I realize it isn't the most user friendly.. (edited)
Avatar
Andrea Giammarchi 09/01/2024 00:22
I like the first idea, for what it matters, it reflects the intent without making the returned node list 'hybrid"
Avatar
elliot0x01 09/01/2024 08:45
I was wondering if it will be possible to use pyscript with static site generators like Hugo and Jekyll, I am no frontend developer and basically suck at UI, I have a static blog using Jekyll but I would like to have a certain part of the page fetch data from exeternal source and display it, was wondering if I can do that with pyscript ๐Ÿค”
Avatar
Avatar
Jeff Glass
I think `pydom[โ€˜#btn1โ€™] returns a collection of elements, even if it matches a single element. Have you tried iterating over the result? Or what happens if you print out the elements of the result?
elliot0x01 09/01/2024 08:46
I will look into this, I am not sure but I can't find the old documentation which had all these covered. The new documentation don't have any example for this, like how to get an element using queryselector with pydom
Avatar
Avatar
elliot0x01
I will look into this, I am not sure but I can't find the old documentation which had all these covered. The new documentation don't have any example for this, like how to get an element using queryselector with pydom
Jeff Glass 09/01/2024 12:01
I believe Fabioโ€™a original demo and writeup is still accurate - I think it was created more as a showcase than as documentation, hence not linking to it from the docs, but it may still be helpful for you: https://fpliger.pyscriptapps.com/pyweb/latest/pydom.html
Avatar
Avatar
elliot0x01
I was wondering if it will be possible to use pyscript with static site generators like Hugo and Jekyll, I am no frontend developer and basically suck at UI, I have a static blog using Jekyll but I would like to have a certain part of the page fetch data from exeternal source and display it, was wondering if I can do that with pyscript ๐Ÿค”
Jeff Glass 09/01/2024 12:03
PyScript and static site generators play great together - my site is built with Hugo, and itโ€™s super easy to put demos and other functionality into the HTML. See for instance https://jeff.glass/post/advent-of-code-2023/ or https://jeff.glass/project/richdemo/
๐Ÿ’ฏ 1
Avatar
elliot0x01 09/01/2024 18:53
Thanks @Jeff Glass , these are gold mine โ™ฅ๏ธ
โค๏ธ 1
Avatar
elliot0x01 09/01/2024 19:10
Can anyone tell what am I doing wrong here ?
19:11
I followed this, I am trying to append some HTML to my output div
19:12
Because appending values directly is resulting into skipping new lines and it outputs as a paragraph
Avatar
Avatar
elliot0x01
Can anyone tell what am I doing wrong here ?
Jeff Glass 09/01/2024 19:44
Are you using pyodide or micropython? That dict_view has no len is only something Iโ€™ve seen with micropython
Avatar
Avatar
elliot0x01
Can anyone tell what am I doing wrong here ?
Jeff Glass 09/01/2024 20:36
Ah, little thing: you don't want to append directly to the out element I think, you'd want to do out.innerHTML += "<h1>Your Text Here</h1>"
Avatar
anfroholic 09/01/2024 23:34
Hi all, I'm trying to get micropython running. My question is about the asyncio module in micropython. I see there is a '_asyncio' module, but it doesn't seem to have any methods inside. Is asyncio just not implemented yet? Thanks
Avatar
Avatar
anfroholic
Hi all, I'm trying to get micropython running. My question is about the asyncio module in micropython. I see there is a '_asyncio' module, but it doesn't seem to have any methods inside. Is asyncio just not implemented yet? Thanks
what do you need in asyncio ? keep in mind that micropython is not a drop-in replacement for cpython+stdlib. So what you are expecting is maybe just not there ( usually because it can be implemented when you need it with the basic bricks provided )
Avatar
Avatar
pmp-p
what do you need in asyncio ? keep in mind that micropython is not a drop-in replacement for cpython+stdlib. So what you are expecting is maybe just not there ( usually because it can be implemented when you need it with the basic bricks provided )
anfroholic 10/01/2024 02:24
I have a framework that runs on the esp32's and utilizes asyncio. It's advantageous to just run the same code in both places. The same code runs in the piodide runtime, but was hoping to drop some weight by using the micropython port
Avatar
esp32 asyncio module is quite light you should not have problem to add what may be missing
Avatar
anfroholic 10/01/2024 02:27
Ok, so it's just not fully implemented yet. That's ok.
Avatar
except when using future+thread for sync calls
Avatar
Avatar
anfroholic
Ok, so it's just not fully implemented yet. That's ok.
yep it is the "batteries not included version"
Avatar
Avatar
pmp-p
yep it is the "batteries not included version"
anfroholic 10/01/2024 02:28
Excelllent! Thank you for your help!
Avatar
Avatar
Jeff Glass
Ah, little thing: you don't want to append directly to the out element I think, you'd want to do out.innerHTML += "<h1>Your Text Here</h1>"
elliot0x01 10/01/2024 06:52
This works but this will keep changing the innerHTML correct? So if i am doing something like this: for i in range(5): out.innerHTML=f"<h1>This is count {i}</h1> The output will be This is count 4(the last statement cause innerHTML will keep overwriting previous entries), that's it
Avatar
Avatar
Jeff Glass
Are you using pyodide or micropython? That dict_view has no len is only something Iโ€™ve seen with micropython
elliot0x01 10/01/2024 06:55
yes you are correct, on changing it to py the display(HTML("<h1>test</h1>")) works fine but I wonder why it will not work with mpy ? ๐Ÿค”
Avatar
elliot0x01 10/01/2024 07:17
Is there a way to use terminal worker to display terminal and take user input when I am hosting a static website on github pages cause there's no way I can set response headers in this case? Should I inject mini-coi.js in my index page itself ?Also, I am really sorry for asking so many questions, I am just too excited to learn pyscript ๐Ÿ™‡โ€โ™‚๏ธ
Avatar
Avatar
elliot0x01
Is there a way to use terminal worker to display terminal and take user input when I am hosting a static website on github pages cause there's no way I can set response headers in this case? Should I inject mini-coi.js in my index page itself ?Also, I am really sorry for asking so many questions, I am just too excited to learn pyscript ๐Ÿ™‡โ€โ™‚๏ธ
elliot0x01 10/01/2024 07:38
Ohh figured out...I am so noob lol ๐Ÿ˜…
Avatar
BananaMilkshake 10/01/2024 08:18
hey folks ! I wanted help with using PyScript with ReactJS. I am trying to run this file https://github.com/nanopb/nanopb/blob/master/generator/nanopb_generator.py using pyscript to generate cpp header files from a given .proto file. This is the code I'm currently using to run the python file in a React component: useEffect(() => { if (!wrapperRef.current) return; const scriptElement = document.createElement("script"); scriptElement.type = "py"; (scriptElement as any).config = `{ "packages": ["protobuf"], }`; scriptElement.src = `${process.env.PUBLIC_URL}/nanopb_generator/nanopb_generator_script.py`; wrapperRef.current.appendChild(scriptElement); return () => {}; }, []); But, the package protobuf does not seem to be installed. I get this error (attached img). Do i have to install it in the py file's init function or something? I am also not sure of the micropip command given in the error message - since I'm using the Pyodide interpreter (i think?). Super new to pyscript so please let me know if I've done some mistakes in the setup or if you need any additional info. Thanks! (edited)
Protocol Buffers with small code size. Contribute to nanopb/nanopb development by creating an account on GitHub.
Avatar
BananaMilkshake 10/01/2024 08:26
also, another query - i was getting another error from pyodide saying __future__ should be on the top of the file when i tried to run the nanopb python file (linked), removing that line fixes it.. are future imports not supported in pyodide ?
Avatar
Avatar
BananaMilkshake
hey folks ! I wanted help with using PyScript with ReactJS. I am trying to run this file https://github.com/nanopb/nanopb/blob/master/generator/nanopb_generator.py using pyscript to generate cpp header files from a given .proto file. This is the code I'm currently using to run the python file in a React component: useEffect(() => { if (!wrapperRef.current) return; const scriptElement = document.createElement("script"); scriptElement.type = "py"; (scriptElement as any).config = `{ "packages": ["protobuf"], }`; scriptElement.src = `${process.env.PUBLIC_URL}/nanopb_generator/nanopb_generator_script.py`; wrapperRef.current.appendChild(scriptElement); return () => {}; }, []); But, the package protobuf does not seem to be installed. I get this error (attached img). Do i have to install it in the py file's init function or something? I am also not sure of the micropip command given in the error message - since I'm using the Pyodide interpreter (i think?). Super new to pyscript so please let me know if I've done some mistakes in the setup or if you need any additional info. Thanks! (edited)
Andrea Giammarchi 10/01/2024 08:50
useEffect(() => { if (!wrapperRef.current) return; const pyScript = Object.assign(document.createElement("script"), { type: "py", src: `${process.env.PUBLIC_URL}/nanopb_generator/nanopb_generator_script.py`; }); pyScript.setAttribute("config", JSON.stringify({ packages: ["protobuf"] })); wrapperRef.current.append(pyScript); return () => {}; }, []); (edited)
08:51
the config attribute is not an accessor like type, src, async are ... Web speaking, so both config or worker attributes must be set explicitly.
Avatar
BananaMilkshake 10/01/2024 08:52
ohh got it. Thanks, will try this!
Avatar
Avatar
BananaMilkshake
ohh got it. Thanks, will try this!
Andrea Giammarchi 10/01/2024 08:52
ooops, just edited as append should have pyScript not scriptElement ... that should work now
Avatar
BananaMilkshake 10/01/2024 08:55
thanks!
Avatar
Avatar
BananaMilkshake
also, another query - i was getting another error from pyodide saying __future__ should be on the top of the file when i tried to run the nanopb python file (linked), removing that line fixes it.. are future imports not supported in pyodide ?
Andrea Giammarchi 10/01/2024 08:55
nothing in here https://pyodide.org/en/stable/search.html?q=__future__ ... I suppose that answers
Avatar
Avatar
Andrea Giammarchi
useEffect(() => { if (!wrapperRef.current) return; const pyScript = Object.assign(document.createElement("script"), { type: "py", src: `${process.env.PUBLIC_URL}/nanopb_generator/nanopb_generator_script.py`; }); pyScript.setAttribute("config", JSON.stringify({ packages: ["protobuf"] })); wrapperRef.current.append(pyScript); return () => {}; }, []); (edited)
BananaMilkshake 10/01/2024 09:49
hey .. so I tried this, and the script tag that was generated looks like this (image.png). I'm not sure if it's supposed to be this way, since I still get the ModuleNotFound error. I tried changing it to a string directly like so: pyScript.setAttribute("config", '{"packages": ["protobuf"]}'); But this also escapes the quotes and generates the same script tag. I also tried this using a toml file for the config but that doesn't work either, gives the same error: useEffect(() => { if (!wrapperRef.current) return; const pyScript = Object.assign(document.createElement("script"), { type: "py", src: `${process.env.PUBLIC_URL}/nanopb_generator/nanopb_generator_script.py` }); pyScript.setAttribute( "config", `${process.env.PUBLIC_URL}/nanopb_generator/config.toml` ); wrapperRef.current.append(pyScript); return () => {}; }, []); The config.toml file is present at this path: public/nanopb_generator/config.toml with this content: packages = ["protobuf" ]
Avatar
Avatar
BananaMilkshake
hey .. so I tried this, and the script tag that was generated looks like this (image.png). I'm not sure if it's supposed to be this way, since I still get the ModuleNotFound error. I tried changing it to a string directly like so: pyScript.setAttribute("config", '{"packages": ["protobuf"]}'); But this also escapes the quotes and generates the same script tag. I also tried this using a toml file for the config but that doesn't work either, gives the same error: useEffect(() => { if (!wrapperRef.current) return; const pyScript = Object.assign(document.createElement("script"), { type: "py", src: `${process.env.PUBLIC_URL}/nanopb_generator/nanopb_generator_script.py` }); pyScript.setAttribute( "config", `${process.env.PUBLIC_URL}/nanopb_generator/config.toml` ); wrapperRef.current.append(pyScript); return () => {}; }, []); The config.toml file is present at this path: public/nanopb_generator/config.toml with this content: packages = ["protobuf" ]
Andrea Giammarchi 10/01/2024 10:54
every attribute is handled escaped, that's not the issue ... the config should be parsed regardless ... or you should have an error in console about the json being broken but I think that is not the case. I start believing protobuf is not provided by Pyodide micropip registry
Avatar
BananaMilkshake 10/01/2024 10:56
oh okay.
protobuf is not provided by Pyodide micropip registry
The error says it's included in the Pyodide distribution but not installed though
Avatar
Andrea Giammarchi 10/01/2024 10:57
example ... const script = document.createElement("script"); script.setAttribute("config", JSON.stringify({packages: ["protobuf"]})); script.outerHTML; // <script config="{&quot;packages&quot;:[&quot;protobuf&quot;]}"></script> // but ... script.getAttribute("config"); // '{"packages":["protobuf"]}' JSON.parse(script.getAttribute("config")); // {packages: ["protobuf"]}
Avatar
BananaMilkshake 10/01/2024 10:58
ahh got it ๐Ÿ‘
Avatar
Avatar
BananaMilkshake
oh okay.
protobuf is not provided by Pyodide micropip registry
The error says it's included in the Pyodide distribution but not installed though
Andrea Giammarchi 10/01/2024 11:09
OK, I gave it a try ... the confusion is that PyScript on the main thread cannot have more than py-config so that either you have a <py-config> on the page already, such as: <body> <py-config> packages = ["protobuf"] </py-config> </body>
11:09
or you need to run each different config in a worker
11:15
something is off though ... I am investigating a bit what's going on with the config, as not even workers seem to do anything
Avatar
BananaMilkshake 10/01/2024 11:19
oh .. I don't have any additional configs, but adding the py-config tag in index.html of the react app itself solved the issue (removed the setAttribute("config", ...) on the script tag). I'll try it once with a worker as well since we might be using pyscript at various places on our platform with different package dependencies and having a global py-config would be bad right?
Avatar
Last night on the stream there was mention of an MQTT library from a university contributor, is it possible to get a link? (Maybe to a pyscript demo and the library github if possible)
Avatar
Avatar
BananaMilkshake
oh .. I don't have any additional configs, but adding the py-config tag in index.html of the react app itself solved the issue (removed the setAttribute("config", ...) on the script tag). I'll try it once with a worker as well since we might be using pyscript at various places on our platform with different package dependencies and having a global py-config would be bad right?
Andrea Giammarchi 10/01/2024 11:24
so this worked for me ... <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script type="module" src="https://cdn.jsdelivr.net/npm/@pyscript/core"></script> <script type="module"> const pyScript = Object.assign(document.createElement("script"), { type: "py", textContent: ` print("Hello World") `, }); pyScript.toggleAttribute("worker"); pyScript.setAttribute("config", "./config.toml"); document.body.appendChild(pyScript); </script> </head> </html>
11:24
but unfortunately it looks like there is a regression around polyscript as any JSON doesn't seem to do what it's supposed to do ... I will file an issue about this.
โœ… 1
11:29
actually, json as file works, JSON as direct attribute doesn't ..
Avatar
Avatar
BananaMilkshake
oh .. I don't have any additional configs, but adding the py-config tag in index.html of the react app itself solved the issue (removed the setAttribute("config", ...) on the script tag). I'll try it once with a worker as well since we might be using pyscript at various places on our platform with different package dependencies and having a global py-config would be bad right?
Andrea Giammarchi 10/01/2024 11:35
thing is, configs are parsed once and are always the same on the main thread, so if you put 12 scripts on the page they will all share both same runtime (pyodide or MicroPython) and the very same config. You could, however, import micropip and use it to import extra stuff only when it's needed but within your own script, basically ignoring completely the need for a config. This would work best on main, imho. With Workers, you bootstrap pyodide every single time because it runs within ... well ... each worker. It's then a matter of tradeoffs
Avatar
BananaMilkshake 10/01/2024 11:39
oh right.. makes sense. thanks Andrea for all the help!
Avatar
Avatar
BananaMilkshake
oh right.. makes sense. thanks Andrea for all the help!
Andrea Giammarchi 10/01/2024 14:33
FYI there was an issue only with workers which has been fixed and will soon land in PyScript too: https://github.com/pyscript/polyscript/issues/76
Even with workers, it's apparently not possible to pass JSON directly, see: Working <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> &l...
๐Ÿ‘ 2
Avatar
Szabolcs Dombi 11/01/2024 07:09
Is there a way to disable ctrl+enter = execute? I am really used to this command, but for "add new line below".
Avatar
Avatar
Andrea Giammarchi
FYI there was an issue only with workers which has been fixed and will soon land in PyScript too: https://github.com/pyscript/polyscript/issues/76
BananaMilkshake 11/01/2024 07:11
oh awesome!
Avatar
Avatar
Szabolcs Dombi
Is there a way to disable ctrl+enter = execute? I am really used to this command, but for "add new line below".
Good question. I don't think so. Raise an issue, over on GitHub so we can work out a solution..?
Avatar
Avatar
ntoll
Good question. I don't think so. Raise an issue, over on GitHub so we can work out a solution..?
Szabolcs Dombi 11/01/2024 11:53
was hesitant on that, is the main pyscript repo the one to use for pyscript.com ui related bugreports?
๐Ÿ‘ 1
Avatar
This is a niche problem, but I'm trying to use the @when decorator with elements that are created by a JavaScript library. The elements are added to the DOM after the Python script runs, so @when seems to miss them. Does anyone have suggestions for adding event handlers dynamically?
Avatar
Avatar
mcintyre
This is a niche problem, but I'm trying to use the @when decorator with elements that are created by a JavaScript library. The elements are added to the DOM after the Python script runs, so @when seems to miss them. Does anyone have suggestions for adding event handlers dynamically?
Jeff Glass 11/01/2024 15:26
Objects with event handler attributes like py-click or py-mouseover etc. are handled by PyScript degardless of when in the page lifecycle they are created, even after PyScript runs.
15:28
The only hitch is the are main-thread only, at least for now. There was an (issue)https://github.com/pyscript/pyscript/issues/1763] about this which has been converted to a [discussion for background, and if your use case gives insight about this, we'd love to hear about it!
This is a purposedly opened discussion that doesn't need an issue stuck in our repo, as the topic is complex and solutions are, still, nowhere, due the variety of caveats and footguns to consid...
๐Ÿ‘ 2
Avatar
Avatar
mcintyre
This is a niche problem, but I'm trying to use the @when decorator with elements that are created by a JavaScript library. The elements are added to the DOM after the Python script runs, so @when seems to miss them. Does anyone have suggestions for adding event handlers dynamically?
Andrea Giammarchi 11/01/2024 15:34
this might be a great argument to bring into that discussion ... yes, @when inevitably happens when the interpreter is ready but, at the same time, py-x events can happen only once that interpreter is ready ... or event.preventDefault() and other synchronous things cannot happen. With workers, there is a specialized way to add event listeners, and yet these can't be known upfront so due event loop requiring synchrous tasks, we might, or might not, be able to solve the issue as a whole. (edited)
15:35
I did, in fact, propose a waitUntil standard event primitive, but while I am happy that proposal hasn't been closed yet, I am not holding my breath that will ever work as we need, or be implemented in general: https://github.com/whatwg/html/issues/9540
The ExtendibleEvent kind of event is exposed only in a ServiceWorker but some of its features could be extremely handy or desirable for any sort of async listener. Issue Desired also for other purp...
Avatar
Jeff Glass 11/01/2024 15:36
I think this is another case where a new @whenever operator could be useful as well, i.e. a Python function which hooks up functions to a Mutaton Observer that matches on event names. Not that it's mutually exclusive with somehow enabling py-*event from workers, just thinking out loud (edited)
Avatar
elliot0x01 11/01/2024 15:37
So we don't have requests and pyodide_http in micropython? like if I use <script type="mpy" src="main.py" config="config.json"> and main.py imports requests package
15:38
i added both packages in config.json but it says no module named requests
Avatar
Avatar
Jeff Glass
I think this is another case where a new @whenever operator could be useful as well, i.e. a Python function which hooks up functions to a Mutaton Observer that matches on event names. Not that it's mutually exclusive with somehow enabling py-*event from workers, just thinking out loud (edited)
Andrea Giammarchi 11/01/2024 15:38
I think @whenever is needed in general, but unfortunately can't solve Python listeners added before the runtime/interpreter has a reason to understand these. (edited)
๐Ÿ‘ 2
Avatar
Avatar
Andrea Giammarchi
I think @whenever is needed in general, but unfortunately can't solve Python listeners added before the runtime/interpreter has a reason to understand these. (edited)
Jeff Glass 11/01/2024 15:41
That's because the MO is watching for DOM changes and updating its handlers/listeners map when it seems matching attributes, yeah? Is there a world where the Interpreter/Runtime parses the DOM at the moment it initializes to create an initial list of listeners, to catch pre-existing elements with matching attributes, and the MO takes over after that?
Avatar
Avatar
Jeff Glass
That's because the MO is watching for DOM changes and updating its handlers/listeners map when it seems matching attributes, yeah? Is there a world where the Interpreter/Runtime parses the DOM at the moment it initializes to create an initial list of listeners, to catch pre-existing elements with matching attributes, and the MO takes over after that?
Andrea Giammarchi 11/01/2024 15:43
if you add any listener after the user performed that action that won't trigger, ever. Events are meant to be sync in terms of preventDefalut() or stopPropagation() (or mainly stopImmediatePropagation()) and there's no way around it (main reason for the push-back to my waitUntil() proposal). (edited)
15:47
if curois about how others fixed this, you can intercept listeners via JS on the main, specially events that are knonw and bubbles (often the default) and then re-trigger that event to the library once it's bootstrapped. In FB, as example, they used to document.documentElement.onclick = ... in a sync script on top, so that any click to any post would be re-triggered once the React stuff is fully working. This is a single event though, and one can patch all of them (with perf degradation) ... but surely that's a way to fix the issue. As a module though, not as an owner of the site, it's super hard to implement the same at distance (i.e. lazy loaded PyScript would still fail at polluting the HTML element with listeners that can intercept reactions as that click event might have happened already). (edited)
15:54
in short: listeners work in the present time; they might half-work in the future (as re-dispatched) but they can't ever work in the past (as already happened) ... this is curently what the DOM / Web events loop offers. Hard to work around it, if even possible. (edited)
Avatar
elliot0x01 11/01/2024 15:55
Can anyone tell what is the correct syntax for removing/adding a class to an element as shown in below js?
15:56
I tried, x=document.getElementById("x) x.remove_class("is-hidden") but it gives error
15:56
can't find the updated syntax
Avatar
Avatar
elliot0x01
can't find the updated syntax
Andrea Giammarchi 11/01/2024 15:58
I tried, x=document.getElementById("x) x.remove_class("is-hidden") but it gives error
x=document.getElementById("x") x.classList.remove("is-hidden")
15:58
that should do
Avatar
Avatar
Andrea Giammarchi
I tried, x=document.getElementById("x) x.remove_class("is-hidden") but it gives error
x=document.getElementById("x") x.classList.remove("is-hidden")
elliot0x01 11/01/2024 15:59
Damn...I am dumb sorry for asking stupid questions ๐Ÿ˜…
Avatar
Andrea Giammarchi 11/01/2024 16:00
that's the JS version ... with PyDom you need to retrieve the element via pydom["#x"] then remove_class("is-hidden") might do the trick.
Avatar
elliot0x01 11/01/2024 16:00
Ohhh, but using pydom gives a list of elements right ?
Avatar
Avatar
elliot0x01
Damn...I am dumb sorry for asking stupid questions ๐Ÿ˜…
Andrea Giammarchi 11/01/2024 16:01
it's not dumb, you used Python syntax we probably support but if it's from PyDom you need to learn ... if you use JS primitives just copy and paste ๐Ÿ˜…
Avatar
elliot0x01 11/01/2024 16:02
Ohh thanks @Andrea Giammarchi , I will learn the correct pydom and switch to using pydom completely
Avatar
Avatar
elliot0x01
Ohhh, but using pydom gives a list of elements right ?
Andrea Giammarchi 11/01/2024 16:02
correct, but as you went full remove_class I guessed you were hoping for PyDom to help .. but then again, you need a PyDom thing to be returned, not explicit JS primitives
Avatar
elliot0x01 11/01/2024 16:02
What do you suggest is more user firendly and easy to go when writing code? the import document from pyscript or using pydom?
Avatar
Andrea Giammarchi 11/01/2024 16:04
I believe our goal is to make PyDom the best user friendly option but there's still work to do ... the JS proxied version, on the other side, is easier to copy/paste and reason about so ... it's really up to you /cc @Fabio
Avatar
Avatar
Andrea Giammarchi
I believe our goal is to make PyDom the best user friendly option but there's still work to do ... the JS proxied version, on the other side, is easier to copy/paste and reason about so ... it's really up to you /cc @Fabio
elliot0x01 11/01/2024 16:06
Thanks Andrea, really appreciate your guidance ๐Ÿ˜ƒ
๐Ÿ‘ 1
Avatar
Andrea Giammarchi 11/01/2024 16:12
easier to copy/paste and reason about
I omitted ... from a Web dev perspective ๐Ÿ˜… ... PyDom supposes to fix also quirks and gotchas, so it's really up to you or at least until PyDom is declared fully functional. We do value early feedbacks too so ... I didn't want to discourage you from using PyDom, it's just out of your example I couldn't provide an esier answer to your issue.
Avatar
@elliot0x01 I had this problem unti it ground me to a total halt. While waiting for pydom I am now using ltk. This is a meld of pydom and jquery. I must say its working very well. I would recommend giving it a look.
Avatar
Avatar
Neon22
@elliot0x01 I had this problem unti it ground me to a total halt. While waiting for pydom I am now using ltk. This is a meld of pydom and jquery. I must say its working very well. I would recommend giving it a look.
elliot0x01 11/01/2024 20:07
Thanks Neon, this looks awesome. Tbh I suck at front end and UI, I come from security/pentesting background and I was hopeful to get some decent UIs using some CSS frameworks, Bulma was the one which was easiest to implement but I still suck at complex designs lol...now feeling it's not my cup of tea ๐Ÿ˜…
Avatar
Avatar
Andrea Giammarchi
easier to copy/paste and reason about
I omitted ... from a Web dev perspective ๐Ÿ˜… ... PyDom supposes to fix also quirks and gotchas, so it's really up to you or at least until PyDom is declared fully functional. We do value early feedbacks too so ... I didn't want to discourage you from using PyDom, it's just out of your example I couldn't provide an esier answer to your issue.
Agree with what Andrea said here. It's up to you to evaluate if pydom is right for you or it's missing something (or maybe you really need something different). If anything, I'd love to hear about what it's missing (from your point of view) and what is good about it, so we can know more about what the community needs.... ltk is really nice indeed! If you are looking for a UI toolkit it might be a good option. It has different goals than Pydom though. While Pydom focus on DOM access and manipulation, ltk focuses on UI generation.
Avatar
Perfect ! Thank you very much. I'm starting a small micropython editor project. I'm using the WebUSB API to access the board's REPL. And I'd like to pass data from the micropython REPL ("hardware") to the pyscript REPL. To get started I'd like as much advice as possible, thank you
Avatar
Avatar
pat_pat
Perfect ! Thank you very much. I'm starting a small micropython editor project. I'm using the WebUSB API to access the board's REPL. And I'd like to pass data from the micropython REPL ("hardware") to the pyscript REPL. To get started I'd like as much advice as possible, thank you
you can probably do the same as mpremote/ampy they use repl
20:35
but really after a while it can get quite slow even with zlib compression if you can use websocket+wifi instead do not hesitate (edited)
20:36
also websocket offer broader compatibility and your project could benefit a lot of boards (also does not need webusb enabled) (edited)
Avatar
christy heaton 13/01/2024 01:51
I made an app that generates a map based on user input. I use the display() method to display the generated map. I want to refresh the display if the user changes the parameters. I tried just using display() again to show a new map, but any additional calls to display() continue adding more maps to the bottom of the page. Is there a way to clear the display so that only the latest call to display() is shown? Here is my app: https://pyscript.com/@cheaton/hike-map-interactive/latest
Avatar
Avatar
christy heaton
I made an app that generates a map based on user input. I use the display() method to display the generated map. I want to refresh the display if the user changes the parameters. I tried just using display() again to show a new map, but any additional calls to display() continue adding more maps to the bottom of the page. Is there a way to clear the display so that only the latest call to display() is shown? Here is my app: https://pyscript.com/@cheaton/hike-map-interactive/latest
Jeff Glass 13/01/2024 02:20
display() takes an optional Boolean argument called append which defaults to True. If you pass append=False, the target location is cleared of old content before the new content is displayed
Avatar
Avatar
Jeff Glass
display() takes an optional Boolean argument called append which defaults to True. If you pass append=False, the target location is cleared of old content before the new content is displayed
christy heaton 13/01/2024 02:29
That does it! Thank you @Jeff Glass!!
๐Ÿฅณ 1
Avatar
Avatar
pat_pat
Perfect ! Thank you very much. I'm starting a small micropython editor project. I'm using the WebUSB API to access the board's REPL. And I'd like to pass data from the micropython REPL ("hardware") to the pyscript REPL. To get started I'd like as much advice as possible, thank you
Andrea Giammarchi 13/01/2024 12:32
a proper REPL in PyScript for MicroPython is coming out soon but we're not there yet as we are missing 2 primitives in the current WASM port. Likely not the answer you were hoping for, but the gist is: "stay tuned to make that happen!"
Avatar
Avatar
Andrea Giammarchi
a proper REPL in PyScript for MicroPython is coming out soon but we're not there yet as we are missing 2 primitives in the current WASM port. Likely not the answer you were hoping for, but the gist is: "stay tuned to make that happen!"
Thanks @pmp-p for the advice, websocket would be nice but in a school room I can't imagine 15 pc with 15 RPi pico in wifi. That would be a problem. Many boards don't have wifi. To give an example: I plan to read the ADCs (analog digital concerter) on my board and display the data in bokeh graphics (python graphics library in pyodide). But I'm looking for the most efficient way to pass my data (from my card's REPL) to pyscript's REPL or simply to pyscript. What would be the best way to link the 2 micropython interpreters? (edited)
Avatar
Avatar
pat_pat
Thanks @pmp-p for the advice, websocket would be nice but in a school room I can't imagine 15 pc with 15 RPi pico in wifi. That would be a problem. Many boards don't have wifi. To give an example: I plan to read the ADCs (analog digital concerter) on my board and display the data in bokeh graphics (python graphics library in pyodide). But I'm looking for the most efficient way to pass my data (from my card's REPL) to pyscript's REPL or simply to pyscript. What would be the best way to link the 2 micropython interpreters? (edited)
i'd use some json-rpc
Avatar
Avatar
pat_pat
Thanks @pmp-p for the advice, websocket would be nice but in a school room I can't imagine 15 pc with 15 RPi pico in wifi. That would be a problem. Many boards don't have wifi. To give an example: I plan to read the ADCs (analog digital concerter) on my board and display the data in bokeh graphics (python graphics library in pyodide). But I'm looking for the most efficient way to pass my data (from my card's REPL) to pyscript's REPL or simply to pyscript. What would be the best way to link the 2 micropython interpreters? (edited)
Andrea Giammarchi 13/01/2024 21:21
15 WS connections are no issues on older PIs neither (v2 or v3) ... I once had a talk with everyone able in the audience to connect to my PI (v1 at that time) and trigger actions on it able to reflect on the screen ... 15 wasn't the bar and today, with any v3+ PI I think you can handle much more. Not a proven data point (techincally speaking) but a class up to 20 (or more) should be fine, imho, and after my previous experience with WS there.
Avatar
Avatar
Andrea Giammarchi
15 WS connections are no issues on older PIs neither (v2 or v3) ... I once had a talk with everyone able in the audience to connect to my PI (v1 at that time) and trigger actions on it able to reflect on the screen ... 15 wasn't the bar and today, with any v3+ PI I think you can handle much more. Not a proven data point (techincally speaking) but a class up to 20 (or more) should be fine, imho, and after my previous experience with WS there.
I meant that each student will have his board connected to his PC via USB. I don't want a PC to drive the neighbor's board.
Avatar
Avatar
pat_pat
I meant that each student will have his board connected to his PC via USB. I don't want a PC to drive the neighbor's board.
Andrea Giammarchi 13/01/2024 21:34
I see ... then each board could have its own web-server/socket but yeah, if it's USB connected, feel free to ignore anything I'v esaid as it's clear your use case is different and apologies for my misreading that.
Avatar
Avatar
Andrea Giammarchi
I see ... then each board could have its own web-server/socket but yeah, if it's USB connected, feel free to ignore anything I'v esaid as it's clear your use case is different and apologies for my misreading that.
No problem. Anyway, no PC has a wifi card. They're all connected via ethernet. Thanks
Avatar
Avatar
pat_pat
No problem. Anyway, no PC has a wifi card. They're all connected via ethernet. Thanks
ideally use a rpc like firmata/telemetrix, i'd go for telemetrix because it would have also native wifi support for free when board have it. I already tested telemetrix from cpython wasm web page toward with an esp8266 driven robot arm (both servos and stepper motors).
Avatar
hey guys, when I use the worker attribute and theres an error in my code, the terminal doesn't show any error, I can only see if I open up the console. When I don't use worker the terminal shows the error (2nd image). Is there a way to have the error show when using worker?
Avatar
Avatar
j_d
hey guys, when I use the worker attribute and theres an error in my code, the terminal doesn't show any error, I can only see if I open up the console. When I don't use worker the terminal shows the error (2nd image). Is there a way to have the error show when using worker?
Jeff Glass 14/01/2024 17:36
I don't think it'll make a difference, but try updating the link to version 2024.1.1 to start
Avatar
yeah no difference
Avatar
Avatar
j_d
yeah no difference
Jeff Glass 14/01/2024 19:27
I think this is a bug... want to file an issue on GitHub?
๐Ÿ‘ 1
Avatar
are there any docs on how i can access the xtermjs object that gets created via the terminal tag. i want to attach a websocket to it
Avatar
Avatar
hosler
are there any docs on how i can access the xtermjs object that gets created via the terminal tag. i want to attach a websocket to it
Jeff Glass 15/01/2024 15:30
As of 2024.1.1, it's the terminal attribute of the script tag itself. See here for info and examples: https://jeff.glass/post/whats-new-pyscript-2024-1-1/#xterm
Avatar
thanks
Avatar
does it have a deconstructor when it gets removed from the dom?
Avatar
also how can i activate it after adding it to the dom with js
Avatar
Avatar
j_d
hey guys, when I use the worker attribute and theres an error in my code, the terminal doesn't show any error, I can only see if I open up the console. When I don't use worker the terminal shows the error (2nd image). Is there a way to have the error show when using worker?
Andrea Giammarchi 15/01/2024 19:20
the main and worker terminal initialization has now been normalized ... it's very possible you don't see the error neither in the main or the worker (npm latest package, not official release channel). If that's the case, I need to also patch the io.stderr or normalize it for both main and worker but an example code, instead of a screenshot, might help way more ... the filed issue has a screenshot I can't even access (for some reason) so I can't read how to reproduce the issue. If by chance you can help out with the most basic thing that fails to you, I'd be more than happy to fix it ASAP so next release will have your back covered. (edited)
Avatar
@Andrea Giammarchi you replied to my Q and A a week or two ago... wondering if you can help me through this probably pretty simple issue (i'm pretty new to python)... just trying to read txt files and write to them. not understanding how it works or where to read documentation on it. you suggested the interpreter.writeFile i believe
Avatar
Avatar
Daunt
@Andrea Giammarchi you replied to my Q and A a week or two ago... wondering if you can help me through this probably pretty simple issue (i'm pretty new to python)... just trying to read txt files and write to them. not understanding how it works or where to read documentation on it. you suggested the interpreter.writeFile i believe
Jeff Glass 16/01/2024 16:26
When Python is running in the Browser (in Pyodide/Polyscript/PyScript) it interacts with 'virtual' filesystem that exists effemerally in-memory as long as the browser page is open. That is, if you do: with open('myfile.txt', 'w+') as f: f.write("Hello world") You can later open that file in the same program and read from it... but as soon as the page refreshes or is reloaded, that file will be lost. Similarly, that file system has no relation to what's on your hard-drive. You as the developer can pre-load files of any kind which are reachable at a URL into the virtual filesystem, using the files key in <py-config>:https://pyscript.github.io/docs/2024.1.1/user-guide/configuration/#files. If you want your users to be able to upload/download files, see the upload and download recipes here: https://pyscript.recipes/2024.1.1/basic/file-upload/
Avatar
yes i want to preload files into the virtual filesystem. i will try that thanks for pointing me to that!
๐ŸŽ‰ 1
16:32
are there any SIMPLE ways to write basic text to a file that will not be lost? i'm trying to keep it simple (stupid) but might be more to it then i realize
Avatar
Jeff Glass 16/01/2024 17:04
Here's a really simple example using localStorage - you can kind of think of it like "cookies" in your browser, but for bits of data. It's not really meant for storing whole documents, but it could be used that way. The key ideas are setItem() and getItem(): <textarea rows="4" cols="60" name="Textbox" id="text"></textarea> <button py-click="store">Store Doc</button> <script type="py"> from pyscript import window, document, display ls = window.localStorage def store(evt): text = document.getElementById("text").value ls.setItem("doc", text) refresh_display() def refresh_display(): if doc:= ls.getItem("doc"): display(f'The stored document contents is: "{doc}"') else: display("No document stored") refresh_display() </script>
17:04
Play around with it - even if you close and re-open the page, the data is persistent in local storage. Lots more info about localStorage is here: https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage
Avatar
i meant moreso on the server side. like as a very simplified example, how i could write to somewhere to keep track of how many times my script was run, if i had a basic script that just opened a txt file from a URL, read the int that was stored there, incremented it and saved it somewhere. if that makes sense?
Avatar
Jeff Glass 16/01/2024 17:25
I'm not sure I understand, sorry... PyScript is running on the client-side - you want to track how many times the client-side script was run, but track it on the server side?
Avatar
it was a simplified example, but yea- or save a new .txt file somewhere NOT on the client side. probably doesn't make sense with how pyscript is but just asking the question. sorry i'm a new to this
18:39
i can't even seem to get a basic version of the read working using TOML. what am i doing wrong??
Avatar
Avatar
Daunt
i can't even seem to get a basic version of the read working using TOML. what am i doing wrong??
Jeff Glass 16/01/2024 19:30
When I go to that link (speakvisually.netโ€ฆ) directly, I get a 404 Not Found error. Is there maybe a typo or error in the URL?
Avatar
fixed. should've been Aircraft
19:32
but still not working
Avatar
Jeff Glass 16/01/2024 19:34
Sorry on mobile, but try adding from pyscript import display and change your call to print to display(โ€ฆ) (edited)
Avatar
hm. not seeing any difference
Avatar
Jeff Glass 16/01/2024 19:40
Hmmmmm
19:41
If you open up the developer console, are there any errors?
Avatar
ah goo dpoint forgot to look there
19:47
Access to fetch at 'https://www.speakvisually.net/extra/aircraft.py' from origin 'https://dauntlessk.pyscriptapps.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
Avatar
Jeff Glass 16/01/2024 19:55
Ahhhhh CORSโ€ฆ. Our favorite issue
19:55
This is actually probably worth raising an issue on GH over - errors like that would like to be displayed visually in the page, I would think, and I think are supposed to
Avatar
Avatar
Daunt
Access to fetch at 'https://www.speakvisually.net/extra/aircraft.py' from origin 'https://dauntlessk.pyscriptapps.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
Jeff Glass 16/01/2024 19:59
But in your case - that server isnโ€™t allowing arbitrary access to that file - Cross Origin Resource Sharing isnโ€™t permitted by that server for that resource, so another site canโ€™t just fetch it programmatically
19:59
Not something we can work around, other than changing permissions for that server, but we CAN fix the fact that the error was hidden in a dev console
Avatar
i will submit that- i am going to try using a different host and see if that works
๐Ÿ‘ 1
Avatar
Jeff Glass 16/01/2024 20:02
I know raw access to files in GitHub public repos generally works, if thatโ€™s an option https://github.com/orgs/community/discussions/22537
I am searching google for about 3 hours and stil not succeed in this. I do data analysis in Python, and iโ€™ve seen a lot of tymes that people unstead of downloading file from hard drive just paste r...
Avatar
oh that would probably be even easier lol nice thank you
Avatar
Avatar
Daunt
oh that would probably be even easier lol nice thank you
Jeff Glass 16/01/2024 20:57
One last thing - if you update your project to pyscript 2024.1.1, does anything change? Thatโ€™s the most recent open source release - the default version on PyScript.com tends to lag by a few weeks
Avatar
i was able to get it to work using github. my other server did not work unfortunately and i didn't care to mess with the CORS settings. was chasing down another weird issue now lol
๐Ÿ‘ 1
Avatar
thanks for your help!
Avatar
Avatar
Daunt
are there any SIMPLE ways to write basic text to a file that will not be lost? i'm trying to keep it simple (stupid) but might be more to it then i realize
Andrea Giammarchi 17/01/2024 11:46
not lost as in ... still in your browser cache? ... or not lost as in saved in some server anyone can reach after from anywhere? for the former, I have this module that can be used with IndexedDB hence persist on your browser only. https://github.com/WebReflection/fs
Emscripten FS, PATH and PATH_FS utilities as a module - GitHub - WebReflection/fs: Emscripten FS, PATH and PATH_FS utilities as a module
11:47
you can see this page source code to have an idea how it works https://webreflection.github.io/fs/test/
Avatar
is pyscript code supposed to evaluate when added to the dom via js event
Avatar
Avatar
hosler
is pyscript code supposed to evaluate when added to the dom via js event
Jeff Glass 18/01/2024 03:14
It should - if you add a <script type=โ€œpyโ€> or <py-script> tag to the DOM it should evaluate
03:14
Is it not?
10:31
Please help man I've been stuck with this stupid error for the last 2 days
Avatar
I've tried making my app run in mpy after getting it working in pyodide. There seems to be a difference when starting up. I'm using this startup screen while loading but the py:ready event does not fire in mpy. It does in pyodide. What is the correct solution ? <script type="module"> const loading = document.getElementById('loading'); addEventListener('py:ready', () => loading.close()); loading.showModal(); </script>
Avatar
Avatar
Neon22
I've tried making my app run in mpy after getting it working in pyodide. There seems to be a difference when starting up. I'm using this startup screen while loading but the py:ready event does not fire in mpy. It does in pyodide. What is the correct solution ? <script type="module"> const loading = document.getElementById('loading'); addEventListener('py:ready', () => loading.close()); loading.showModal(); </script>
Try 'mpy:ready' in stead of 'py:ready'
๐Ÿ‘ 1
Avatar
Avatar
Parviz O
Try 'mpy:ready' in stead of 'py:ready'
Ah. Thanks - so simple.
Avatar
Avatar
Neon22
I've tried making my app run in mpy after getting it working in pyodide. There seems to be a difference when starting up. I'm using this startup screen while loading but the py:ready event does not fire in mpy. It does in pyodide. What is the correct solution ? <script type="module"> const loading = document.getElementById('loading'); addEventListener('py:ready', () => loading.close()); loading.showModal(); </script>
anfroholic 19/01/2024 04:28
I have recently moved up to the latest version of pyscript and miss the old loading animation. Would you share a little more of your code so I may have that back again. Thanks
Avatar
Avatar
anfroholic
I have recently moved up to the latest version of pyscript and miss the old loading animation. Would you share a little more of your code so I may have that back again. Thanks
I got it from this list somewhere - maybe @Jeff Glass Put this in your index.html file in the Head section before the pyscript tags: <style> #loading { outline: none; border: none; background: transparent } </style> <script type="module"> const loading = document.getElementById('loading'); addEventListener('py:ready', () => loading.close()); loading.showModal(); </script> Then in the Body put this at the top <dialog id="loading"> <h1>Building Blah blah blah.</h1> <h3>Assembling bits and pieces...</h3> <h3>Almost there...</h3> </dialog> Just before your <script type="py" src=... I think it could do with a little bit of endless progress bar as well but that's more guff. Maybe use animation css woudl be nice.
10:36
If you add a layer of refinement please post it back here. Thanks...
Avatar
anfroholic 19/01/2024 10:37
This is so great! Thank you!!
Avatar
Avatar
Neon22
If you add a layer of refinement please post it back here. Thanks...
anfroholic 19/01/2024 10:37
Will do. Thank you
Avatar
Avatar
Neon22
If you add a layer of refinement please post it back here. Thanks...
anfroholic 19/01/2024 10:49
Just playing around a bit. but it's good enough for now head <style> .loading-animation { border: 8px solid #068770; border-top: 8px solid #f0f0f0; border-radius: 50%; width: 50px; height: 50px; margin: auto; animation: spin 1s linear infinite; } #loading { justify-content: center; background-color: #304050; border-radius: 10px; } @keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } } </style> <script type="module"> const loading = document.getElementById('loading'); addEventListener('py:ready', () => loading.close()); loading.showModal(); </script> body <dialog id="loading"> <div class="loading-animation"></div> <h1>Building Blah blah blah.</h1> <h3>Assembling bits and pieces...</h3> <h3>Almost there...</h3> </dialog> Thanks again! **small edit (edited)
Avatar
Avatar
anfroholic
Just playing around a bit. but it's good enough for now head <style> .loading-animation { border: 8px solid #068770; border-top: 8px solid #f0f0f0; border-radius: 50%; width: 50px; height: 50px; margin: auto; animation: spin 1s linear infinite; } #loading { justify-content: center; background-color: #304050; border-radius: 10px; } @keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } } </style> <script type="module"> const loading = document.getElementById('loading'); addEventListener('py:ready', () => loading.close()); loading.showModal(); </script> body <dialog id="loading"> <div class="loading-animation"></div> <h1>Building Blah blah blah.</h1> <h3>Assembling bits and pieces...</h3> <h3>Almost there...</h3> </dialog> Thanks again! **small edit (edited)
I further modified it with: .loadslide {animation: slide 9s ease-in; } @keyframes slide { from { transform: translateX(0%); } to { transform: translateX(40%); } } and added the class to: <h3 class="loadslide">Almost there...</h3>
Avatar
Avatar
Neon22
I further modified it with: .loadslide {animation: slide 9s ease-in; } @keyframes slide { from { transform: translateX(0%); } to { transform: translateX(40%); } } and added the class to: <h3 class="loadslide">Almost there...</h3>
anfroholic 19/01/2024 11:29
haha that's great! Thanks
Avatar
Mars Industrial 19/01/2024 14:30
I'm writing a game engine for a text based RPG, and I'd like some feedback on the packaging method I'm thinking about
14:31
I want to package the project and upload it to pypi, and I want to make it easy for people to install the project using pip, and the get the pyscript game working locally.
14:33
I think the project will be organized like this:
  • src/frontend:
    • index.html The UI HTML
    • main.py. The game loader + interface adapter
    • src/engine/...: The game engine logic
    • /tests/...: frontend and engine tests
    • /pyproject.toml: project build and tool config
  • /pyscript.toml: pyscript config
14:35
Entry Points will be defined in Pyproject.toml and will include: build: Fetch the contents of the engine and frontend, and put them together in the /dist folder, ready for upload to web server, or live serving live: Run a live server and rebuild project on changes
Avatar
Avatar
Mars Industrial
I think the project will be organized like this:
  • src/frontend:
    • index.html The UI HTML
    • main.py. The game loader + interface adapter
    • src/engine/...: The game engine logic
    • /tests/...: frontend and engine tests
    • /pyproject.toml: project build and tool config
  • /pyscript.toml: pyscript config
Jeff Glass 19/01/2024 14:37
Sounds like a neat project! When you say you want to get the PyScript game working locally... do you mean without internet? In that case, you'd need to vendor PyScript and all its dependencies, which may not be desirable.
Avatar
Mars Industrial 19/01/2024 14:42
That's a good think to keep in mind. When the user does pip install mygame and mygame-build, that deploys to dist and starts a live server. I think by default this uses html that loads pysrcript's core.js over the network. I guess it users ask for it, we can add an option mygame-build vendor that streamlines the vedor code into dist.
Avatar
Mars Industrial 19/01/2024 14:52
@Jeff Glass does that seem like a reasonable way to separate different source packages, and still deploy them to a coherent, hostable form? One advantage of the layout is that I can do: pip install . && pytest This will install the packages into the environment, and let me access them from from the tests package. I think I will need a little extra code to do E2E tests with the HTML, but I think selenium has me covered
14:53
The biggest uncertainty I have is: do I need to build to a dist folder? (edited)
14:54
Idea: I could just live-serve the index.html, main.py, and my engine package, right there from the src directory! Heck yeah!
Avatar
Jeff Glass 19/01/2024 14:57
You sure can! Only little thing is it might make sense to more pyscript.toml into the src directory, since that's pretty closely related to index.html etc
Avatar
chris.laffra 20/01/2024 11:31
MicroPython starts up so quickly, I doubt you actually want to show a loading screen...
Avatar
Alas it still takes 8 seconds for mine to open. It could be how far away I am from any servers and how slow my internet connection is.
Avatar
Avatar
chris.laffra
MicroPython starts up so quickly, I doubt you actually want to show a loading screen...
anfroholic 21/01/2024 01:59
I would definitely love to use MicroPython instead, but the codebase I'm using is the same code I'm running on an ESP32 and it's uses the asyncio package which does not seem to be supported yet on the MicroPython port just yet.
Avatar
Avatar
Neon22
Alas it still takes 8 seconds for mine to open. It could be how far away I am from any servers and how slow my internet connection is.
chris.laffra 21/01/2024 08:25
@anfroholic I am assuming you load from pyscript.com? You can analyze the load time of your app using Chrome Devtools. Here is the pattern I see for the minimal LTK app hosted on pyscript.com, which can be found at https://laffra.pyscriptapps.com/ltk-on-micropython/latest/. You can see long waits for latest/, pyscript.toml, and main.py. The browser does not cache those files, as they change constantly on the server while you edit them. In contrast, when I load LTK Kitchensink, https://laffra.github.io/ltk/, hosted at github.io, you can see the artifacts are returned by the GitHub web server with caching instructions. This makes them fast to load, as they all come out of the browser's disk cache or memory cache now. Try hosting your code on a production server somewhere to find the actual performance characteristics. With an empty browser cache, LTK takes around 1.4s to load from Amsterdam. Warm cache loads take around 0.3s with Devtools open. With Devtools closed, warm load times go to <0.2s for the entire app to show. When you load https://laffra.github.io/ltk/, what numbers do you see for the first load time and successive load times for refresh of the page?
Avatar
anfroholic 21/01/2024 08:31
refreshes on your page are about 5 seconds
08:32
But like I said before, it doesn't seem like asyncio is available on micropython as of yet. If I'm wrong please correct me, but until it's available I will just stick with Pyodide. It's not horrible
08:34
But side note, thanks for everything you're doing with your LTK library, I haven't used any of it and am not sure if it's applicable to what I'm doing, but is very cool. Especially PySheets, looking forward to whatever it is you're doing there
Avatar
chris.laffra 21/01/2024 08:37
@anfroholic PySheets runs on MicroPython, consists of numerous modules, loads data from a server and then renders the entire UI. I host it on Google AppEngine and the entire UI loads within 1.3s (from Amsterdam). Inside the sheet, you can run any arbitrary Python code. Things like Pandas don't work on MicroPython, so I use a worker using PyOdide. That last one takes around 5-6s to load all of pandas, matplotlib, etc.
Avatar
anfroholic 21/01/2024 08:38
I don't know exatly everyting I'm supposed to be looking at, but I think basically all the startup delay is just the time it takes to spin up Pyodide
Avatar
Avatar
chris.laffra
@anfroholic PySheets runs on MicroPython, consists of numerous modules, loads data from a server and then renders the entire UI. I host it on Google AppEngine and the entire UI loads within 1.3s (from Amsterdam). Inside the sheet, you can run any arbitrary Python code. Things like Pandas don't work on MicroPython, so I use a worker using PyOdide. That last one takes around 5-6s to load all of pandas, matplotlib, etc.
anfroholic 21/01/2024 08:40
Yeah, I've looked at your excel-in-python repo and played with it for a bit. Seems real neat!
08:40
oops forgot to attach pic from last post
08:46
UI loads within 1.3s (from Amsterdam). Inside the sheet, you can run any arbitrary Python code. Things like Pandas don't work on MicroPython, so I use a worker using PyOdide. That last one takes around 5-6s to load all of pandas, matplotlib, etc.
I read your comment again, and I see what you're saying now about having micropython running the ui and piodide running as a worker in the background. I'm pretty new to playing with this and haven't even breached the part about learning about workers, starting them and everything. If there's a good resource you could point me towards, that would be greatly appreciated.
Avatar
chris.laffra 21/01/2024 08:52
Go watch the videos of @Jeff Glass - they are absolutely awesome. Here is one where he talks about loading a worker: https://youtu.be/Um2oqjpYk8A?t=782
Avatar
anfroholic 21/01/2024 09:10
Thanks, and yeah, I've had the honor of meeting Jeff and was at the event where he talked about Pyscript https://www.youtube.com/watch?v=b8TpDutiduE I'll watch this one now.
Avatar
Jeff Glass 21/01/2024 15:58
@chris.laffra @anfroholic youโ€™re both too kind! ๐Ÿ‘
Avatar
ํฌ๋กœ 22/01/2024 04:39
Can't I put the pyscript auto-completion feature in the vsc? please help me. sorry, I don't speak english well
Avatar
Hi I always get this error:
18:07
Avatar
Avatar
Oasis4.0
Hi I always get this error:
try this "packages"= ["pymysql","spotipy","cgi-tools","ssl"] in spotipy you have spotipy.outh2 and for cgi you need cgi-tools package.
Avatar
I think I asked this before but can't find the answer - how can I permanently hide the terminal in PyScript? I don't want any printed warnings or messages visible to my users. Especially when I can't control what warnings third party libraries show and ignoring the warnings doesn't seem to work.
Avatar
Avatar
Owen
I think I asked this before but can't find the answer - how can I permanently hide the terminal in PyScript? I don't want any printed warnings or messages visible to my users. Especially when I can't control what warnings third party libraries show and ignoring the warnings doesn't seem to work.
What version of PyScript are you using. If you update to latest, warnings and messages go to console.log not a terminal window in the DOM.
Avatar
Avatar
ntoll
What version of PyScript are you using. If you update to latest, warnings and messages go to console.log not a terminal window in the DOM.
I was on 2023.05.1 - I did try updating to 2024.1.1 and it didn't seem to make a difference. Either made some naive mistake like forgetting to wipe a browser cache... or I'm not sure. This is a Panel app, not sure if it could be creating a terminal automatically - but it is tagged something like py-terminal which made me assume it was PyScript. I'll try upgrading again
Avatar
Avatar
ntoll
What version of PyScript are you using. If you update to latest, warnings and messages go to console.log not a terminal window in the DOM.
checking Jeff's blog, I see a lot have changed since 2023.05.1... I probably need to update more than what I did
Avatar
Is it possible to direct the py-editor output to a terminal?
Avatar
Is there a way to send anything to worker? I want to call python functions in worker thread based on user interaction (say button click). Since I am using react, i don't think i can use when decorator. I see worker sync, but looks like that is only worker to main and not vice versa. I see postMessage and onmessage, but I am not proficient enough in web tech to know if that works.
Avatar
New Question! Is there a way to get access python you create yourself in the new py-editor? For example: if I wrote a separate .py file and want to access by using import how would I be able to do this. All of the necessaary dependencies worked in the version where there was a py-repl.
Avatar
Avatar
sai
Is there a way to send anything to worker? I want to call python functions in worker thread based on user interaction (say button click). Since I am using react, i don't think i can use when decorator. I see worker sync, but looks like that is only worker to main and not vice versa. I see postMessage and onmessage, but I am not proficient enough in web tech to know if that works.
Andrea Giammarchi 25/01/2024 11:24
sync works dual way ... you expose a xworker.sync.method = () => {}; in main and you can call sync.method() in workers and vice-versa.
๐Ÿ‘ 1
11:25
you can expose a sync.method = () => {} in workers and in main, you can call that as xworker.sync.method() which will be inevitably async. From the worker though, all callbacks via sync are ... well, ... sync
11:25
to grab the xworker reference on main query the DOM to find your pyscript element and it's attached in there.
11:32
that being said, if you want to add a listener to an element from the worker you can via DOM API
11:32
so you don't need sync for that.
Avatar
Avatar
Eric
New Question! Is there a way to get access python you create yourself in the new py-editor? For example: if I wrote a separate .py file and want to access by using import how would I be able to do this. All of the necessaary dependencies worked in the version where there was a py-repl.
Andrea Giammarchi 25/01/2024 11:33
if the file is in the config you pass around it gets embedded in the virtual FS and you can import it
Avatar
Avatar
Andrea Giammarchi
if the file is in the config you pass around it gets embedded in the virtual FS and you can import it
So if the file is in the .toml like so terminal = true docked = true packages = [ "opencv-python", "numpy", "pillow", "matplotlib",] [[fetch]] files = ['widgets.py', 'UITracker.py',"./camera.py","uiInterface.py"] then if i would want to import uiInterface in the py-editor I could do this: from uiInterface import getButton, getLed, getCanvas, getCamera, getImageFrame
Avatar
Avatar
Eric
So if the file is in the .toml like so terminal = true docked = true packages = [ "opencv-python", "numpy", "pillow", "matplotlib",] [[fetch]] files = ['widgets.py', 'UITracker.py',"./camera.py","uiInterface.py"] then if i would want to import uiInterface in the py-editor I could do this: from uiInterface import getButton, getLed, getCanvas, getCamera, getImageFrame
Andrea Giammarchi 25/01/2024 15:13
I've no idea what's in those files but my answer is basically yes
Avatar
I didn't know you can specify terminal in the TOML.
16:08
Oh. Not the pyscript one. Nvm.
Avatar
Andrea Giammarchi 25/01/2024 17:08
terminal in PyScript is a <script type="py" terminal> away and it gets super powers with <script type="py" terminal worker> as in there you can also import code and then code.interact() to have a whole, non-blocking, REPL (edited)
Avatar
Avatar
Andrea Giammarchi
I've no idea what's in those files but my answer is basically yes
Ok Cool. I get getting this error when I try to import the the method from uiInterface. Error: Traceback (most recent call last): File "/lib/python311.zip/_pyodide/_base.py", line 571, in eval_code_async await CodeRunner( File "/lib/python311.zip/_pyodide/_base.py", line 394, in run_async coroutine = eval(self.code, globals, locals) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "", line 1, in ModuleNotFoundError: No module named 'uiInterface' Am I not setting up the .toml file right?
Avatar
Avatar
Eric
Ok Cool. I get getting this error when I try to import the the method from uiInterface. Error: Traceback (most recent call last): File "/lib/python311.zip/_pyodide/_base.py", line 571, in eval_code_async await CodeRunner( File "/lib/python311.zip/_pyodide/_base.py", line 394, in run_async coroutine = eval(self.code, globals, locals) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "", line 1, in ModuleNotFoundError: No module named 'uiInterface' Am I not setting up the .toml file right?
Andrea Giammarchi 25/01/2024 17:23
that's possible, but it's all wild guessing without a full example ... can you drop your example in pyscript.com maybe?
Avatar
Avatar
Andrea Giammarchi
that's possible, but it's all wild guessing without a full example ... can you drop your example in pyscript.com maybe?
Yes I can do that
๐Ÿ‘ 1
Avatar
Avatar
Andrea Giammarchi
that's possible, but it's all wild guessing without a full example ... can you drop your example in pyscript.com maybe?
Here is the link: https://ericmrohloff.pyscriptapps.com/widgets/latest/ The css is off because I am coding mostly in VScode, but I can also send the github if that is more helpful.
Avatar
Avatar
Eric
Here is the link: https://ericmrohloff.pyscriptapps.com/widgets/latest/ The css is off because I am coding mostly in VScode, but I can also send the github if that is more helpful.
Andrea Giammarchi 25/01/2024 20:12
so ... I don't see any error about your code in there?
20:14
the only relevant error at some point is: Uncaught PythonError: Traceback (most recent call last): File "<exec>", line 60, in test File "/home/pyodide/UITracker.py", line 47, in loadData s.savedData[widgetClass.__name__])) ~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^ TypeError: 'NoneType' object is not subscriptable
20:14
meaning UITracker.py is there and loaded, as expected ... how can I help here?
Avatar
Avatar
Eric
Here is the link: https://ericmrohloff.pyscriptapps.com/widgets/latest/ The css is off because I am coding mostly in VScode, but I can also send the github if that is more helpful.
Here is a simple code.May be you can connect with this code and make changes in yours.https://_12.pyscriptapps.com/sweet-breeze/latest/
Avatar
Hi, anyway to turn off pyscript log messages in the console when a page with pyscript is loading? They are super helpful during development, but I don't want then once i deploy.
Avatar
Avatar
sai
Hi, anyway to turn off pyscript log messages in the console when a page with pyscript is loading? They are super helpful during development, but I don't want then once i deploy.
Jeff Glass 26/01/2024 20:20
What version of PyScript are you running?
Avatar
Avatar
Jeff Glass
What version of PyScript are you running?
https://pyscript.net/releases/2024.1.1/core.js
Avatar
Jeff Glass 26/01/2024 20:27
So thereโ€™s not a way currentlyโ€ฆ but itโ€™s a feature request weโ€™d had off and on for a while now. Would you mind filing an issue, or re-upping an existing one?
๐Ÿ‘ 1
Avatar
hmm.. actually reading through an issue on github and then looking back at the console, those logs seem to be coming from pyodide.asm.js or micropython.mjs So if I am not wrong, those are not in pyscript control right?
20:34
Avatar
Avatar
sai
hmm.. actually reading through an issue on github and then looking back at the console, those logs seem to be coming from pyodide.asm.js or micropython.mjs So if I am not wrong, those are not in pyscript control right?
Andrea Giammarchi 26/01/2024 21:09
those are Pyodide logs ... nothing we can do about, correct!
Avatar
Avatar
zobi
Here is a simple code.May be you can connect with this code and make changes in yours.https://_12.pyscriptapps.com/sweet-breeze/latest/
This is more towards what I was aiming for but the user could imput in the py-editor
Avatar
Avatar
Eric
This is more towards what I was aiming for but the user could imput in the py-editor
Exactly what is it going to do After taking inputs from py-editor?
Avatar
So iIm just getting into PyScript, the functions here work fine by themselves, buy when I put them into the .html and go to the website, errors show up. How can I fix this?
Avatar
Avatar
Carsoneo
So iIm just getting into PyScript, the functions here work fine by themselves, buy when I put them into the .html and go to the website, errors show up. How can I fix this?
chris.laffra 27/01/2024 22:03
Simplest would be to visit pyscript.com, create a new project there and see what kind of code that IDE generates for you.
Avatar
Avatar
Carsoneo
So iIm just getting into PyScript, the functions here work fine by themselves, buy when I put them into the .html and go to the website, errors show up. How can I fix this?
@chris.laffra is right. I am not an expert on either python or pyscript, but, I copied your code to pyscript.com and made I it to work. Look at: https://pyscript.com/@parvizo/temp-snippet-for-carsone/ (edited)
Avatar
Avatar
zobi
Exactly what is it going to do After taking inputs from py-editor?
Essentially the problem I am having is being unable to import in the py-editor
19:45
So having a file i created and the user can call methods for it
19:49
In the py-editor
Avatar
Jeff Glass 28/01/2024 19:59
We essentially have three related-but-different-feeling ways of running code (in script tags, via users entering it into a terminal, and the py-editor), and we'd like to have a way to stitch those all together. It's not necessarily straightforward - any examples and things you've run into would be really helpful part of the conversation! This is the gist of the discussion that's happening over in the GitHub Discussions, and similar to this discussion.
Jotting these ideas down after @WebReflection and I had a chat where such ideas were proposed and initially explored. Our conclusion: ask folks for feedback for future refinement or rejection of th...
๐Ÿ‘ 1
Avatar
Is it possibly to load the webpage in an enviornment that is not the main thread?
Avatar
Jeff Glass 28/01/2024 20:15
How do you mean? Like run the Python code not in the main thread?
Avatar
Yeah like essentially have it so the py-editor and main thread are part of the same enviornment
20:32
For example, I can specify that the py-editor is in env="first", but how do I allow the thread to draw from methods in the .toml file
Avatar
Jeff Glass 28/01/2024 20:34
Two things - py-editors are a worker-only feature, and unfortunately there's not currently a way to share code with a <script type="py"> tag. Hence that discussion. It's a much-wanted feature and something that was present in Classic when we were main-thread-only, but things have gotten more complicated now that workers are in the mix
Avatar
Ok thanks for the clarification, is there any work around using the terminal?
Avatar
Hi, I am seeing a weird issue around importing pandas and using document.getElementById. If i have import pandas as pd in my python code, then pyscript.document.getElementById stops working. https://sai1494.pyscriptapps.com/getelementbyid-error-with-pandas/ I am able to reproduce this on pyscript.com In worker.py file, comment out the import pandas as pd line and the code works.
Avatar
Avatar
sai
Hi, I am seeing a weird issue around importing pandas and using document.getElementById. If i have import pandas as pd in my python code, then pyscript.document.getElementById stops working. https://sai1494.pyscriptapps.com/getelementbyid-error-with-pandas/ I am able to reproduce this on pyscript.com In worker.py file, comment out the import pandas as pd line and the code works.
Jeff Glass 28/01/2024 23:07
Huh! That is... odd
Avatar
Avatar
Jeff Glass
Huh! That is... odd
would this be a bug on pyscript or on pyodide?
Avatar
Avatar
sai
would this be a bug on pyscript or on pyodide?
Jeff Glass 29/01/2024 02:50
To be honest? I have no idea. Is it only only with a terminal and/a worker?
Avatar
Avatar
Jeff Glass
To be honest? I have no idea. Is it only only with a terminal and/a worker?
not related to terminal. I don't have terminal in my application and just tested it on pyscript.com as well. So here are some additional details based on some testing async + worker = error. pyscript.document.getElementById becomes None async only (no worker, so main) = works worker only (no async) = works no attributes = works So only time it fails is with both async and worker attributes. (edited)
Avatar
Avatar
sai
Hi, I am seeing a weird issue around importing pandas and using document.getElementById. If i have import pandas as pd in my python code, then pyscript.document.getElementById stops working. https://sai1494.pyscriptapps.com/getelementbyid-error-with-pandas/ I am able to reproduce this on pyscript.com In worker.py file, comment out the import pandas as pd line and the code works.
Andrea Giammarchi 29/01/2024 09:10
I couldn't see any error in there but here some wild guessing ... pandas traps js.document somehow internally before we manage to normalize that within our code or pandas changes something to document.getElementById ... as the issue happens only when you import pandas, here some extra check I would perform:
  • try to install via import micropip pandas within your code, not through the config ... see if in async + worker that works (you have top level await with async so you shouldn't change much your program)
  • try to bind getElementById before you import pandas ... getElementById = pyscript.document.getElementById.bind(pyscript.document) ... then use getElementById(...) directly instead
๐Ÿ‘ 1
09:10
if none of these workarounds solve anything please let me know so I can have a look, but as this happens only with pandas (if I understood correctly) it might be neither Pyodide nor PyScript issue, rather some pandas internal.
Avatar
Avatar
Andrea Giammarchi
I couldn't see any error in there but here some wild guessing ... pandas traps js.document somehow internally before we manage to normalize that within our code or pandas changes something to document.getElementById ... as the issue happens only when you import pandas, here some extra check I would perform:
  • try to install via import micropip pandas within your code, not through the config ... see if in async + worker that works (you have top level await with async so you shouldn't change much your program)
  • try to bind getElementById before you import pandas ... getElementById = pyscript.document.getElementById.bind(pyscript.document) ... then use getElementById(...) directly instead
both these workarounds work great independently!!
Avatar
Avatar
sai
both these workarounds work great independently!!
Andrea Giammarchi 29/01/2024 16:43
awesome ... this might be something to consider in PyDom too then as we never want 3rd party packages to break our code ... /cc @Fabio
16:44
btw, it's also very possible we are better off fixing the js.document reference before resolving packages ... I'll try to look at that so that async or not, we're good in workers.
Avatar
Avatar
sai
both these workarounds work great independently!!
Andrea Giammarchi 29/01/2024 19:13
I had a look at the code ... am I correct in assuming you were importing pandas before importying pyscript ? I believe we might have a chicken/egg issue there if pyscript is not the top-most imported script ... although in theory our standard lib should be there before anything else so I am curious to better understand the details of your issue, thanks!
Avatar
Avatar
Andrea Giammarchi
I had a look at the code ... am I correct in assuming you were importing pandas before importying pyscript ? I believe we might have a chicken/egg issue there if pyscript is not the top-most imported script ... although in theory our standard lib should be there before anything else so I am curious to better understand the details of your issue, thanks!
Here is how i initially had it. print("Hello from worker!") import pyscript import pandas as pd print(f"Type of getElementById is: {type(pyscript.document.getElementById)}") try: el = pyscript.document.getElementById("py-script-output") el.innerHTML = "test" except Exception as e: print(e) I had made some changes to the files to test out your suggestions.
19:18
print statement output is Type of getElementById is: <class 'NoneType'> (edited)
Avatar
Andrea Giammarchi 29/01/2024 19:19
and if you print that before importing pandas that's something that is not a NoneType ? ๐Ÿค”
Avatar
yes, Type of getElementById is: <class 'pyodide.ffi.JsProxy'>
Avatar
Andrea Giammarchi 29/01/2024 19:20
wow, that's messed up! ... only worker + async, correct?
19:21
it is something to do with pandas for sure but I just "don't care much" as I want to be sure our code actually doesn't fail, than you for the minimal example, it will help me a lot!
Avatar
oh man, weird things.. now that I have the print statement before importing pandas, things work properly.
19:21
I mean this code now works. I am able to set innerHTML print("Hello from worker!") import pyscript print(f"Type of getElementById is: {type(pyscript.document.getElementById)}") import pandas as pd try: el = pyscript.document.getElementById("py-script-output") el.innerHTML = "test" except Exception as e: print(e)
๐Ÿคฏ 1
Avatar
Andrea Giammarchi 29/01/2024 19:21
yeah, I start thinking I should import the document instead out of the box... maybe this is some lazy import thing that is not working as expected on async scenarios
19:22
basically if you do from pyscript import document everything should work in your code
19:23
(in that code, not yours in general)
Avatar
doesn't seem to work. Importing as from pyscript import document and then accessing getElementById after pandas doesn't work print("Hello from worker!") from pyscript import document import pandas as pd print(f"Type of getElementById is: {type(document.getElementById)}") ... This has the same issue. Print statement gives NoneType
Avatar
Andrea Giammarchi 29/01/2024 19:25
WUT?
19:26
then it is pandas messing up the document there ... this is overly annoying but thanks for the minimal example, I might be able to eventually catch the code that messes up stuff in pandas and I will eventually follow up with a PR there ๐Ÿ‘
Avatar
you're welcome. Thanks for the assistance! I'll probably end up using bind or micropip. Both solve the issue as of now.
Avatar
Andrea Giammarchi 29/01/2024 19:29
I think (but I haven't tested it yet), this might be enough ... for reasons I don't yet fully understand
19:31
print("Hello from worker!") import pyscript # just access it before pandas kicks in pyscript.document.getElementById import pandas as pd print(f"Type of getElementById is: {type(pyscript.document.getElementById)}") (edited)
Avatar
Andrea Giammarchi 29/01/2024 19:55
OK, this is absolutely sketchy ... if pandas is a package it doesn't matter when or how you use the document, it's fairly unpredictable ... it might be a matter of cache or I don't know, but if I change the code next time I might have that working, on refresh, it doesn't ... if I remove pandas from packages, it's always there and working and never missing ... I wouldn't know where to even file an issue with pandas like this so if anyone knows please share! /cc @hood
19:58
the TL;DR only on async context and a worker where we do patch js.document at the global level to actuall improve packages compatibilities, pandas might or might not mess up getElementById (while the document is always the expected one).
Avatar
Andrea Giammarchi 29/01/2024 20:10
<script worker async type="py" config='{"packages":["pandas"]}'> import pyscript # import pandas print(f"Type of getElementById is: {type(pyscript.document.getElementById)}") try: el = pyscript.document.getElementById("py-script-output") el.innerHTML = "test" except Exception as e: print(e) </script> <div id="py-script-output"></div>
20:13
for history sake ... if I disable the network cache I get more chances to have an error ... if cache is enabled it's almost always fine unless I also import pandas so that sometimes there are issues with cache (but not always) while with disabled network almost every time ... if I use any other package that pandas needs, I have 100% success ... same as if I use zero packages, and this only when pyodide.runAsync is used, in combination with a worker. Sorry I don't have an answer to the why, but this looks pretty bad and I can't find pandas pyodide related code out there, or the specific pyodide version.
Avatar
Avatar
Andrea Giammarchi
for history sake ... if I disable the network cache I get more chances to have an error ... if cache is enabled it's almost always fine unless I also import pandas so that sometimes there are issues with cache (but not always) while with disabled network almost every time ... if I use any other package that pandas needs, I have 100% success ... same as if I use zero packages, and this only when pyodide.runAsync is used, in combination with a worker. Sorry I don't have an answer to the why, but this looks pretty bad and I can't find pandas pyodide related code out there, or the specific pyodide version.
Ok... help me understand. It seems that code importing pandas actually breaks document.getElementById? And that happens randomly and in a non predicable and reproducible way? Is my statement correct? and ss that true for the whole document or just document.getElementById?
Avatar
Well it's unlikely it's anything pandas specific. Try iterating through all the other packages and seeing if you can reproduce with them.
Avatar
Avatar
Eric
Essentially the problem I am having is being unable to import in the py-editor
py-editor is blocking imports.I consider py-editor as DOM.Generally prints work for terminal and display work for DOM.In py-editor prints work for DOM?I have got not idea.
Avatar
Avatar
hood
Well it's unlikely it's anything pandas specific. Try iterating through all the other packages and seeing if you can reproduce with them.
Andrea Giammarchi 30/01/2024 08:44
nope, I cannot ... only pandas causes issues ... but as the issue is intermittent, I am not excluding I am just being lucky with timing ... on our side, the only difference between the broken document methods and the working one is pyodide.runAsync used instead of pyodide.run and in both cases the result is awaited before completion of the engine bootstrap.
Avatar
Avatar
Fabio
Ok... help me understand. It seems that code importing pandas actually breaks document.getElementById? And that happens randomly and in a non predicable and reproducible way? Is my statement correct? and ss that true for the whole document or just document.getElementById?
Andrea Giammarchi 30/01/2024 08:46
the document is always defined which is the odd part ... that's patched upfront witohut issues but its methods become sometimes not reachable ... I haven't found a way to guarantee that's not the case when pandas is part of the packages, even less when pandas is imported in the code, no matter where that happens (before or after, but when it's after, chances methods are broken are higher).
Avatar
Avatar
zobi
py-editor is blocking imports.I consider py-editor as DOM.Generally prints work for terminal and display work for DOM.In py-editor prints work for DOM?I have got not idea.
Andrea Giammarchi 30/01/2024 08:47
PyEditor is meant as playground for Python code and in Python code you don't have specific PyScript utilities such as display so, in that regard, it works more like a live terminal with a single output per run.
Avatar
Andrea Giammarchi 30/01/2024 09:36
FYI I can confirm the same issue in polyscript just to narrow down this is not a PyScript specific issue ... my pandas.py code: from polyscript import currentScript try: from polyscript.xworker.window import document except: from js import document import pandas print(f"In {currentScript.getAttribute('env')} the type of getElementById is: {type(document.getElementById)}") try: el = document.getElementById("py-script-output") el.innerHTML = "test" except Exception as e: print(e) This is the related HTML layout: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script type="module" src="https://cdn.jsdelivr.net/npm/polyscript"></script> </head> <body> <!-- always work --> <script env="main" type="pyodide" config='{"packages":["pandas"]}' src="./pandas.py"></script> <script env="async" async type="pyodide" config='{"packages":["pandas"]}' src="./pandas.py"></script> <script env="worker" worker type="pyodide" config='{"packages":["pandas"]}' src="./pandas.py"></script> <!-- most time doesn't work --> <script env="async-worker" async worker type="pyodide" config='{"packages":["pandas"]}' src="./pandas.py"></script> <div id="py-script-output"></div> </body> </html> And finally this is the outcome: In main the type of getElementById is: <class 'pyodide.ffi.JsProxy'> In async the type of getElementById is: <class 'pyodide.ffi.JsProxy'> In worker the type of getElementById is: <class 'pyodide.ffi.JsProxy'> In async-worker the type of getElementById is: <class 'NoneType'> 'NoneType' object is not callable (edited)
09:40
P.S. apologies for polluting this channel but this issue really bothers me and I'd love to find the culprit so I am pinging @hood again hopening there is literally anything I can investigate or anything different that could happen if runAsync has, internally, blocking calls to the main so that something locking the script execution might not be fully trusted or it might fail behind "conditions" ... thanks!
Avatar
Andrea Giammarchi 30/01/2024 10:01
@sai at least there is a solution which is to not use getElementById but use querySelector instead !!! /cc @hood @Fabio ... so this is some pandas issue (nope, fouund it!) because it makes literally no sense that nothing else is affected on the document but the getElementById is busted there ... with this code I don't have any issue anymore: from polyscript import currentScript try: from polyscript.xworker.window import document except: from js import document import pandas print(f"In {currentScript.getAttribute('env')} the type of querySelector is: {type(document.querySelector)}") try: el = document.querySelector("#py-script-output") el.innerHTML = "test" except Exception as e: print(e) (edited)
Avatar
Andrea Giammarchi 30/01/2024 10:14
on the other hand, I can't find anything at all related to getElementById in pandas source code and I wonder if the pyodide version has anything different at all ... this issue is so odd!
Avatar
Andrea Giammarchi 30/01/2024 10:26
uhm ... never mind, I've found the culprit, will follow up with an issue and an explanation. Sorry for the pings everyone, it's not pandas.
Avatar
Andrea Giammarchi 30/01/2024 11:11
to whom it might concern the issue has been solved and it will be published to npm + we're releasing again soon so please bear with us, thank you!
Description This MR brings in latest polyscript with latest coincident which include a fix for an uncanny situation presented when heavy modules (i.e. pandas) interrupt the code execution in an asy...
Avatar
Crazy! Thank you!! I probably gave you a sleepless night! ๐Ÿ˜…
๐Ÿคฃ 1
Avatar
Avatar
sai
Crazy! Thank you!! I probably gave you a sleepless night! ๐Ÿ˜…
Andrea Giammarchi 30/01/2024 17:22
in retrospective this was a "noob like" error from my side ... I did implement the right thing in other places but here it was burried down 2 holes of dependencies and that "tiny yet majestic detail" was obvious, once spotted. Thank you again for the heads-up and apologies again for spamming the channel last "few" hours ... it's all good now, which is what matters!
17:26
by "it's all good now" I mean on main, we'll release again ASAP, we just discussed that
๐Ÿ‘ 1
Avatar
Avatar
Andrea Giammarchi
PyEditor is meant as playground for Python code and in Python code you don't have specific PyScript utilities such as display so, in that regard, it works more like a live terminal with a single output per run.
you mean to say PyEditor code is not going to display on terminal?
Avatar
Avatar
zobi
you mean to say PyEditor code is not going to display on terminal?
Andrea Giammarchi 30/01/2024 17:32
it does? I am saying those are two different use cases and components ... but the PyEditor has a highlighted and editable area, the terminal, specially the interactive one, is a different "beast"
17:33
to clarify, one is XTerm (PyTerminal) while the other one is codemirror (PyEditor)
17:34
the output you see in latter case, PyEditor, is not a terminal in PyScript language, it's an output target for the editor result.
17:34
at least that's the current state/offer of the PyEditor.
Avatar
Avatar
Andrea Giammarchi
it does? I am saying those are two different use cases and components ... but the PyEditor has a highlighted and editable area, the terminal, specially the interactive one, is a different "beast"
Are the imports going to work in PyEditor?
17:38
I tried but failed to import packages.
Avatar
Avatar
zobi
Are the imports going to work in PyEditor?
Andrea Giammarchi 30/01/2024 17:41
you still need a py-config or an mpy-config to declare imports you want available ... otherwise you can import micropip and do whatever you like there.
Avatar
what's micropip?
17:43
Any documentation to refer?
Avatar
Avatar
zobi
Any documentation to refer?
Andrea Giammarchi 30/01/2024 17:57
it's already available in PyScript and it's pretty simple to use: https://github.com/pyodide/micropip#readme
A lightweight Python package installer for Pyodide - GitHub - pyodide/micropip: A lightweight Python package installer for Pyodide
๐Ÿ‘ 1
Avatar
Using WSL on windows 10, I cloned the github pyscript repository. I was able to run the test-integration In my environment. it seems that it ran successfully. I am trying to run a sample pyscript html file against this environment: 1 โ€“Do I need to start a server on my cloned pyscirpt environment, if so how to start such a server. 2. How do I reference the core.js and core.css from my html file to this server.
Avatar
Avatar
Parviz O
Using WSL on windows 10, I cloned the github pyscript repository. I was able to run the test-integration In my environment. it seems that it ran successfully. I am trying to run a sample pyscript html file against this environment: 1 โ€“Do I need to start a server on my cloned pyscirpt environment, if so how to start such a server. 2. How do I reference the core.js and core.css from my html file to this server.
Andrea Giammarchi 30/01/2024 19:05
feels like a bug in our development documentation but in general I'd like to maybe help with these facts:
  • you don't need any specific OS to run PyScript ... the whole thing runs out of any browser you like: Edge, Chrome, Chromium, Safari, WebKit (with flag enabled but that's not your case), Android (Chrome), iOS (Safari mobile), and so on
  • to start a server able to serve all PyScript features you need a server able to enable SharedArrayBuffer ... one option, as you've cloned and installed successfully so I believe you have node and npx command available, is to npx static-handler --coi . where the last . is the root folder you'd like to test and keep in mind if such folder doesn't contain an index.html in it, you'll see nothing and you need to reach the exact path instead
  • npx static-handler --coi . is basically the same as runnign npm start server to host your file from the root of the project
  • to build latest/greatest you need to run an npm run build before testing anything else
  • if you are editing stuff frequently, you can do npm run dev instead and in a different shell/console/tab run npm run server
  • to find your latest built of PyScript, point at ./dist/core.js and ./dist/core.css from relative URLs you have in your pipe
  • if you NO_MIN=true npm run build before tests, you'll have non minified files to dig into source code out of a fork ... that's basically the same, just a tad slower, than npm run dev but after latest command you also need to still run npm run build if you want prod usable artifacts
(edited)
Avatar
Avatar
Andrea Giammarchi
feels like a bug in our development documentation but in general I'd like to maybe help with these facts:
  • you don't need any specific OS to run PyScript ... the whole thing runs out of any browser you like: Edge, Chrome, Chromium, Safari, WebKit (with flag enabled but that's not your case), Android (Chrome), iOS (Safari mobile), and so on
  • to start a server able to serve all PyScript features you need a server able to enable SharedArrayBuffer ... one option, as you've cloned and installed successfully so I believe you have node and npx command available, is to npx static-handler --coi . where the last . is the root folder you'd like to test and keep in mind if such folder doesn't contain an index.html in it, you'll see nothing and you need to reach the exact path instead
  • npx static-handler --coi . is basically the same as runnign npm start server to host your file from the root of the project
  • to build latest/greatest you need to run an npm run build before testing anything else
  • if you are editing stuff frequently, you can do npm run dev instead and in a different shell/console/tab run npm run server
  • to find your latest built of PyScript, point at ./dist/core.js and ./dist/core.css from relative URLs you have in your pipe
  • if you NO_MIN=true npm run build before tests, you'll have non minified files to dig into source code out of a fork ... that's basically the same, just a tad slower, than npm run dev but after latest command you also need to still run npm run build if you want prod usable artifacts
(edited)
Thanks @Andrea Giammarchi I am sure as you and the team continue the development, the documentation gets better too. Currently the documents are a lot better than few months ago. Anyway I follow your suggestions.
Avatar
Josh (EduBlocks) 30/01/2024 21:19
Is there a way in PyScript where I listen into stdout? i.e. i want to do something off the back of print("hello world") (edited)
Avatar
Avatar
Josh (EduBlocks)
Is there a way in PyScript where I listen into stdout? i.e. i want to do something off the back of print("hello world") (edited)
Andrea Giammarchi 31/01/2024 10:00
you can import hooks from @pyscript/core and then change the io field on ready
Avatar
สŸแดœแดแด€ 31/01/2024 15:19
question, was JsArray removed from pyodide.ffi ? This error started appearing on my website: ImportError: cannot import name 'JsArray' from 'pyodide.ffi' (/lib/python3.10/site-packages/pyodide/ffi/__init__.py)
Avatar
That's a question for Pyodide..?
Avatar
สŸแดœแดแด€ 31/01/2024 15:21
ah, thought both projects were related. my bad
Avatar
Avatar
สŸแดœแดแด€
ah, thought both projects were related. my bad
Andrea Giammarchi 31/01/2024 15:44
what's the use case for needing that, if I might ask? ๐Ÿค”
Avatar
Avatar
Andrea Giammarchi
what's the use case for needing that, if I might ask? ๐Ÿค”
สŸแดœแดแด€ 31/01/2024 15:44
Imported from urllib, which is imported by another package and so on (edited)
Avatar
Avatar
สŸแดœแดแด€
Imported from urllib, which is imported by another package and so on (edited)
Andrea Giammarchi 31/01/2024 15:45
so ... are 3rd party libraries/packages breaking? or it's your code using it? if it's your code, you still should have ffi.to_js(listOrTuple) which might be doing just about the same.
Avatar
Avatar
สŸแดœแดแด€
question, was JsArray removed from pyodide.ffi ? This error started appearing on my website: ImportError: cannot import name 'JsArray' from 'pyodide.ffi' (/lib/python3.10/site-packages/pyodide/ffi/__init__.py)
Jeff Glass 31/01/2024 15:45
It hasn't been, no... but also if it's pointing to Python 3.10, that's a somewhat older version of Pyodide
Avatar
Andrea Giammarchi 31/01/2024 15:46
but yeah, when it comes to Pyodide ffi that's really Pyodide territory, I am afraid.
Avatar
Avatar
Andrea Giammarchi
so ... are 3rd party libraries/packages breaking? or it's your code using it? if it's your code, you still should have ffi.to_js(listOrTuple) which might be doing just about the same.
สŸแดœแดแด€ 31/01/2024 15:46
3rd party libs. Yeah my mistake
Avatar
Andrea Giammarchi 31/01/2024 15:48
it's very possible default wheels are kept updated if you don't specify/pin these via the config, but I think Jeff got it right: you are on a older version of pyodide, you might consider updating to latest ... and we just released (yet another one) so maybe that solves for you.
Avatar
Avatar
sai
Crazy! Thank you!! I probably gave you a sleepless night! ๐Ÿ˜…
Andrea Giammarchi 31/01/2024 15:57
FYI PyScript 2024.1.3 is out and your issue should be fixed officially out of the box ๐Ÿ‘
๐Ÿ‘ 2
Avatar
Avatar
Eric
Is it possibly to load the webpage in an enviornment that is not the main thread?
You can try linking html to main thread. https://_12.pyscriptapps.com/frosty-hill-copy/latest/
Avatar
Avatar
Andrea Giammarchi
you still need a py-config or an mpy-config to declare imports you want available ... otherwise you can import micropip and do whatever you like there.
I tried micropip install but throws an error. https://_12.pyscriptapps.com/royal-disk/latest/
Raffaele started a thread. 01/02/2024 16:58
Avatar
Josh (EduBlocks) 02/02/2024 12:22
I've had an EduBlocks user report they can't use exit() with pyscript, as it throws an error. This might be a pyodide thing, but wondering if anyone had any ideas? https://app.edublocks.org/project/1mSFefRFBwaCl2U3UE2G4MfTDqr2/4IYhhucClXuRS7iHOfoe
Avatar
Avatar
Josh (EduBlocks)
I've had an EduBlocks user report they can't use exit() with pyscript, as it throws an error. This might be a pyodide thing, but wondering if anyone had any ideas? https://app.edublocks.org/project/1mSFefRFBwaCl2U3UE2G4MfTDqr2/4IYhhucClXuRS7iHOfoe
Andrea Giammarchi 02/02/2024 13:26
what do you expect to happen?
13:29
This prints a better set of error messages in case someone calls os._exit() or in C code exit() is used. Resolves #3486. Add a CHANGELOG entry
13:29
would os._exit() produc ethe same?
Avatar
@Josh (EduBlocks) exit() etc... doesn't really make sense in PyScript. Exit is how to terminate a Python program, but what should be terminated... the browser tab..? Etc etc etc - feels like a "don't do that" bug ๐Ÿ˜‰
Avatar
Avatar
Andrea Giammarchi
what do you expect to happen?
Josh (EduBlocks) 02/02/2024 13:59
good question, i don't know! ๐Ÿ˜„
Avatar
Avatar
ntoll
@Josh (EduBlocks) exit() etc... doesn't really make sense in PyScript. Exit is how to terminate a Python program, but what should be terminated... the browser tab..? Etc etc etc - feels like a "don't do that" bug ๐Ÿ˜‰
Josh (EduBlocks) 02/02/2024 14:00
exactly what i was thinking, i agree that it doesn't really make sense in this context. i think i will craft a response based on that.
๐Ÿ‘ 1
Avatar
Andrea Giammarchi 02/02/2024 14:02
you could grab the current terminal and related script and kill that node but I don't know if pyodide will free its memory after
Avatar
Hi! I can disable a button with runButton=document.getElementById("run") then runButton.setAttribute("disabled", "true") but cannot re-enable it by setAttribute("disabled","false")... what's going on?
Avatar
Avatar
CJBAIGET
Hi! I can disable a button with runButton=document.getElementById("run") then runButton.setAttribute("disabled", "true") but cannot re-enable it by setAttribute("disabled","false")... what's going on?
solved: runButton.removeAttribute("disabled")
Avatar
elliot0x01 02/02/2024 21:02
Hi can anyone help with this? I am trying to use requests/urequests module in micropython but it causes this issue when requesting pyodide_http.
Avatar
Avatar
elliot0x01
Hi can anyone help with this? I am trying to use requests/urequests module in micropython but it causes this issue when requesting pyodide_http.
Jeff Glass 02/02/2024 21:25
Pyodide_http resides on PyPI or is built into Pyodide, whereas Micropython loads from micropython-lib. The two are unfortunately not compatible.
Avatar
Avatar
Josh (EduBlocks)
I've had an EduBlocks user report they can't use exit() with pyscript, as it throws an error. This might be a pyodide thing, but wondering if anyone had any ideas? https://app.edublocks.org/project/1mSFefRFBwaCl2U3UE2G4MfTDqr2/4IYhhucClXuRS7iHOfoe
This code worked for me
04:31
def main(): print("This is a simple program using the exit function.") userinput = input("Type 'exit' to terminate the program: ") if userinput.lower() == 'exit': print('\x1bc') print("Exiting the program.") else: print("You didn't type 'exit'. Program continues.") if __name == "main": main()
Avatar
Avatar
Jeff Glass
Pyodide_http resides on PyPI or is built into Pyodide, whereas Micropython loads from micropython-lib. The two are unfortunately not compatible.
elliot0x01 03/02/2024 09:18
So there is no way to use request module with micro python right ?
Avatar
Avatar
elliot0x01
So there is no way to use request module with micro python right ?
chris.laffra 03/02/2024 10:54
This is Python, so everything is possible ๐Ÿค“ . Here is a quick example I just created that uses the requests module on MicroPython: https://pyscript.com/@laffra/using-requests-micropython/latest
Avatar
Avatar
chris.laffra
This is Python, so everything is possible ๐Ÿค“ . Here is a quick example I just created that uses the requests module on MicroPython: https://pyscript.com/@laffra/using-requests-micropython/latest
elliot0x01 03/02/2024 12:35
Damnnn....this is so cool Chris
๐Ÿ‘ 1
12:36
Thanks for this, works like a charm. True, python keeps surprising each one of us ๐Ÿ™‚
Avatar
bonkey bear 04/02/2024 06:40
Hey guys I'm trying to make a sort of virtual lab and I don't know how I'm supposed to "pick up" a square that I drew on the canvas
06:40
Can anyone help me
Avatar
Hi all, just wondering how to read a text file (.txt) from a URL, using python/pyscript?
11:00
I saw to import urllib2, but I don't think this is a package provided by pyscript
Avatar
Avatar
koko
Hi all, just wondering how to read a text file (.txt) from a URL, using python/pyscript?
Andrea Giammarchi 04/02/2024 12:02
๐Ÿ‘ 1
Avatar
Hola... I'm trying to read from a python file within a web app using <py-script> or <script type="py"> within an html file. When running the web app through my port, it returns to me a py404 error telling me to check the filename or directory. What i want to know is, why is it not picking up on the .py file (simple hello world) and what do i need to do to get the html page to run the python script without embedding it in the page itself
Avatar
FabioRosado 05/02/2024 16:06
I've been scratching my head for a while with this one, assume the following code: import asyncio async def _test(): #await asyncio.sleep(1) return 1 def test(): loop = asyncio.get_event_loop() future = asyncio.Future() # Run the async function in the background using run_coroutine_threadsafe async_task = asyncio.ensure_future(_test(), loop=loop) async_task.add_done_callback(lambda f: future.set_result(f.result())) return_value = loop.run_until_complete(future) return return_value test() print(test()) Running locally it returns me 1 but on pyscript it returns me a <Future pending>, any pointers as to why and how to make it run the right thing? ๐Ÿค”
Avatar
Avatar
FabioRosado
I've been scratching my head for a while with this one, assume the following code: import asyncio async def _test(): #await asyncio.sleep(1) return 1 def test(): loop = asyncio.get_event_loop() future = asyncio.Future() # Run the async function in the background using run_coroutine_threadsafe async_task = asyncio.ensure_future(_test(), loop=loop) async_task.add_done_callback(lambda f: future.set_result(f.result())) return_value = loop.run_until_complete(future) return return_value test() print(test()) Running locally it returns me 1 but on pyscript it returns me a <Future pending>, any pointers as to why and how to make it run the right thing? ๐Ÿค”
Andrea Giammarchi 05/02/2024 16:19
is the script sync or async ?
Avatar
Avatar
hopf
Hola... I'm trying to read from a python file within a web app using <py-script> or <script type="py"> within an html file. When running the web app through my port, it returns to me a py404 error telling me to check the filename or directory. What i want to know is, why is it not picking up on the .py file (simple hello world) and what do i need to do to get the html page to run the python script without embedding it in the page itself
Andrea Giammarchi 05/02/2024 16:20
you miss an src attribute or an entry in the py-config that points at that file so you can import it
Avatar
Avatar
FabioRosado
I've been scratching my head for a while with this one, assume the following code: import asyncio async def _test(): #await asyncio.sleep(1) return 1 def test(): loop = asyncio.get_event_loop() future = asyncio.Future() # Run the async function in the background using run_coroutine_threadsafe async_task = asyncio.ensure_future(_test(), loop=loop) async_task.add_done_callback(lambda f: future.set_result(f.result())) return_value = loop.run_until_complete(future) return return_value test() print(test()) Running locally it returns me 1 but on pyscript it returns me a <Future pending>, any pointers as to why and how to make it run the right thing? ๐Ÿค”
Andrea Giammarchi 05/02/2024 16:23
as example, this works for me: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="https://pyscript.net/releases/2024.1.3/core.css"> <script type="module" src="https://pyscript.net/releases/2024.1.3/core.js"></script> <script type="py" async> import asyncio async def _test(): #await asyncio.sleep(1) return 1 def test(): loop = asyncio.get_event_loop() future = asyncio.Future() # Run the async function in the background using run_coroutine_threadsafe async_task = asyncio.ensure_future(_test(), loop=loop) async_task.add_done_callback(lambda f: future.set_result(f.result())) return_value = loop.run_until_complete(future) return return_value print(await test()) </script> </head> </html>
16:26
from a JS point of view, you can't block / pause any asynchronous entry in place (within the same thread, you can simulate that cross worker) so even if you run_until_complete you still can't block the code from dealing with just a promise ... if you return that test() within a runAsync in pyodide interpreter though, that will eventually give you the 1 you are after, but if you print that without await in place, I am not sure how that could ever possibly work.
Avatar
FabioRosado 05/02/2024 16:32
I'm confused, test is sync and is doing some async magic under the hood, so I shouldn't need to use the async attribute right? The goal is to use said test function in a sync context but is wrapping some code that could blood, so running that code will make sure that the main thread doesn't block, for example this code will not block the main thread. import asyncio from time import sleep async def _test(): print("sleeping...") sleep(2) print("another sleep") sleep(4) def test(): loop = asyncio.get_event_loop() future = asyncio.Future() # Run the async function in the background using run_coroutine_threadsafe async_task = asyncio.ensure_future(_test(), loop=loop) async_task.add_done_callback(lambda f: future.set_result(f.result())) return_value = loop.run_until_complete(future) return return_value print("Test block...") test() print("done!") It will print: Test block... done! sleeping... another sleep The surprising thing on this code is that in pyscript/pyodide I can't get a return value, but locally I can
Avatar
Andrea Giammarchi 05/02/2024 16:36
that example blocks the whole UI if it run on the main thread ... it is blocking!
16:37
in a worker, it would not block the main thread but still block the worker if you don't use pyscript.sync.sleep instead
16:37
about promises, you need to await these ... they can't block the thread ... asyncio is not 100% web compat in this case, it's a Pyodide thing and non-existent on MicroPython at all.
Avatar
FabioRosado 05/02/2024 16:37
wait is blocking? Shouldn't the print statement stop before test() prints if that's the case?
Avatar
Andrea Giammarchi 05/02/2024 16:38
as we have async attribute, you usually don't ever even need asyncio in general.
16:38
on web that sleep(4) blocks for 4 seconds the UI ... put a button aroud and see you unable to click it for 4 seconds
16:39
still on the Web, there is no way you can pause a Promise ... but again, we have async attribute with top-level await capability, so you can just drop asyncio there and do the same
16:41
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="https://pyscript.net/releases/2024.1.3/core.css"> <script type="module" src="https://pyscript.net/releases/2024.1.3/core.js"></script> <script type="py" async worker> from pyscript import sync async def test(): print("sleep 1 sec") sync.sleep(1) print("slept 1 sec") return 1 print(await test()) </script> </head> </html>
16:41
as example (this won't ever block a thing on the main) (edited)
Avatar
Avatar
Andrea Giammarchi
you miss an src attribute or an entry in the py-config that points at that file so you can import it
Do I have to use a <py-config> tag? I have added a config attribute to the <script> tage to point to a pyconfig.json containing the file reference, why doesn't that work?
16:42
Also thank you @Andrea Giammarchi for the quick response
Avatar
Avatar
hopf
Do I have to use a <py-config> tag? I have added a config attribute to the <script> tage to point to a pyconfig.json containing the file reference, why doesn't that work?
Andrea Giammarchi 05/02/2024 16:42
I wouldn't know without having any code to test or check or without knowing which version of PyScript you are running on
๐Ÿ‘ 1
Avatar
Avatar
Andrea Giammarchi
I wouldn't know without having any code to test or check or without knowing which version of PyScript you are running on
I'll post something later since it's on my personal computer and im not near it atm. Thanks again!
Avatar
Avatar
hopf
I'll post something later since it's on my personal computer and im not near it atm. Thanks again!
Andrea Giammarchi 05/02/2024 16:44
I won't be around later but I'll be happy to help tomorrow or maybe somebody else might chime in ๐Ÿ˜‰
Avatar
Avatar
Andrea Giammarchi
still on the Web, there is no way you can pause a Promise ... but again, we have async attribute with top-level await capability, so you can just drop asyncio there and do the same
FabioRosado 05/02/2024 16:46
Hmm... yeah I see that now I was just going with the print statements and assumed it wasn't blocking, is it safe to assume that then we should really be always async if you want to use any sort of api calls?
Avatar
Avatar
FabioRosado
Hmm... yeah I see that now I was just going with the print statements and assumed it wasn't blocking, is it safe to assume that then we should really be always async if you want to use any sort of api calls?
Andrea Giammarchi 05/02/2024 16:48
it's safe to assume every time you think you need asyncio you should have an async attribute in the script instead and remove every code that uses asyncio as that won't be needed anymore thanks to async and top-level await
16:48
blocking requests (APIs call) are already blocking too via synchronous XMLHttpRequest behind the scene and all of them are better off the main thread
Avatar
FabioRosado 05/02/2024 16:49
yeah i expected that my async magic would prevent blocking the main thread but I was wrong ๐Ÿคฃ
Avatar
Andrea Giammarchi 05/02/2024 16:50
last, but not least, for portability sake ... MicroPython doesn't have, and doesn't plan to, asyncio module, because indeed the conversation was like "what does it do or how and is it needed with runAsync ability?"
Avatar
FabioRosado 05/02/2024 16:50
does it change anything if using js fetch?
Avatar
Avatar
FabioRosado
yeah i expected that my async magic would prevent blocking the main thread but I was wrong ๐Ÿคฃ
Andrea Giammarchi 05/02/2024 16:51
it cannot on main (no way to pause the thread on Promises) and it can work only on workers ... at that point just use async and await when needed, no asyncio is needed to me ... if it is, gotta await the final call/result as the Web can't block promises even on workers.
Avatar
FabioRosado 05/02/2024 16:52
for context the idea is to provide a nicer way to use .com features so instead of telling users "call this api and figure it out" we would say "use x and you get the result" kind of thing. But under the hood we would have to do the api call and add any other logic that might be necessary, that's why is started with an attempt to not block the main thread haha
Avatar
Avatar
FabioRosado
for context the idea is to provide a nicer way to use .com features so instead of telling users "call this api and figure it out" we would say "use x and you get the result" kind of thing. But under the hood we would have to do the api call and add any other logic that might be necessary, that's why is started with an attempt to not block the main thread haha
Andrea Giammarchi 05/02/2024 16:55
you can do that only via workers and assuming the API calls are in the main thread so you can have Atomics working but this is way more complicated than tell users just to await API calls to me ... although, there is no other way around (that I know) if not by blocking with synchronous XHR which is already deprecated on main so it's a slippery slope and and a short term harakiri to me. Sorry for not providing the answers you were looking for.
16:55
Python on Web still has Web constraints, unfortunately ... something can be bypassed, something else cannot.
Avatar
Avatar
Andrea Giammarchi
you can do that only via workers and assuming the API calls are in the main thread so you can have Atomics working but this is way more complicated than tell users just to await API calls to me ... although, there is no other way around (that I know) if not by blocking with synchronous XHR which is already deprecated on main so it's a slippery slope and and a short term harakiri to me. Sorry for not providing the answers you were looking for.
FabioRosado 05/02/2024 16:57
Thanks that helps a lot I've been scratching my head on this for a while haha
Avatar
IWBNI there was an offical example where the (say) upload recipe (which uses async, was implemented in a worker. With passing of file data back to main. Or vice versa if the guidance is to have teh UI in the main and ancillary tasks in workers...
Avatar
I've just begun and for some reason the main.py function cannot find the reference for "today" element in the html? Heres my code: html: <!DOCTYPE html> <html lang="en"> <head> <title>Random Number Generator</title> <!-- Recommended meta tags --> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width,initial-scale=1.0"> <!-- PyScript CSS --> <link rel="stylesheet" href="https://pyscript.net/releases/2024.1.1/core.css"> <!-- This script tag bootstraps PyScript --> <script type="module" src="https://pyscript.net/releases/2024.1.1/core.js"></script> </head> <body> <p id="today"><p> <script type="py" src="./main.py" config="./pyscript.toml" terminal></script> </body> </html> python: import datetime as dt def get_rand_num(): Element("today").write(str(dt.date.today())) get_rand_num()
01:17
the video I got it from is 9 months old so wasn't sure if the syntax to finding elements had changed or not
Avatar
Avatar
Joshua1N
I've just begun and for some reason the main.py function cannot find the reference for "today" element in the html? Heres my code: html: <!DOCTYPE html> <html lang="en"> <head> <title>Random Number Generator</title> <!-- Recommended meta tags --> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width,initial-scale=1.0"> <!-- PyScript CSS --> <link rel="stylesheet" href="https://pyscript.net/releases/2024.1.1/core.css"> <!-- This script tag bootstraps PyScript --> <script type="module" src="https://pyscript.net/releases/2024.1.1/core.js"></script> </head> <body> <p id="today"><p> <script type="py" src="./main.py" config="./pyscript.toml" terminal></script> </body> </html> python: import datetime as dt def get_rand_num(): Element("today").write(str(dt.date.today())) get_rand_num()
Andrea Giammarchi 06/02/2024 08:56
we don't leak the Element globally anymore, you need to import it from pydom then if it still doesn't work try with a proper CSS selector such as #today.
Avatar
I'm using 2024.1.3/core.js but getting pyodide.ffi.JsException: Error: This borrowed proxy was automatically destroyed at the end of a function call. Try using create_proxy or create_once_callable. For more information about the cause of this error, use 'pyodide.setDebug(true)' errors. If I import pyodide and pyodide.setDebug(true) it tells me no such function. How do I debug which thing is being destroyed and properly proxy it.
Avatar
Avatar
Neon22
I'm using 2024.1.3/core.js but getting pyodide.ffi.JsException: Error: This borrowed proxy was automatically destroyed at the end of a function call. Try using create_proxy or create_once_callable. For more information about the cause of this error, use 'pyodide.setDebug(true)' errors. If I import pyodide and pyodide.setDebug(true) it tells me no such function. How do I debug which thing is being destroyed and properly proxy it.
Andrea Giammarchi 06/02/2024 11:13
that's something else but you could try to use experimental_create_proxy = "auto" in your TOML and see if that error goes away.
11:15
otherwise from pyodide.ffi import create_proxy and wrap the listener there ... if it's a one-off listener there is the create_callable_once utility but this is pyodide internals https://pyodide.org/en/stable/usage/type-conversions.html#calling-javascript-functions-from-python
Avatar
Thanks - it is kind of weird we have to do something at all. I just made it - why is the default to destroy it. I think mpy is not so aggressive... but hey - we wait on pyodide maybe...
Avatar
Avatar
Neon22
Thanks - it is kind of weird we have to do something at all. I just made it - why is the default to destroy it. I think mpy is not so aggressive... but hey - we wait on pyodide maybe...
Andrea Giammarchi 06/02/2024 12:01
it's a long story but the gist is here: https://github.com/pyscript/polyscript/pull/79
Background Pyodide requires create_proxy not only for event listeners but also for any JS API that might use a callback that is not instantly invoked: GeoLocation watchPosition and others anything...
Avatar
Avatar
Andrea Giammarchi
we don't leak the Element globally anymore, you need to import it from pydom then if it still doesn't work try with a proper CSS selector such as #today.
I did both of these suggestions but nothing has worked, it doesnโ€™t know what import pydom is, and if I leave it out it cannot find the Element method? Are there any recent videos showing the basics?
19:44
I found where the Element.write is in the docs but the code included is using py-script in html, code also doesnโ€™t work when I transfer it to the python file
Avatar
Is there a way to pull information from the DOM
20:03
Such as an array of elements
20:04
and use within a worker enviorment
Avatar
do I have to add a .toml file to my project to import different libraries not already included by pyscript? I was trying to import Selenium in my .py script which I already have imported, but it says it couldnt import it
Avatar
Avatar
Joshua1N
do I have to add a .toml file to my project to import different libraries not already included by pyscript? I was trying to import Selenium in my .py script which I already have imported, but it says it couldnt import it
Jeff Glass 07/02/2024 03:00
Youโ€™ll either need a config file, or to add an inline configuration to one of your tags. Check out the configuration docs for more details - specifically, the packages key, which you can think of as roughly the equivalent of pip installโ€ฆ
Avatar
I did what it said but its still not getting recognizing it. pyscript.toml: packages = ["selenium"] python: from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # URL of the website url = "https://finance.yahoo.com/losers/" # XPath of the map sliders element_xpath = '//*[@id="scr-res-table"]/div[1]/table/tbody/tr[1]/td[3]/fin-streamer' # Setting up the WebDriver driver = webdriver.Chrome() try: # Open the website driver.get(url) driver.maximize_window() element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, element_xpath))) # Get the text value of the element value = element.text print(value) finally: # Close the browser pass # XPATH to the list of containers: //*[@id="scr-res-table"]/div[1]/table/tbody/tr[1] -- Change the index on ending
04:32
error: JsException(PythonError: Traceback (most recent call last): File "/lib/python3.10/site-packages/_pyodide/_base.py", line 429, in eval_code .run(globals, locals) File "/lib/python3.10/site-packages/_pyodide/_base.py", line 300, in run coroutine = eval(self.code, globals, locals) File "", line 1, in ModuleNotFoundError: No module named 'selenium' )
04:33
Its already installed, used in another file already
Avatar
bonkey bear 07/02/2024 07:24
does anyone know how to get the position of the mouse on the browser?
Avatar
Avatar
Joshua1N
I did what it said but its still not getting recognizing it. pyscript.toml: packages = ["selenium"] python: from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # URL of the website url = "https://finance.yahoo.com/losers/" # XPath of the map sliders element_xpath = '//*[@id="scr-res-table"]/div[1]/table/tbody/tr[1]/td[3]/fin-streamer' # Setting up the WebDriver driver = webdriver.Chrome() try: # Open the website driver.get(url) driver.maximize_window() element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, element_xpath))) # Get the text value of the element value = element.text print(value) finally: # Close the browser pass # XPATH to the list of containers: //*[@id="scr-res-table"]/div[1]/table/tbody/tr[1] -- Change the index on ending
Andrea Giammarchi 07/02/2024 08:27
selenium can't run out of a browser, it requires various privileges but most importantly the Chrome webdriver is nowhere published as WASM or python wheel so I don't think that can ever work in pyodide. amend I can install selenium without issues but starting a webdriver might be a stretch to its possibilities as PyScript module. (edited)
Avatar
Avatar
bonkey bear
does anyone know how to get the position of the mouse on the browser?
Andrea Giammarchi 07/02/2024 08:28
both click and pointermove events would give you that.
Avatar
bonkey bear 07/02/2024 08:46
ok Thanks
Avatar
Avatar
Andrea Giammarchi
selenium can't run out of a browser, it requires various privileges but most importantly the Chrome webdriver is nowhere published as WASM or python wheel so I don't think that can ever work in pyodide. amend I can install selenium without issues but starting a webdriver might be a stretch to its possibilities as PyScript module. (edited)
chromedriver.exe path can be given but pyscript doesn't upload some of the .exe files and even if i upload the folder it just uploads the license file and not the .exe file. It's an issue or pyscript is blocking it?
Avatar
Avatar
zobi
chromedriver.exe path can be given but pyscript doesn't upload some of the .exe files and even if i upload the folder it just uploads the license file and not the .exe file. It's an issue or pyscript is blocking it?
Andrea Giammarchi 07/02/2024 09:12
I think you are expecting windows exacutable to run out of a Python WASM targeting runtime and that's not exactly what this project is about.
09:14
I am not sure we prevent .exe files but as that could be a way to spread also malwhare and as .exe files on PyScript don't make sense maybe you should explain what you are trying to do or maybe you should consider PyScript still runs out of a browser, it's not an OS
Avatar
I am trying to upload chromedriver.exe but i am unable to upload the file
09:21
Even tesseract.exe is .exe file unable to upload it.
Avatar
Avatar
zobi
Even tesseract.exe is .exe file unable to upload it.
Andrea Giammarchi 07/02/2024 09:41
again, what's the purpose?
Avatar
No purpose just to have knowledge about it.I was trying to help Joshua1N.He is having trouble with this line driver = webdriver.Chrome(). I know driver=webdriver.Chrome(chromedriver.exe) will work. (edited)
Avatar
Avatar
zobi
No purpose just to have knowledge about it.I was trying to help Joshua1N.He is having trouble with this line driver = webdriver.Chrome(). I know driver=webdriver.Chrome(chromedriver.exe) will work. (edited)
Jeff Glass 07/02/2024 12:55
I would be very surprised if that works in PyScript - as Andrea says, a windows executable file is unlikely to just run in the browser
Avatar
Avatar
Andrea Giammarchi
again, what's the purpose?
I am trying my jupyte rnotebook project here. I should write Tesseract-OCR\tesseract.exe to use the package.When i upload Tesseract-OCR folder .exe files are missing from the folder. There is no tesseract.exe in the folder how can i use the package??
Avatar
Avatar
zobi
I am trying my jupyte rnotebook project here. I should write Tesseract-OCR\tesseract.exe to use the package.When i upload Tesseract-OCR folder .exe files are missing from the folder. There is no tesseract.exe in the folder how can i use the package??
Andrea Giammarchi 07/02/2024 14:02
you cannot use use .exe file, any of them, via browser ... PyScript runs in a browser through WASM targeting interpreters, Pyodide and MicroPython. None of these are Windows machines, Windows OS, or able to run Windows executable ... I hope this clarifies.
๐Ÿ‘ 1
Avatar
PinkTarrLargo 07/02/2024 20:06
i am so sorry if this is obvious, but how do i "pip install" this, i tried pip install pyscript, but that installed another thing with the same name... so sorry lol
20:06
vscode likes to take autocomplete from pip installed stuff btw
Avatar
Avatar
PinkTarrLargo
i am so sorry if this is obvious, but how do i "pip install" this, i tried pip install pyscript, but that installed another thing with the same name... so sorry lol
chris.laffra 07/02/2024 20:58
Just click on the "Install" link on this page: https://pyscript.net/ ๐Ÿค“
Run Python code in your HTML.
21:00
The best way to get started with PyScript is to follow the "Try it out" link on that same page, taking you to https://pyscript.com
Avatar
PinkTarrLargo 07/02/2024 23:57
k thanks
Avatar
Avatar
Andrea Giammarchi
it does? I am saying those are two different use cases and components ... but the PyEditor has a highlighted and editable area, the terminal, specially the interactive one, is a different "beast"
I tried older version of PyEditor , Prints are going to terminal and display is going to DOM. Can't it get updated in the latest version?
Avatar
Hi, I am working with main and worker threads and I realized that they have different file systems (is this True)? So access main thread files in worker, i am using main's interpreter.FS in worker python code to read files and write them to worker threads file system. So first is this is the correct way to move files from main FS to worker FS? I am able to do this for csv files by reading them as text, but i can't do that for pdfs because that is binary data and will probably get corrupted with utf8 encoding. interpreter.FS.readFile gives me a Uint8Array object (probably a proxy because i am running this in python), how can i use python write function to write this Uint8Array to a file? I tried the approach on pyscript.recipes, but it doesn't work. I get an AttributeError for arrayBuffer. Edit: The interpreter.FS is the micropython interpreter which i capture using the hook. (edited)
Avatar
Avatar
sai
Hi, I am working with main and worker threads and I realized that they have different file systems (is this True)? So access main thread files in worker, i am using main's interpreter.FS in worker python code to read files and write them to worker threads file system. So first is this is the correct way to move files from main FS to worker FS? I am able to do this for csv files by reading them as text, but i can't do that for pdfs because that is binary data and will probably get corrupted with utf8 encoding. interpreter.FS.readFile gives me a Uint8Array object (probably a proxy because i am running this in python), how can i use python write function to write this Uint8Array to a file? I tried the approach on pyscript.recipes, but it doesn't work. I get an AttributeError for arrayBuffer. Edit: The interpreter.FS is the micropython interpreter which i capture using the hook. (edited)
There are two recipes here dealing with uploading and downloading files. I have used them successfully for text, binary data. Use them as a reference. https://pyscript.recipes/2024.1.1/basic/file-upload/
Avatar
Avatar
sai
Hi, I am working with main and worker threads and I realized that they have different file systems (is this True)? So access main thread files in worker, i am using main's interpreter.FS in worker python code to read files and write them to worker threads file system. So first is this is the correct way to move files from main FS to worker FS? I am able to do this for csv files by reading them as text, but i can't do that for pdfs because that is binary data and will probably get corrupted with utf8 encoding. interpreter.FS.readFile gives me a Uint8Array object (probably a proxy because i am running this in python), how can i use python write function to write this Uint8Array to a file? I tried the approach on pyscript.recipes, but it doesn't work. I get an AttributeError for arrayBuffer. Edit: The interpreter.FS is the micropython interpreter which i capture using the hook. (edited)
Andrea Giammarchi 09/02/2024 08:34
main uses the same interpreter (either pyodide or MicroPython) but each worker uses a new interpreter ... they share nothing.
Avatar
Avatar
Andrea Giammarchi
main uses the same interpreter (either pyodide or MicroPython) but each worker uses a new interpreter ... they share nothing.
Andrea Giammarchi 09/02/2024 08:39
one way to share the virtual FS is to make it persistent on the browser ... this might be handy to experiment but files from the interpreter vFS cannot easily be moved into that storage so more manual work is needed https://github.com/WebReflection/fs#readme
Emscripten FS, PATH and PATH_FS utilities as a module - GitHub - WebReflection/fs: Emscripten FS, PATH and PATH_FS utilities as a module
Avatar
Avatar
Andrea Giammarchi
one way to share the virtual FS is to make it persistent on the browser ... this might be handy to experiment but files from the interpreter vFS cannot easily be moved into that storage so more manual work is needed https://github.com/WebReflection/fs#readme
Andrea Giammarchi 09/02/2024 10:06
a-ha ... there is also already the possibility via https://pyodide.org/en/stable/usage/file-system.html#mounting-a-file-system
Avatar
Avatar
Andrea Giammarchi
a-ha ... there is also already the possibility via https://pyodide.org/en/stable/usage/file-system.html#mounting-a-file-system
Andrea Giammarchi 09/02/2024 10:18
FWIWI I have also started a discussion about this ... I think it'd be great to have an out-of-the-box solution to persist the FS across multiple run (bear in mind it would persist per browser/user, not in a server) https://github.com/pyscript/pyscript/discussions/1973
In more than an occasion our users asked how to persist data in PyScript or how to share the FileSystem between main and workers. There are, in my opinion, at least 2 ways to go here: provide an ea...
Avatar
Can I read python311.zip without unzipping?
sai started a thread. 09/02/2024 23:12
Avatar
Avatar
zobi
Can I read python311.zip without unzipping?
Szabolcs Dombi 10/02/2024 09:12
The python core has zipimport which allows you to import from a zip. It only works for zipsafe modules which means no c extensions basically. (Probably also no package resources either...) At the latest version i think zipimport is already a frozen module. Look these up it might help (edited)
Avatar
TheCharlie654 11/02/2024 23:26
I've tried to implement a basic terminal worker for playing a text based hangman game in a web browser - however when the input field is left blank, it repeats the same prompt and then just hangs
23:27
This is shown in the console
23:29
and yes it does work fine when running the python file locally
Avatar
yooo, i have threading issue, so bassicaly i just used the threading library for threading but i realized that thread does not work exacly in pyscript, i was wondering if thier is a alternative because I know worker allows for multi threading but i dont get how it works or how to use it. It could be easier to just use html or js to this but I am not sure. here is my project: https://pyscript.com/@cheesy740/proj/latest any help or at least pointing me in the right direction would help. Thanks!
Avatar
Avatar
TheCharlie654
This is shown in the console
Andrea Giammarchi 12/02/2024 09:20
it's hard to help without a reproducible example page or demo code (not a screenshot of it)
Avatar
Andrea Giammarchi 12/02/2024 14:27
what's the purpose of time.sleep(0.5) in there and why that's not from pyscript import sync and then a sync.sleep(0.5) instead? ๐Ÿค”
Avatar
Avatar
Andrea Giammarchi
what's the purpose of time.sleep(0.5) in there and why that's not from pyscript import sync and then a sync.sleep(0.5) instead? ๐Ÿค”
Andrea Giammarchi 12/02/2024 14:29
to clarify, we don't get to control time native module, we do offer a sync.sleep out of the box though ... but in general, from a worker, I don't think you ever want to sleep(X) for the sake of it, it's not necessary.
Avatar
Avatar
Andrea Giammarchi
to clarify, we don't get to control time native module, we do offer a sync.sleep out of the box though ... but in general, from a worker, I don't think you ever want to sleep(X) for the sake of it, it's not necessary.
Andrea Giammarchi 12/02/2024 14:29
from what I can tell though, you are doing seppuku in a while loop and I am not sure I understand why that's needed .
Avatar
Avatar
Andrea Giammarchi
what's the purpose of time.sleep(0.5) in there and why that's not from pyscript import sync and then a sync.sleep(0.5) instead? ๐Ÿค”
TheCharlie654 12/02/2024 14:30
Ok got it, that was just in the original program but I guess that doesn't translate over to pyscript
Avatar
Avatar
TheCharlie654
Ok got it, that was just in the original program but I guess that doesn't translate over to pyscript
Andrea Giammarchi 12/02/2024 14:31
I think even in the original program it has no purpose ... if those requests are sync I don't understand why you would let the user wait extra half second more ... ๐Ÿค”
Avatar
Avatar
Andrea Giammarchi
I think even in the original program it has no purpose ... if those requests are sync I don't understand why you would let the user wait extra half second more ... ๐Ÿค”
Andrea Giammarchi 12/02/2024 14:32
I see sleeps all over the place though, so maybe I am not really understanding the purpose of the original program ... but for PyScript, you should time = sync.time and import that sync from the pyscript module. A try on top of the program should make it portable.
14:35
in particular I don't understand this Schrรถdinger winning condition ... ๐Ÿ˜… # If the game loop has ended and the win variable is True if win: # Calculates the total number of guesses by adding the lengths of the guessed_letters and guessed_words lists # Subtracts 1 from this to allow the first guess to be without penalty total_guesses = len(guessed_letters) + len(guessed_words) - 1 score = 26 - total_guesses print(f"\nYou win! The answer is {selected_word}") time.sleep(0.75) print(f"You lost {total_guesses} points based on the number of guesses") print(f"Score for this game: {score}") else: print(f"\nYou lose. The correct answer is {selected_word}")
14:35
you get a chance to win for like ... 0.75 seconds then you lost anyway ๐Ÿค”
14:38
on a second thought ... I get that score is accumulating and you don't necessarily win all the things on first attempt, still that part made me wonder more than I should've ... but hopefully I helped you moving forward ๐Ÿคž (edited)
Avatar
TheCharlie654 12/02/2024 17:53
Yeah thanks for the advice, the original program just used sleeps to delay certain parts of the game for animation/user experience purposes so that things don't all appear at the same time
Avatar
Syed Talal Jilani 12/02/2024 19:41
Can i web scrape data through pyscript using beautiful soup
Avatar
can you flush/update a line in terminal like you would do typicaly in a python console
Avatar
Goood mornin! I'm working on a chrome extension and I keep getting this error for everything I try to import. <py-config> [[interpreters]] src = "/runtime/pyodide.js" name = "pyodide-0.25.0" lang = "python" </py-config> <link rel="stylesheet" href="popup.css" /> <link rel="stylesheet" href="/runtime/pyscript.css" /> <script src="/runtime/pyscript.js"></script> </head> <body> <button id="hi" class="btn">Hello!</button> <button id="bye" class="btn">Goodbye!</button> <py-script src="./scripts/main.py"></py-script> </body> </html>from pyscript import window, when @when('click', '.btn') def handler(evt): id = evt.target.getAttribute("id") window.alert(id) I think the problem I'm running into has something to do with the fact that I have to install everything locally because of chromes Content Security Policy Maybe someone with more knowledge in this kind of thing could help me figure this out (edited)
Avatar
Avatar
Xenkuru
Goood mornin! I'm working on a chrome extension and I keep getting this error for everything I try to import. <py-config> [[interpreters]] src = "/runtime/pyodide.js" name = "pyodide-0.25.0" lang = "python" </py-config> <link rel="stylesheet" href="popup.css" /> <link rel="stylesheet" href="/runtime/pyscript.css" /> <script src="/runtime/pyscript.js"></script> </head> <body> <button id="hi" class="btn">Hello!</button> <button id="bye" class="btn">Goodbye!</button> <py-script src="./scripts/main.py"></py-script> </body> </html>from pyscript import window, when @when('click', '.btn') def handler(evt): id = evt.target.getAttribute("id") window.alert(id) I think the problem I'm running into has something to do with the fact that I have to install everything locally because of chromes Content Security Policy Maybe someone with more knowledge in this kind of thing could help me figure this out (edited)
Andrea Giammarchi 14/02/2024 17:09
it's interpreter = "/runtime/pyodide.js" eventually but it should rather point to .mjs ... be sure you are on latest PyScript though
Avatar
Avatar
Andrea Giammarchi
it's interpreter = "/runtime/pyodide.js" eventually but it should rather point to .mjs ... be sure you are on latest PyScript though
Andrea Giammarchi 14/02/2024 17:12
for instance, also the script that loads PyScript should be a type="module"
Avatar
Avatar
Andrea Giammarchi
it's interpreter = "/runtime/pyodide.js" eventually but it should rather point to .mjs ... be sure you are on latest PyScript though
ah! thank you! But I am still getting cannot import name 'document' from 'pyscript'
Avatar
Andrea Giammarchi 14/02/2024 17:16
so you're not on latest
Avatar
How does one check what version I am on?
Avatar
Andrea Giammarchi 14/02/2024 17:17
I will write once for all a gist on how to run locally MicroPython or Pyodide ... even if I think I wrote it already somewhere
Avatar
I used wget https://pyscript.net/latest/pyscript.js -o pyscript.js wget https://pyscript.net/latest/pyscript.css -o pyscript.css
17:18
Which should be the latest?
Avatar
I'm running pyodide 0.26.0a1 aswell
Avatar
Andrea Giammarchi 14/02/2024 17:19
latest is not latest ... it's actually defunct
Avatar
ah!
Avatar
Andrea Giammarchi 14/02/2024 17:21
this should be part of our docs but so far we didn't get much time for it ... pinging @ntoll as this topic keeps coming up almost once per week.
Avatar
Avatar
Andrea Giammarchi
this should be part of our docs but so far we didn't get much time for it ... pinging @ntoll as this topic keeps coming up almost once per week.
I appreciate your help! Took a lot of digging to figure what I needed to do lol. Thank you for pointing me to the right place. ๐Ÿ˜
๐Ÿ‘ 1
Avatar
Avatar
Andrea Giammarchi
this should be part of our docs but so far we didn't get much time for it ... pinging @ntoll as this topic keeps coming up almost once per week.
OK... it's on my TODO list. ๐Ÿ‘
๐Ÿฅณ 2
Avatar
2spooky2play 14/02/2024 21:57
hello! i am trying to call a python function when the page loads using <div py-onload="other_func" id="other_text"></div> this is the function def other_func(event): other_text = document.querySelector("#other_text") other_text.innerText = "Hello World" but the text is not appearing. what am i doing wrong?
Avatar
Avatar
2spooky2play
hello! i am trying to call a python function when the page loads using <div py-onload="other_func" id="other_text"></div> this is the function def other_func(event): other_text = document.querySelector("#other_text") other_text.innerText = "Hello World" but the text is not appearing. what am i doing wrong?
Jeff Glass 14/02/2024 22:52
PyScript doesnโ€™t load/initialize until after page load, so your listener wonโ€™t ever catch that event. To run as soon as possible, Iโ€™d just stick your code in an adjacent script tag.
Avatar
2spooky2play 14/02/2024 22:56
how do i do that? i havent done web dev in a while
Avatar
Avatar
2spooky2play
how do i do that? i havent done web dev in a while
Andrea Giammarchi 15/02/2024 09:34
the onload event is usually a window thing, not a per-element one. If you want to do anything with PyScript once the interpreter is ready you have py:ready event on the main or py:done once the code has been executed, so that this should actually work: <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="https://pyscript.net/releases/2024.1.3/core.css"> <script type="module" src="https://pyscript.net/releases/2024.1.3/core.js"></script> </head> <body> <div id="other_text"></div> <script type="py"> from pyscript import window, document # Pyodide specific thing from pyodide.ffi import create_once_callable, to_js def other_func(event): other_text = document.querySelector("#other_text") other_text.innerText = "Hello World" window.addEventListener( 'py:done', create_once_callable(other_func), to_js({"once": True}) ) </script> </body> </html>
Avatar
Avatar
Andrea Giammarchi
the onload event is usually a window thing, not a per-element one. If you want to do anything with PyScript once the interpreter is ready you have py:ready event on the main or py:done once the code has been executed, so that this should actually work: <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="https://pyscript.net/releases/2024.1.3/core.css"> <script type="module" src="https://pyscript.net/releases/2024.1.3/core.js"></script> </head> <body> <div id="other_text"></div> <script type="py"> from pyscript import window, document # Pyodide specific thing from pyodide.ffi import create_once_callable, to_js def other_func(event): other_text = document.querySelector("#other_text") other_text.innerText = "Hello World" window.addEventListener( 'py:done', create_once_callable(other_func), to_js({"once": True}) ) </script> </body> </html>
Andrea Giammarchi 15/02/2024 09:36
Please note that within a worker none of those Pyodide specific APIs are needed: <script type="py" worker> from pyscript import window, document def other_func(event): other_text = document.querySelector("#other_text") other_text.innerText = "Hello World" window.addEventListener('py:done', other_func, {"once": True}) </script> This would do the same and it will work eventually out of the box in MicroPython too (we're waiting for a release).
Avatar
PyScript offline docs: https://pyscript.github.io/docs/2024.1.3/user-guide/offline/ Feedback most welcome
โ€ผ๏ธ 1
๐Ÿ˜ 1
10:01
@Andrea Giammarchi ^^^^
Avatar
Avatar
ntoll
PyScript offline docs: https://pyscript.github.io/docs/2024.1.3/user-guide/offline/ Feedback most welcome
Andrea Giammarchi 15/02/2024 10:02
I did some minor edit (typos) yesterday to the comment I wrote a while ago and that definitively didn't go through but we can amend later as long as we have a reference page for this ๐Ÿ‘ Thank you very much indeed for tackling this, Nicholas ๐Ÿ™
Avatar
Aha... OK. We can refine. I did a quick edit / rewrite for readability. Happy to accept updates etc. ๐Ÿš€
Avatar
With the Pyscript Classic, I used <py-repl></py-repl> for debugging my app by simply putting it after <body> tag without importing anything. It helpped me to examine, change variables and HTML elements on the fly. This is missing from Pyscript Next With the Pyscript Next I tried <script type="py" terminal worker>. Unless I am missing something, it is not practical as a debugging tool. Also as soon as I enter two carriage returns, the Interactive Console exits. (edited)
Avatar
Wondering if someone could help me understand what im doing wrong. I'm working on my pyscript using pyodide as my interpreter and loading everything locally. I'm having trouble importing to_js from js and not sure what package files I need from the pyodide release or how to configure. <py-config> packages = ["js"] interpreter = "./pyodide/pyodide.mjs" </py-config> from pyscript import when import js from pyodide.ffi import to_js @when('click', '.copyText') def copyText(evt): element = evt.target js.navigator.clipboard.writeText(element.value)
03:53
ImportError: can't import name to_js
03:55
I would like to keep the size of the extension I'm making as small as possible so that why I haven't just pasted the whole pyodide release folder into my build
Avatar
nevermind this works from pyscript import when import js #copies an elements text to clipboard @when('click', '.copyText') def copyText(evt): element = evt.target.innerHTML js.navigator.clipboard.writeText(f"{element}") (edited)
Avatar
Avatar
Parviz O
With the Pyscript Classic, I used <py-repl></py-repl> for debugging my app by simply putting it after <body> tag without importing anything. It helpped me to examine, change variables and HTML elements on the fly. This is missing from Pyscript Next With the Pyscript Next I tried <script type="py" terminal worker>. Unless I am missing something, it is not practical as a debugging tool. Also as soon as I enter two carriage returns, the Interactive Console exits. (edited)
Andrea Giammarchi 16/02/2024 13:30
Also as soon as I enter two carriage returns, the Interactive Console exits.
we might look at this (very odd) and it might be an XTerm option we're missing but the terminal in PyScript (next) is not meant for debugging the dom, even if you should be able to reach nodes by importing pyscript.document and do Web stuff.
Avatar
Avatar
Xenkuru
nevermind this works from pyscript import when import js #copies an elements text to clipboard @when('click', '.copyText') def copyText(evt): element = evt.target.innerHTML js.navigator.clipboard.writeText(f"{element}") (edited)
Andrea Giammarchi 16/02/2024 13:39
it won't work on worker scripts ... from pyscript import window, when is the way, then window.navigator ... on the main page is the same js namespace, on workers it brigde main synchronously even if things are async in there.
Avatar
I'm getting the error "a is undefined" from this code and I have no idea why import matplotlib.pyplot as plt import seaborn import csv import pyscript def generate_graph(event): with open(r".\assets\parsed_dataset.csv") as csv_file: csv_reader = csv.reader(csv_file, delimiter=',') # Parses the data in the dataset csv file to obtain a list of raw data heart_rates = [] for i in csv_reader: heart_rates.append(i[1]) del heart_rates[0] # Converts the data from string to float for i in range(0, len(heart_rates)): heart_rates[i] = float(heart_rates[i]) # Compiles a graph from the data seaborn.set(rc={"figure.figsize": (8, 4)}) plt.ylabel("Frequency") graph = seaborn.distplot(heart_rates) # Outputs the graph pyscript.display(graph)
Avatar
Avatar
OpGamer97
I'm getting the error "a is undefined" from this code and I have no idea why import matplotlib.pyplot as plt import seaborn import csv import pyscript def generate_graph(event): with open(r".\assets\parsed_dataset.csv") as csv_file: csv_reader = csv.reader(csv_file, delimiter=',') # Parses the data in the dataset csv file to obtain a list of raw data heart_rates = [] for i in csv_reader: heart_rates.append(i[1]) del heart_rates[0] # Converts the data from string to float for i in range(0, len(heart_rates)): heart_rates[i] = float(heart_rates[i]) # Compiles a graph from the data seaborn.set(rc={"figure.figsize": (8, 4)}) plt.ylabel("Frequency") graph = seaborn.distplot(heart_rates) # Outputs the graph pyscript.display(graph)
Jeff Glass 17/02/2024 04:01
Very odd! Does it give you a line number or anything?
Avatar
Avatar
Andrea Giammarchi
it won't work on worker scripts ... from pyscript import window, when is the way, then window.navigator ... on the main page is the same js namespace, on workers it brigde main synchronously even if things are async in there.
it works in my extension now thank you!
๐Ÿฅณ 1
Avatar
how to do preventDefault on an event like in JS? I have this:
16:46
@when("submit", "#poster") async def on_submit(event: JsProxy): pass
16:46
but preventDefault is not a method of JsProxy (edited)
Avatar
and what is equivalent to console.log? I used print but nothing appears in the JavaScript console, and display meanwhile appends to the actual HTML (edited)
Avatar
Avatar
Ninjaa
and what is equivalent to console.log? I used print but nothing appears in the JavaScript console, and display meanwhile appends to the actual HTML (edited)
Jeff Glass 17/02/2024 17:14
Print should indeed appear in the consoleโ€ฆ are you running on the main thread or in a worker? It shouldnโ€™t matter, but Iโ€™m curious
Avatar
Avatar
Jeff Glass
Print should indeed appear in the consoleโ€ฆ are you running on the main thread or in a worker? It shouldnโ€™t matter, but Iโ€™m curious
My mistake, I must have been checking something wrong. print does appear in console
Avatar
Avatar
Jeff Glass
(To create a new FormData object)
has this become possible? I am using pyfetch just fine, but need to pass a body that is FormData. So how do I construct this FormData on the python side?
Avatar
it's ugly but I got it working:
18:14
from js import FormData @when("click", "#poster") async def on_submit(_): data = FormData.new() [input_el] = pydom["#files"] for file in input_el._js.files: data.append(file.name, file) await pyfetch(f"{API_HOST}/record", method="post", body=data)
18:15
I guess you need the from js import, need to do FormData.new() instead of FormData(), and need to access "private" _js
18:16
hopefully some of that becomes native soon, like pydom is without requiring exposure to the JS
Avatar
Getting this error while working with "beautifulsoup4", I'm working from the pyscripts.com web IDE. I have set it up in the package toml file but still getting this error,
Avatar
Avatar
OpGamer97
I'm getting the error "a is undefined" from this code and I have no idea why import matplotlib.pyplot as plt import seaborn import csv import pyscript def generate_graph(event): with open(r".\assets\parsed_dataset.csv") as csv_file: csv_reader = csv.reader(csv_file, delimiter=',') # Parses the data in the dataset csv file to obtain a list of raw data heart_rates = [] for i in csv_reader: heart_rates.append(i[1]) del heart_rates[0] # Converts the data from string to float for i in range(0, len(heart_rates)): heart_rates[i] = float(heart_rates[i]) # Compiles a graph from the data seaborn.set(rc={"figure.figsize": (8, 4)}) plt.ylabel("Frequency") graph = seaborn.distplot(heart_rates) # Outputs the graph pyscript.display(graph)
turt1eback 18/02/2024 00:20
in your html file, make sure you say "generate_graph" instead of "generate_graph()". i had this error earlier.
03:35
is this a function that pyscript would be able to run? it calls other functions and writes a file to the folder it's located in.
03:36
when i click the button that should run the function... nothing happens.
Avatar
Avatar
Arinze
Getting this error while working with "beautifulsoup4", I'm working from the pyscripts.com web IDE. I have set it up in the package toml file but still getting this error,
Avatar
Avatar
OpGamer97
I'm getting the error "a is undefined" from this code and I have no idea why import matplotlib.pyplot as plt import seaborn import csv import pyscript def generate_graph(event): with open(r".\assets\parsed_dataset.csv") as csv_file: csv_reader = csv.reader(csv_file, delimiter=',') # Parses the data in the dataset csv file to obtain a list of raw data heart_rates = [] for i in csv_reader: heart_rates.append(i[1]) del heart_rates[0] # Converts the data from string to float for i in range(0, len(heart_rates)): heart_rates[i] = float(heart_rates[i]) # Compiles a graph from the data seaborn.set(rc={"figure.figsize": (8, 4)}) plt.ylabel("Frequency") graph = seaborn.distplot(heart_rates) # Outputs the graph pyscript.display(graph)
Can you share your .csv file?
Avatar
Is it possible to use pyscript in jinja files? i try it in flask app, but im using jinjax for the template engine instead of flask default render template, here is the code
09:50
09:51
here is the pys.py to manipulate the value of id value
09:51
138 bytes
09:51
when i ran it in flask, got this error (without pressing + button)
09:52
09:57
i tried to put the functions in pys.py into the DevTest.jinja and it works
09:57
09:58
but unfortunately it doesnt look good imo having py-script tag inside the jinja files, especially if i want to reuse the functions again and again
Avatar
I can't access the example you posted could you link me to github repo or even share the code snippet. The link white blank.
Avatar
ok I have seen the example, thanks.
Avatar
Avatar
ReYu
Click to see attachment ๐Ÿ–ผ๏ธ
As far as I know Py-script always look for index.html as source. DevTest.jinja may not work as source but your code still works if you replace DevTest.jinja as index.html . https://_12.pyscriptapps.com/odd-voice/latest/
Avatar
Avatar
zobi
As far as I know Py-script always look for index.html as source. DevTest.jinja may not work as source but your code still works if you replace DevTest.jinja as index.html . https://_12.pyscriptapps.com/odd-voice/latest/
Nah, it works now, check it #โ•ฐโ•ธpython-help
Avatar
How to install a Pyscript package?
Avatar
Avatar
ReYu
but unfortunately it doesnt look good imo having py-script tag inside the jinja files, especially if i want to reuse the functions again and again
Andrea Giammarchi 19/02/2024 09:18
we always recommended external files instead of inline content and this is one of those great examples why we do so ๐Ÿ˜‰
Avatar
Avatar
0x77
How to install a Pyscript package?
Andrea Giammarchi 19/02/2024 09:19
via py-config tag or a config JSON/TOML file ... namely: packages = ["pandas"] as example. Also please use window from pyscript instead of importing stuff from js unless you really need the current worker global context there.
Avatar
Avatar
Andrea Giammarchi
via py-config tag or a config JSON/TOML file ... namely: packages = ["pandas"] as example. Also please use window from pyscript instead of importing stuff from js unless you really need the current worker global context there.
How can I get code completion to work, instead of being told that the PyScript package doesn't exist?
Avatar
Avatar
0x77
How can I get code completion to work, instead of being told that the PyScript package doesn't exist?
Andrea Giammarchi 19/02/2024 09:24
the list of available pyodide packages is here: https://pyodide.org/en/stable/usage/packages-in-pyodide.html
09:25
code completion is an IDE feature, we can't get you that via JS/Python itself ... but also I don't think we have a VS/X IDE extension for that, or at least not yet for config ... maybe we should at some point.
Avatar
Because it always has annoying error prompts
Avatar
Avatar
0x77
Because it always has annoying error prompts
Andrea Giammarchi 19/02/2024 09:26
if those annoy you visually you can pass plugins = ["!error"] and see errors only in console but the error would likely be the same.
Avatar
Avatar
Andrea Giammarchi
if those annoy you visually you can pass plugins = ["!error"] and see errors only in console but the error would likely be the same.
Yes, I just want to eliminate visual distractions. I'll give it a try.
Avatar
Andrea Giammarchi 19/02/2024 09:28
it might be that for broken out of the bo bootstrap errors that prompt might be still there, we didn't get many requests to fully drop the error but I haven't checked recently, sorry.
Avatar
Hi. I am trying to read a csv table using <input type="file" id="file-upload"> such that the user can select the table from his local machine. What I want to do is to read and display the head. Then I want the user to make some selection via drop down. Once the user clicks a second button I want to continue to read the file. I haven't done any web development before, so I am not really sure how to tackle this. What I had done so far is converting the file to bytes and then using BytesIO to get the data. my_bytes: bytes = await get_bytes_from_file(first_item) As far as I understand, I am reading in the whole file here. Which can be very inefficient with regards to the browsers memory (?) especially with regards to large files. Is there any way to read a file line by line without causing any browser security issues? (edited)
Avatar
Avatar
shyz
Hi. I am trying to read a csv table using <input type="file" id="file-upload"> such that the user can select the table from his local machine. What I want to do is to read and display the head. Then I want the user to make some selection via drop down. Once the user clicks a second button I want to continue to read the file. I haven't done any web development before, so I am not really sure how to tackle this. What I had done so far is converting the file to bytes and then using BytesIO to get the data. my_bytes: bytes = await get_bytes_from_file(first_item) As far as I understand, I am reading in the whole file here. Which can be very inefficient with regards to the browsers memory (?) especially with regards to large files. Is there any way to read a file line by line without causing any browser security issues? (edited)
Andrea Giammarchi 19/02/2024 11:58
I think the moment the OS passes the file to the browser you already have it in memory ... to not have it in memory you should stream the file instead from a backend endpoint and be able to parse and understand bytes in the header of the csv ... but that's not really possible AFAIK with just the file upload API/feature on the Web although it looks like events on progress might be triggered but that's 100% JS world https://w3c.github.io/FileAPI/#APIASynch
Avatar
Thank you for clarifying. If I understand you correctly, then I need to use JS to read the data line by line. Is it possible to combine JS and pyscript such that I can do something with each line using python while reading in the data? So basically what I would like to do is for each line send the data to pyscript, do something and then repeat with the next line.
Avatar
Avatar
shyz
Thank you for clarifying. If I understand you correctly, then I need to use JS to read the data line by line. Is it possible to combine JS and pyscript such that I can do something with each line using python while reading in the data? So basically what I would like to do is for each line send the data to pyscript, do something and then repeat with the next line.
Andrea Giammarchi 19/02/2024 13:37
on the main thread, you could use a FileReader via JS and add that progress listener and eventually abort the reading once you read enough data but ... you also need to parse yourself the binary data as it comes, because buffers are buffers without any knowledge about CSV or PNG or JPG or whatever file you are reading so it's entirely on you to parse every chunk of data, stop where you want, eventually keep reading from that point later on but like I've said, the next time you'll need to re-instrument the whole thing from the scratch, 'cause the API is not meant to resume binary reads so we're kinda incapable of providing all this out of the box and, most importantly, from a worker it'd be a mess because the File API will exists only on main but worker listeners are inevitably async (if meaning to work on main) so ... don't hold your breath yet, as I am afraid we need more primitives on the Web to offer that. (edited)
Avatar
chris.laffra 19/02/2024 16:36
This is the first time I try a PyScript app on an iPad. It all seems to work well, except the worker does not appear to start. I am trying to open the browser's console, but no luck. I tried Safari, Chrome, and Brave. Questions: (1) do workers work on iPad? (2) how do I open the console on a browser on an iPad? (I have a keyboard and Cmd+Option+I does not do the trick).
Avatar
Avatar
chris.laffra
This is the first time I try a PyScript app on an iPad. It all seems to work well, except the worker does not appear to start. I am trying to open the browser's console, but no luck. I tried Safari, Chrome, and Brave. Questions: (1) do workers work on iPad? (2) how do I open the console on a browser on an iPad? (I have a keyboard and Cmd+Option+I does not do the trick).
Andrea Giammarchi 19/02/2024 16:44
I am afraid I need an iPad to test but maybe my iPhone would answer that too ... SharedArrayBuffer might be to blame but I've tested our project in my iPhone and it worked so that knowwing more about your iPad OS version might help! (edited)
Avatar
Avatar
Andrea Giammarchi
I am afraid I need an iPad to test but maybe my iPhone would answer that too ... SharedArrayBuffer might be to blame but I've tested our project in my iPhone and it worked so that knowwing more about your iPad OS version might help! (edited)
chris.laffra 19/02/2024 16:47
iPad OS 17.3.1. I think all three browsers use WebKit (because iOS) and not Chromium. But not entirely certain.
Avatar
chris.laffra 19/02/2024 17:08
OK, you need a Macbook to see the dev console on an iPad. Interesting. I got this error message in the console
17:09
The SharedArrayBuffer object is used to represent a generic raw binary data buffer, similar to the ArrayBuffer object, but in a way that they can be used to create views on shared memory. A SharedArrayBuffer is not a Transferable Object, unlike an ArrayBuffer which is transferable.
17:13
My server uses these settings, which conflicts with the suggested setting for COEP. I will play a bit with those, I guess. In the past, I had issues with setting it to "require-corp", I remember...
17:18
Wow. Setting the header to require-corp actually fixed my issue on iPad. PySheets works remarkably well on an iPad, I must say ๐Ÿค“
๐Ÿ‘ 1
Avatar
well done chris. I was worried you were going to be hit by the PWA ban Apple is doing on all their iphone and ipad prods. but perhaps that's only in Europe ? - https://www.theregister.com/2024/02/16/apple_web_apps/
iBiz expresses regret for the impact of its entirely avoidable decision
Avatar
Avatar
chris.laffra
Wow. Setting the header to require-corp actually fixed my issue on iPad. PySheets works remarkably well on an iPad, I must say ๐Ÿค“
Andrea Giammarchi 20/02/2024 08:48
Yes, Safari is the only one requiring a special combo for those headers but that combo works fine on Chromium and Firefox too which is hwy we suggest that combo and that's also what mini-coi does. Glad you managed to solve and glad it worked! I did test on my iOS and everything was fine so I am happy to hear that's the case indeed.
Avatar
Avatar
Neon22
well done chris. I was worried you were going to be hit by the PWA ban Apple is doing on all their iphone and ipad prods. but perhaps that's only in Europe ? - https://www.theregister.com/2024/02/16/apple_web_apps/
Andrea Giammarchi 20/02/2024 08:51
We need to see what happens once 17.4 is out (and I am in EU so I should be affected) but if headers are set on the backend/server nobody will have any issue. The only possible issue is the usage of mini-coi as ServiceWorker in case this gets completely disabled as GitHub pages, to name one, won't be able anymore to enable SharedArrayBuffer but I think that won't be the case and, if it is, we will need to investigate possible alternatives although that would be a horrible breaking change in general ... Service Workers are not used only for PWAs, these are an essential primitive to intercept and deal with users' requests.
Avatar
Avatar
Andrea Giammarchi
on the main thread, you could use a FileReader via JS and add that progress listener and eventually abort the reading once you read enough data but ... you also need to parse yourself the binary data as it comes, because buffers are buffers without any knowledge about CSV or PNG or JPG or whatever file you are reading so it's entirely on you to parse every chunk of data, stop where you want, eventually keep reading from that point later on but like I've said, the next time you'll need to re-instrument the whole thing from the scratch, 'cause the API is not meant to resume binary reads so we're kinda incapable of providing all this out of the box and, most importantly, from a worker it'd be a mess because the File API will exists only on main but worker listeners are inevitably async (if meaning to work on main) so ... don't hold your breath yet, as I am afraid we need more primitives on the Web to offer that. (edited)
I think I changed my approach a bit. So now to know what file I am reading I am extracting the file extension from the name. Instead of the FileReader I am using papaparse. It builds on the FileReader API and now each row is an dictionary like object. This is what the JS code looks like currently. I probably can't use the progress listener but step kind of does the same thing. As you said, it is not possible to pause and restart a worker, so I'll have to stay on the main thread. Is it possible to send each row I've read to pyscript, do something there (line 58), and only continue reading once pyscript is done? I am sorry I don't yet understand if this is possible.
Avatar
Avatar
shyz
I think I changed my approach a bit. So now to know what file I am reading I am extracting the file extension from the name. Instead of the FileReader I am using papaparse. It builds on the FileReader API and now each row is an dictionary like object. This is what the JS code looks like currently. I probably can't use the progress listener but step kind of does the same thing. As you said, it is not possible to pause and restart a worker, so I'll have to stay on the main thread. Is it possible to send each row I've read to pyscript, do something there (line 58), and only continue reading once pyscript is done? I am sorry I don't yet understand if this is possible.
Andrea Giammarchi 20/02/2024 09:08
it is not possible to pause and restart a worker
the worker can be blocked it's the FileReader that AFAIK won't be available on a Worker so you need the library on main. What you also cannot pause and restart is the FileReader while reading a huge file ... you can abort once hedaers and rows you are after are read, you cannot then keep parsing from there. This library seems to do what you are looking for though, and hopefully it's smart enough to give you a way to render only parts of the huge CSV, still you need to handle manually those rows as these come.
09:15
if you do that in a worker though via Python it should also just work as long as papaparse is a js_modules.main library ... basically just copy over the code as Python, it should be doable.
Avatar
Avatar
Andrea Giammarchi
Yes, Safari is the only one requiring a special combo for those headers but that combo works fine on Chromium and Firefox too which is hwy we suggest that combo and that's also what mini-coi does. Glad you managed to solve and glad it worked! I did test on my iOS and everything was fine so I am happy to hear that's the case indeed.
chris.laffra 20/02/2024 10:48
My worker is loading fine now on iOS with COEP=require-corp and CORP=cross-origin headers set on my server. ๐ŸŽ‰ However, these headers now disable an embedded iframe on the same page that used to work fine ๐Ÿคทโ€โ™‚๏ธ . Not sure if I can get both to work at the same time. Ironically, the iframe is not even loading external HTML and uses a "srcdoc" argument with literal HTML. However, inside that HTML, script tags with external JS files fail to load.
Avatar
Avatar
chris.laffra
My worker is loading fine now on iOS with COEP=require-corp and CORP=cross-origin headers set on my server. ๐ŸŽ‰ However, these headers now disable an embedded iframe on the same page that used to work fine ๐Ÿคทโ€โ™‚๏ธ . Not sure if I can get both to work at the same time. Ironically, the iframe is not even loading external HTML and uses a "srcdoc" argument with literal HTML. However, inside that HTML, script tags with external JS files fail to load.
Andrea Giammarchi 20/02/2024 12:20
not sure it helps but jsdlvr works fine with those headers set (usually) but other CDNs might not ... if you can reach that external file via jsdlvr give it a try. As example, unpkg.com doesn't work with PyScript and workers neither.
Avatar
Avatar
chris.laffra
My worker is loading fine now on iOS with COEP=require-corp and CORP=cross-origin headers set on my server. ๐ŸŽ‰ However, these headers now disable an embedded iframe on the same page that used to work fine ๐Ÿคทโ€โ™‚๏ธ . Not sure if I can get both to work at the same time. Ironically, the iframe is not even loading external HTML and uses a "srcdoc" argument with literal HTML. However, inside that HTML, script tags with external JS files fail to load.
Andrea Giammarchi 20/02/2024 13:43
allow="cross-origin-isolated" as iframe attribute might solve that, please share if it does, thank you!
13:43
Cross-origin isolation enables a web page to use powerful features such as SharedArrayBuffer. This article explains how to enable cross-origin isolation on your website.
Avatar
chris.laffra 20/02/2024 13:50
The load issue came from images that Folium loads using leaflet.js, which come from cartodb.com. Both the scripts and images it loads are cross-origin. I patched the script to add crossorign="anonymous" to each script and img inside the frame and that does the trick for this case.
๐Ÿ‘ 1
Avatar
This would be another excellent thing to add to the examples (like a worker example).
Avatar
Avatar
Andrea Giammarchi
Also as soon as I enter two carriage returns, the Interactive Console exits.
we might look at this (very odd) and it might be an XTerm option we're missing but the terminal in PyScript (next) is not meant for debugging the dom, even if you should be able to reach nodes by importing pyscript.document and do Web stuff.
Thanks. @Andrea Giammarchi .
Avatar
Hi guys, is there a possibility using pyscript to copy text from the browser to the clipboard ?
Avatar
Avatar
dast
Hi guys, is there a possibility using pyscript to copy text from the browser to the clipboard ?
I did it using the ltk and: ltk.Button("Copy text", ltk.proxy(lambda event: copy_iso_text(event))).addClass("copybtn") and def copy_iso_text(event): """ select the iso text input and copy - for user to paste elsewhere """ ltk.jQuery("#isolabel").select() ltk.window.document.execCommand("copy")
โค๏ธ 1
Avatar
Avatar
Neon22
I did it using the ltk and: ltk.Button("Copy text", ltk.proxy(lambda event: copy_iso_text(event))).addClass("copybtn") and def copy_iso_text(event): """ select the iso text input and copy - for user to paste elsewhere """ ltk.jQuery("#isolabel").select() ltk.window.document.execCommand("copy")
thank you. looks interesting. But what is ltk ?
11:20
SCA and DAST for one low price. Fix vulnerabilities on every build. Easy-to-integrate solution. Build, manage, and monitor SBOMs.
11:20
I guess you mean this. I will check it out
Avatar
Avatar
Neon22
This would be another excellent thing to add to the examples (like a worker example).
chris.laffra 21/02/2024 13:42
Definitely. It was quite a puzzle ๐Ÿค“
Avatar
Avatar
Andrea Giammarchi
if you do that in a worker though via Python it should also just work as long as papaparse is a js_modules.main library ... basically just copy over the code as Python, it should be doable.
Great idea. Papaparse is a js_modules.main library and I've managed to translate most of the code to python. But I haven't found a way to change the main part to python which is lines 53 to 63. How to change step : function(row){ ... such that python knows to trigger the function when step is called? Also this part looks a bit like a dict. So I tried that, but dict keys can't be called like attributes, which is what papaparse seems to be doing internally. (edited)
Avatar
Avatar
shyz
Great idea. Papaparse is a js_modules.main library and I've managed to translate most of the code to python. But I haven't found a way to change the main part to python which is lines 53 to 63. How to change step : function(row){ ... such that python knows to trigger the function when step is called? Also this part looks a bit like a dict. So I tried that, but dict keys can't be called like attributes, which is what papaparse seems to be doing internally. (edited)
Andrea Giammarchi 21/02/2024 16:43
it's an ongoing discussion and I hear you ... for multiple callls to the same function use pyodide,ffi,create_prosy to wrap it, for Python to JS object literals use pyodide.ffi.to_js and pass js.Object.fromEntries as dict_converter option. We're actively trying to make this as smooth as posible as we speak!
Avatar
Hey all, I was trying out PyScript but came across this weird behavior of print() function when used with input. This is the code I am writing print("Hello") a = input() print("You entered", a) But on the terminal, it prints "Hello" twice on two lines and then wait for another input. My question is: Why is one print statement printing stuff twice? Note: This only happens when I use input() after print(). Example link - https://pyscript.com/@justani/weathered-lab/latest
Avatar
Probably has to do with how xterm.js behaves. Had a similar issue too: https://discord.com/channels/972017612454232116/1187721609193213972/1187721611080638485
Avatar
Andrea Giammarchi 22/02/2024 13:52
I think I need to dig more into this specific issue as we have no issues otherwise: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="https://pyscript.net/releases/2024.1.3/core.css"> <script type="module" src="https://pyscript.net/releases/2024.1.3/core.js"></script> </head> <body> <script type="mpy" worker> from pyscript import window print("Hello") a = window.prompt() print("You entered", a) </script> </body> </html> (edited)
Avatar
Andrea Giammarchi 22/02/2024 14:03
I also have no issue whatsoever with this test case: <!doctype html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>PyTerminal Main</title> <link rel="stylesheet" href="https://pyscript.net/releases/2024.1.3/core.css"> <script type="module" src="https://pyscript.net/releases/2024.1.3/core.js"></script> <style>.xterm { padding: .5rem; }</style> </head> <body> <script type="py" worker terminal> import code code.interact() </script> <script type="py" worker terminal> import code code.interact() </script> </body> </html>
14:04
you can try test = input("> "); print("Hello, World!"); print(test); and either provide a line or not, it will work as expected
Avatar
Jeff Glass 22/02/2024 14:06
I can recreate @justani 's issue using the following: <!doctype html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>PyTerminal Main</title> <link rel="stylesheet" href="https://pyscript.net/releases/2024.1.3/core.css"> <script type="module" src="https://pyscript.net/releases/2024.1.3/core.js"></script> <style>.xterm { padding: .5rem; }</style> </head> <body> <script type="py" worker terminal> print("Hello") a = input() print("You entered", a) </script> </body> </html>
14:07
"Hello" appears twice in the terminal
14:07
Avatar
One hack to fix this is to always give a prompt text as a parameter to input(). Something like this doesn't print "hello" twice: print("hello") a = int(input('type a number')) print(a)
Avatar
Andrea Giammarchi 22/02/2024 14:25
thanks @Jeff Glass , I can now reproduce it too ... it looks to me this is an Xterm.js upstream bug because either empty input or input("") won't stop nor ask for user input, it just keeps going which is undesired. AFAIK and IIRC we don't change that behavior at all so maybe this could be even readline module to blame, which is why I think we need a deeper investigation. Put any content in the input("...") like even an invisible zero-width space char and everything is fine. This works to me indeed: <!doctype html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>PyTerminal Main</title> <link rel="stylesheet" href="https://pyscript.net/releases/2024.1.3/core.css"> <script type="module" src="https://pyscript.net/releases/2024.1.3/core.js"></script> <style>.xterm { padding: .5rem; }</style> </head> <body> <script type="py" worker terminal> print("Hello") a = input("โ€‹") print("You entered", a) </script> </body> </html> (edited)
14:25
please do copy/paste exactly that code, there is an invisible zero-width spce char in that input ๐Ÿ˜‰
๐Ÿ‘€ 1
14:26
The zero-width space (โ€‹), abbreviated ZWSP, is a non-printing character used in computerized typesetting to indicate word boundaries to text-processing systems for scripts that do not use explicit spacing, or after characters not followed by a visible space after which there may be a line break.
Avatar
Jeff Glass 22/02/2024 14:27
In a few minutes of poking around, I would concur, this feels like an upstream error, as we're not doing much but passing through to the xterm/readline.
14:27
@justani Thanks for reporting this here! If you wanted to open a bug on GitHub with your sample code/findings, we'd be appreciative ๐Ÿ˜
๐Ÿ‘ 1
Avatar
Andrea Giammarchi 22/02/2024 14:29
I can also open an issue there but I am sure there is one already ... will check
Avatar
I went through the issues and docs before posting here, found no such example.
Avatar
Andrea Giammarchi 22/02/2024 15:12
issue opened ... it's also possible this is stdin in pyodide or a combination with readline https://github.com/xtermjs/xterm.js/issues/4966#issuecomment-1959612496
Details Browser and browser version: any OS version: any xterm.js version: latest Steps to reproduce We are using this (lovely btw) project to fuel PyScript and our users noticed that if an input()...
Avatar
Andrea Giammarchi 22/02/2024 15:24
I am basically forwarding this (wrongly opened) Xterm.js issue: xtermjs/xterm.js#4966 Apparently when no argument/parameter is passed to input(), or input("") with empty string, expectati...
Avatar
Jeff Glass 22/02/2024 18:37
That's the directory used as the home directory by the Pyodide runtime https://pyodide.org/en/stable/search.html?q=directory#. Keeping home there isn't mandatory, but it does potentially increase compatibility with other packages built with/for Pyodide
Avatar
How would one define a javascript fucntion in the latets version and then use that in Pyscript?
Avatar
Avatar
Eric
How would one define a javascript fucntion in the latets version and then use that in Pyscript?
Jeff Glass 22/02/2024 19:33
<script> function myfunc(val) { console.log("You said", val); } </script> <script type="py"> from pyscript import window window.myfunc("Hi from PyScript") </script>
19:34
Note that if you're declaring a variable that you want to pull into PyScript, you'll want to use var instead of let or const
Avatar
Thanks for this
19:35
What if you want to import a fucntion from a module you have in your page is that the same proceedure
Avatar
Jeff Glass 22/02/2024 19:37
Not the same - the syntax above only works for objects in the page global scope (window) - you can use the js_modules feature in your configuration to import that module directly via PyScript, which replaces the need to include that module via a script tag https://pyscript.github.io/docs/2024.2.1/user-guide/configuration/#javascript-modules
Avatar
So if I defined a variable that holds an imported function I would not be able to pass it?
Avatar
Jeff Glass 22/02/2024 19:50
Here's an example using js_modules to bring a module in directly: <!-- Index.html --> <py-config> [js_modules.main] "./mymod.js" = "mymod" </py-config> <script type="py"> from pyscript.js_modules import mymod print(f"Hello {mymod.name}, my favorite number is {mymod.number}") </script> //mymod.js export const name = 'ric'; export const number = 42;
Avatar
Avatar
Eric
So if I defined a variable that holds an imported function I would not be able to pass it?
Jeff Glass 22/02/2024 19:50
Can you explain a little more here? I'm not sure I understand
Avatar
Greetings
05:01
I guess no oneโ€™s on this time of night.
Avatar
@Jeff Glass How can I import python transformers lib?
Avatar
Avatar
daryl4240
I guess no oneโ€™s on this time of night.
We're in all sorts of timezones... ๐Ÿ˜‰
Avatar
Sorry I fell asleep as I was listening to audible. Iโ€™m new to pyscripting, yet I have a program that I made and itโ€™s on my website but itโ€™s not performing correctly, I believe because of my lack of pay-script syntax knowledgeโ€ฆ
11:36
Excuse me pay-script
11:36
This dang spell corrector I said. Py Script
11:37
How do you turn off this spell corrector in here?
Avatar
Avatar
daryl4240
Sorry I fell asleep as I was listening to audible. Iโ€™m new to pyscripting, yet I have a program that I made and itโ€™s on my website but itโ€™s not performing correctly, I believe because of my lack of pay-script syntax knowledgeโ€ฆ
Andrea Giammarchi 23/02/2024 13:15
without any live example to check or see it's hard to help but be aware that Pyodide takes up to X seconds to bootstrap so if it's about bootstrap time consider using MicroPyton instead or delegate to a worker anything Pyodide related.
Avatar
Szabolcs Dombi 23/02/2024 13:41
under the [files] section of the pyscript.toml, Can I add an archive (.zip or .tar.gz) which gets unpacked automatically? Pyodide supports it natively link.
Avatar
Avatar
Szabolcs Dombi
under the [files] section of the pyscript.toml, Can I add an archive (.zip or .tar.gz) which gets unpacked automatically? Pyodide supports it natively link.
Jeff Glass 23/02/2024 13:43
No automatically, youโ€™d need to either use the Pyodide FFI or a Python library like zipfile in the standard library to unpack it
๐Ÿ‘ 1
Avatar
Szabolcs Dombi 23/02/2024 13:49
i think the reason for this api is to support many files with folder structure plus not to have the content duplicated. because wheels are already zip, the unzip functionality is there, so this api is free in terms of extra code size for pyodide. i think it would be a nice-to-have feature if it would be working out of the box with [files]. what do you think?
Avatar
Jeff Glass 23/02/2024 13:51
I worry that this overcomplicates [files] - if we automatically unpack zip files, how do we handle the case where a use wants to include a zip-file as-is?
Avatar
Szabolcs Dombi 23/02/2024 14:00
i see, right now entries are in form of "url" = "path/to/file" or "url" = "" i can think of a special prefix on the right hand side. unpacking based on just the type would be terrible just like the Dockerfile ADD command. "url" = "unpack:path/to/extract" or "url" = {"path": "path/to/extract", "format": "gztar"} first one looks shady, not sure if the second is doable. an explicit format is actually nice to have as pyodide also has a format parameter.
Avatar
Andrea Giammarchi 23/02/2024 14:05
I think there are no name limitations so that unpack:folder could be already a folder ... rather than a prefix then, I'd consider a suffix ... "/url/folder.zip" = "./folder/*" to define where to extract content ... it's true that theoreticlaly a file or folder could also be named ./folder/* but I suspet that's an extremely edge case and it looks for troubles out of any path convention. (edited)
14:06
gosh this space has issues with ./folder/* - edit it was me forgetting the ` around ... ๐Ÿคฆ (edited)
14:06
now, that's what I meant
Avatar
Szabolcs Dombi 23/02/2024 14:07
both ending with a / or /* makes sense
Avatar
Szabolcs Dombi 23/02/2024 14:15
I have just tested what happens at the moment with the /*. Currently we get a file called "*" which can be opened and unzipped from python. https://pyscript.com/@szabolcsdombi/zip-test/latest i don't think it would disrupt anyone's existing code if the archive would have been unpacked instead. i now like the /* ending more. (edited)
14:17
not sure if micropython has unpack though
Avatar
Andrea Giammarchi 23/02/2024 14:28
the current convention is, IIRC, that "source.txt" = "./folder/" simplifies the result as ./folder/source.txt so just the trailing / is not enough, it was this Discrod incapable of dealing with my Markdown input, I always meant ./folder/* as special meaning for .zip source files only and nothing else ... we can then consider .tar.gz and stuff but let's move one step per time here ... if it works with .zip it might work with others but the more formats we handle, the least MicroPython is likely able to catch up ... @Jeff Glass do you know if mp can handl zip from its package remote repo? ๐Ÿค”
14:33
this looks ideal for that scenario though ... I see what I can do https://pyodide.org/en/stable/usage/api/js-api.html#pyodide.unpackArchive
Avatar
Avatar
Andrea Giammarchi
the current convention is, IIRC, that "source.txt" = "./folder/" simplifies the result as ./folder/source.txt so just the trailing / is not enough, it was this Discrod incapable of dealing with my Markdown input, I always meant ./folder/* as special meaning for .zip source files only and nothing else ... we can then consider .tar.gz and stuff but let's move one step per time here ... if it works with .zip it might work with others but the more formats we handle, the least MicroPython is likely able to catch up ... @Jeff Glass do you know if mp can handl zip from its package remote repo? ๐Ÿค”
Jeff Glass 23/02/2024 14:38
I looked around a bitโ€ฆ looks like uzlib is as close as it comes, which can do zlib and gzip but not generic zip
Avatar
Avatar
Jeff Glass
I looked around a bitโ€ฆ looks like uzlib is as close as it comes, which can do zlib and gzip but not generic zip
Andrea Giammarchi 23/02/2024 14:57
nothing else in mip ? I think that module is for streams or single entries, it wouldn't work with .tar.gz or .zip with structure .. I have successfully implemented packages extractor on pyodide though, and tested it works as expected ... not sure we should move forward but maybe that's welcome for more complex scenario, big zips and MicroPython likely don't like each other anyway ... thoughts? /cc @ntoll
14:59
this might at least enable .tar.gz entries ... not sure though, the fact it has no updates since 2016 looks sketchy to me https://pypi.org/project/micropython-utarfile/
utarfile module for MicroPython
Avatar
Avatar
Andrea Giammarchi
this might at least enable .tar.gz entries ... not sure though, the fact it has no updates since 2016 looks sketchy to me https://pypi.org/project/micropython-utarfile/
Andrea Giammarchi 23/02/2024 15:02
this snippet looks promising though ... https://forum.micropython.org/viewtopic.php?t=10270
Avatar
Avatar
Andrea Giammarchi
I think there are no name limitations so that unpack:folder could be already a folder ... rather than a prefix then, I'd consider a suffix ... "/url/folder.zip" = "./folder/*" to define where to extract content ... it's true that theoreticlaly a file or folder could also be named ./folder/* but I suspet that's an extremely edge case and it looks for troubles out of any path convention. (edited)
Andrea Giammarchi 23/02/2024 15:19
to strenght further the fact the whole thing is going to be extracted (possibly with subfolders too) maybe /** would be more explicit, or even /**/* ... although the /* looks fine to me. (edited)
Avatar
Andrea Giammarchi 23/02/2024 16:15
anyway, PR here for at least enabling pyodide ... we can tackle MicroPython later on, imho https://github.com/pyscript/polyscript/pull/84
In this Discrod conversation a user asked to be able to provide a single .zip file (or .tar.gz or other formats) to be easily extracted into a generic destination folder. As pyodide provides a way ...
๐Ÿฅณ 1
๐Ÿš€ 1
Avatar
Is anyone on right now?
04:04
Hello?
04:05
I need help from an experienced pyscriptor
04:10
I guess whatโ€™s gonna be my problem is getting on this thing when one of the experts happen to be on it
04:13
Well I guess Iโ€™ll have to do it differently. I would like an expert to please visit my website firstmessiahnic.life. After you get there look in the top right corner and you will see a link Social_Community_Activities.
04:15
Please click on the link. It will open up to more pages. The tope page says color by type. Please click that link and tell me if you see anything after the word output is displayed. If you see a blank page thatโ€™s my problem.
04:19
Whatโ€™s supposed to happen is I have a python program where a canvas can be drawn on by typing in the answers to a number of questions. The program works great, but my pyscript HTML knowledge is what I believe the problem is that the program does not display.
04:20
Since no one is here at this time I hope one of the pyscript experts can visit my site and inform me what pyscript tags I should use so the program can display and respond to input
Avatar
@Andrea Giammarchi Thanks for responding but I have no Idea what you are talking about, however if you could just read my HTML and please tell me what syntax I am missing. I would really appreciate it.
Avatar
I use the code "img = cv2.imread(file)" to read the image in my pythonProject, I just open .jpg or .png on my desktop. But, I don't know how to read the file which selected from Html page. What should I do in the part of pyscript codes?
Avatar
Hello
07:32
@Minecrazy Iโ€™m not an expert, I donโ€™t think any of the professionals are here right now. Iโ€™m sorry but I canโ€™t answer your question
Avatar
Avatar
daryl4240
Hello
Hello
Avatar
I just learned about pyscript 2 weeks ago
Avatar
I just learned about pyscript 2 days ago
Avatar
Thatโ€™s cool
Avatar
my English is not good, it is hard for me to find the solution
Avatar
Well what language do you speak>
07:36
?
Avatar
chinese
Avatar
Are you using an interpreter program?
Avatar
There are almost no pyscript tutorials on the Chinese Internet
Avatar
Avatar
daryl4240
Are you using an interpreter program?
sometimes
Avatar
Pyscript is only 2 years old and still in development
07:38
The inventors have already changed the basic syntax about three times, trying to improve the process
Avatar
I ask Chatgpt3.5 for help, but it don't know about anything of pyscript
Avatar
Currently most chatgptโ€™s in the US are three years behind
07:41
So the only place to learn pyscript right now is pyscript dot co and dot net
07:41
Pyscript dot com
07:42
Oh and this chat room, but the experts are asleep right now
07:46
All I can tell you is that pyscript is only 2 years old so itโ€™s still under development
Avatar
cv2.imread() needs the file path, however e.target.files get the file but not the path. How can I read e.target.files.item(0) while using cv2?
Avatar
I solved it. It need numpy.
Avatar
Avatar
Andrea Giammarchi
nothing else in mip ? I think that module is for streams or single entries, it wouldn't work with .tar.gz or .zip with structure .. I have successfully implemented packages extractor on pyodide though, and tested it works as expected ... not sure we should move forward but maybe that's welcome for more complex scenario, big zips and MicroPython likely don't like each other anyway ... thoughts? /cc @ntoll
Is there not a browser native zip/tgz API we could use no matter the runtime target..? I mean, browsers deal with compressed assets all the time. (Just wondering out loud)
Avatar
Avatar
ntoll
Is there not a browser native zip/tgz API we could use no matter the runtime target..? I mean, browsers deal with compressed assets all the time. (Just wondering out loud)
Andrea Giammarchi 24/02/2024 12:17
there's only this I might try but it's super bloated with stuff we don't really need unless we want to provide our own mip module for MicroPython https://gildas-lormeau.github.io/zip.js/ ... I will give it a shot on Monday but I can tell already it needs re-packaging (it's OK, we do that already ... a lot) and it will then enable only zip files and not others like pyodide does instead.
zip.js is a JavaScript library to zip and unzip files.
Avatar
Hello?
14:09
Is anybody here?
Avatar
Ok I will try a different approach
14:19
Here is my syntax for the pyscript. When I run this page I donโ€™t get a display.
Avatar
Actually itโ€™s the <body> tag portion: <body> <h1>Color By Coordinates</h1> <dialog id=โ€œLoadingโ€><h1>Loadingโ€ฆ</h1></dialog> <br> <section> <div id=โ€œOutputโ€>Output</div> </section> <py-script src=โ€œ/color_by_coordinates.py config=โ€œcolor_pyscript.tomlโ€></py-script> </body>
14:32
So when I run this in the browser I get the primary color by coordinates to display. The dialog message does not display, but the Output word displays, then thereโ€™s nothing else. And I waited for a long time thinking that it just takes a while to load the program but thatโ€™s not the issue.
14:32
The problem I believe is my syntax. Iโ€™m either missing something or using the wrong syntax
14:33
Can an experienced person please help me?
Avatar
Avatar
daryl4240
Actually itโ€™s the <body> tag portion: <body> <h1>Color By Coordinates</h1> <dialog id=โ€œLoadingโ€><h1>Loadingโ€ฆ</h1></dialog> <br> <section> <div id=โ€œOutputโ€>Output</div> </section> <py-script src=โ€œ/color_by_coordinates.py config=โ€œcolor_pyscript.tomlโ€></py-script> </body>
I think you are using the wrong syntax for dialog message. You must use <dialog open id=โ€œLoadingโ€><h1>Loadingโ€ฆ</h1></dialog> (edited)
Avatar
@zobi Thanks but what about the py-script? What am I doing wrong there?
Avatar
Now it says Loadingโ€ฆ for the last 1/2 hour
Avatar
while Iโ€™m thinking on it, can pyscript utilize OpenPyXl and Tkinter and SQLite3?
22:18
Oh and what about PyQt5?
22:19
Or MySQL
Avatar
I guess people dnโ€™t come to this page very often
Avatar
@daryl4240 most of those packages don't have python only code but use ext c? libraries. "Pyscript" allows you to run python instead of javascript (ok, alongside Javascript) in the browser. How could tkinter work inside a browser. Wouldn't you use html and css instead of tkinter. I mean that's what the browser does so well. You can run Python standalone (not in the browser) by just using it from your own filesystem and using tkinter (I like customtkinter https://pypi.org/project/customtkinter/ myself). I suggest you read the docs page and set your projects up like in the examples. Then experiment to see if you can get it to do what you want. But its not nodejs and doesn't run on a server. Its 'just' python instead of Javascript in the browser. Latest docs are here: https://pyscript.github.io/docs/2024.2.1/ and there are examples also. You can use pyscript.com to run test scripts without doing the full setup/hosting yourself. Hope this helps...
Create modern looking GUIs with Python
Avatar
@Neon22 Thanks for the advice. Are you still in the room?
Avatar
Ok I asked earlier if someone could help me with the syntax that I have on my website that is supposed to give users access to my python based drawing program. The program works great, but my pyscript HTML is not allowing the program to be run. I am hoping I can find someone to let me know what syntax is missing. Actually itโ€™s the <body> tag portion: <body> <h1>Color By Coordinates</h1> <dialog id=โ€œLoadingโ€><h1>Loadingโ€ฆ</h1></dialog> <br> <section> <div id=โ€œOutputโ€>Output</div> </section> <py-script src=โ€œ/color_by_coordinates.py config=โ€œcolor_pyscript.tomlโ€></py-script> </body>
03:42
I already fixed the dialog tag
03:42
But the program does not load or show
Avatar
I looked at your color page. You reference sys, numpy, +more but not in your toml file. Not sure why you'd need sys. My strong suggestion is the following. open a page on pyscript.com. Modify the exaple using the help docs as a guide (e.g. section there on the toml stuff) then - slowly - one package at a time - add them and make a small python prog that prints out something in the terminal. Use pyodide initially (its got a terminal). Then you'll get everything in and working. Then move to your personal page. IMHO its too much to ask people to follow links and debug your pages and check your locations for where you've put your packages (e.g. I hope I don't have access to your webhosting protected pages behind .htaccess etc.) Its very insecure even if you don't mind if people do. IMHO if you eat the Elephant one bite at a time - you will get there. Hope this helps
๐Ÿ‘ 1
03:51
That's how I do it and my Elephant is getting smaller every day.
Avatar
So you were able to see the python code as well?
03:53
And what does IMHO mean I donโ€™t like abbreviations
03:54
So are you saying that the python coding like import says and os could be the cause of the problem?
03:54
Import says
03:55
Hey do you know how to turn off the spell corrector in this room?
03:56
I hate spell correctors. You type one thing and it changes your words
Avatar
I don't know I don't turn on/off spelling corrections - I don't get corrections. I can't help you with debugging discord. sys is an important python package which you should really learn about. Its very much outside the scope of this discord group to explain such things. There is an enormous amount of information freely available to you to look up. I hit F11 (that's an abbreviation btw) and used the inspector to open the console and its says just one line - Uncaught SyntaxError: Unexpected token 'export' You can see these things for yourself. Do you know Chrome browser or firefox has a debugger. Once again - out of the scope of this discord chat server to explain how to use the Browser. I think you need to do quite a bit more research. I wish you luck. I still strongly suggest you follow the approach I have outlined.
Avatar
Ok Iโ€™ll look it up on my own I just thought the experts would simply tell me what to type for the index.html portionโ€ฆ
Avatar
unfortunately most people are very busy with their own stuff. I have several projects that need my attention right now. I don;t think there is a simple - just do this - for your problems. They are layered one in top of each other and need direct examination. Hence my suggestion to start simple and then get more complex
04:06
the index.html seems fine. THe src file and teh tml need a lot of attention
Avatar
I already know itโ€™s most likely the HTML syntax that I have to redo. The program itโ€™s self works fine on my computer and also on this site called repl
04:07
So I know it can be done, probably not with pyscript yet
Avatar
nope. I think you have misunderstood what it means for python to run in the browser. You should look at the examples.
04:08
There is no site called repl. The repl is an interactive python shell. That's all it is
04:09
You are sandboxed inside the Browser. Look up sandbox
Avatar
Youโ€™re probably right, I might have to resort to getting a deeper understanding of Django which is what Iโ€™m currently trying to do, but I hope one day pyscript can be a little more able to work with this program
04:10
Yes it is on my iPad itโ€™s an app where people can code all sorts of languages
Avatar
Django is a serverside CMS. I don't understand the connecton between these things. They are all entirely different
04:11
nope that's not what Django is. Sorry
Avatar
My mistake itโ€™s Replit
04:13
I know but The say that currently to get python programs to work on the website you have to use Django or flask
04:13
However I was hoping that I could use pyscript because itโ€™s less confusing to me
04:14
But for now I think Iโ€™m going to have to use those tools to make my program work on the website
04:15
Yet I have this gut feeling that pyscript will work if I just find the right syntaxโ€ฆ
Avatar
replit is an online IDE. I don't know how well this integrates with pyscript. You shoudl be asking their dev team about this. Not here. Django is a server side CMS. It doesn't run python in the browser. I think you need to look all these things up on Wikipedia. Pyscript allows you to run python in the browser.
04:16
yes - looking at what your site indicates you want to do - yes pyscript wil probably do it. but start at the beginning please. Sadly I have to go now. I still strongly suggest you follow the sequence of actions I have porposed
Avatar
No my drawing program was able to work on REplit not the pyscript
Avatar
ask replit. not here. this is not replit
Avatar
I was saying that if the drawing program could work there then I know that with the proper syntax it can work in pyscript
Avatar
I just agreed with you. So learn how to do it - as above. Cheers...
Avatar
I know pyscript is a new thing and itโ€™s still in development
04:20
Alright have a great time. Thanks for your help
Avatar
Avatar
daryl4240
@zobi Thanks but what about the py-script? What am I doing wrong there?
You forgot to write open in the dialog tag.You can refer this.https://_12.pyscriptapps.com/round-bush/latest/
Avatar
Avatar
daryl4240
Ok I asked earlier if someone could help me with the syntax that I have on my website that is supposed to give users access to my python based drawing program. The program works great, but my pyscript HTML is not allowing the program to be run. I am hoping I can find someone to let me know what syntax is missing. Actually itโ€™s the <body> tag portion: <body> <h1>Color By Coordinates</h1> <dialog id=โ€œLoadingโ€><h1>Loadingโ€ฆ</h1></dialog> <br> <section> <div id=โ€œOutputโ€>Output</div> </section> <py-script src=โ€œ/color_by_coordinates.py config=โ€œcolor_pyscript.tomlโ€></py-script> </body>
FabioRosado 25/02/2024 15:43
Can you share your full code either as a repo, replit or on pyscript.com there might be a lot going on and itโ€™s hard to help without seeing the full code
Avatar
Oh so if I share it all on pyscript.com then you all can see it and help me with it from there?
Avatar
Thanks I feel so silly not thinking of that before
16:47
So I ran the code in python.com as Color_by_typing. Hereโ€™s the link: https://darylpacheco.pyscriptapps.com/color-by-typing/latest/
16:48
I donโ€™t really understand how to correct the syntax in my main.py syntax.
Avatar
Do I need to make the suggested changes in my main.py syntax or the index.HTML syntax?
17:06
@FabioRosado Thanks I didnโ€™t realize just putting everything on pyscript.com would reveal what the problem is. Now I just donโ€™t understand how to fix the problemโ€ฆ
Avatar
Avatar
Andrea Giammarchi
it's an ongoing discussion and I hear you ... for multiple callls to the same function use pyodide,ffi,create_prosy to wrap it, for Python to JS object literals use pyodide.ffi.to_js and pass js.Object.fromEntries as dict_converter option. We're actively trying to make this as smooth as posible as we speak!
Thanks a lot @Andrea Giammarchi. This worked for me. For anyone who needs to do something similar with a JS function like structure, this is what I did: In index.html I created var config within script tags as shown before, but leaving step undefined. I edit this in pyscript using window.config.step = create_proxy(step_function) the step_function has two arguments row, which is the same as in JS and parserwhich seems to be implicit in JS. step_function is a python function, which is what I wanted. What didn't work for me was to pause or abort the parser even though I was on the main thread ๐Ÿค” But I think this is a papaparse issue
Avatar
Avatar
daryl4240
@FabioRosado Thanks I didnโ€™t realize just putting everything on pyscript.com would reveal what the problem is. Now I just donโ€™t understand how to fix the problemโ€ฆ
FabioRosado 26/02/2024 10:37
Had a look at your code, are you talking about this error? Main function Error: input() doesn't work when PyScript runs in the main thread. For pyscript the best approach would be adding an input box and then using the @when decorator to invoke the function, this section of the docs might help https://pyscript.github.io/docs/2024.2.1/user-guide/builtins/#pyscriptwhen
Avatar
Avatar
Andrea Giammarchi
there's only this I might try but it's super bloated with stuff we don't really need unless we want to provide our own mip module for MicroPython https://gildas-lormeau.github.io/zip.js/ ... I will give it a shot on Monday but I can tell already it needs re-packaging (it's OK, we do that already ... a lot) and it will then enable only zip files and not others like pyodide does instead.
Andrea Giammarchi 26/02/2024 13:39
this has been implemented in the following MR https://github.com/pyscript/polyscript/pull/84 - MicroPython can unpack / unarchive .zip too (and only zip). (edited)
In this Discrod conversation a user asked to be able to provide a single .zip file (or .tar.gz or other formats) to be easily extracted into a generic destination folder. As pyodide provides a way ...
Avatar
Hey! I'm new to pyscript and I'm trying to import a package from pypi. The package import worked, but then I noticed something was wrong, and the package has now been updated (as of yesterday). When tried to import the package today, the un-updated package is still loaded.
20:13
I have tried the various ways of specifying packages in my toml file (specifying the url of the wheel, copying the wheel to my directory) and neither has resulted in a successful package import
Avatar
Avatar
sautedman
I have tried the various ways of specifying packages in my toml file (specifying the url of the wheel, copying the wheel to my directory) and neither has resulted in a successful package import
Jeff Glass 27/02/2024 00:00
What is the package?
Avatar
pyhfst
Avatar
Avatar
sautedman
pyhfst
just write packages = ["pyhfst"] in your pyscript.toml file and you can import the package. https://_12.pyscriptapps.com/muddy-waterfall/latest/ If print function displays it means you have successfully imported your package.
Avatar
The problem is that when I do that, the old (broken) version of the package loads. I know the new version of the package works because I installed it with pip today and my code that references the package works. But when I run the code in the browser, I still get the old broken behavior.
05:21
here's the browser throwing the error:
05:23
here's python3 in my terminal running the same script without a problem (because it loads the new version of the package)
05:25
I'm happy to attach my files if it helps
Avatar
Avatar
sautedman
The problem is that when I do that, the old (broken) version of the package loads. I know the new version of the package works because I installed it with pip today and my code that references the package works. But when I run the code in the browser, I still get the old broken behavior.
I think terminal needs "pip install package" for package installation and browser needs pyscript.toml for package installation.If we use pyscript.toml in other environments without "pip install package" then it works fine on browser but show error on terminal.
Avatar
Avatar
sautedman
here's python3 in my terminal running the same script without a problem (because it loads the new version of the package)
Andrea Giammarchi 27/02/2024 10:06
because it loads the new version of the package
this feels like a pyodide upstream issue to me or maybe it's a browser cache issue and you keep getting the old package no matter what ... could you please try, by any chance, the config the point explicitly at the latest in a fresh new browser or in incognito with your current browser?
Avatar
Avatar
Andrea Giammarchi
because it loads the new version of the package
this feels like a pyodide upstream issue to me or maybe it's a browser cache issue and you keep getting the old package no matter what ... could you please try, by any chance, the config the point explicitly at the latest in a fresh new browser or in incognito with your current browser?
Andrea Giammarchi 27/02/2024 10:07
also please keep in mind until Pyodide releases a new version with all latest wheels updated it's very likely it will always give you the old package instead.
Avatar
Avatar
Andrea Giammarchi
also please keep in mind until Pyodide releases a new version with all latest wheels updated it's very likely it will always give you the old package instead.
Jeff Glass 27/02/2024 12:40
Thatโ€™s only the case for the pre-built packages in the Pyodide repo https://pyodide.org/en/stable/usage/packages-in-pyodide.html. pyhfst should be loading straight from PyPI
12:41
This does smell a little like a cache issue, or maybe something else oddโ€ฆ @sautedman if you donโ€™t mind posting your files, that would help a ton.
Avatar
Avatar
Jeff Glass
Thatโ€™s only the case for the pre-built packages in the Pyodide repo https://pyodide.org/en/stable/usage/packages-in-pyodide.html. pyhfst should be loading straight from PyPI
Andrea Giammarchi 27/02/2024 13:30
TIL ... I didn't know there was a difference but now that I do it makes sense too.
Avatar
I just tried opening the page in incognito mode, same result. UPDATE: I also tried Chrome instead of my usual Firefox, it still throws an error (edited)
13:30
posting my files momentarily ...
13:33
860 bytes
Avatar
Andrea Giammarchi 27/02/2024 13:43
what if ... import micropip micropip.install( 'https://files.pythonhosted.org/packages/eb/f5/3ea71e974dd0117b95a54ab2c79d781b4376d257d91e4c2249605f4a54ae/pyhfst-1.2.0-py2.py3-none-any.whl' ) #import hfst import regex import pyhfst # ... the rest of the code edit you should try removing pyhfst from your config too (edited)
Avatar
Avatar
Andrea Giammarchi
what if ... import micropip micropip.install( 'https://files.pythonhosted.org/packages/eb/f5/3ea71e974dd0117b95a54ab2c79d781b4376d257d91e4c2249605f4a54ae/pyhfst-1.2.0-py2.py3-none-any.whl' ) #import hfst import regex import pyhfst # ... the rest of the code edit you should try removing pyhfst from your config too (edited)
Andrea Giammarchi 27/02/2024 13:57
if by any chance this works, it is indeed an upstream pyodide issue as all we do is to use micropip to install packages, we don't interfere with that part in particular https://github.com/pyscript/polyscript/blob/main/esm/interpreter/pyodide.js#L98 (edited)
Polyscript - PyScript single core to rule them all - pyscript/polyscript
Avatar
Let's see what happens! I just modified the file and pushed the changes. I have to wait a few minutes for them to take on github pages.
14:10
If it is a pyodide issue, are there any magic words I should use when I bug those folks to make sure I get their attention? I don't want to just look like some rando on the internet with a weird problem.
14:10
Even if that is what I am ๐Ÿ™‚
14:12
Something along the lines of "the top notch minds at pyscript have determined this is a problem with pyodide"
Avatar
umm... we have a problem
14:47
I first got an error saying that 'await' needed to be used, so I added it following the example shown here: https://micropip.pyodide.org/en/stable/project/usage.html
14:47
Now it throws a syntax error saying 'await' needs to be used inside a function
14:52
928 bytes
Avatar
Avatar
sautedman
umm... we have a problem
make sure you have async flagged: see here: https://pyscript.github.io/docs/2024.2.1/user-guide/first-steps/
Avatar
done
866 bytes
Avatar
Ok, even with the direct call to micropip, I still am getting the error that the old version of the package produced, even in a private-mode browser window. Which is really weird
15:53
and the toml file still does not call pyhfst, so the only way it can be getting pyhfst is through micropip
15:55
here are all the files, so they are all in one place
866 bytes
928 bytes
Avatar
Avatar
sautedman
Ok, even with the direct call to micropip, I still am getting the error that the old version of the package produced, even in a private-mode browser window. Which is really weird
I am able to read packages but i think your .hfstol file exceeds maximum size 1MB.https://_12.pyscriptapps.com/noisy-thunder/latest/
Avatar
yeah, I'm using github to get around the tiny size limit on pyscript (update: pyscript.net) (edited)
17:14
Tools for applying linguistic analyzers to text, and checking the output. The end goal is producing glossaries, lemmatizations, or interlinearizations. - bowersd/textAnalysis
Avatar
chris.laffra 27/02/2024 18:02
What do you mean by "tiny size limit on pyscript?"
Avatar
I mean the 1MB limit on pyscript.net is small (edited)
Avatar
I just did a sanity check on someone else's browser, and the error is real. This is really breaking my brain, because micropip is failing to access the wheel despite being given the address directly. I suppose this is an upstream problem then? (edited)
Avatar
Anybody there?
Avatar
elliot0x01 28/02/2024 16:24
Is there a way to overcome the Cors error caused when making a GET request? The issue occurs only when making a GET request with pyscript, works fine on terminal and streamlit
Avatar
Avatar
elliot0x01
Is there a way to overcome the Cors error caused when making a GET request? The issue occurs only when making a GET request with pyscript, works fine on terminal and streamlit
Andrea Giammarchi 28/02/2024 17:02
how are you making that request and why that's not a js.fetch instead? ๐Ÿ˜…
Avatar
Avatar
Andrea Giammarchi
how are you making that request and why that's not a js.fetch instead? ๐Ÿ˜…
elliot0x01 28/02/2024 17:03
I am using the requests package patched with http pyodide
Avatar
Avatar
Andrea Giammarchi
how are you making that request and why that's not a js.fetch instead? ๐Ÿ˜…
elliot0x01 28/02/2024 17:03
Can you share me an example of js.fetch please
Avatar
Avatar
elliot0x01
Can you share me an example of js.fetch please
Andrea Giammarchi 28/02/2024 17:17
it's not synchronous ... but I could share a Python fetch that is synchronous and you can tell me if it works def fetch(url, method="GET"): from js import XMLHttpRequest xhr = XMLHttpRequest.new(method, url, False) xhr.send(None) return xhr.responseText # or response
Avatar
I really appreciate all you have done. Since my problem still isn't solved, should I go elsewhere?
Avatar
The folks over at pyodide have potentially diagnosed my problem. It is not with micropip apparently, but within pyhfst
Avatar
can you share your compressed morphophonologyclitics_analyze.hfstol file?
Avatar
Did anyone observe pyscript display doesn't work in other environments instead IPython display worked for me.
Avatar
Avatar
zobi
Did anyone observe pyscript display doesn't work in other environments instead IPython display worked for me.
Not sure I understand. Can you provide a reproducable example of PyScript's display not working? (edited)
Avatar
Avatar
ntoll
Not sure I understand. Can you provide a reproducable example of PyScript's display not working? (edited)
I got confused was using print for the DOM.It works fine as expected.Is there any way to make terminal same as the browser because my terminal shows as img1 and my browser shows img2. (edited)
Avatar
Avatar
Andrea Giammarchi
it's not synchronous ... but I could share a Python fetch that is synchronous and you can tell me if it works def fetch(url, method="GET"): from js import XMLHttpRequest xhr = XMLHttpRequest.new(method, url, False) xhr.send(None) return xhr.responseText # or response
elliot0x01 29/02/2024 13:26
Thanks I will try
Avatar
Avatar
ntoll
Not sure I understand. Can you provide a reproducable example of PyScript's display not working? (edited)
elliot0x01 29/02/2024 13:27
Display don't work with micropython
Avatar
Why does this code not work? I get an attribute error for plotter <script> import * as Plot function plotter() { const plot = Plot.rectY({length: 10000}, Plot.binX({y: "count"}, {x: Math.random})).plot(); const div = document.querySelector("#myplot"); div.append(plot); } </script> <script type="py"> from pyscript import window, document class Plotter: def __init__(self): self.plot = None # Accessing JavaScript Plot object def graph(self): window.plotter() plot_ths = Plotter() plot_ths.graph() </script> (edited)
Avatar
elliot0x01 29/02/2024 14:32
Can we make extensions with pyscript ?
Avatar
Avatar
elliot0x01
Can we make extensions with pyscript ?
elliot0x01 29/02/2024 14:42
Browser extensions ๐Ÿค”
Avatar
No reason why you can't. You'll need to ensure PyScript is "inside" the extension. This should help make that possible: https://pyscript.github.io/docs/2024.2.1/user-guide/offline/
๐Ÿ‘ 1
Avatar
Avatar
Eric
Why does this code not work? I get an attribute error for plotter <script> import * as Plot function plotter() { const plot = Plot.rectY({length: 10000}, Plot.binX({y: "count"}, {x: Math.random})).plot(); const div = document.querySelector("#myplot"); div.append(plot); } </script> <script type="py"> from pyscript import window, document class Plotter: def __init__(self): self.plot = None # Accessing JavaScript Plot object def graph(self): window.plotter() plot_ths = Plotter() plot_ths.graph() </script> (edited)
Jeff Glass 01/03/2024 00:18
The JavaScript plotter() function doesnโ€™t actually return anything, does it?
Avatar
Hi All, I'm wondering if something was changed in pyscript in 2024.2.1 that would have resolved an issue with Firefox and the FFI? I posted an issue here in November (https://discord.com/channels/972017612454232116/972020206538997822/1169668125877354666) where I had an intermittent issue where Firefox would fail (but only sometimes). The team asked for a repeatable issue, which I couldn't create. The issue was that it would succeed most of the time and fail intermittently. So, my 'fix' was put a banner up on the app (https://projectassessment.app -- you can use https://projectassessment.app/data/large.csv as a test file) that says it does not support Firefox. However, when I upgraded to version 2024.2.1, the problem simply disappeared. I've tested it in version 123 and the beta of 124 and I havenโ€™t seen the issue after extensive testing. Iโ€™m just wondering if anyone has an idea what might have happened? Part of my reason for asking is I want to know if it is safe to take down the banner. Second, this issue has driven me crazy for months and I want to understand how it could have caused intermittent issues.
Avatar
Avatar
Ben Smith
Hi All, I'm wondering if something was changed in pyscript in 2024.2.1 that would have resolved an issue with Firefox and the FFI? I posted an issue here in November (https://discord.com/channels/972017612454232116/972020206538997822/1169668125877354666) where I had an intermittent issue where Firefox would fail (but only sometimes). The team asked for a repeatable issue, which I couldn't create. The issue was that it would succeed most of the time and fail intermittently. So, my 'fix' was put a banner up on the app (https://projectassessment.app -- you can use https://projectassessment.app/data/large.csv as a test file) that says it does not support Firefox. However, when I upgraded to version 2024.2.1, the problem simply disappeared. I've tested it in version 123 and the beta of 124 and I havenโ€™t seen the issue after extensive testing. Iโ€™m just wondering if anyone has an idea what might have happened? Part of my reason for asking is I want to know if it is safe to take down the banner. Second, this issue has driven me crazy for months and I want to understand how it could have caused intermittent issues.
Hi @Ben Smith - I'm a Firefox user and use PyScript every day with that browser. I was unable to reproduce your problem. Was that a "vanilla" version of Firefox (i.e. without any plugins or other potential modifications of the way the browser behaved). In any case, this could be because we've updated PyScript to use the latest Pyodide and the issue was in that project. In any case, I'm relieved to hear that it's resolved for you... perhaps more to do with serendipity than anything else we can put our finger on??? ๐Ÿ‘
Avatar
Avatar
ntoll
Hi @Ben Smith - I'm a Firefox user and use PyScript every day with that browser. I was unable to reproduce your problem. Was that a "vanilla" version of Firefox (i.e. without any plugins or other potential modifications of the way the browser behaved). In any case, this could be because we've updated PyScript to use the latest Pyodide and the issue was in that project. In any case, I'm relieved to hear that it's resolved for you... perhaps more to do with serendipity than anything else we can put our finger on??? ๐Ÿ‘
It was a fresh version and I tested it both on Mac and on a Windows virtual machine. The big frustration was that it was super intermittent. The issue was still happening in 2024.1.3 if that provides any clue of if it was Pyodide. I also recognize it might have been firefox itself given that was also updating over this time period.
Avatar
Yeah. Tech is hard. So many moving targets.
Avatar
Having trouble with a pyscript that runs fine in chrome, firefox but does not get past loading screen in Safari. I don't have a mac - just getting bug reports from mac users. Any clue as to what I might be looking for ? This py.ready does not seem to fire. <script type="module"> const loading = document.getElementById('loading'); addEventListener('py:ready', () => loading.close()); loading.showModal(); </script>
Avatar
Does pyscript work in Safari ? Anyone know ?
Avatar
Avatar
Neon22
Does pyscript work in Safari ? Anyone know ?
minimal should be safari 15
Avatar
Avatar
Neon22
Does pyscript work in Safari ? Anyone know ?
Andrea Giammarchi 04/03/2024 11:36
latest Safari / Mobile work fine ... I never tracked down the minimal requirement but Safari (IIRC) is an evergreen browser so there shouldn't be any issue with it, as long as your target is not an iPad stuck on iOS 12.x but there's not much we can do there as WASM support is also subpair.
Avatar
Avatar
Neon22
Having trouble with a pyscript that runs fine in chrome, firefox but does not get past loading screen in Safari. I don't have a mac - just getting bug reports from mac users. Any clue as to what I might be looking for ? This py.ready does not seem to fire. <script type="module"> const loading = document.getElementById('loading'); addEventListener('py:ready', () => loading.close()); loading.showModal(); </script>
FabioRosado 04/03/2024 17:58
Just tested it both using pyscript.com where I can see your code and pyscript apps where the whole app is being served. In safari 17.2.1 I see no blocking issues on load, I can even click the buttons once it finishes loading
17:59
Now it's possible that the user might be using a very old version of safari that doesn't support some wasm features maybe
Avatar
Ahh thanks that is almost certainly true, will check. Cheers...
Avatar
termmaster64 06/03/2024 09:25
Can I have the python code be hidden from the user? Like server side scripting?
๐Ÿ˜ข 1
Avatar
@termmaster64 sadly that's impossible due to the way PyScript and the browser works. You could obfuscate... I believe @chris.laffra has been looking into that. But your code will always be available (if not readable due to obfuscation) to the user.
Avatar
Avatar
termmaster64
Can I have the python code be hidden from the user? Like server side scripting?
chris.laffra 06/03/2024 12:27
I have written a minimizer for Python, similar to how JavaScript minimizers work. It turns my PyScript application into minimized (obfuscated) Python code, as shown below. This would deter someone from copying my application literally, but it does not make it impossible, of course:
๐ŸŽ‰ 1
Avatar
Avatar
chris.laffra
I have written a minimizer for Python, similar to how JavaScript minimizers work. It turns my PyScript application into minimized (obfuscated) Python code, as shown below. This would deter someone from copying my application literally, but it does not make it impossible, of course:
Andrea Giammarchi 06/03/2024 13:46
very nice! does it come with source maps too? ๐Ÿ˜
Avatar
CyberneticDaisy 07/03/2024 05:56
hi! background: i'm trying to start an interactive python terminal after installing a package from a local wheel (i.e. in development both the containing webpage and the wheel are on localhost, served by apache). I was able to do the terminal part easily using 'terminal worker' and 'code.interact'. I got the wheel install working fine too using packages in config (either inline or in a toml file), however I wasn't able to put both these together without the terminal failing. From searching the discord I hit on the solution of using micropip to install my wheel instead of doing it through config. (I also tried apy-config tag to specify the package but that killed the terminal the same way) the problem: installing the wheel through micropip is now almost working but now I get a CORS error. It installed the same wheel from the same url just fine via packages in config, so I'm not sure what's different. My server is sending the Access-Control-Allow-Origin localhost header I've attached both my page and the config which was successfully loading and using the wheel, with the exact same url as is giving an error from micropip. the question: is there anything I should be doing differently to get micropip to work the same as through packages in config? am I loading it wrong? is there another solution? is this a question I should be asking piodide instead?
956 bytes
Avatar
Avatar
Andrea Giammarchi
very nice! does it come with source maps too? ๐Ÿ˜
chris.laffra 07/03/2024 11:33
Not right now. Production stack traces are equally cryptic now, but most problems I found are reproducible in dev mode.
Avatar
Can any one help me with this code. It works fine in other environments but not PyScript.com. Even after imports code throws an error. https://_12.pyscriptapps.com/summer-bread/latest/
Sticker
Avatar
@zobi the packages you need don't work in a browser context (pyttsx4 etc...)
Avatar
Avatar
CyberneticDaisy
hi! background: i'm trying to start an interactive python terminal after installing a package from a local wheel (i.e. in development both the containing webpage and the wheel are on localhost, served by apache). I was able to do the terminal part easily using 'terminal worker' and 'code.interact'. I got the wheel install working fine too using packages in config (either inline or in a toml file), however I wasn't able to put both these together without the terminal failing. From searching the discord I hit on the solution of using micropip to install my wheel instead of doing it through config. (I also tried apy-config tag to specify the package but that killed the terminal the same way) the problem: installing the wheel through micropip is now almost working but now I get a CORS error. It installed the same wheel from the same url just fine via packages in config, so I'm not sure what's different. My server is sending the Access-Control-Allow-Origin localhost header I've attached both my page and the config which was successfully loading and using the wheel, with the exact same url as is giving an error from micropip. the question: is there anything I should be doing differently to get micropip to work the same as through packages in config? am I loading it wrong? is there another solution? is this a question I should be asking piodide instead?
CyberneticDaisy 08/03/2024 10:59
Could it be that the request for the wheel works from main but fails from the worker? That fits the behaviour I'm seeing. If that's the case it's gonna be a pain to get this to work. The challenge seems to be I need to load the package in main but can only start the interactive terminal from the worker and that has to be after the package has loaded.
Avatar
Avatar
ntoll
@zobi the packages you need don't work in a browser context (pyttsx4 etc...)
"espeak" for tts alone is quite easy to work with (edited)
16:15
"vosk" for speech to text, works too but not very accurate probably not enough cpu power in most cases (edited)
Avatar
is it just me or the pyscript example page takes forever to load people's examples? just trying to get an overview of how to manage an internal dashboard with pyscript and so far i havent found any examples of how people would do it
Avatar
Avatar
pmp-p
"vosk" for speech to text, works too but not very accurate probably not enough cpu power in most cases (edited)
"espeak" is .exe don't work in a browser context as well. When is the desktop version available to use?
Avatar
there's a javascript port of espeak that you can run in a iframe and call with postmessage
Avatar
I want to call a function in a worker which will draw on a canvas (or svg) in my dom when its finished working it out. What's the best way to organize that ? Do I pass in the selector (say #mycanvas) or wait for a signal in my main and then update the dom with a response from the worker, or ? (edited)
Avatar
Avatar
pmp-p
there's a javascript port of espeak that you can run in a iframe and call with postmessage
i think iframe is blocked in pyscript.com
โ˜น๏ธ 1
Avatar
Preview is displayed for the following link https://pyscript.com/@ntoll/fancy-moon/latest but same code doesn't display the preview for me. https://pyscript.com/@_12/fancy-moon-copy/latest What could be the reason?Can someone help?
Avatar
NaturalAliens 10/03/2024 14:40
Hello! I've got a nice little online-rts side project going well with pyscript, but I'm stumped on android by the very aggressive caching from Chrome: only way I can see to get my script update through is with opening a browser tab in incognito mode, all of the other ways to not use a cached script that I see documented most places just do not work. If I could version the script in a way that the browser can see this would be great, but all I got in my index.html is <py-config hidden="True"> [[fetch]] from = "https://my-site/pyscript/" files = ["models.py", "threejs_world.py"] </py-config> ....I do not see any nice way to put any versionning information in there, did anyone get this issue and solve it any other way than renaming the .py files on each change? (edited)
Avatar
Christian Clauss 10/03/2024 17:32
I am porting an existing app over to https://pyscript.com but I am unsure how to run my pytests in that environment. Is there a pyscript.com example that runs pytests or some docs on testing? (edited)
Avatar
Christian Clauss 10/03/2024 18:26
Answered my own question: Running pytest in https://pyscript.com was merely a question of calling pytest.main() which I found at https://docs.pytest.org/en/stable/how-to/usage.html#calling-pytest-from-python-code To run the pytests background worker connected to a terminal do: <script type="py" config="./pyscript.toml" terminal worker> import pytest pytest.main() </script> All the file naming for pytest discover, plus the __init__.py files, plus the [files] section of pyscript.toml are required but it work for me! Also see: https://pyscript.com/@antocuni/pytest-in-pyterminal (edited)
Neon22 started a thread. 11/03/2024 10:36
Avatar
How would I add pyscript to my sveltekit project ?
Avatar
Phelsong (Josh) 11/03/2024 16:08
have an endpoint serve an html file/blob with the appropriate scripts
16:12
@pages.get("/", response_class=HTMLResponse) async def index( ) -> HTMLResponse: """Base Route for redirect""" html = ( """ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <!-- ================================================================================== --> <link defer rel="stylesheet" href="/static/index.css" /> <script defer type="module" src="/packages/pyscript/core.js"></script> <!-- ================================================================================== --> <title>redact</title> </head> <body> <!-- ==================================================== --> <py-terminal id="py-out"></py-terminal>""" f"<script type='py' src='/web/app.py' config='/web/config/config.toml'></script>" f"</body>" f"</html>" ) return HTMLResponse(html, media_type="text/html", status_code=200)
16:13
^ similar to that, however that looks in svelte, with w/e the current cdn link for pyscript is
Avatar
MattyTrentini 15/03/2024 11:36
Hey folks, does anyone have a simple example of using PyScript with MicroPython? I'd like to present it at the next Melbourne MicroPython Meetup. Something like the examples at pyscript would be ideal: https://pyscript.com/ ...but using MicroPython. ๐Ÿ˜‰ (I tried changing the Tic-Tac-Toe example - since it looked MicroPython compatible - to use 'mpy' instead of 'py' but the app appeared to no longer function.)
Avatar
Christian Clauss 15/03/2024 14:23
Yesterday on FUN pyscript I demonstrated an app where I have dozens of images that I want to be clickable. Normally one would use @pyscript.when decorator to statically add an on_click() function. Is there a recommended way to dynamically add/remove an on_click() function to dozens of objects at runtime? https://pyscript.github.io/docs/2024.3.1/user-guide/builtins/#pyscriptwhen
Avatar
hi folks is there a way to import pyscript on "py-editor"?
Avatar
@Christian Clauss in the convtext of multiple objects, PyScript's @when takes two args: the event type (e.g. click) and the selector (e.g. #my_id). The docs (https://pyscript.github.io/docs/2024.3.1/user-guide/builtins/#pyscriptwhen) explain that the selector can be any valid CSS selector. So, if you have a collection of objects that have the same CSS class, you can just use .myclass as the selector. The story around removing such event handling is a tad more complicated (the @when is convenient usually because folks only want to add). Under the hood, if you're using Pyodide as the runtime, Pyodide's add_event_listener wrapper around the JS addEventListener function is used to hook things together (see: https://pyodide.org/en/stable/usage/api/python-api/ffi.html#pyodide.ffi.wrappers.add_event_listener). There is a corresponding remove_event_listener wrapper function in Pyodide too: https://pyodide.org/en/stable/usage/api/python-api/ffi.html#pyodide.ffi.wrappers.remove_event_listener. If you're using MicroPython we use the standard JS addEventListener under the hood. There is, of course, the removeEventListener function too (https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/removeEventListener) but you'll need a reference to the handling function in order to remove it from the desired element/event context. Does this help..?
The removeEventListener() method of the EventTarget interface removes an event listener previously registered with EventTarget.addEventListener() from the target. The event listener to be removed is identified using a combination of the event type, the event listener function itself, and various optional options that may affect the matchin...
๐Ÿš€ 1
Avatar
Avatar
fleebor
hi folks is there a way to import pyscript on "py-editor"?
Andrea Giammarchi 15/03/2024 16:52
I am afraid we don't share the same hooks used to bootstrap type="py" or type="mpy" and indeed the py-editor is the cleanest interpreter you can get right now and it shares nothing with the surrounding scripts ... it should be possible to use the main thread to reference modules in there with or without the editor (it's an environment a part, always) so maybe that could work? what in particular do you need there, if I might ask?
Avatar
it should be possible to use the main thread to reference modules in there with or without the editor (it's an environment a part, always) so maybe that could work?
I didn't quite get this. "use the main thread" means spawning the editor in the main thread (I assumed this was not possible)?
what in particular do you need there, if I might ask?
so... this I know is sounds hacky, but there's a JSON API response that is available in the backend that I want to make available in the editor for manipulation, in the front-end. And my not so bright idea was to add the API response in a hidden div and load it in the editor ๐Ÿ˜ถ . I just saw that 2024.03.01 was released and now I'm doing this instead: response = call_api() raw_response = json.dumps(response) return f""" <script type="py-editor" env="env" setup> import json raw = r"""{raw_response}""" response = json.loads(raw) </script> <script type="py-editor" env="env"> </script> """ Still hacky, but maybe less hacky, and it works ๐Ÿ™‚ so I'm happy for now and I now longer really need to access the DOM from within a py-editor
Christian Clauss started a thread. 15/03/2024 17:55
Avatar
Avatar
fleebor
it should be possible to use the main thread to reference modules in there with or without the editor (it's an environment a part, always) so maybe that could work?
I didn't quite get this. "use the main thread" means spawning the editor in the main thread (I assumed this was not possible)?
what in particular do you need there, if I might ask?
so... this I know is sounds hacky, but there's a JSON API response that is available in the backend that I want to make available in the editor for manipulation, in the front-end. And my not so bright idea was to add the API response in a hidden div and load it in the editor ๐Ÿ˜ถ . I just saw that 2024.03.01 was released and now I'm doing this instead: response = call_api() raw_response = json.dumps(response) return f""" <script type="py-editor" env="env" setup> import json raw = r"""{raw_response}""" response = json.loads(raw) </script> <script type="py-editor" env="env"> </script> """ Still hacky, but maybe less hacky, and it works ๐Ÿ™‚ so I'm happy for now and I now longer really need to access the DOM from within a py-editor
Andrea Giammarchi 15/03/2024 20:23
I am super happy the new setup feature unlocked you /cc @ntoll but I still think expecting pyscript module in a py-editor might be asked more and more as feature ... there's quite some work to do but it will be on the radar / pipeline
Avatar
Christian Clauss 16/03/2024 07:33
Are there any docs on running pyscript on iOS devices? It seems like some things work while others do not. It would be helpful to know what to avoid when running on iOS.
Avatar
Avatar
Christian Clauss
Are there any docs on running pyscript on iOS devices? It seems like some things work while others do not. It would be helpful to know what to avoid when running on iOS.
which ios version ?
Avatar
Avatar
zobi
can you share your compressed morphophonologyclitics_analyze.hfstol file?
I got it to work. It ended up being a problem with specifying where to look for the file. (Maybe this would be something that new users could get a guide on)
Avatar
Now I'm trying to manage a file upload. There are many recipes out there (https://github.com/amrrs/pyscript-file-uploader/blob/main/index.html, https://jeff.glass/post/pyscript-image-upload/) but the addresses and preferred wrappers change (https://jeff.glass/post/pyscript-why-create-proxy/) ... and some browsers support some methods (https://www.jhanley.com/blog/pyscript-files-and-file-systems-part-2/), so it is a little hard to trust anything. I currently have been trying to use this in my index.html: <label for="Upload a File"></label> <input type="file" id="file-upload"> <div id="output_upload"></div> <script type="py" src="./main.py" config="./pyscript.toml" async></script> And this in my main.py (modified from https://jeff.glass/post/pyscript-image-upload/) import asyncio from js import document, window, console from pyodide.ffi.wrappers import add_event_listener def _upload_file_and_show(e): console.log("Attempted file upload: " + e.target.value) file_list = e.target.files first_item = file_list.item(0) new_image = document.createElement('img') new_image.src = window.URL.createObjectURL(first_item) document.getElementByID("output_upload").appendChild(new_image) upload_file = document.getElementById("file-upload") add_event_listener(upload_file, "change", _upload_file_and_show) Unfortunately, when I click on my upload button and look at the console, I see this: Uncaught PythonError: Traceback (most recent call last): File "<exec>", line 137, in _upload_file_and_show AttributeError: getElementByID k pyodide.asm.js:9 new_error pyodide.asm.js:9 _pythonexc2js pyodide.asm.js:9 callPyObjectKwargs pyodide.asm.js:9 callPyObject pyodide.asm.js:9 apply pyodide.asm.js:9 apply pyodide.asm.js:9 ...
Pyodide's create_proxy function explained
In this article, I will show how to use the File System Access API. This API is a web platform API that enables developers to build powerful web apps that interact with files on the userโ€™s local device. There are a few caveats in using this API such as accessing local files requires user interaction, such as clicking a button.
15:01
Thank you very much for your help in the past. I greatly appreciate any help you can give with this!
Avatar
Avatar
sautedman
Thank you very much for your help in the past. I greatly appreciate any help you can give with this!
Jeff Glass 16/03/2024 17:58
There's some style things thare are out of date in this recipe, but it should still be basically functional: https://pyscript.recipes/2024.1.1/basic/file-upload/
17:58
Ah, I suspect you're running your code in a worker tag, yes?
17:59
If so, you'll want to change your imports a bit: from pyscript import document, window from js import console
18:00
The reason being - from js import... always grabs from the current global namespace, whether worker or main thread. But if you import window or document from the pyscript package, you always get a reference to the main thread's window or document, whether you're running in a worker or not
18:02
Oh wait, I see you're not running in a worker tag.... perhaps it is something else. Either way, the above is good general advice.
Avatar
FWIW - Jeff's file upload is working well for me - in main thread.
Avatar
Avatar
pmp-p
which ios version ?
Christian Clauss 16/03/2024 21:01
Always the most current release of iOS.
Avatar
Avatar
Jeff Glass
There's some style things thare are out of date in this recipe, but it should still be basically functional: https://pyscript.recipes/2024.1.1/basic/file-upload/
I looked back through, and ... it was a capitalization error (sheesh). Thanks so much! I did change my imports to match what you said as well!
๐Ÿฅณ 1
Avatar
Hi folks Is there a way to send commands to a python a worker terminal? For instance, suppose I have a terminal worker on the page and I want to send the โ€œprint(โ€˜you clicked the buttonโ€™)โ€ command to it every time a button is clicked on the same page.
Avatar
Hello, I'm slowly discovering Pyscript ๐Ÿ™‚ I have an html interface when I connect my phone to my raspberry's wifi. This allows me to control a camera. Is PyScript able to turn off my raspberry with a button on my html page? If it is possible, can you help me a little? Thanks!
Avatar
Avatar
olka
Hello, I'm slowly discovering Pyscript ๐Ÿ™‚ I have an html interface when I connect my phone to my raspberry's wifi. This allows me to control a camera. Is PyScript able to turn off my raspberry with a button on my html page? If it is possible, can you help me a little? Thanks!
use a websocket server + some RPC on your pi to send the poweroff request, or telemetrix + websockify for even more possibilities (edited)
Avatar
Aquanafrahudy 19/03/2024 17:08
Hi I'm trying to use pyscript, but whenever I try to use the "terminal worker" nothing works, and it just gives a blank terminal.
<py-script terminal worker> print("Hello, world") </py-script>
This is my code And this is what shows up:
17:09
I'm not quite sure what I'm doing wrong, so if somebody more tech-savvy could explain that would be absolutely marvellous
Avatar
Avatar
Aquanafrahudy
Hi I'm trying to use pyscript, but whenever I try to use the "terminal worker" nothing works, and it just gives a blank terminal.
<py-script terminal worker> print("Hello, world") </py-script>
This is my code And this is what shows up:
Andrea Giammarchi 19/03/2024 17:34
be sure your headers to allow SharedArrayBuffer are in place and it will work ... if no idea how to, use npx mini-coi ./ to bootstrap a server that allows that target folder to server the right headers. npx is provided by npm package, I think you might be able to install it if that's not in there already. (edited)
Avatar
Avatar
Aquanafrahudy
Hi I'm trying to use pyscript, but whenever I try to use the "terminal worker" nothing works, and it just gives a blank terminal.
<py-script terminal worker> print("Hello, world") </py-script>
This is my code And this is what shows up:
I think you are using the old version of py-script.py-script is replaced by script tag.<script type="py" terminal workers> print("Hello, world") </script> this should work.
Avatar
Jeff Glass 19/03/2024 17:35
Similarly, if you're using plain old JS, you can include the raw mini-coi.js in your project folder and include it directly with a script tag https://github.com/WebReflection/mini-coi
๐Ÿ‘ 1
Avatar
Avatar
zobi
I think you are using the old version of py-script.py-script is replaced by script tag.<script type="py" terminal workers> print("Hello, world") </script> this should work.
Andrea Giammarchi 19/03/2024 17:36
it's a valid suggestion but I think we enable both for the time being, IIRC (edited)
Avatar
Jeff Glass 19/03/2024 17:37
I was just going to say... both should still work the same at the moment, but for a number of reasons we'd recommend switching to <script type='py'>....
๐Ÿ‘ 1
Avatar
Avatar
zobi
I think you are using the old version of py-script.py-script is replaced by script tag.<script type="py" terminal workers> print("Hello, world") </script> this should work.
Aquanafrahudy 19/03/2024 17:58
No, still doesn't work
17:58
(I think I had it like that originally and then changed it just in case the other one worked)
Avatar
Avatar
Andrea Giammarchi
be sure your headers to allow SharedArrayBuffer are in place and it will work ... if no idea how to, use npx mini-coi ./ to bootstrap a server that allows that target folder to server the right headers. npx is provided by npm package, I think you might be able to install it if that's not in there already. (edited)
Aquanafrahudy 19/03/2024 17:59
Sorry, do I do this in the terminal or the code?
Avatar
Avatar
Aquanafrahudy
Sorry, do I do this in the terminal or the code?
Andrea Giammarchi 19/03/2024 18:14
it's to bootstrap your server instead of python -m http.server or whatever, so console ... but now I wonder: are you using latest PyScript? your source code is not clear from the image ๐Ÿ˜…
๐Ÿ‘ 1
Avatar
Avatar
Andrea Giammarchi
it's to bootstrap your server instead of python -m http.server or whatever, so console ... but now I wonder: are you using latest PyScript? your source code is not clear from the image ๐Ÿ˜…
Aquanafrahudy 19/03/2024 18:15
I copied it from the PyScript help thing:
<meta name="viewport" content="width=device-width,initial-scale=1.0"> <!-- PyScript CSS --> <link rel="stylesheet" href="https://pyscript.net/releases/2024.3.1/core.css"> <!-- This script tag bootstraps PyScript --> <script type="module" src="https://pyscript.net/releases/2024.3.1/core.js"></script>
Avatar
Avatar
Andrea Giammarchi
it's to bootstrap your server instead of python -m http.server or whatever, so console ... but now I wonder: are you using latest PyScript? your source code is not clear from the image ๐Ÿ˜…
Aquanafrahudy 19/03/2024 18:15
Okay, thanks
18:15
I'll try that
Avatar
Andrea Giammarchi 19/03/2024 18:16
OK, that's good ... so, bootstrap a server not via python but via my instructions within the same folder
18:16
fingers crossed that work ๐Ÿคž
Avatar
Avatar
Andrea Giammarchi
fingers crossed that work ๐Ÿคž
Aquanafrahudy 19/03/2024 18:21
Thank you! That worked
Avatar
CyberneticDaisy 20/03/2024 05:56
Is it possible to install a wheel from the browser based file system (emfs://) using micropip?
Avatar
CyberneticDaisy 20/03/2024 06:08
seems not, and I can't even do it using config, it says there's possibly a cors error. after already loading the file.
06:10
I can load, install and run a package from a wheel, but as soon as I add worker to the script tag (to make interactive terminal work) it doesn't even attempt to load the wheel
06:11
(that's just doing it the normal way with config with no emfs involved) (edited)
Avatar
Avatar
Andrea Giammarchi
it's a valid suggestion but I think we enable both for the time being, IIRC (edited)
Yes,it works for both.thanks for verifying.
Avatar
Avatar
CyberneticDaisy
seems not, and I can't even do it using config, it says there's possibly a cors error. after already loading the file.
Andrea Giammarchi 20/03/2024 09:28
for CORS errors be sure your backend serves CORS headers https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
Cross-Origin Resource Sharing (CORS) is an HTTP-header based mechanism that allows a server to indicate any origins (domain, scheme, or port) other than its own from which a browser should permit loading resources. CORS also relies on a mechanism by which browsers make a "preflight" request to the server hosting the cross-origin resource, in ord...
Avatar
Avatar
Andrea Giammarchi
for CORS errors be sure your backend serves CORS headers https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
CyberneticDaisy 20/03/2024 09:44
No the file loads from the backend fine, the "maybe cors" error appears when I specify installing the whl addressed with emfs as shown in the configuration docs. I also get a cors error trying to request and install the wheel through micropip in a worker thread even though the same file loads fine when not in a worker. The crux of the problem (see my earlier posts) is that which I can install the wheel fine the normal way through the package config, and can import and use it, I can't get that AND interactive terminal (which requires 'worker') to function together.
Avatar
@CyberneticDaisy I feel your pain. Here's the thing: the whole CORS thing is down to the browser's built in Single Origin Policy (SOP) protections to stop data leaking and so on. TL;DR there's nothing we (PyScript or folks using PyScript) can do about this context because it's a web standard. Here's why you are encountering it (hold onto your hat!):
  • You CAN pip install things from another domain (pypi) that's different to the one your PyScript app is being served from IF you're NOT using web workers.
  • In order to make the terminal work in an interactive manner with Python (the crux of the matter is the browser doesn't block, but Python expects to be blocked when waiting for user input, for example), we run Python in a worker that we can pause when the terminal is waiting for user activity.
  • BUT! Because we use a shared array buffer to communicate between a worker and the main thread and pause the Python interpreter, browsers enforce a much stricter CORS policy (over which we don't have any control).
So, you're at that annoying not-so-sweet spot of a combination of different stuff that doesn't work (yet!). FWIW - I encounter this most days, and it's something we want to fix, or at least have an easy story for, moving forward. Just remember PyScript is relatively young and we're still making stuff work โ„ข๏ธ. In any case, you have basically two options: 1. Use Mini-Coi (https://github.com/WebReflection/mini-coi - the README explains how). This should "just work" โ„ข๏ธ - please ask questions if it doesn't. 2. Take a copy of the Python wheel from PyPI and host it at the same server you have your app, and reference it by direct URL to that copy, rather than package name (but this is a bit hacky). Hope this helps. Keep asking questions. Let us know how you get on..! ๐Ÿš€
A minimalistic version of coi-serviceworker. Contribute to WebReflection/mini-coi development by creating an account on GitHub.
thankyou 1
Avatar
CyberneticDaisy 20/03/2024 10:32
I did notice miin-coi a little while ago and it is the next thing I was gonna try. the wheel I'm installing is actually already served from the same place as the page (currently localhost using apache server). (edited)
Avatar
Hmm. That's weird. It should work then.
10:33
Ahhhh
10:34
EMFS ๐Ÿคฆ
10:34
Are you copying the package onto the filesystem?
10:35
Also, why not just use a URL instead..?
10:35
(browser based file system I mean - i.e. you'll need to use the files section to copy it from a URL to a location on the filesystem)
Avatar
Avatar
ntoll
Also, why not just use a URL instead..?
CyberneticDaisy 21/03/2024 08:14
so, using a URL works until I do it in a worker to make the interactive terminal work. when I try that something fails but I don't see any error messages or even a request being made. therefore I was trying every other variation to see if I could get anything to function better. I tried installing it from the URL with micropip and that raised a cors error. so I thought I'd try loading it into the browser based filesystem with files - which works - and then install it with micropip but that seems to not be able to handle emfs:// (also there's no guarantee it would attempt that only after the file is available, I suspect). so then I tried requesting the file with files and then installing it with packages pointing at the package with emfs, and that raised a cors error. or rather a "an error occured which could be cors", to paraphrase. oh I also tried manually starting a worker to initiate the terminal after importing the package but I couldn't get that to work either. anyhow I'm gonna try mini-coi now
Avatar
CyberneticDaisy 21/03/2024 08:28
if I do this: <script type="py" config='{"packages":["./dist/floofbytes-2024.0.2-py3-none-any.whl"]}' terminal > from pyscript import document, display term = document.querySelector('script[terminal]').terminal term.resize(90, 32) display("resized") from floofbytes.core import Game, examine me = Game().get_player() display(examine(me)) import code code.interact() </script> I get output from my package displayed, and as expected I get Exception: input() doesn't work when PyScript runs in the main thread. if I add worker to the script tag I get no errors (even in the browser console) and no output at all in the console and my .whl file was never requested according to the network monitor. this is with mini-coi.js added (it's loading fine from localhost). adding it didn't change the behaviour at all. I think I'll have to load all my python files individually for now. not that I actually have more than one but this is just proof of concept for a potential project structure.
Avatar
@CyberneticDaisy so, (once again), I feel your pain. Also, I've gotta say you're turning the awesomeness dial up to a solid 11 with a namespace like floofbytes - that had me giggling like a 2yo. ๐Ÿคฃ Is there any way you can share this code so I (or others - @Andrea Giammarchi for instance) could have a poke around to see what's going on..? If you stick everything on PyScript.com we can easily clone your project and make our own private mess. Quite understand if this isn't possible, but we're kinda flying blind here in terms of supporting you. ๐Ÿ‘ Another possibility is to drop into a video call here on discord and you can show us around / demo the problem and we can try to figure it out. I'm in the UK and when the US wakes up I'm usually in meetings with US colleagues so mornings (UK time - but not this morning or tomorrow) are usually good for ad hoc pairing. (edited)
Avatar
Avatar
ntoll
@CyberneticDaisy so, (once again), I feel your pain. Also, I've gotta say you're turning the awesomeness dial up to a solid 11 with a namespace like floofbytes - that had me giggling like a 2yo. ๐Ÿคฃ Is there any way you can share this code so I (or others - @Andrea Giammarchi for instance) could have a poke around to see what's going on..? If you stick everything on PyScript.com we can easily clone your project and make our own private mess. Quite understand if this isn't possible, but we're kinda flying blind here in terms of supporting you. ๐Ÿ‘ Another possibility is to drop into a video call here on discord and you can show us around / demo the problem and we can try to figure it out. I'm in the UK and when the US wakes up I'm usually in meetings with US colleagues so mornings (UK time - but not this morning or tomorrow) are usually good for ad hoc pairing. (edited)
CyberneticDaisy 21/03/2024 10:00
I'm in neither UK or the US and it's evening for me now and I'm about to head to bed. What I've got is pretty small and I could make it even more minimal to share in my tomorrow. Glad you like the name!
Avatar
Avatar
CyberneticDaisy
if I do this: <script type="py" config='{"packages":["./dist/floofbytes-2024.0.2-py3-none-any.whl"]}' terminal > from pyscript import document, display term = document.querySelector('script[terminal]').terminal term.resize(90, 32) display("resized") from floofbytes.core import Game, examine me = Game().get_player() display(examine(me)) import code code.interact() </script> I get output from my package displayed, and as expected I get Exception: input() doesn't work when PyScript runs in the main thread. if I add worker to the script tag I get no errors (even in the browser console) and no output at all in the console and my .whl file was never requested according to the network monitor. this is with mini-coi.js added (it's loading fine from localhost). adding it didn't change the behaviour at all. I think I'll have to load all my python files individually for now. not that I actually have more than one but this is just proof of concept for a potential project structure.
Andrea Giammarchi 21/03/2024 10:00
to me it feels like the usual issue: you need to allow Workers via specific headers ... but as you wrote already you're going to try mini-coi I am with Nicholas please put anything on pyscript.com so we can better understand what's going on,thank you!
10:01
the terminal without worker can't block the main thread so that input is disabled but that's not a bug, it's a Web limitation we prefer to throw errors around it and encourage the usage of Worker when input is desired.
Avatar
Avatar
CyberneticDaisy
I'm in neither UK or the US and it's evening for me now and I'm about to head to bed. What I've got is pretty small and I could make it even more minimal to share in my tomorrow. Glad you like the name!
Aha... ack on the timezones (it's hard). The more info we have, or example code we can poke with a proverbial stick, the better.
Avatar
Andrea Giammarchi 21/03/2024 10:01
npx mini-coi . to bootstrap a NodeJS server with the right headers then reach that localhost and see if you have any better result would be my first advice.
โ˜๏ธ 1
Avatar
CyberneticDaisy 21/03/2024 10:02
The interactive terminal does work for me (with worker of course), at least until I try introducing the package installed via wheel
Avatar
Haha... this is why it's such an interesting edge case.
Avatar
Andrea Giammarchi 21/03/2024 10:03
have you tried to give that wheel a fully qualiied URL instead? that is http://localhost:PORT/dist/wheel/thingy.whl
Avatar
You're the first to encounter it (bravo for reporting it).
Avatar
Andrea Giammarchi 21/03/2024 10:03
I mean in that package ... not the solution, I am just trying to understand what is it that you struggle with.
Avatar
BRB - my dog is asking for walkies with a desparate look on his face. Poor old boy. ๐Ÿ•โ€๐Ÿฆบ
๐Ÿ˜บ 1
Avatar
CyberneticDaisy 21/03/2024 10:04
Perhaps my headers are only part correct. I've not tried full url cos I figured it would not be portable outside the dev environment. I'll try tomorrow
๐Ÿ‘ 2
Avatar
Avatar
CyberneticDaisy
hi! background: i'm trying to start an interactive python terminal after installing a package from a local wheel (i.e. in development both the containing webpage and the wheel are on localhost, served by apache). I was able to do the terminal part easily using 'terminal worker' and 'code.interact'. I got the wheel install working fine too using packages in config (either inline or in a toml file), however I wasn't able to put both these together without the terminal failing. From searching the discord I hit on the solution of using micropip to install my wheel instead of doing it through config. (I also tried apy-config tag to specify the package but that killed the terminal the same way) the problem: installing the wheel through micropip is now almost working but now I get a CORS error. It installed the same wheel from the same url just fine via packages in config, so I'm not sure what's different. My server is sending the Access-Control-Allow-Origin localhost header I've attached both my page and the config which was successfully loading and using the wheel, with the exact same url as is giving an error from micropip. the question: is there anything I should be doing differently to get micropip to work the same as through packages in config? am I loading it wrong? is there another solution? is this a question I should be asking piodide instead?
CyberneticDaisy 21/03/2024 10:06
10:07
I configured that with Apache (which I'd not used before)
Avatar
Avatar
CyberneticDaisy
Andrea Giammarchi 21/03/2024 10:09
these are mini-coi hedaers https://github.com/WebReflection/mini-coi/blob/main/mini-coi.js#L21-L23 + Access-Control-Allow-Origin which is * in our case
A minimalistic version of coi-serviceworker. Contribute to WebReflection/mini-coi development by creating an account on GitHub.
10:10
A very simple static files handler. Contribute to WebReflection/static-handler development by creating an account on GitHub.
Avatar
CyberneticDaisy 21/03/2024 10:10
Okay that's another thing for me to try, thanks
๐Ÿ‘ 1
๐ŸŽ‰ 1
Avatar
Avatar
zobi
Yes,it works for both.thanks for verifying.
Why can't we have <script></script> for js and <py-script></py-script> for both python and js?This way both python and js could share same terminal and reuse the codes instead of changing from one programming language to another?Multiple terminals are blocked but single terminal can be shared.<script> should goto DOM and <py-script> to both DOM and Terminal?
Avatar
Avatar
zobi
Why can't we have <script></script> for js and <py-script></py-script> for both python and js?This way both python and js could share same terminal and reuse the codes instead of changing from one programming language to another?Multiple terminals are blocked but single terminal can be shared.<script> should goto DOM and <py-script> to both DOM and Terminal?
Andrea Giammarchi 22/03/2024 12:30
I don't understand this question or in general what you are talking about but <py-script> as custom element is basically deprecated because it suffers DOM parsing which is full of unexpected surprises so you better use <script type="py"> or <script type="mpy"> and be always safe from undesired errors in code you put in there, as there's no DOM parsing involved ever by standard contract.
Avatar
Is there anything you recommend I read?
Avatar
Avatar
AKIL
Is there anything you recommend I read?
Our docs (in English, sorry) can be found here: https://pyscript.github.io/docs/2024.3.1/
Avatar
Avatar
ntoll
Our docs (in English, sorry) can be found here: https://pyscript.github.io/docs/2024.3.1/
thank you but already read
14:00
I entered the server because of the document
14:01
thanks anyway
Avatar
thanks anyway
Avatar
Avatar
ntoll
Our docs (in English, sorry) can be found here: https://pyscript.github.io/docs/2024.3.1/
It's okay to be in English
Avatar
Understood! (edited)
Avatar
Avatar
Andrea Giammarchi
I don't understand this question or in general what you are talking about but <py-script> as custom element is basically deprecated because it suffers DOM parsing which is full of unexpected surprises so you better use <script type="py"> or <script type="mpy"> and be always safe from undesired errors in code you put in there, as there's no DOM parsing involved ever by standard contract.
Refer to the link below you might understand what i am trying to say. This way we can write half of our code in python and half of our code in javascript and combine their results. https://_12.pyscriptapps.com/snowy-cherry/latest/ not sure why it's not working in 2024.3.1 release.
Avatar
Avatar
AKIL
Is there anything you recommend I read?
Refer to the reply.
Avatar
Marco Aprea 23/03/2024 17:40
hi
Avatar
Marco Aprea 23/03/2024 20:01
I am currently endeavoring to transition from an initial example of a Single Page Application implemented in JavaScript. Initially, I undertook this task using Brython, although it was transpiled. Now, I am attempting to achieve the same functionality with Pyscript. However, I find myself struggling to grasp its workings. Despite finding examples demonstrating how to access DOM elements and manage events, I encounter issues with their proper functionality. This project is intended for my students, with the primary focus being on understanding that the Dictionary serves as the Application. Here are the different versions of the examples: initial example in js: https://pyscript.com/@marcoaprea/single-page-application-with-jquery-v1-0/latest bryton example v.1 (global variable): https://pyscript.com/@marcoaprea/single-page-application-with-brython-v1-0/latest bryton example v.2 (global variable and lambda function): https://pyscript.com/@marcoaprea/single-page-application-with-brython-v1-0/latest bryton example v.3 (class implementation): https://pyscript.com/@marcoaprea/single-page-application-with-brython-v3-0/latest Pyscript example: https://pyscript.com/@marcoaprea/single-page-application-with-pyscript-v1-0/latest PS: I acknowledge that the variable names may not be self-explanatory. However, the primary focus lies in defining the callbacks. Additionally, not all variables within the dictionary are necessary; only two callbacks require storing the state of the app, while others can utilize the DOM. Nevertheless, this is merely a demonstration aimed at comprehending how an event-driven program operates.
Neon22 started a thread. 23/03/2024 20:59
Avatar
Avatar
Andrea Giammarchi
have you tried to give that wheel a fully qualiied URL instead? that is http://localhost:PORT/dist/wheel/thingy.whl
CyberneticDaisy 24/03/2024 05:16
no way. no waaaaaay. that fixed it!
05:18
<script type="py" config='{"packages":["http://localhost:80/dist/floofbytes-2024.0.2-py3-none-any.whl"]}' worker terminal > this loads the package AND shows the terminal! first time i've seen both work together
๐ŸŽ‰ 1
05:23
I presume it's cos using . doesn't bind it to a hostname in the way CORS wants
๐Ÿคฆ 1
Avatar
I like to pronounce CORS with a slight Oirish lilt, a la "curse". ๐Ÿ˜›
๐Ÿคฃ 1
Avatar
Avatar
CyberneticDaisy
I presume it's cos using . doesn't bind it to a hostname in the way CORS wants
Andrea Giammarchi 25/03/2024 09:49
in worker, your code run as a one-off blob created for you to allow hooks and portability within the project. That blob is not seen as localhost, as it's an internal browser URL that exists only once for that worker session and it can also eventually be destroyed. That's why your CORS policy should be broader and the easy way is to allow * (all) so that even the relative path might work ... however, there is a special config flag to specify your base url for things, including packages, and with the inline conifg it defaults to the current URL but if you move that config elsewhere (as external .json or .toml file) you should see also relative paths working even from a worker. (edited)
Avatar
Avatar
Andrea Giammarchi
in worker, your code run as a one-off blob created for you to allow hooks and portability within the project. That blob is not seen as localhost, as it's an internal browser URL that exists only once for that worker session and it can also eventually be destroyed. That's why your CORS policy should be broader and the easy way is to allow * (all) so that even the relative path might work ... however, there is a special config flag to specify your base url for things, including packages, and with the inline conifg it defaults to the current URL but if you move that config elsewhere (as external .json or .toml file) you should see also relative paths working even from a worker. (edited)
CyberneticDaisy 25/03/2024 09:52
I tried changing it to * as shown to me above, however I couldn't get it to change. Thanks for the info on the config flag. I do usually use an external config, I just moved it inline to it was simpler to show.
Avatar
Andrea Giammarchi 25/03/2024 09:55
Alternatively, you can create at runtime the config providing fully qualified URL accordingly with your location.href ... this is an ugly way to do it, but there are more elegant one. <!doctype html> <html> <head><!-- your head things --></head> <body> <script>document.write(`<py-config>{"packages":["${location.protocol}//${location.host}/dist/floofbytes-2024.0.2-py3-none-any.whl"]}</py-config>`)</script> <!-- the rest of your body --> <script type="py" worker terminal> import that-wheel # or your code in here </script> </body> </html> (edited)
Eruvanos [UTC+1] started a thread. 25/03/2024 20:14
Avatar
Eruvanos [UTC+1] 25/03/2024 21:40
Anybody tried to use react from pyscript? I tried to use js_modules in my pyscript.toml: [js_modules.main] "https://unpkg.com/react@18/umd/react.development.js" = "react" "https://unpkg.com/react-dom@18/umd/react-dom.development.js" = "react_dom" but importing React, ReactDOM and accessing create_root did not work :/ from pyscript.js_modules import react, react_dom # react is a jsobj, and not usable # I would also have to access # import { createRoot } from 'react-dom/client'; # no clue how that could work :/ # goal: from pyscript import document # TODO somehow import create_root from creat-dom/client # TODO somehow import React and ReactDOM document.body.innerHTML = '<div id="app"></div>'; root = createRoot(document.getElementById('app')); root.render(<h1>Hello, world</h1>); (edited)
Avatar
@Eruvanos [UTC+1] can you tell us what errors you're seeing..?
Eruvanos [UTC+1] started a thread. 26/03/2024 12:04
Avatar
Hi, Iโ€™m making a simple calculator, thereโ€™s a lot of boring context but right now the user is supposed to enter a number, which is checked for a bunch of things until it becomes a value between 0-1. Then multiplies that number by 3600 and outputs the result. I get the error: Traceback (most recent call last): File "<exec>", line 5, in runCalculate TypeError: float() argument must be a string or a real number, not 'pyodide.ffi.JsProxy' Python is here: from pyscript import document def runCalculate(event): input_text = float(document.querySelector("#calc")) if input_text.value > 1: run = input_text.value / 100 elif input_text.value < 0: if input_text.value > -1: run = input_text.value / 100 else: run = input_text else: run = input_text.value seconds = run * 3600 output_div = document.querySelector("#output") output_div.innerText = seconds
Avatar
Jeff Glass 26/03/2024 20:58
Maybe I'm way out of touch here, but I can't seem to import the pyscript package in a py-editor? <script src="mini-coi.js"></script> <script type="module" src="https://pyscript.net/releases/2024.3.2/core.js"></script> <script type="py-editor" env="one"> import pyscript </script> ModuleNotFoundError: No module named 'pyscript'
20:59
(In the py-editor) import os os.listdir(".") []
Avatar
Avatar
Jeff Glass
Maybe I'm way out of touch here, but I can't seem to import the pyscript package in a py-editor? <script src="mini-coi.js"></script> <script type="module" src="https://pyscript.net/releases/2024.3.2/core.js"></script> <script type="py-editor" env="one"> import pyscript </script> ModuleNotFoundError: No module named 'pyscript'
Hmm that's not good. Sorry about that. I'm afk at the moment to check what's going on. @Andrea Giammarchi any ideas?
Avatar
Avatar
ntoll
Hmm that's not good. Sorry about that. I'm afk at the moment to check what's going on. @Andrea Giammarchi any ideas?
Andrea Giammarchi 27/03/2024 08:24
It's been brought up that it's desired to eventually import pyscript module within a py-editor and that's not currently the case. Hooks are super clean on py-editor but it means import ...
08:28
in short it's always been the case ... editors are 100% clean envs, nothing we provide on terminal or regular scripts is in there because everything is different (i.e. error handling, where to display things which is part of the editor, and so on). We can keep it simple or make it very complicated, it's up to us.
08:29
@Jeff Glass now I am also going to ask you why you need pyscript module in there ... to do what? we need to be careful in what we enable because we have already a target output, the editor as input, and errors handled within the output as required.
08:31
happy to learn about use cases, so far two devs got surprised, but beside import pyscript I haven't seen any concrete use case ... even the document is complicated in there to be honest because they might mess up the editor, but also editors are always in workers, which adds complexity.
Avatar
@Jeff Glass / @Andrea Giammarchi Ok. So let's have a quick chat on GitHub about this (so we leave evidence of our decision making for the future). I've tried to summarise the discussion/context so far, and added my 2c to the existing discussion started and linked to by Andrea a couple of weeks ago: https://github.com/pyscript/pyscript/discussions/1997
It's been brought up that it's desired to eventually import pyscript module within a py-editor and that's not currently the case. Hooks are super clean on py-editor but it means import ...
๐Ÿ‘ 1
Avatar
Jeff Glass 27/03/2024 11:21
Thanks yโ€™all! In short, now that we have the setup py-editors, thereโ€™s some setup I would find useful to use the pyscript module for. Will leave details over in GitHub
11:22
Fair warning - iโ€™m on my way to a 9 hour creative session on overhauling our Coal Mine exhibitโ€ฆ. Will try to get some notes in GitHub over lunch, but more likely it will be tonight/tomorrow Chicago time. ๐Ÿ˜…
Avatar
@Jeff Glass I grew up in a coal mining village in north Nottinghamshire (Edwinstowe, site of Thoresby Pit, the largest deep shaft mine in Europe... now closed). So, I'm kinda intrigued by your coal mine exhibit. ๐Ÿ˜„ โ›๏ธ
Avatar
When I went down the pit it was smelly, dark, hot and very (very) dusty, although I enjoyed the danger of the "man-riders".... conveyor belts for coal that folks would also double up and use as person transporters (a health and safety nightmare, with folks regularly losing fingers). Most of my school friends were expecting to go work down the pit, until they were all shut.
Avatar
Andrea Giammarchi 27/03/2024 15:41
@ntoll @Jeff Glass this PR is ready to land, it brings our pyscript module and config ability to both mpy-editor and py-editor https://github.com/pyscript/pyscript/pull/2010
Description This MR should fully solve this discussion #1997 by providing, from core itself, our stdlib within the PyEditor custom type (both py-editor and mpy-editor). All concerns are likely prem...
Avatar
@Andrea Giammarchi does it take into account the situations I enumerated in my last comment to the discussion..? (This is great work BTW)
Avatar
Avatar
ntoll
@Andrea Giammarchi does it take into account the situations I enumerated in my last comment to the discussion..? (This is great work BTW)
Andrea Giammarchi 27/03/2024 15:45
not sure ... reading now, but it brings 100% pyscript module as it is in every other pyscript tag.
Avatar
Jeff Glass 27/03/2024 15:46
@ntoll If I understand right, your comment boils down to "one config per env, and if the env has a setup tag, the config must be on that setup tag"? Is that the gist?
Avatar
Yes... I should have simplified my enumeration... but going through the enumeration was a useful exercise in imagining the combinations of settings.
Avatar
Jeff Glass 27/03/2024 15:56
Oh for sure, it helped me understand it too! Work putting those in the docs as examples, I think, when we get to that point (edited)
Avatar
Exactly. ๐Ÿ‘
Avatar
Andrea Giammarchi 27/03/2024 16:14
I think I messed up forgetting that no env means new env each time ... and I don't know anymore what's the best way forward ... I wrote a few alternatives, but the bottom line is that the code is there, we need to think about what we want to ship but anything works already.
Avatar
Yeah... just catching up on the conversation. Will reply over there. ๐Ÿ‘
Avatar
Avatar
ntoll
Yeah... just catching up on the conversation. Will reply over there. ๐Ÿ‘
Andrea Giammarchi 27/03/2024 17:28
I think we're there ๐Ÿคž
Avatar
Andrea Giammarchi 28/03/2024 09:12
it'd be lovely to move that forward so I can publish to npm and we can test what we decided ๐Ÿฅณ
Avatar
Looking at the PR right now.
09:29
Approved. ๐Ÿšข
Avatar
crogers#1770 28/03/2024 09:44
@Andrea Giammarchi - good morning - how can I read and write directly to the terminal? I want to have the terminal be a terminal to a upython device connected over serial or webBLE. So I want to be able to read from the serial port and push to the terminal - and read from the terminal and push to the serial write. I also want to have all keystrokes (up arrow, tab, etc) passed back and forth and full VT100 emulation - is that possible? Thanks
Avatar
Avatar
crogers#1770
@Andrea Giammarchi - good morning - how can I read and write directly to the terminal? I want to have the terminal be a terminal to a upython device connected over serial or webBLE. So I want to be able to read from the serial port and push to the terminal - and read from the terminal and push to the serial write. I also want to have all keystrokes (up arrow, tab, etc) passed back and forth and full VT100 emulation - is that possible? Thanks
Andrea Giammarchi 28/03/2024 09:46
if you read from serial and push to the terminal then read from the terminal and push to the serial you are echoing or doing some harakiri there ... do you mean to read only users' input, and pass these to the serial, and write in the terminal the serial output, once read?
Avatar
Avatar
Andrea Giammarchi
if you read from serial and push to the terminal then read from the terminal and push to the serial you are echoing or doing some harakiri there ... do you mean to read only users' input, and pass these to the serial, and write in the terminal the serial output, once read?
Andrea Giammarchi 28/03/2024 09:49
also, is this a py-terminal with a worker attribute based on pyodide? I am not sure how code.interact() works there but I think there should be a way to have what you are asking for ... it is indeed simulating a tty but I need to learn a bit more about what you are after, thank you!
๐Ÿ‘ 1
Avatar
Using the worker demo in the help (under Buitins) but can't get the example(hello) to work. Getting message "Cannot read properties of undefined (reading 'module')" Any ideas
11:50
Tried adding this to my toml as well [[fetch]] files = ["./worker1.py"]
Avatar
Hello, I had a working code using Pyscript 2024.2.1. I didn't change anything, but now I am having the following error message (s. image). I don't import tornado as a package, so I guess it is something imported in the background. Is there anything I can do to fix this, or did something change during the update? I am still using Pyscript 2024.2.1, but it also doesn't work in the latest version.
Avatar
Avatar
Neon22
Using the worker demo in the help (under Buitins) but can't get the example(hello) to work. Getting message "Cannot read properties of undefined (reading 'module')" Any ideas
Andrea Giammarchi 28/03/2024 14:41
this works https://pyscript.com/@agiammarchi/worker-demo-copy/latest and you don't need to fetch files in the virtual FS, that file is grabbed from the same folder online regardless. The issue seems to be that we don't pass the interpreter type by default but you can do that explicitly.
Avatar
blackbooks 28/03/2024 15:10
Hello, i am trying my hands at pyscript. I have a application build in flask with a sqlalchemy database which i want to transfer to the frontend. right now i am trying my first steps and simply want to see if _ i can import the sqlalchemy library
  • create an in-memory database using my models
  • query this database with my querymodule
  • and send the queried data to a data collection server i am failing at the first step, i found this older tutorial for importing non standard libraries:
using py_env tag: <py-env>
  • matplotlib
  • numpy
</py-env> <py-script> # Run this using "asyncio" async def main(): await micropip.install(["matplotlib", "numpy"]) await loop.run_until_complete(main()) import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 2 * np.pi, 100) plt.plot(x, np.sin(x)) plt </py-script> this worked with the older versions of pyscript: <link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" /> <script defer src="https://pyscript.net/alpha/pyscript.js"></script> however with the newest version it fails: <link rel="stylesheet" href="https://pyscript.net/releases/2024.1.1/core.css" /> <script type="module" src="https://pyscript.net/releases/2024.1.1/core.js"></script> could someone help me out?
(edited)
Avatar
Avatar
blackbooks
Hello, i am trying my hands at pyscript. I have a application build in flask with a sqlalchemy database which i want to transfer to the frontend. right now i am trying my first steps and simply want to see if _ i can import the sqlalchemy library
  • create an in-memory database using my models
  • query this database with my querymodule
  • and send the queried data to a data collection server i am failing at the first step, i found this older tutorial for importing non standard libraries:
using py_env tag: <py-env>
  • matplotlib
  • numpy
</py-env> <py-script> # Run this using "asyncio" async def main(): await micropip.install(["matplotlib", "numpy"]) await loop.run_until_complete(main()) import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 2 * np.pi, 100) plt.plot(x, np.sin(x)) plt </py-script> this worked with the older versions of pyscript: <link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" /> <script defer src="https://pyscript.net/alpha/pyscript.js"></script> however with the newest version it fails: <link rel="stylesheet" href="https://pyscript.net/releases/2024.1.1/core.css" /> <script type="module" src="https://pyscript.net/releases/2024.1.1/core.js"></script> could someone help me out?
(edited)
Jeff Glass 28/03/2024 15:13
For sure! PyScript has come a long way since alpha. Here's the equivalent of your code, rewritten for the latest release: <script type="module" src="https://pyscript.net/releases/2024.3.2/core.js"></script> <link rel="stylesheet" href="https://pyscript.net/releases/2024.3.2/core.css"> <py-config> packages=['matplotlib', 'numpy'] </py-config> <script type="py"> import matplotlib.pyplot as plt import numpy as np from pyscript import display x = np.linspace(0, 2 * np.pi, 100) plt.plot(x, np.sin(x)) display(plt) </script> (edited)
Avatar
blackbooks 28/03/2024 15:15
Aww thanks alot that was quick, i apologize i havent used the discord much recently and will try to use the code formatting for better readability next time
Avatar
Jeff Glass 28/03/2024 15:16
No worries! Some particulars to point out:
  • py-env became py-config and gained some new features/syntax
  • We no longer automatically output the last expression of a file, hence the use of pyscript's builtin display()
  • No need to use asyncio here, which is always nice ๐Ÿ™‚
  • You can still use <py-script>, but we recommend <script type="py"> to avoid a nasty class of parsing errors the browser can trip on
(edited)
๐Ÿ‘ 1
Avatar
Andrea Giammarchi 28/03/2024 16:05
... (edited)
16:06
oh, right, discord didn't update my view ... what Jeff said then ๐Ÿ˜
Avatar
Avatar
Andrea Giammarchi
this works https://pyscript.com/@agiammarchi/worker-demo-copy/latest and you don't need to fetch files in the virtual FS, that file is grabbed from the same folder online regardless. The issue seems to be that we don't pass the interpreter type by default but you can do that explicitly.
Thanks @Andrea Giammarchi . Looking forward to demos/examples of pyscript's excellent features. Cheers... As a side note - new projects in pyscript.com are not yet using 2024.3.2 and so the code doesn't work without manual mod to the latest release... (edited)
Avatar
Just a worker follow up question. I see onmessage and postMessage available in worker api. Is this for making Web Workers ? Is the sync() designed for Service Workers? Is sync doing this kind of thing: addEventListener('sync', function (event) {}) ? Any guidance docs you can point us at. I am guessing the new fetch are designed to enable Service workers to do Background syncs ? What's the plan ? Thanks. (edited)
Avatar
OK. I can see I can do a lot with sync but how do I send information from main to the worker for it to work on ?
Avatar
@Neon22 Hiya - I couldn't find the thread where we were talking about the fact that when doesn't work in class... Here's a workaround.... from pyscript import display, document from pyscript.ffi import create_proxy class App: def __init__(self): """Wire up your event listeners in here...""" self.when("click", "#click-me", self.when_click_me_clicked) def when(self, event_type, selector, method): """ Listen for "event_type" events on all elements that match the specified css selector. """ for element in document.querySelectorAll(selector): element.addEventListener(event_type, create_proxy(method)) def when_click_me_clicked(self, event): """Called when the click-me button is pressed.""" display("Clicked!", self) app = App() (edited)
Avatar
Any clues on how to send data to the worker using the sync method (as shown in the help) would be appreciated. Thanks...
Avatar
Avatar
Neon22
Any clues on how to send data to the worker using the sync method (as shown in the help) would be appreciated. Thanks...
The interface looks symmetrical: could you assign the function in the worker and call from main? Just extrapolating from the docs as I'm interested in the answer too.
Avatar
A short, top-line, main priority question ... when I call add_event_listener() can I specify arguments for the python function I call inside it? I currently have a monolithic function that manages a file upload and carries out operations on the file, but I would like to break it into pieces so that there is a function that handles the file upload, and then with subsequent button clicks, the user decides which operations to apply. This feeds into a more general (and much lower priority...feel free to ignore it) confusion about how arguments and variables are used in the pyscript/pyodide examples I have seen. For the functions that do a lot of the work between the 'website side' javascript and the 'developer side' python, the function definitions often include arguments that are not referenced in the body of the function ... while variables that I would want to set up as arguments are just referred to in the function body, all while the function itself does not return a value that would enable you to chain functions together . For instance, here (from https://pyscript.recipes/2024.1.1/basic/file-download/), there is never a reference to any *args ... while the variable 'data' is defined outside of it, and directly referenced in the function body: data = "Hello world, this is some text." def downloadFile(*args): encoded_data = data.encode('utf-8') my_stream = io.BytesIO(encoded_data) js_array = Uint8Array.new(len(encoded_data)) js_array.assign(my_stream.getbuffer()) file = File.new([js_array], "unused_file_name.txt", {type: "text/plain"}) url = URL.createObjectURL(file) hidden_link = document.createElement("a") hidden_link.setAttribute("download", "my_other_file_name.txt") hidden_link.setAttribute("href", url) hidden_link.click() add_event_listener(document.getElementById("download"), "click", downloadFile) (edited)
Avatar
Hello! I've been working on a web interface for a python package that runs a scientific simulation (with scipy/numpy/matplotlib). Pyscript has made it very easy so far! Now though, the whole UI is blocked while the long calculation is run. I'd like to implement friendly features like a progress bar/cancel button/animated plot etc that aren't currently possible. I'm trying to figure out how best to proceed, but I have some conceptual hangups on the differences between asyncio and PyScript Workers. Is the main difference that a "Worker" actually runs on an independent thread while async just mimics that behavior using a single thread at the expense of some special treatment in the code? Simply defining my high-level "run_simulation()" function as async doesn't produce the intended result, presumably because none of the function calls within run_simulation() to other packages (e.g. scipy) are asynchronous. Is my understanding correct? A PyScript Worker should be more appropriate, right? Unfortunately my first attempts down this path have only produced matplotlib errors... I'd be appreciative if anyone knows of an example using matlotlib and a worker thread together. Actually, I can't find any demo in the pyscript example gallery that implements a worker, so even that could help me. Thanks!
Neon22 started a thread. 30/03/2024 20:30
Avatar
Avatar
nickburns
Hello! I've been working on a web interface for a python package that runs a scientific simulation (with scipy/numpy/matplotlib). Pyscript has made it very easy so far! Now though, the whole UI is blocked while the long calculation is run. I'd like to implement friendly features like a progress bar/cancel button/animated plot etc that aren't currently possible. I'm trying to figure out how best to proceed, but I have some conceptual hangups on the differences between asyncio and PyScript Workers. Is the main difference that a "Worker" actually runs on an independent thread while async just mimics that behavior using a single thread at the expense of some special treatment in the code? Simply defining my high-level "run_simulation()" function as async doesn't produce the intended result, presumably because none of the function calls within run_simulation() to other packages (e.g. scipy) are asynchronous. Is my understanding correct? A PyScript Worker should be more appropriate, right? Unfortunately my first attempts down this path have only produced matplotlib errors... I'd be appreciative if anyone knows of an example using matlotlib and a worker thread together. Actually, I can't find any demo in the pyscript example gallery that implements a worker, so even that could help me. Thanks!
The worker example thread (just two above this) will show you how to have a worker perform a task and hand the result back to you (using sync). However it does not show how to send data to the worker... yet...
Avatar
Avatar
Neon22
The worker example thread (just two above this) will show you how to have a worker perform a task and hand the result back to you (using sync). However it does not show how to send data to the worker... yet...
Thanks @Neon22, looks like I missed that thread! It appears that PyWorker just runs the associated script file as soon as it is created. It feels like the best configuration for me would be to have a single worker object containing my expensive simulation function, and then call that function from the main thread UI as many times as I want. Is this possible or would I need to create a new PyWorker on each call to the simulation function? If the answer is creating a new PyWorker, does this also mean that a new pyodide interpreter has to be loaded each time? Apologies for the repeat question. I searched back through some previous comments and saw that the worker "sync" feature can be used in both directions (to and from main) and that I may also just be able to attach my DOM event listeners from the worker script anyway. (edited)
Avatar
I wish I knew. Hopefully someone who knows will respond.
Avatar
Jeff Glass 31/03/2024 03:11
@nickburns Are you looking to cancel the previous calls to the expensive simulation function and 'restart' them with each call? Or run multiple, independant copies of that calculation while others are still in progress? If it's the latter, yes you will need multiple workers, and they will need to bootstrap the interpreter each time
Avatar
I am pretty sure I can run the worker again. I just have to call worker.sync.hello = hello again. I have got it working several times. so the `worker.sync.function_name calls the function in the worker
๐Ÿ‘ 1
03:13
ahh but aparenty I am mistaken. So Jeff. How do you send args to the worker ?
Avatar
Jeff Glass 31/03/2024 03:13
Like how to pass arguments to a function defined in a worker?
Avatar
for me - I wanto run the worker with some args. Wait for the sync messgae at the end. Display teh results and start the process again with different args. I only need one running at a time
Avatar
Jeff Glass 31/03/2024 03:15
Ahhh, I see. Let me copy in a slightly different example I already have built (triggered the function manually via event) and then work on your specific example
Avatar
Imagin I'm calculating a prime from some factors and I'm prepared to wait for results before I start a new calc.
Avatar
Jeff Glass 31/03/2024 03:16
This is a page with two text inputs and a 'calculate' button - it uses the input fields as the seed and number of iterations for a pseudorandom number generator which runs in a worker: <!-- index.html --> <script type="module" src="https://pyscript.net/releases/2024.3.2/core.js"></script> <label for="seed">Seed</label> <input type="text" name="seed" id="seed"> <br><label for="iterations">Iterations</label> <input type="text" name="iterations" id="iterations"> <br><button id="btn">Calculate</button> <div id="result"></div> <script type="module"> import { PyWorker } from 'https://cdn.jsdelivr.net/npm/@pyscript/core@0.4.11/+esm'; const { sync } = await PyWorker( './worker.py', { config: { sync_main_only: true }} ); document.getElementById('btn').addEventListener('click', async () => { const seed = parseInt(document.getElementById("seed").value) const iters = parseInt(document.getElementById("iterations").value) const result = await sync.pseudorandom(seed, iters) document.getElementById("result").innerText += result + "\n" }) </script> #worker.py from pyscript import sync def pseudorandom(seed: int, iters: int): """Linear congruential generator. Parameters borrowed from POSIX and are probably bad""" modulus = 2**48 a = 25214903917 increment = 11 for _ in range(iters): seed = (a * seed + increment) % modulus return seed % (2 ** 47) sync.pseudorandom = pseudorandom
03:16
What this doesn't include is your "Wait for the sync message, then restart the calc with new args" part, let me think on that
Avatar
I was planning on sending a sep sync back to another function flagging tha the calc had finished but I think its unnecessary.
Avatar
Jeff Glass 31/03/2024 03:18
Yeah you can just await the function from the worker inside some larger async handler, then take whatever appropriate action there is after it finishes
03:19
If that makes any sense
Avatar
ok. yes I see JS example. Mine would be in pyscript. Thanks
Avatar
Jeff Glass 31/03/2024 03:21
There's some examples in issue #1979 that are in PyScript, and may be helpful as well. The story around accessing the PyWorker utility inside PyScript/Pyodide is a little mushy... see Andrea's notes in issue #1980
03:22
Actually your thoughts would be helpful in that issue, just around how we could clear up how PyWorkers actually behave within Python. @nickburns your thoughts would be welcome as well
Avatar
Yeah - we don't mind if it change in a new release - we just want to know how to make it work in current.. or not
Avatar
Jeff Glass 31/03/2024 03:23
Honestly same... I'm a little lost on creating a PyWorker from within Pyodide (edited)
03:23
I thought maybe we had removed that ability at some point, but I could be wrong. And I still at least see a thing called PyWorker in the PyScript package...
03:23
๐Ÿคทโ€โ™‚๏ธ
Avatar
I just commented - Thanks Jeff for the clarity. Its possible I'm only a smidgen away from a working solution. Just not sure if I can send args with worker.sync.draw_floats = draw_floats and adding some args
03:47
yes - it was removed in the prev release I think but back in this one. Got to get eth worker-options set
Avatar
Hi @Neon22 and @Jeff Glass, thanks for your help. I adapted the prng code above and made something workable using pyscript instead of JS for the function call to the worker (p.s. I get a polyscript TypeError with the JS version anyway). You can see the example here: https://pyscript.com/@nickburns/simulation-worker/latest. This appears to achieve the desired behavior. Clicking the button to run the simulation doesn't block the UI, and it is also possible to pass parameters to the worker function.
๐Ÿ‘ 1
Avatar
Avatar
nickburns
Hi @Neon22 and @Jeff Glass, thanks for your help. I adapted the prng code above and made something workable using pyscript instead of JS for the function call to the worker (p.s. I get a polyscript TypeError with the JS version anyway). You can see the example here: https://pyscript.com/@nickburns/simulation-worker/latest. This appears to achieve the desired behavior. Clicking the button to run the simulation doesn't block the UI, and it is also possible to pass parameters to the worker function.
Jeff Glass 31/03/2024 19:47
Thanks nick! This example is quite helpful. I'm having trouble combining this with the sync_main_only parameter though, to ideally be able to spawn PyWorkers from PyScript without SharedArrayBuffer access
19:48
i.e. something like: <py-config> sync_main_only = true </py-config> <script type="py"> from pyscript import PyWorker from pyscript.ffi import to_js options = to_js({"type": "pyodide"}) # Should _this_ have sync_main_only config'd? p = PyWorker('./foo.py', options) </script> (edited)
Avatar
Avatar
nickburns
Hi @Neon22 and @Jeff Glass, thanks for your help. I adapted the prng code above and made something workable using pyscript instead of JS for the function call to the worker (p.s. I get a polyscript TypeError with the JS version anyway). You can see the example here: https://pyscript.com/@nickburns/simulation-worker/latest. This appears to achieve the desired behavior. Clicking the button to run the simulation doesn't block the UI, and it is also possible to pass parameters to the worker function.
wow - brilliant. Thanks
Avatar
Avatar
Jeff Glass
Thanks nick! This example is quite helpful. I'm having trouble combining this with the sync_main_only parameter though, to ideally be able to spawn PyWorkers from PyScript without SharedArrayBuffer access
Hi Jeff, I fixed the example code. You need to put the sync_main_only parameter inside the config option like this: worker_options = ffi.to_js({"type": "pyodide", "config": {"sync_main_only": True}}) (edited)
๐Ÿ‘€ 1
Avatar
Avatar
nickburns
Hi Jeff, I fixed the example code. You need to put the sync_main_only parameter inside the config option like this: worker_options = ffi.to_js({"type": "pyodide", "config": {"sync_main_only": True}}) (edited)
Nice! Is there a plan for pyworker to evolve to take python args instead of needing to_js()? Is programmatically spawning the worker with pyworker() currently the only way to use the sync behaviour? If so it might be an easier learning curve if a script with the worker tag could also have a name then main could access the worker by name as per this example.
Avatar
Avatar
nickburns
Hi Jeff, I fixed the example code. You need to put the sync_main_only parameter inside the config option like this: worker_options = ffi.to_js({"type": "pyodide", "config": {"sync_main_only": True}}) (edited)
Jeff Glass 31/03/2024 23:14
Thank you!
Avatar
Avatar
Neon22
Just a worker follow up question. I see onmessage and postMessage available in worker api. Is this for making Web Workers ? Is the sync() designed for Service Workers? Is sync doing this kind of thing: addEventListener('sync', function (event) {}) ? Any guidance docs you can point us at. I am guessing the new fetch are designed to enable Service workers to do Background syncs ? What's the plan ? Thanks. (edited)
Andrea Giammarchi 02/04/2024 09:07
worker and fetch are not strictly coupled with Service Workers and vice-versa ... these are ust primitives. The sync is a namespace where you expose utilities, as functions, from either the worker world or he main one, and you can then invoke these utilities from the outer world either synchronously (from worker) or awaiting results (from main). There's no sync event as you can expose hundreds utilities and that would make little sense + the onmessage is thever because you effectively deal with a worker instance, it's just augmented with the sync utility but it is already a Worker 100%. Service Worker is nowhere used in the project except for mini-coi but it's completely unrelated and used only to enable/allow headers needed for the SharedArrayBuffer primitive.
Avatar
elliot0x01 05/04/2024 14:56
What's the syntax to give custom height to the terminal window?
Avatar
Avatar
elliot0x01
What's the syntax to give custom height to the terminal window?
Jeff Glass 05/04/2024 15:25
If you want to adjust columns/rows, the __terminal__ attribute is a reference to the xtermjs terminal itself. You can use resize() to change the rows/columns at runtime. Example here: https://jeff.glass/post/whats-new-pyscript-2024-q1/#terminal-attribute
What's new and what's changed in PyScript in the first part of 2024
๐Ÿ’ฏ 1
Avatar
Avatar
Jeff Glass
If you want to adjust columns/rows, the __terminal__ attribute is a reference to the xtermjs terminal itself. You can use resize() to change the rows/columns at runtime. Example here: https://jeff.glass/post/whats-new-pyscript-2024-q1/#terminal-attribute
elliot0x01 05/04/2024 16:23
Thanks Jeff
Avatar
Hi so im trying to use pyscript for a project im working on however i want to know if its possible to use JS to pull data from a python function as im stuck
Avatar
Avatar
AndrewD
Nice! Is there a plan for pyworker to evolve to take python args instead of needing to_js()? Is programmatically spawning the worker with pyworker() currently the only way to use the sync behaviour? If so it might be an easier learning curve if a script with the worker tag could also have a name then main could access the worker by name as per this example.
Hi @AndrewD, I can't speak for the developers' plans, but I made a copy of the simulation demo script that creates the worker in javascript, and makes "sync" a javascript global, which can be accessed from the python code running in main: https://nickburns.pyscriptapps.com/simulation-worker-copy/. When the worker is created programmatically from main.py (as in the earlier example) it can get mega slow as it has to first wait until pyodide and all packages for main.py are loaded before then loading the additional pyodide+packages for the worker. It's not a big deal for a toy example, but it adds up fast when more packages are needed. In this updated configuration, both main and worker are loaded in parallel, which is much more efficient.
Avatar
Avatar
Jrd458
Hi so im trying to use pyscript for a project im working on however i want to know if its possible to use JS to pull data from a python function as im stuck
Jeff Glass 08/04/2024 03:20
If you mean you want to call a Python function from JS, check out this recipe: https://pyscript.recipes/2024.1.1/js-integration/export-js/
blackbooks started a thread. 09/04/2024 12:38
Avatar
I'm writing a pyscript HTML file that I want to be able to be customized using query arguments in the URL (adding an optional "year" argument). I'm borrowing heavily from Blake Rayfields "Data Visualization with Pyscript" presentation. Is this how to get access to query args in the URL? <!-- Short JS script to extract "year" query arg, if given --> <script> var queryString = window.location.search; var urlParams = new URLSearchParams(queryString); var urlYear = urlParams.get("year") </script> <script type="py"> from operator import attrgetter from pyscript import display, HTML import pyscript from pyodide.http import open_url import matplotlib.pyplot as plt import littletable as lt # use littletable to import data from remote CSV URL; # transforms convert data for those attributes, all others are loaded as strs url = "https://raw.githubusercontent.com/datasets/s-and-p-500/master/data/data.csv" stock_data = lt.csv_import( open_url(url), transforms={ "Date": lt.Table.parse_date("%Y-%m-%d"), "SP500": float, } ) # add a computed field to summarize or fetch by year get_year = attrgetter("Date.year") stock_data.add_field("Year", get_year) # extract Javascript variable if a query arg `year=####` was part of the URL; # if given, filter the retrieved data for just this particular year year = pyscript.window.urlYear if year is not None: stock_data = stock_data.where(Year=int(year)) Or have I just reinvented something that is already in the Pyscript library? (I'm also trying to see if using my littletable package instead of pandas does any good.)
Avatar
Jeff Glass 09/04/2024 18:12
Parsing of URL params isn't something that's currently in the PyScript ffi - the way you're doing it is just fine, though I might suggest moving your JS logic into PyScript for cleanliness, if doing it in JS isn't critical: <script type="py"> # ... Some lines omitted # add a computed field to summarize or fetch by year get_year = attrgetter("Date.year") stock_data.add_field("Year", get_year) # vvvv Get the Querys within PyScript vvvvv # if given, filter the retrieved data for just this particular year def getQueryYear() -> str | None: queryString = pyscript.window.location.search; urlParams = pyscript.window.URLSearchParams.new(queryString); return urlParams.get("year") year = getQueryYear() if year is not None: stock_data = stock_data.where(Year=int(year)) print(f"Got data for year {year}") else: print("No query parameter found") </script>
๐Ÿ’ฏ 1
๐Ÿฆธโ€โ™‚๏ธ 1
Avatar
Avatar
ptmcg
I'm writing a pyscript HTML file that I want to be able to be customized using query arguments in the URL (adding an optional "year" argument). I'm borrowing heavily from Blake Rayfields "Data Visualization with Pyscript" presentation. Is this how to get access to query args in the URL? <!-- Short JS script to extract "year" query arg, if given --> <script> var queryString = window.location.search; var urlParams = new URLSearchParams(queryString); var urlYear = urlParams.get("year") </script> <script type="py"> from operator import attrgetter from pyscript import display, HTML import pyscript from pyodide.http import open_url import matplotlib.pyplot as plt import littletable as lt # use littletable to import data from remote CSV URL; # transforms convert data for those attributes, all others are loaded as strs url = "https://raw.githubusercontent.com/datasets/s-and-p-500/master/data/data.csv" stock_data = lt.csv_import( open_url(url), transforms={ "Date": lt.Table.parse_date("%Y-%m-%d"), "SP500": float, } ) # add a computed field to summarize or fetch by year get_year = attrgetter("Date.year") stock_data.add_field("Year", get_year) # extract Javascript variable if a query arg `year=####` was part of the URL; # if given, filter the retrieved data for just this particular year year = pyscript.window.urlYear if year is not None: stock_data = stock_data.where(Year=int(year)) Or have I just reinvented something that is already in the Pyscript library? (I'm also trying to see if using my littletable package instead of pandas does any good.)
Jeff Glass 09/04/2024 18:13
You could also handle URL parsing with urllib from the Python standard library, if you wanted to: def getQueryYear() -> str | None: import urllib.parse as parse queryString = str(pyscript.window.location) urlQuery = parse.urlparse(queryString).query queryDict = parse.parse_qs(urlQuery) return queryDict['year'][0] if 'year' in queryDict else None (edited)
Avatar
// Use the retrieved roomTypes list document.addEventListener('DOMContentLoaded', function() { var selectElement = document.getElementById('room-type-select'); // Populate the select element with options roomTypes.forEach(function(roomType) { var option = document.createElement('option'); option.value = roomType; option.textContent = roomType; selectElement.appendChild(option); }); }); // Execute Python function using <Py-script> and retrieve room_types list var roomTypes = <Py-script> def get_room_types(): room_types = ["Computers", "Labs", "Computers"] print (room_types) return room_types get_room_types() // Call the function to return the room_types list </Py-script>; why does this not populate the select ?
Avatar
blackbooks 09/04/2024 20:47
I created a thread for my issue thinking it would be more concise, i would love to get some feedback there please or i could also repeat my question directly. (edited)
Avatar
Avatar
blackbooks
I created a thread for my issue thinking it would be more concise, i would love to get some feedback there please or i could also repeat my question directly. (edited)
Jeff Glass 09/04/2024 20:49
I'm not seeing your thread... is it in this channel?
Avatar
Avatar
Jrd458
// Use the retrieved roomTypes list document.addEventListener('DOMContentLoaded', function() { var selectElement = document.getElementById('room-type-select'); // Populate the select element with options roomTypes.forEach(function(roomType) { var option = document.createElement('option'); option.value = roomType; option.textContent = roomType; selectElement.appendChild(option); }); }); // Execute Python function using <Py-script> and retrieve room_types list var roomTypes = <Py-script> def get_room_types(): room_types = ["Computers", "Labs", "Computers"] print (room_types) return room_types get_room_types() // Call the function to return the room_types list </Py-script>; why does this not populate the select ?
Jeff Glass 09/04/2024 20:50
You can't use PyScript inside of JavaScript like that - <py-script> or <script type='py'> is a separate HTML tag that executes Python
Avatar
so how would i go about getting data from a DB in Python translate it to JS to modify a html page
Avatar
Avatar
Jrd458
so how would i go about getting data from a DB in Python translate it to JS to modify a html page
Jeff Glass 09/04/2024 20:56
You can do it all directly within PyScript: <script type="py"> from pyscript import window, document def get_room_types(): room_types = ["Computers", "Labs", "Computers"] print (room_types) return room_types selectElement = document.getElementById('room-type-select') for roomType in get_room_types(): option = document.createElement('option') option.value = roomType option.textContent = roomType selectElement.appendChild(option) </script>
20:56
pyscript.window is a proxy to the JavaScript global namespace; pyscript.document is a proxy to the DOM document
Avatar
ok would i be able to have this pyscript in a seperate file so its not all on the HTML page?
Avatar
Jeff Glass 09/04/2024 20:57
For sure - if that code is in myapp.py, you can do <script type="py" src="myapp.py"></script>
Avatar
would i be able to run the scripts to run different Functions. say if i press a button run this function.
Avatar
Jeff Glass 09/04/2024 21:00
So there's a couple ways to do that:
Avatar
so i tried to do the code above and it says it can not find the url for myapp.py
Avatar
Jeff Glass 09/04/2024 21:01
The first is with the py-[event] syntax - any HTML tag that has an attribute like py-click or py-mouseover will be hooked up to the function with the value of that attribute: <button py-click="foo">Click Me!</button> <script type="py"> from pyscript import display def foo(evt): display("Hey you clicked the button!") </script>
Avatar
Jeff Glass 09/04/2024 21:04
The other way is to use PyScript's @when decorator, which takes an event type and a css selector: <button>One</button> <button>Two</button> <script type="py"> from pyscript import display, when @when("click", "button") def foo(evt): display(f"Hey you clicked the button with text '{evt.target.innerText}'") </script>
Avatar
Avatar
Jrd458
so i tried to do the code above and it says it can not find the url for myapp.py
Jeff Glass 09/04/2024 21:04
As for the fetch error - how are you loading/launching your page? Are you double-clicking on your file, or using a server, or...?
Avatar
its locally hosted using iis
21:05
but i portforwarded so i can access external.
Avatar
Jeff Glass 09/04/2024 21:06
Hmmmmm.... If you go to the myapp.py file directly in your browser, does it show/download that file?
21:06
Like if you type in whatever the complete URL of that file is?
Avatar
no have i missed something setting this up
21:07
21:07
would i need to enable udp as well on the port
Avatar
Jeff Glass 09/04/2024 21:08
UDP shouldn't matter
21:09
I'm not sure how IIS interacts with how the browser is using fetch()
21:09
If you open the browser's dev console, do you see additional errors there?
21:11
Ah yeah, so it seems like the myapp.py file isn't actually being served
Avatar
how would i go about serving it
Avatar
Jeff Glass 09/04/2024 21:13
One way is to, from the command line, run python3 -m http.server in the folder in question
21:13
(Defaults to localhost:8000)
Avatar
would i want to do it on the project folder for the whole thing or like the sub folder
21:14
Avatar
Jeff Glass 09/04/2024 21:14
It might depend on whether the site is part of a larger project, but probably in the project folder?
Avatar
should i change the port
Avatar
Jeff Glass 09/04/2024 21:16
If you're forwarding something else, probably. It's the first positional argument, i.e. binding to port 1234 (and a local IP) is: You can also add python -m http.server 1234 --bind 127.0.0.1
Avatar
so i would want 80 and then my local machine ip
Avatar
Jeff Glass 09/04/2024 21:17
presumably? Depends how your forwarding system works
Avatar
ive got tcp on port 80 to my local
Avatar
Jeff Glass 09/04/2024 21:17
I'm not an expert, but that doesn't sound super secure - are you firewalled off from the internet? the builtin Python http server isn't really secured at all, it's for testing
Avatar
its for a closed project so no public are going to be on it for private and testing
๐Ÿ‘ 1
21:19
21:21
so it doesnt like it on port im using to host webpage so should i use a different port and port forawrd that?
Avatar
Jeff Glass 09/04/2024 21:22
That makes sense?
Avatar
ok so i setup a new portforward and im running the cmd and it still can not fine the myapp.py
Avatar
Jeff Glass 09/04/2024 21:24
I'm not sure how to best help here... if your browser can't see/access that file, that's something to do with the setup there and not something PyScript is causing.
21:24
For what it's worth, our basic setup is here
21:24
But if sounds like your port-forwarding setup is a little more complicated
Avatar
Hello Good People! I'm using pyscript.com to practice creating projects, and in pyscript.toml, as long as I only had packages = ["banglanltk"] and in main.py import banglanltk as bn from pyscript import document everything was well and good. But then, when I changed it to packages = ["banglanltk", "collections"] and import banglanltk as bn from pyscript import document from collections import defaultdict, Counter all the buttons in the html page has gone inactive suddenly. Any suggestions regarding how to deal with this? Thanks in advance. (edited)
Avatar
Avatar
Moum
Hello Good People! I'm using pyscript.com to practice creating projects, and in pyscript.toml, as long as I only had packages = ["banglanltk"] and in main.py import banglanltk as bn from pyscript import document everything was well and good. But then, when I changed it to packages = ["banglanltk", "collections"] and import banglanltk as bn from pyscript import document from collections import defaultdict, Counter all the buttons in the html page has gone inactive suddenly. Any suggestions regarding how to deal with this? Thanks in advance. (edited)
Jeff Glass 09/04/2024 21:44
You don't need to put collections in your packages key - since collections in the standard library, it's installed by default and can just be imported
Avatar
Hey, sorry, resolved.
Avatar
Avatar
Jeff Glass
You don't need to put collections in your packages key - since collections in the standard library, it's installed by default and can just be imported
Thank you Jeff, for the help. Really appreciate it.
๐Ÿฅณ 1
Avatar
Avatar
Moum
Thank you Jeff, for the help. Really appreciate it.
Jeff Glass 09/04/2024 22:26
My pleasure!
Avatar
@Jeff Glass I managed to get access to the file on my website now im getting a new error. [pyexec] Python exception: Traceback (most recent call last): File "/home/pyodide/pyscript/internal.py", line 104, in runpyscript result = eval_code(code, globals=__main.dict) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/lib/python311.zip/pyodide/base.py", line 468, in eval_code .run(globals, locals) ^^^^^^^^^^^^^^^^^^^^ File "/lib/python311.zip/_pyodide/_base.py", line 310, in run coroutine = eval(self.code, globals, locals) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "<exec>", line 1, in <module> ImportError: cannot import name 'window' from 'pyscript' (/home/pyodide/pyscript/__init.py)
Avatar
Avatar
Jrd458
@Jeff Glass I managed to get access to the file on my website now im getting a new error. [pyexec] Python exception: Traceback (most recent call last): File "/home/pyodide/pyscript/internal.py", line 104, in runpyscript result = eval_code(code, globals=__main.dict) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/lib/python311.zip/pyodide/base.py", line 468, in eval_code .run(globals, locals) ^^^^^^^^^^^^^^^^^^^^ File "/lib/python311.zip/_pyodide/_base.py", line 310, in run coroutine = eval(self.code, globals, locals) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "<exec>", line 1, in <module> ImportError: cannot import name 'window' from 'pyscript' (/home/pyodide/pyscript/__init.py)
Jeff Glass 09/04/2024 22:52
Sounds like maybe youโ€™re using an older version of PyScript - the most recent release is 2024.3.2
Avatar
OMG ITS WORKING (edited)
๐Ÿฅณ 1
23:05
now i just need to point it at the DBs (edited)
23:08
and its broke
Avatar
I'm interested in reading data matrix "barcodes" with pyscript: can anyone suggest a JS library they have used for this with pyscript? There seem to be lots of options, so i thought I'd ask before i start blindly experimenting ๐Ÿ˜ƒ
Avatar
I'm getting an error now where it can not find the module for my db connection on pyodide?
Avatar
Basically i need a way to connect my SSMS server to python then through Pyscript into JS then to HTML
Avatar
Avatar
AndrewD
I'm interested in reading data matrix "barcodes" with pyscript: can anyone suggest a JS library they have used for this with pyscript? There seem to be lots of options, so i thought I'd ask before i start blindly experimenting ๐Ÿ˜ƒ
Andrea Giammarchi 10/04/2024 08:36
haven't tried myself but this library seems to be pretty good at that https://www.npmjs.com/package/@ericblade/quagga2
An advanced barcode-scanner written in JavaScript. Latest version: 1.8.4, last published: 6 months ago. Start using @ericblade/quagga2 in your project by running npm i @ericblade/quagga2. There are 17 other projects in the npm registry using @ericblade/quagga2.
Avatar
Avatar
Andrea Giammarchi
haven't tried myself but this library seems to be pretty good at that https://www.npmjs.com/package/@ericblade/quagga2
Thanks, unfortunately it seems to only support 1D barcodes. I did find this one that seems to say the right things. I'll see if I can glue it into pyscript easily without any prior background ๐Ÿค”: https://github.com/zxing-js/library (edited)
Multi-format 1D/2D barcode image processing library, usable in JavaScript ecosystem. - zxing-js/library
Avatar
Does pyscript handle vpython lib, if yes, can someone help me fix this issue
Avatar
Avatar
AndrewD
Thanks, unfortunately it seems to only support 1D barcodes. I did find this one that seems to say the right things. I'll see if I can glue it into pyscript easily without any prior background ๐Ÿค”: https://github.com/zxing-js/library (edited)
Andrea Giammarchi 10/04/2024 12:22
that has way more stars so I think it's a good library
Avatar
Avatar
Andrea Giammarchi
that has way more stars so I think it's a good library
I just tripped over this browser API that is probably even better, likely to be more robust? https://developer.mozilla.org/en-US/docs/Web/API/Barcode_Detection_API
The Barcode Detection API detects linear and two-dimensional barcodes in images.
Avatar
Avatar
AndrewD
I just tripped over this browser API that is probably even better, likely to be more robust? https://developer.mozilla.org/en-US/docs/Web/API/Barcode_Detection_API
Andrea Giammarchi 10/04/2024 12:34
it's a Chromium only thing https://developer.mozilla.org/en-US/docs/Web/API/Barcode_Detection_API#browser_compatibility ... it's a great starting point and if you don't care about other browsers, also the way I would go myself.
The Barcode Detection API detects linear and two-dimensional barcodes in images.
Avatar
Avatar
AndrewD
I just tripped over this browser API that is probably even better, likely to be more robust? https://developer.mozilla.org/en-US/docs/Web/API/Barcode_Detection_API
Andrea Giammarchi 10/04/2024 12:39
watch out no browser here shows support ... "BarcodeDetector" in globalThis is false in both Chrome and MSEdge
Avatar
Avatar
Andrea Giammarchi
watch out no browser here shows support ... "BarcodeDetector" in globalThis is false in both Chrome and MSEdge
Maybe mobile only? That could be ok if I'm just playing around initially. On a computer could use an external scanner too.
Avatar
All new to me: pyscript got me interested in web stuff again for the first time since some minor php hacking
โค๏ธ 2
Avatar
Avatar
AndrewD
Maybe mobile only? That could be ok if I'm just playing around initially. On a computer could use an external scanner too.
Andrea Giammarchi 10/04/2024 14:13
it's not about the device, it's about the browser exposing that API ... which doesn't in my Linux Desktop. You can use your Webcam of just upload images with bar codes and that API should work. The safe side though might be that zx library you mentioned, hopefully that works regardless of the browser/env.
๐Ÿ‘ 1
Avatar
I think opencv has a barcode reader section. Not sure if cv2 is importable into pyscript
Avatar
it should be pyodide support cv2 with numpy < 2
21:48
zbar is also an option
Avatar
Avatar
shyz
Hello, I had a working code using Pyscript 2024.2.1. I didn't change anything, but now I am having the following error message (s. image). I don't import tornado as a package, so I guess it is something imported in the background. Is there anything I can do to fix this, or did something change during the update? I am still using Pyscript 2024.2.1, but it also doesn't work in the latest version.
Hi there. It seems like my message got lost a bit. But I really need your help with this. How come I get an "ValueError: Can't find a pure Python 3 wheel for: 'tornado>=6.2'" if I don't do anything specific about this?
Avatar
I am getting the same error when running some of the public examples on https://pyscript.com/@examples (see: KMeans in Panel, Panel and hvPlot or Streaming in Panel). I am not sure who can do something about this. Maybe @Andrea Giammarchi ?
Avatar
Avatar
shyz
I am getting the same error when running some of the public examples on https://pyscript.com/@examples (see: KMeans in Panel, Panel and hvPlot or Streaming in Panel). I am not sure who can do something about this. Maybe @Andrea Giammarchi ?
Andrea Giammarchi 11/04/2024 08:51
tornado is missing in Pyodide upstream, I am not sure what I can do but there's an issue around it already https://github.com/pyscript/examples/issues/16
Checklist I added a descriptive title I searched for other issues and couldn't find a solution or duplication I already searched in Google and didn't find any good information or help What ...
๐Ÿ‘ 1
Avatar
elliot0x01 11/04/2024 12:28
Hi, is there any way to make links which appear on terminal clickable?
12:31
currently they appear as plain text, like <script type="mpy" terminal worker> base="http ://evil,com" x=input("username:") print(f"Download link for user x is: {base}/x/random_uuid/" ) </script> Any method to make the download link printed on terminal clickable?
Avatar
Avatar
elliot0x01
currently they appear as plain text, like <script type="mpy" terminal worker> base="http ://evil,com" x=input("username:") print(f"Download link for user x is: {base}/x/random_uuid/" ) </script> Any method to make the download link printed on terminal clickable?
Andrea Giammarchi 11/04/2024 14:07
was evil.com really necessary as example? ๐Ÿ˜… ... the CTRL/META + click, or a hover for what looks like a link, is a feature not super easy to implement on the Web out of the box, but we could eventually parse the output AOT and print a <a href="..."> instead although I am not sure how XTerm.js would handle that, but maybe it's worth an investigation.
14:07
btw you forgot {x} in there ๐Ÿ˜„
Avatar
Jeff Glass 11/04/2024 14:08
There's an add-on for xtermjs that makes links clickable: https://github.com/xtermjs/xterm.js/tree/master/addons/addon-web-links (edited)
A terminal for the web. Contribute to xtermjs/xterm.js development by creating an account on GitHub.
๐Ÿคฉ 2
Avatar
Avatar
elliot0x01
Hi, is there any way to make links which appear on terminal clickable?
Jeff Glass 11/04/2024 14:13
You can add the weblinks addon to the terminal like so: <py-config> [js_modules.main] "https://cdn.jsdelivr.net/npm/@xterm/addon-web-links@0.11.0/+esm" = "weblinks" </py-config> <script type="py" terminal> from pyscript import js_modules addon = js_modules.weblinks.WebLinksAddon.new() __terminal__.loadAddon(addon) print("Check out https://google.com") </script>
14:13
@Andrea Giammarchi btw have I told you how much I love the ESM modules feature??
โค๏ธ 2
Avatar
Avatar
Jeff Glass
There's an add-on for xtermjs that makes links clickable: https://github.com/xtermjs/xterm.js/tree/master/addons/addon-web-links (edited)
Andrea Giammarchi 11/04/2024 14:16
the terminal is 99% lazy (I mean, the code itself lands ASAP but its bootstrap and dependencies are still 100% lazy) ... do we want to include most common addons there? this one in particular seems useful, and as I can see that's how my GNOME console works despite the underline REPL (a basic echo works the same) maybe we could have this in too? as @Jeff Glass showed it's pretty trivial to bring in your addon though, so maybe not ... thoughts? /cc @ntoll
Avatar
Jeff Glass 11/04/2024 14:20
IMO Web links is kind of handy, might be fun to have out of the box. Do we already use the Fit add on? I feel like maybe we do. The think the unicode addons I donโ€™t think are necessary (ones for an old version of unicode, one is experimental)
Avatar
Avatar
Jeff Glass
IMO Web links is kind of handy, might be fun to have out of the box. Do we already use the Fit add on? I feel like maybe we do. The think the unicode addons I donโ€™t think are necessary (ones for an old version of unicode, one is experimental)
Andrea Giammarchi 11/04/2024 14:25
unicode issue is a MicroPython one, I don't think we have unicode problems at all right now but the Fit add on is already in ... the difference with the config approach is that being in a worker those addons only land when required, not on bootstrap, but if the addon is handy and relatively small, why not?
๐Ÿ‘ 1
14:25
this one in particular really feels like just 2K https://cdn.jsdelivr.net/npm/@xterm/addon-web-links@0.11.0/+esm
๐Ÿ‘€ 1
Avatar
Avatar
Andrea Giammarchi
this one in particular really feels like just 2K https://cdn.jsdelivr.net/npm/@xterm/addon-web-links@0.11.0/+esm
Jeff Glass 11/04/2024 14:35
I'd vote to go ahead and add it into core personally, unless anyone feels strongly otherwise?
๐Ÿ’ฏ 1
Avatar
blackbooks 11/04/2024 15:12
i did some more experimenting with my sqlalchemy and pyscript, i try to use a sqlite database in memory i have db_service .pyfile which has a create_database, insert person and query_person method when i call this from my pyscript.html page one after another it works fine <h1> Database creation and insert</h1> <button py-click="create_database"> Create Database </button> <div id="creation_ok"></div> <button py-click="insert_person"> Insert Person </button> <div id="insert_ok"></div> <button py-click="query_person"> Query Person </button> <div id="query_ok"></div> <script type="py" src="{{ url_for('pyscriptapp.static', filename='db_service.py') }}" config="{{ url_for('pyscriptapp.static', filename='database.json') }}"></script> <a href="{{ url_for('pyscriptapp.pyscript_second') }}">Back</a> and it returns the inserted record however when i route to pyscript_second.html(basicly a copy of the first) and just try to query the person, it cant find a table named Person So my question is, how could i either keep the database persistent over a route or should i/do i need to keep everything on one page and do what i want to do all locally until i am finished with the database?
Avatar
Avatar
blackbooks
i did some more experimenting with my sqlalchemy and pyscript, i try to use a sqlite database in memory i have db_service .pyfile which has a create_database, insert person and query_person method when i call this from my pyscript.html page one after another it works fine <h1> Database creation and insert</h1> <button py-click="create_database"> Create Database </button> <div id="creation_ok"></div> <button py-click="insert_person"> Insert Person </button> <div id="insert_ok"></div> <button py-click="query_person"> Query Person </button> <div id="query_ok"></div> <script type="py" src="{{ url_for('pyscriptapp.static', filename='db_service.py') }}" config="{{ url_for('pyscriptapp.static', filename='database.json') }}"></script> <a href="{{ url_for('pyscriptapp.pyscript_second') }}">Back</a> and it returns the inserted record however when i route to pyscript_second.html(basicly a copy of the first) and just try to query the person, it cant find a table named Person So my question is, how could i either keep the database persistent over a route or should i/do i need to keep everything on one page and do what i want to do all locally until i am finished with the database?
Jeff Glass 11/04/2024 15:14
Anything that's stored in memory is going to get deleted when you route to a new page. So keeping everything on the same page will be simplest
15:15
Another option would be to use the web storage API or IndexedDB to store and reload a copy of your sqllite database across multiple pages. Not sure what size of data you'd be looking at and how feasible that would be
Avatar
hello guys ! I'm new using pyscript, so I've been following the steps-by-steps given in the official page for testing purposes. By the "running offline" section, I'm trying this approach on the Parrot Translate Project (to make it run offline), but i'm having much difficult on the pyodide packaging part: I got all the pyodide stuff, .whl files; and it ,that should theoretically work, doesn't. I'd like to add that I'm getting sha-256 integrity error even though the .whl files have the requisited versions. Thank you so much in advance ...
Avatar
Avatar
nop
hello guys ! I'm new using pyscript, so I've been following the steps-by-steps given in the official page for testing purposes. By the "running offline" section, I'm trying this approach on the Parrot Translate Project (to make it run offline), but i'm having much difficult on the pyodide packaging part: I got all the pyodide stuff, .whl files; and it ,that should theoretically work, doesn't. I'd like to add that I'm getting sha-256 integrity error even though the .whl files have the requisited versions. Thank you so much in advance ...
Jeff Glass 11/04/2024 15:22
Hi! Can you share teh specific sha-256 integrity error that you're getting?
Avatar
Avatar
Jeff Glass
Hi! Can you share teh specific sha-256 integrity error that you're getting?
"Failed to find a valid digest in the 'integrity' attribute for resource 'http://localhost:8000/pyodide/micropip-0.5.0-py3-none-any.whl' with computed SHA-256 integrity 'KFZn0rkbV86HyQAlSLlknsu+HplrTX9Miq+rebxMs8A='. The resource has been blocked."
Avatar
Avatar
Jeff Glass
Another option would be to use the web storage API or IndexedDB to store and reload a copy of your sqllite database across multiple pages. Not sure what size of data you'd be looking at and how feasible that would be
Andrea Giammarchi 11/04/2024 16:34
or use this FS instead as module, which persists behind the scene via IndexedDB https://github.com/WebReflection/fs/blob/main/test/index.js ... live demo: https://webreflection.github.io/fs/test/
Emscripten FS, PATH and PATH_FS utilities as a module - WebReflection/fs
Avatar
Avatar
nop
"Failed to find a valid digest in the 'integrity' attribute for resource 'http://localhost:8000/pyodide/micropip-0.5.0-py3-none-any.whl' with computed SHA-256 integrity 'KFZn0rkbV86HyQAlSLlknsu+HplrTX9Miq+rebxMs8A='. The resource has been blocked."
Andrea Giammarchi 11/04/2024 16:36
are you using any CSP specific header?
Avatar
Avatar
Andrea Giammarchi
are you using any CSP specific header?
Andrea Giammarchi 11/04/2024 16:36
also a bit hard to answer without a minimal example to reproduce ... at least your index.html page
Avatar
Avatar
Andrea Giammarchi
also a bit hard to answer without a minimal example to reproduce ... at least your index.html page
all right, I'm gonna perform it...
Avatar
Avatar
Andrea Giammarchi
also a bit hard to answer without a minimal example to reproduce ... at least your index.html page
if you catch the errors/problems, you'll be a hero <3 - gabrielpmelow/parrot-offline-test
Avatar
Avatar
Andrea Giammarchi
also a bit hard to answer without a minimal example to reproduce ... at least your index.html page
That's here, sorry for the delay
Avatar
Avatar
Andrea Giammarchi
or use this FS instead as module, which persists behind the scene via IndexedDB https://github.com/WebReflection/fs/blob/main/test/index.js ... live demo: https://webreflection.github.io/fs/test/
Just a note about this. Chrome informs me deprecated: Listener added for a synchronous 'DOMSubtreeModified' DOM Mutation Event. This event type is deprecated (https://w3c.github.io/uievents/#legacy-event-types) and work is underway to remove it from this browser.
Avatar
Hello there. I've been trying to use different fonts in Matplotlib for Bangla language, so, in PyCharm, for example, by using fontname = 'Siyam Rupali' , it's being done, but not sure how to do this in PyScript.
06:36
I've also tried this: from matplotlib import rc rc('font',**{'family':'Kalpurush', 'size': 12})
06:37
Any suggestions? Basically any tips on how to get a font for Matplotlib in PyScript would be of huge help. (edited)
Avatar
Avatar
Moum
Hello there. I've been trying to use different fonts in Matplotlib for Bangla language, so, in PyCharm, for example, by using fontname = 'Siyam Rupali' , it's being done, but not sure how to do this in PyScript.
Andrea Giammarchi 12/04/2024 08:18
have you tried looking for similar issues upstream in Pyodide?
Avatar
Avatar
Andrea Giammarchi
have you tried looking for similar issues upstream in Pyodide?
No ... quite new to the whole thing actually.
Avatar
Avatar
Moum
No ... quite new to the whole thing actually.
Andrea Giammarchi 12/04/2024 10:40
here a starter (with no followup) https://github.com/pyodide/pyodide/discussions/2252
Hi everyone, First, let me say that I am very impressed of the extent to which Matplotlib "just works" with Pyodide (via https://starboard.gg/). I'd like to know if there is a way to ...
Avatar
Avatar
Andrea Giammarchi
here a starter (with no followup) https://github.com/pyodide/pyodide/discussions/2252
Andrea Giammarchi 12/04/2024 10:41
here an open issue with no resolution: https://github.com/pyodide/matplotlib-pyodide/issues/14
From #9 for now, matplotlib only has access to the "shipped" fonts
Avatar
Avatar
Andrea Giammarchi
here an open issue with no resolution: https://github.com/pyodide/matplotlib-pyodide/issues/14
Andrea Giammarchi 12/04/2024 10:41
it points at a merged issue with all boxes ticked except the custom font one.
10:42
I'm afraid we, in PyScript, don't get to control what Pyodide, or its 3rd party, does or offer, but it feels like they are working on it, or at least that bug is still open.
Avatar
Avatar
Andrea Giammarchi
I'm afraid we, in PyScript, don't get to control what Pyodide, or its 3rd party, does or offer, but it feels like they are working on it, or at least that bug is still open.
I see ... Thank you for pointing these out. Appreciate it.
Avatar
Avatar
Andrea Giammarchi
was evil.com really necessary as example? ๐Ÿ˜… ... the CTRL/META + click, or a hover for what looks like a link, is a feature not super easy to implement on the Web out of the box, but we could eventually parse the output AOT and print a <a href="..."> instead although I am not sure how XTerm.js would handle that, but maybe it's worth an investigation.
elliot0x01 12/04/2024 11:03
๐Ÿ˜… ahhh that's my go to domain when testing stuff
๐Ÿ˜ 1
Avatar
Avatar
Andrea Giammarchi
btw you forgot {x} in there ๐Ÿ˜„
elliot0x01 12/04/2024 11:03
ahhh, nice catch ๐Ÿ˜†
Avatar
Avatar
Jeff Glass
You can add the weblinks addon to the terminal like so: <py-config> [js_modules.main] "https://cdn.jsdelivr.net/npm/@xterm/addon-web-links@0.11.0/+esm" = "weblinks" </py-config> <script type="py" terminal> from pyscript import js_modules addon = js_modules.weblinks.WebLinksAddon.new() __terminal__.loadAddon(addon) print("Check out https://google.com") </script>
elliot0x01 12/04/2024 11:05
Damnn...you guys are truly skilled in you work. Thanks Jef and Andrea ๐Ÿ™‚
๐Ÿฅณ 1
Avatar
Avatar
Neon22
Just a note about this. Chrome informs me deprecated: Listener added for a synchronous 'DOMSubtreeModified' DOM Mutation Event. This event type is deprecated (https://w3c.github.io/uievents/#legacy-event-types) and work is underway to remove it from this browser.
Andrea Giammarchi 12/04/2024 11:09
I don't think you can find that DOMSubtreeModified string anywhere in my projects ... it's either some 3rd party script or your extensions doing something weird, imho.
Avatar
ooh dear. I'll go looking. thanks
Avatar
Avatar
Jeff Glass
I'd vote to go ahead and add it into core personally, unless anyone feels strongly otherwise?
Andrea Giammarchi 12/04/2024 15:01
Description This MR automatically link well formed URLs just like a regular shell would do. Changes added @xterm/addon-web-links as lazy loaded terminal dependency tested everything is fine Check...
Avatar
chris.laffra 12/04/2024 21:01
@Moum Matplotlib uses different renderers. By default in PyScript, it uses a web renderer. I had to switch over for performance reasons, not fonts, and used the following. Maybe that helps you as well?
๐Ÿ™Œ 1
Avatar
Avatar
Moum
Hello there. I've been trying to use different fonts in Matplotlib for Bangla language, so, in PyCharm, for example, by using fontname = 'Siyam Rupali' , it's being done, but not sure how to do this in PyScript.
If it works in PyCharm it should probably work in Py-script as well.For me Bangla language doesn't show up in PyCharm. Only few languages showup.
Avatar
Avatar
chris.laffra
@Moum Matplotlib uses different renderers. By default in PyScript, it uses a web renderer. I had to switch over for performance reasons, not fonts, and used the following. Maybe that helps you as well?
That's a big performance difference.
Avatar
Avatar
AndrewD
That's a big performance difference.
chris.laffra 14/04/2024 08:21
You tried it? What differences do you see?
Avatar
Avatar
chris.laffra
You tried it? What differences do you see?
No, I just read the comment in the code you posted and was surprised at the difference you observed
Avatar
chris.laffra 14/04/2024 11:41
Yeah, I create my matplolib images in a worker. If you use the web model, the worker needs to talk to the DOM using message passing. Matplotlib makes hundreds, if not more, calls to the DOM to draw its figures. With "agg", the figures are all drawn in memory, inside the worker.
Avatar
Avatar
chris.laffra
Yeah, I create my matplolib images in a worker. If you use the web model, the worker needs to talk to the DOM using message passing. Matplotlib makes hundreds, if not more, calls to the DOM to draw its figures. With "agg", the figures are all drawn in memory, inside the worker.
That explains it very elegantly, thanks
Avatar
I copy pasted matplotlib example for running locally, it ran just fine. So I want to try something with networkx, but I get this ModuleNotFoundError: The module 'networkx' is included in the Pyodide distribution, but it is not installed. You can install it by calling: await micropip.install("networkx") in Python, or await pyodide.loadPackage("networkx") in JavaScript See https://pyodide.org/en/stable/usage/loading-packages.html for more details. I already have networkx installed on my system. I modified pyscript.toml to have networkx in packages, and restarted the python http.server but still getting the same error And in python, import micropip gives ModuleNotFoundError. How can I get this to work
Avatar
Avatar
psi2kgcm
I copy pasted matplotlib example for running locally, it ran just fine. So I want to try something with networkx, but I get this ModuleNotFoundError: The module 'networkx' is included in the Pyodide distribution, but it is not installed. You can install it by calling: await micropip.install("networkx") in Python, or await pyodide.loadPackage("networkx") in JavaScript See https://pyodide.org/en/stable/usage/loading-packages.html for more details. I already have networkx installed on my system. I modified pyscript.toml to have networkx in packages, and restarted the python http.server but still getting the same error And in python, import micropip gives ModuleNotFoundError. How can I get this to work
Jeff Glass 15/04/2024 01:14
Can you share your full code/html document?
Avatar
Avatar
Jeff Glass
Can you share your full code/html document?
Sure, here it is index.html -- https://dpaste.com/BFK6ZF7AN main.py -- https://dpaste.com/DL4SREDQL pyscript.toml -- https://dpaste.com/65NLRXVBA
Avatar
You're using 2024.1.1 - try 2024.3.2 to get latest. (index.html)
Avatar
Avatar
psi2kgcm
Sure, here it is index.html -- https://dpaste.com/BFK6ZF7AN main.py -- https://dpaste.com/DL4SREDQL pyscript.toml -- https://dpaste.com/65NLRXVBA
I think the problem is with installing mailbox package not with networkx package.I am unable to install the mailbox package instead django-mailbox package got installed resulting in some empty plot.https://_12.pyscriptapps.com/still-scenee/latest/ (edited)
Avatar
@Neon22 we released 2024.4.1 today ๐Ÿ˜‰ (edited)
Avatar
im having issues when trying to add pyscript into nginx web server, does someone knows how to make it work? (edited)
Avatar
Avatar
bandix
im having issues when trying to add pyscript into nginx web server, does someone knows how to make it work? (edited)
Andrea Giammarchi 16/04/2024 12:22
pyscript runs in a browser, I am not sure what you are trying to do there ...
Avatar
Are you trying to serve static sites (that use PyScript) with nginx..?
Avatar
How much interactivity is possible with a matplotlib plot through PyScript? Clicking on data points? Tool tips? Hover? I have an app that lets me add custom content in an IFrame, and using PyScript will be a great way to do more than just HTML or Markdown, especially if I can make it responsive to mouse events. (I already have a prototype that displays a static chart, but I'm pretty sure my users will want to interact with it.)
Avatar
Avatar
ntoll
Are you trying to serve static sites (that use PyScript) with nginx..?
I want to run a web server and I need python in html on it and i cannot make it work with anything
Avatar
@bandix you just serve the HTML and other assets you need as simple basic static files.
17:29
(There is no magic ๐Ÿ˜‰ )
Avatar
Avatar
ptmcg
How much interactivity is possible with a matplotlib plot through PyScript? Clicking on data points? Tool tips? Hover? I have an app that lets me add custom content in an IFrame, and using PyScript will be a great way to do more than just HTML or Markdown, especially if I can make it responsive to mouse events. (I already have a prototype that displays a static chart, but I'm pretty sure my users will want to interact with it.)
Andrea Giammarchi 16/04/2024 17:36
we don't have answers to custom (awesome) solutions so my best hint is "try that and inform us later if it worked" ... apologies for not being the best answer you were looking for.
Avatar
Avatar
Andrea Giammarchi
we don't have answers to custom (awesome) solutions so my best hint is "try that and inform us later if it worked" ... apologies for not being the best answer you were looking for.
Understood - just thought I'd ask whether anyone has already blazed that trail. (edited)
Avatar
Avatar
ptmcg
Understood - just thought I'd ask whether anyone has already blazed that trail. (edited)
Andrea Giammarchi 16/04/2024 17:38
one thing that will differ is time to interact but you can combine both by serving your Python thing and reuse 1:1 your Python code on the client once that page landed so users see results ASAP then bootstrap PyScript and re-render the same chart with 100% DOM based interactivity
๐Ÿ‘ 1
Avatar
Hi there, i am looking for a way to call a pyscript function from javascript. Can Anyone point me into the right direction?
Avatar
Avatar
Lester
Hi there, i am looking for a way to call a pyscript function from javascript. Can Anyone point me into the right direction?
Avatar
elliot0x01 17/04/2024 11:16
Is there a way to change the terminal color/background to different shade from the default greyish black ?
Avatar
elliot0x01 17/04/2024 11:29
Wow the clickable link thing has already gone live...super work team ๐Ÿ’ฏ
Avatar
elliot0x01 17/04/2024 17:07
is there a way to clear the terminal screen yet ?
Avatar
Avatar
elliot0x01
is there a way to clear the terminal screen yet ?
Jeff Glass 17/04/2024 17:39
You can use standard asni control characters to do that from within Python: <script type="py" terminal async> import asyncio print("Clearing the screen in:") for i in range(5, 0, -1): print(i) await asyncio.sleep(1) print('\033[2J') # clear terminal print('\033[H', end = "") # move to home position, don't print a newline print("You should only see this") </script>
Avatar
Avatar
elliot0x01
Is there a way to change the terminal color/background to different shade from the default greyish black ?
Jeff Glass 17/04/2024 17:50
You can override the css styling of the viewport to change the background color: <style> .xterm .xterm-viewport { background: #00F !important; } </style> <script type="py" terminal> print("Hello, world!") </script>
17:50
For foreground colors, I'd again use the ANSI color codes for consistency
Avatar
Avatar
Jeff Glass
You can use standard asni control characters to do that from within Python: <script type="py" terminal async> import asyncio print("Clearing the screen in:") for i in range(5, 0, -1): print(i) await asyncio.sleep(1) print('\033[2J') # clear terminal print('\033[H', end = "") # move to home position, don't print a newline print("You should only see this") </script>
Jeff Glass 17/04/2024 17:52
You can also use __terminal__.clear(): <script type="py" terminal async> import asyncio print("Clearing the screen in:") for i in range(5, 0, -1): print(i) await asyncio.sleep(1) __terminal__.clear() print("You should only see this") </script> That's not something PyScript added btw - the __terminal__ global is a direct reference to the underlying xtermjs Terminal object
๐Ÿ˜ 1
๐Ÿ”ฅ 1
Avatar
Avatar
Jeff Glass
You can also use __terminal__.clear(): <script type="py" terminal async> import asyncio print("Clearing the screen in:") for i in range(5, 0, -1): print(i) await asyncio.sleep(1) __terminal__.clear() print("You should only see this") </script> That's not something PyScript added btw - the __terminal__ global is a direct reference to the underlying xtermjs Terminal object
Jeff Glass 17/04/2024 18:02
And finally - if you want to clear the terminal programmatically (from an external event or script), you can grab a reference to the terminal and clear it: <script type="py" terminal> print("hello, world") </script> <script type="py"> from pyscript import document def clearTerm(evt): terminal_element = document.querySelector("script[type='py'][terminal]") terminal_element.terminal.clear() </script> <button py-click="clearTerm">Click to Clear</button>
Avatar
thank you, i managed to export my function from pyscript by creating an entry in window. Calling it from Javascript without importing anything or using a namespace/module feels just hacky. But it works! So i can use websockets in JS and have them call my pyscript code.
Avatar
Avatar
Jeff Glass
And finally - if you want to clear the terminal programmatically (from an external event or script), you can grab a reference to the terminal and clear it: <script type="py" terminal> print("hello, world") </script> <script type="py"> from pyscript import document def clearTerm(evt): terminal_element = document.querySelector("script[type='py'][terminal]") terminal_element.terminal.clear() </script> <button py-click="clearTerm">Click to Clear</button>
elliot0x01 18/04/2024 10:21
Thanks for the valuable information Jeff ๐Ÿ™‚
Avatar
Andrea Giammarchi 18/04/2024 12:39
to whom it might concern, everything Jeff said works on MicroPython terminal too
๐ŸŽ‰ 2
12:39
feel free to test it in here https://webreflection.github.io/coincident/test/pyterminal/ ... add ?py to use pyodide instead https://webreflection.github.io/coincident/test/pyterminal/?py (the default is ?mpy)
Avatar
Avatar
chris.laffra
@Moum Matplotlib uses different renderers. By default in PyScript, it uses a web renderer. I had to switch over for performance reasons, not fonts, and used the following. Maybe that helps you as well?
Hello Chris, sorry for the late reply ... have been away. I'll check it out. Thanks very much. ๐Ÿฅน
Avatar
Hey all, I'm working on a simple website where I want to display a plot. At first I did it using matplotlib and I had no issues, but now I'm trying to use plotly and I get this error when the library is fetched: The FetchEvent for "https://cdn.plot.ly/plotly-2.31.1.min.js" resulted in a network error response: Cross-Origin-Resource-Policy prevented from serving the response to the client. Has anyone experienced this? Any ideas on how to solve it? Idk if it has any effect, but I'm using mini coi, which I expected to take care of all cross-origin related stuff
Avatar
Avatar
tom
Hey all, I'm working on a simple website where I want to display a plot. At first I did it using matplotlib and I had no issues, but now I'm trying to use plotly and I get this error when the library is fetched: The FetchEvent for "https://cdn.plot.ly/plotly-2.31.1.min.js" resulted in a network error response: Cross-Origin-Resource-Policy prevented from serving the response to the client. Has anyone experienced this? Any ideas on how to solve it? Idk if it has any effect, but I'm using mini coi, which I expected to take care of all cross-origin related stuff
Andrea Giammarchi 18/04/2024 16:54
if that's an ESM module use https://esm.run/plotly instead of home made CDNs with missing headers to be usable
Avatar
Avatar
Andrea Giammarchi
if that's an ESM module use https://esm.run/plotly instead of home made CDNs with missing headers to be usable
Andrea Giammarchi 18/04/2024 16:55
althought that points at plotly@1.0.6 ... not sure they ditched some npm publishing or if it's about their own CDN nothing we can do about it, I am afraid
Avatar
Got it. Thanks for the quick response!
Avatar
Avatar
Jeff Glass
You can override the css styling of the viewport to change the background color: <style> .xterm .xterm-viewport { background: #00F !important; } </style> <script type="py" terminal> print("Hello, world!") </script>
@Jeff Glass how can I get multiple colors on terminal?Something like the image?
Avatar
Mason Nortac 19/04/2024 17:19
I downloaded the zip file from pyscript.net, extracted the files and uploaded them to my webserver maintaining the folders and structure. I found a sample tic tac toe example. I placed the sample tic tac toe on my webserver and was able to play. How do I get that sample script to access the files locally on the sever?
Avatar
Marco Aprea 19/04/2024 22:21
I wanted to share my experience using PyScript in my lesson. I presented my work using PyScript instead of JavaScript, but I encountered some difficulties in understanding the example presented. These are the problems I encountered: 1. WYSIWYG functionality is missing, which would have made the development process more intuitive. 2. The browser tends to cache sources, so code changes are not always detected unless you use incognito browsing. (<!-- Meta tag per ricaricare il file da CORS ad ogni modifica --> <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate"> doesn't work) 3. You can't use the browser's internal debugging and set breakpoints to catch errors in your code. 4. You can't use IDEs like PyCharm, as they can't find the imports needed for PyScript code (Pyeditor doesn't view Pyscript module). 5. I don't think the following link can be used for my students who are beginners: https://pyodide.org/en/stable/development/debugging.html https://pyscript.com/@marcoaprea/single-page-application-with-pyscript-add-event-listener/latest https://pyscript.com/@marcoaprea/single-page-application-with-pyscript-canvas/latest https://pyscript.com/@marcoaprea/pyeditor/latest (edited)
Avatar
elliot0x01 20/04/2024 11:00
I am trying to change the font of my terminal, but I am not able to do so. Can anyone tell what am I doing wrong? .xterm .xterm-viewport .terminal{ font-family: "gohu"; } @font-face { font-family: "gohu"; src: url("/assets/gohu.woff") format('woff'); }
๐Ÿ‘€ 1
Avatar
font-change works in style.css way not sure of this way. *{ font-family: 'gohu'; } (edited)
Avatar
Avatar
elliot0x01
I am trying to change the font of my terminal, but I am not able to do so. Can anyone tell what am I doing wrong? .xterm .xterm-viewport .terminal{ font-family: "gohu"; } @font-face { font-family: "gohu"; src: url("/assets/gohu.woff") format('woff'); }
Jeff Glass 21/04/2024 14:55
After defining the font family, you can use __terminal__.option = ... to change the font family: <style> @font-face { font-family: "gohu"; src: url("ResotE-Rose-89c1.woff") format('woff'); } </style> <script type="mpy" terminal> from pyscript.ffi import to_js __terminal__.options = to_js({"fontFamily": 'gohu'}) print("Hello, world!") </script>
14:55
It seems that xtermjs may not correctly detect font spacing
14:56
14:57
You could also add .xterm-rows { font-family: "gohu" !important; } to your styles
14:58
^^^^^ wrong spacing with terminal.__options__
14:58
. (edited)
14:59
^^^^^ correct spacing with css rule
14:59
Or rather, perhaps xtermjs (fairly) expects the used font to be monospaced
Avatar
elliot0x01 21/04/2024 17:46
Thanks Jeff
17:46
What would be the best approach to play/stop background music using pyscript ? Also is it possible to autoplay music when user visits my website
Avatar
hi there, when creating a new element via pydom how can i set the onclick to call another method in javascript?
Avatar
elliot0x01 22/04/2024 05:53
Why does display() don't work with micropython ?
Avatar
gewonegebruiker 22/04/2024 08:24
hey guys, i'm using this: https://pyscript.recipes/2024.1.1/basic/file-download/ to create a download button for my app. the problem is that i cant seem to figure out how to make the downloadable of the .xlsx type. whatever i try i get the error that the excel file is corrupt. does someone know how to make the correct file object using the above mentioned recipe this down here is how i create my excle file that i want to be able to download. thanks in advance for looking into it. import pandas as pd from io import BytesIO # Assuming `df` is your DataFrame output = BytesIO() writer = pd.ExcelWriter(output, engine='xlsxwriter') data = { 'Column1': ['Value1', 'Value2'], 'Column2': ['Value3', 'Value4'] } df = pd.DataFrame(data) df.to_excel(writer, sheet_name='Sheet1') writer.save() (edited)
Avatar
elliot0x01 22/04/2024 13:05
Do we have a pydom guide? I just don't know much of javascript and manipulating dom is now becoming headache using the traditional import js method and way lengthy too ๐Ÿค”
Avatar
@elliot0x01 you need to ask @Fabio about this... although I believe development is ongoing and so now might not yet be the time to solidify the docs if changes are yet to happen. ๐Ÿ‘
Avatar
Avatar
elliot0x01
Why does display() don't work with micropython ?
Jeff Glass 22/04/2024 15:54
It shouldโ€ฆ can you share an example?
Avatar
Avatar
Jeff Glass
It shouldโ€ฆ can you share an example?
elliot0x01 22/04/2024 15:56
This code works absolutely fine when using python
15:56
but display won't work in micropython
Avatar
Avatar
elliot0x01
This code works absolutely fine when using python
Andrea Giammarchi 22/04/2024 16:16
the HTML import has ben recently changed (like ... today) to not break on MicroPython, apologies for any inconvenience, it was not documented at all and you are the first user noticying it https://github.com/pyscript/pyscript/pull/2033
Description While working on our own documentation I've realized that the current HTML class utility is not working on MicroPython due a reversed(...) call that does not exist in there. Changes...
Avatar
Avatar
Andrea Giammarchi
the HTML import has ben recently changed (like ... today) to not break on MicroPython, apologies for any inconvenience, it was not documented at all and you are the first user noticying it https://github.com/pyscript/pyscript/pull/2033
elliot0x01 22/04/2024 16:20
Thanks Andrea, do I need to update my dependencies to the the latest release or what. I earlier faced similar issue with mpy
16:23
Avatar
Avatar
elliot0x01
Thanks Andrea, do I need to update my dependencies to the the latest release or what. I earlier faced similar issue with mpy
Andrea Giammarchi 22/04/2024 20:51
we haven't released a PyScript version with those fixes in yet, but that might happen soon! /cc @ntoll
Avatar
Avatar
gewonegebruiker
hey guys, i'm using this: https://pyscript.recipes/2024.1.1/basic/file-download/ to create a download button for my app. the problem is that i cant seem to figure out how to make the downloadable of the .xlsx type. whatever i try i get the error that the excel file is corrupt. does someone know how to make the correct file object using the above mentioned recipe this down here is how i create my excle file that i want to be able to download. thanks in advance for looking into it. import pandas as pd from io import BytesIO # Assuming `df` is your DataFrame output = BytesIO() writer = pd.ExcelWriter(output, engine='xlsxwriter') data = { 'Column1': ['Value1', 'Value2'], 'Column2': ['Value3', 'Value4'] } df = pd.DataFrame(data) df.to_excel(writer, sheet_name='Sheet1') writer.save() (edited)
Jeff Glass 22/04/2024 21:54
Here's a bit of a hacky version, which writes the contents to the virtual filesystem, then reloads them as bytes to download: from js import Uint8Array, File, URL from pyscript import document import pandas as pd filename = "myfile.xlsx" def downloadFile(*args): # Assuming `df` is your DataFrame writer = pd.ExcelWriter(filename, engine='xlsxwriter') data = { 'Column1': ['Value1', 'Value2'], 'Column2': ['Value3', 'Value4'] } df = pd.DataFrame(data) df.to_excel(writer, sheet_name='Sheet1') writer.save() with open(filename, 'rb') as data: my_stream = data.read() js_array = Uint8Array.new(len(my_stream)) js_array.assign(my_stream) file = File.new([js_array], "unused_file_name.xlsx", {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}) url = URL.createObjectURL(file) hidden_link = document.createElement("a") hidden_link.setAttribute("download", "my_other_file_name.xlsx") hidden_link.setAttribute("href", url) hidden_link.click()
โค๏ธ 1
21:55
Note the new mime type, using a string filename instad of BytesIO, and opening that file in rb mode
21:57
It seems like there should be a way to use a BufferedRandom, but I couldn't figure it out
22:00
By hacky I mean it works fine, I just feel like there should be a way to do it without writing to virtual files and reading them again.
Avatar
Avatar
ntoll
@elliot0x01 you need to ask @Fabio about this... although I believe development is ongoing and so now might not yet be the time to solidify the docs if changes are yet to happen. ๐Ÿ‘
The theme of docs this week is a perfect match. ๐Ÿ™‚ Will try to work on it this week
Avatar
Avatar
Jeff Glass
It seems like there should be a way to use a BufferedRandom, but I couldn't figure it out
gewonegebruiker 23/04/2024 13:08
would not have come up with this idea. but this works perfect for me, thanks a lot ๐Ÿ‘
๐Ÿš€ 1
Avatar
how can i add a <li> element to an <ul> with pure html. I need to add the li with explicit html to set the onclick to a javascript function. so far i tried pydom.create and pydom['#abc'].append but both would create an html element where i could only manipulate the style, not an onclick hook. i also tried using display and HTML which is technically working but is adding another div around my created element. this div is messing up the layout. Can anyone help me by: a. creating an element with pyscript or javascript onclick hook with pydom b. removing the div around the new element when using display and HTML (sidequest: activate IDE support to figure this out by myself)
Avatar
Avatar
Lester
how can i add a <li> element to an <ul> with pure html. I need to add the li with explicit html to set the onclick to a javascript function. so far i tried pydom.create and pydom['#abc'].append but both would create an html element where i could only manipulate the style, not an onclick hook. i also tried using display and HTML which is technically working but is adding another div around my created element. this div is messing up the layout. Can anyone help me by: a. creating an element with pyscript or javascript onclick hook with pydom b. removing the div around the new element when using display and HTML (sidequest: activate IDE support to figure this out by myself)
Andrea Giammarchi 24/04/2024 13:03
display has an append=False option ... have you tried that?
13:05
... although I can't remember if that also erase the container content ... but why do you need a string to attach a JS function? ๐Ÿค”
Avatar
๐Ÿ‘‹ @Lester good question. I'm not that au fait with PyDom... but can you add an event listener for a click event..? (Rather than trying to manipulate the onclick attribute - which is generally frowned upon these days because "reasons").
Avatar
Avatar
Andrea Giammarchi
... although I can't remember if that also erase the container content ... but why do you need a string to attach a JS function? ๐Ÿค”
Yes it clears the content and inserts the element without surrounding div. I would love something in between.
Avatar
Avatar
ntoll
๐Ÿ‘‹ @Lester good question. I'm not that au fait with PyDom... but can you add an event listener for a click event..? (Rather than trying to manipulate the onclick attribute - which is generally frowned upon these days because "reasons").
How do I do that? I was looking for any method or attribute with dir and inspect but the closest thing I found was manipulating the ._js and adding a pyodide.ffi.jsproxy. which is working while throwing errors
Avatar
Good question. I'm not sure when it comes to PyDOM, but if you have a reference to the actual HTML element object, you can just do: my_element.addEventListener("click", create_proxy(my_function)) (Off the top of my head, please check)
Avatar
I will try that, thank you
Avatar
Andrea Giammarchi 24/04/2024 16:17
without create_proxy you can always do my_element.onclick = my_function as that binds the reference to a foreign Proxy and both interpreters are smart enough to never kill it from the runtime GC
16:18
gosh I think this also might be worth a spot in the docs
๐Ÿ‘ 1
Avatar
Avatar
Andrea Giammarchi
gosh I think this also might be worth a spot in the docs
Andrea Giammarchi 24/04/2024 20:34
well, it's not reviewed and not merged yet, if ever, but this part of our F.A.Q. might be of interest to anyone willing to dig deeper behind the ffi scene: https://github.com/pyscript/docs/pull/93 (edited)
I don't know if this is the right page to provide all these details, but I feel like we gotta talk about these differences, as these are also pretty common questions in Discord (like today too)...
Avatar
Good afternoon everyone. I'm using pyscript to implement a python code editor. And the question arose, can I somehow get the code that the user enters into this editor? I tried to do this using a crutch, access through document.querySelector, but for some reason I canโ€™t reach this element where the user code is stored. Are there ready-made solutions to do this? Let's say this is already provided for output. But in addition to the answer, I also need to receive the code itself. Help me please
12:43
this is the editor I mean
Avatar
Avatar
screammy
Good afternoon everyone. I'm using pyscript to implement a python code editor. And the question arose, can I somehow get the code that the user enters into this editor? I tried to do this using a crutch, access through document.querySelector, but for some reason I canโ€™t reach this element where the user code is stored. Are there ready-made solutions to do this? Let's say this is already provided for output. But in addition to the answer, I also need to receive the code itself. Help me please
Jeff Glass 25/04/2024 13:52
What version of PyScript are you using? The py-repl class was deprecated / removed back in 2023.11.1
Avatar
Avatar
Jeff Glass
What version of PyScript are you using? The py-repl class was deprecated / removed back in 2023.11.1
I looked at the example on your website https://pyscript.net/examples/repl2.html.
Avatar
Is it possible to implement what I described? Help please..
Avatar
I think I've already found the answer to my question. Thanks
Avatar
Good afternoon again, does anyone have an example of using pyscript in Vue? I saw last yearโ€™s repository above, but out-of-date pyscript is connected there and many features are not there
Avatar
Or can anyone guide me on how to use pyscript functions inside vue components? If use the old version with <py-script> tags, then everything works, but the new version already uses just <script> instead of <py-script>, as I understand it. But in Vue you canโ€™t connect script like that (script)
Avatar
Avatar
screammy
Or can anyone guide me on how to use pyscript functions inside vue components? If use the old version with <py-script> tags, then everything works, but the new version already uses just <script> instead of <py-script>, as I understand it. But in Vue you canโ€™t connect script like that (script)
Andrea Giammarchi 29/04/2024 08:31
we still support <py-script> tags, even if we encourage the usage of <script> instead because that's a code block, the former isn't and it easily leads to DOM parsing related issues.
Avatar
Good afternoon. When I connect the editor to run code inside it, I get an error. This did not happen when using the old version and the <py-repl> tag. How can this problem be solved?
Avatar
ร‚ne Mou 29/04/2024 17:40
Hi there, I'm new to pyscript and I'd like to understand how to use requestAnimationFrame, I only managed to make animations with set_interval function so far. from pyscript import document, window canvas = document.getElementById("GameCanvas") canvas.width = 320 canvas.height = 240 ctx = canvas.getContext("2d") square = { "x": 0, "y": 32, "w": 16, "h": 16, "color": "#FFAA00" } def update(timestamp=0): ctx.fillStyle = "#000000" ctx.fillRect(0, 0, 320, 240) ctx.fillStyle = square["color"] ctx.fillRect(square["x"], square["y"], square["w"], square["h"]) if square["x"] < 160: square["x"] += 1 window.requestAnimationFrame(update) window.requestAnimationFrame(update)
Avatar
Avatar
screammy
Good afternoon. When I connect the editor to run code inside it, I get an error. This did not happen when using the old version and the <py-repl> tag. How can this problem be solved?
Jeff Glass 29/04/2024 18:08
Using workers requires a secure environment, which you can either achieve by setting specific headers on your server, or using a shim like mini-coi.js See the docs for details
18:08
I use mini-coi 99% of the time, since I'm often just hosting static sites/examples
Avatar
Avatar
ร‚ne Mou
Hi there, I'm new to pyscript and I'd like to understand how to use requestAnimationFrame, I only managed to make animations with set_interval function so far. from pyscript import document, window canvas = document.getElementById("GameCanvas") canvas.width = 320 canvas.height = 240 ctx = canvas.getContext("2d") square = { "x": 0, "y": 32, "w": 16, "h": 16, "color": "#FFAA00" } def update(timestamp=0): ctx.fillStyle = "#000000" ctx.fillRect(0, 0, 320, 240) ctx.fillStyle = square["color"] ctx.fillRect(square["x"], square["y"], square["w"], square["h"]) if square["x"] < 160: square["x"] += 1 window.requestAnimationFrame(update) window.requestAnimationFrame(update)
Jeff Glass 29/04/2024 18:15
It's just one little thing! Opening the dev console, I see the error: Uncaught Error: This borrowed proxy was automatically destroyed at the end of a function call. Try using create_proxy or create_once_callable
18:15
You can resolve this by: from pyscript.ffi import create_proxy # ... @create_proxy def update(timestamp=0): ctx.fillStyle = "#000000" ctx.fillRect(0, 0, 320, 240) # ... (edited)
Avatar
ร‚ne Mou 29/04/2024 18:16
thx I'll try it right now ๐Ÿ˜„
Avatar
ร‚ne Mou 29/04/2024 18:25
Does it work for you ? maybe I made amistake while copying it: from pyscript import document, window from pyscript.ffi import create_proxy canvas = document.getElementById("GameCanvas") canvas.width = 320 canvas.height = 240 ctx = canvas.getContext("2d") square = { "x": 0, "y": 32, "w": 16, "h": 16, "color": "#FFAA00" } @create_proxy def update(timestamp=0): ctx.fillStyle = "#000000" ctx.fillRect(0, 0, 320, 240) ctx.fillStyle = square["color"] ctx.fillRect(square["x"], square["y"], square["w"], square["h"]) if square["x"] < 160: square["x"] += 1 window.requestAnimationFrame(update) window.requestAnimationFrame(update)
Avatar
Jeff Glass 29/04/2024 18:27
It does! Here's my full page, if it helps:
18:27
<link rel="stylesheet" href="https://pyscript.net/releases/2024.4.1/core.css"> <script type="module" src="https://pyscript.net/releases/2024.4.1/core.js"></script> <canvas id="GameCanvas"></canvas> <script type="py"> from pyscript import document, window from pyscript.ffi import create_proxy canvas = document.getElementById("GameCanvas") canvas.width = 320 canvas.height = 240 ctx = canvas.getContext("2d") square = { "x": 0, "y": 32, "w": 16, "h": 16, "color": "#FFAA00" } @create_proxy def update(timestamp=0): ctx.fillStyle = "#000000" ctx.fillRect(0, 0, 320, 240) ctx.fillStyle = square["color"] ctx.fillRect(square["x"], square["y"], square["w"], square["h"]) if square["x"] < 160: square["x"] += 1 window.requestAnimationFrame(update) window.requestAnimationFrame(update) </script>
18:28
I guess I should be using 2024.4.2 but either works
18:28
I don't really see a difference between mine and yours though....
Avatar
ร‚ne Mou 29/04/2024 18:32
It works, I think the error came from my index.html lol <!DOCTYPE html> <html lang="fr"> <head> <title>PyScript Test</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width,initial-scale=1.0"> <link rel="stylesheet" href="./css/main.css"> <link rel="stylesheet" href="https://pyscript.net/releases/2024.1.1/core.css" /> <script type="module" src="https://pyscript.net/releases/2024.1.1/core.js"></script> </head> <body> <canvas id="GameCanvas"></canvas> <script type="py" src="./main.py" config="./pyscript.toml"></script> </body> </html> I changed with your code it works fine now ๐Ÿ™‚
๐Ÿฅณ 2
Avatar
Good afternoon! Is it possible to make the code entered into the editor see the code included as a separate file? In my case, the user enters code into the editor, my task is to check this code using tests that are stored in the test.py file. I looked through all the documentation and videos, but found nothing about this(
Avatar
elo- i'm trying to host pyscript in an angular component; i can get the pandas icream demo to work and rework with edits by posting edits to the innerhtml of a div with an angular async pipe wrapping the code with <py-script> tags as follows this.viewerContent = this.sanitizer.bypassSecurityTrustHtml(` ${this.pythonREPLContent} `); console.log('sanitized latest code commit'); this.viewerContentSubject.next(this.viewerContent); <div *ngIf="viewerContent$ | ngrxPush as pythonCode" [innerHTML]="pythonCode" ></div> but when using <script type='py-editor'>, the code editor loads and renders the green go button but when the go button is pushed the below message is emitted in the chrome logs MessageEvent {type: 'error', error: Error: Unable to use SharedArrayBuffer due insecure environment. Please read requirements in MDN: hโ€ฆ} polyfills.js:1 Error: Unable to use SharedArrayBuffer due insecure environment. Please read requirements in MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer#security_requirements at 719ef878-2ecc-4b5c-9โ€ฆdfe81dcde4a:2:28017 any ideas? (on how to circumvent this evident angular security thing) (edited)
Avatar
Avatar
sechele
elo- i'm trying to host pyscript in an angular component; i can get the pandas icream demo to work and rework with edits by posting edits to the innerhtml of a div with an angular async pipe wrapping the code with <py-script> tags as follows this.viewerContent = this.sanitizer.bypassSecurityTrustHtml(` ${this.pythonREPLContent} `); console.log('sanitized latest code commit'); this.viewerContentSubject.next(this.viewerContent); <div *ngIf="viewerContent$ | ngrxPush as pythonCode" [innerHTML]="pythonCode" ></div> but when using <script type='py-editor'>, the code editor loads and renders the green go button but when the go button is pushed the below message is emitted in the chrome logs MessageEvent {type: 'error', error: Error: Unable to use SharedArrayBuffer due insecure environment. Please read requirements in MDN: hโ€ฆ} polyfills.js:1 Error: Unable to use SharedArrayBuffer due insecure environment. Please read requirements in MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer#security_requirements at 719ef878-2ecc-4b5c-9โ€ฆdfe81dcde4a:2:28017 any ideas? (on how to circumvent this evident angular security thing) (edited)
Andrea Giammarchi 30/04/2024 17:11
it's not an Angular thing, it's about headers on your server ... use npx mini-coi . to bootstrap a server that enables worker attribute, or see the docs about it: https://docs.pyscript.net/2024.4.1/user-guide/workers/#http-headers
Avatar
Avatar
Andrea Giammarchi
it's not an Angular thing, it's about headers on your server ... use npx mini-coi . to bootstrap a server that enables worker attribute, or see the docs about it: https://docs.pyscript.net/2024.4.1/user-guide/workers/#http-headers
i will review the atomics docs - thanks
Avatar
thanks @Andrea Giammarchi it was a matter of making certain cors headers i've never heard of look just like they do on pyscript.com in asp.net core you need something similar to or probably exactly like app.UseCors( options => options .WithOrigins(allowedCORSOrigins.ToArray()) .AllowAnyMethod() .AllowAnyHeader() .AllowCredentials() .SetIsOriginAllowed(hostname => true) ); app.Use(async (context, next) => { context.Response.Headers.Add("Cross-Origin-Opener-Policy", "same-origin"); context.Response.Headers.Add("Cross-Origin-Embedder-Policy", "credentialless"); context.Response.Headers.Add("Cross-Origin-Resource-Policy", "cross-origin"); await next(); }); if you get this wrong your spa components will load partially when they try to load assets from syncfusion and so forth thanks (edited)
๐Ÿ‘ 1
Avatar
ร‚ne Mou 01/05/2024 12:48
Hi, there I have a new pb, I'm trying to make a mini tilemap editor, and I struggle with event listener, how do I use the scroll event ? My problem is that it should print something on the terminal, but nothing happens. Here's my code: https://pastebin.com/WAXCirjv. This is what I'm trying to do: https://ane_mou.pyscriptapps.com/pyscript-test/latest/ I did the same I saw on a tutorial, line 274: tileInput.addEventListener("scroll", create_proxy(tileScroll)) (edited)
Avatar
ร‚ne Mou 01/05/2024 13:31
I'm just stupid it's alright I found the solution, it's mousewheel not scroll lol (edited)
๐Ÿ˜… 1
Avatar
Avatar
ร‚ne Mou
I'm just stupid it's alright I found the solution, it's mousewheel not scroll lol (edited)
We've all been there... ๐Ÿ™‚
Avatar
Andrea Giammarchi 02/05/2024 12:05
oddly enough, on window is actually scroll
Avatar
Hi, I want to make offline python tutorial for young learner at school. So after each new skill explained (text/image/vid) there is a box for the student to apply the new skill by typing and running the code using. It is working online, but if make it offline by following docs topic, i got this : Error: Unable to use SharedArrayBuffer due insecure environment.. I did not understand what MDN says .. so my question: can i get around this error and run "py-editor" locally offline?
`||<html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>PyScript Offline</title> <script type="module" src="/pyscript/core.js"></script> <link rel="stylesheet" href="/pyscript/core.css"> </head> <body> <script type="mpy-editor"></script> </body> </html>||`
(edited)
Avatar
Avatar
m28
Hi, I want to make offline python tutorial for young learner at school. So after each new skill explained (text/image/vid) there is a box for the student to apply the new skill by typing and running the code using. It is working online, but if make it offline by following docs topic, i got this : Error: Unable to use SharedArrayBuffer due insecure environment.. I did not understand what MDN says .. so my question: can i get around this error and run "py-editor" locally offline?
`||<html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>PyScript Offline</title> <script type="module" src="/pyscript/core.js"></script> <link rel="stylesheet" href="/pyscript/core.css"> </head> <body> <script type="mpy-editor"></script> </body> </html>||`
(edited)
Andrea Giammarchi 02/05/2024 13:46
run a local webserver that allows headers needed ... try mini-coi as example via npx or bun x ... example: npx mini-coi . starts a local server that enables SharedArrayBuffer. (edited)
Avatar
Avatar
Andrea Giammarchi
run a local webserver that allows headers needed ... try mini-coi as example via npx or bun x ... example: npx mini-coi . starts a local server that enables SharedArrayBuffer. (edited)
Thanks ... I'll try to decipher your reply (i'm not familiar with these stuff, but i'll try) AI Suggested packing this offline Pyscript using Electron to avoid this problem and lower the security setting. do you think the problem will be solved if i try this solution?
Avatar
OK, when i use <script type="mpy"> , python code get executed offline with no problem using the local interpreter defined in <mpy-config>. But if i use <script type="mpy-editor"> then the page will request blob/interpreter from internet, not the one told to use inside <mpy-config>. How to force the browser to use local interpreter when using type="mpy-editor"? (edited)
Avatar
Avatar
m28
OK, when i use <script type="mpy"> , python code get executed offline with no problem using the local interpreter defined in <mpy-config>. But if i use <script type="mpy-editor"> then the page will request blob/interpreter from internet, not the one told to use inside <mpy-config>. How to force the browser to use local interpreter when using type="mpy-editor"? (edited)
Jeff Glass 02/05/2024 21:12
Try adding a separate mpy-editor with the setup attribute and a config attribute pointing to the config in a file
Avatar
Andrea Giammarchi 02/05/2024 21:26
are you using the suggested way to run offline? you need to specify an interpreter in your config to do so, and setup attribute helps you provide a config ... that should do the trick as behind the scene it's all the same ๐Ÿค”
Avatar
Yes i'm. If the type is "mpy" then micropython get loaded from local host, but if the type is "mpy-editor" then it will try to get it from online CDN. this what i tried: <script type="mpy-editor" config="pyscript.json" env="test" setup></script> <script type="mpy-editor" env="test"></script> pyscript.json: { "interpreter": "/micropython/micropython.mjs" } (edited)
Avatar
elliot0x01 03/05/2024 08:35
Is it possible to start a new interactive code instance within my existing instance. I have a interactive terminal which supports 2 commands, one of which is "code" command, if the user enters code then start a python code interactive terminal for them and when they exit then they are returned back to original interactive terminal asking for new choice again
Avatar
Avatar
m28
Yes i'm. If the type is "mpy" then micropython get loaded from local host, but if the type is "mpy-editor" then it will try to get it from online CDN. this what i tried: <script type="mpy-editor" config="pyscript.json" env="test" setup></script> <script type="mpy-editor" env="test"></script> pyscript.json: { "interpreter": "/micropython/micropython.mjs" } (edited)
Andrea Giammarchi 03/05/2024 11:09
you are right, the interpreter is ignored in there so here it is: https://github.com/pyscript/pyscript/pull/2043
Description This MR addresses the concern exposed in Discord where indeed the interpreter config property gets fully ignored when present. Changes pass along the version to bootstrap the right int...
๐Ÿฅณ 1
11:12
@m28 as that's already live on npm dev channel, you can test it already by using https://cdn.jsdelivr.net/npm/@pyscript/core@0.4.26/dist/core.js for the JS script and https://cdn.jsdelivr.net/npm/@pyscript/core@0.4.26/dist/core.css for the CSS link
Avatar
Avatar
Andrea Giammarchi
@m28 as that's already live on npm dev channel, you can test it already by using https://cdn.jsdelivr.net/npm/@pyscript/core@0.4.26/dist/core.js for the JS script and https://cdn.jsdelivr.net/npm/@pyscript/core@0.4.26/dist/core.css for the CSS link
Andrea Giammarchi 03/05/2024 11:13
please let us know if that MR fixed it (but I've tried it and tested it, it fixed it for me).
Avatar
Avatar
elliot0x01
Is it possible to start a new interactive code instance within my existing instance. I have a interactive terminal which supports 2 commands, one of which is "code" command, if the user enters code then start a python code interactive terminal for them and when they exit then they are returned back to original interactive terminal asking for new choice again
Andrea Giammarchi 03/05/2024 11:15
there's no exit from code.interact() AFAIK but you can always drop that terminal, actually also invoking terminate() on the associated worker, and create a new one on demand when new code needs to be added. You probably need to keep track of the global references if you manage to pass these around but that's one way to try and see how far you could go. (edited)
Avatar
blackbooks 03/05/2024 14:10
i have a flask application and want to do some data evaluation on the client system. I managed to setup my py-Script environment and upload a data file from the user into the browser. Now i need to get some data from my server, which are stored in a sqlite database. Usally in Flask i would query the data in the route and pass it as an object to the render_template function, then i have access to that object in the jinja template. is there a way to interject somewhere in this proces to get the data to the python interpreter or does it come down to make a request towards the server, serialize the data and download a json file? What woule be a efficient approach?
Avatar
Avatar
blackbooks
i have a flask application and want to do some data evaluation on the client system. I managed to setup my py-Script environment and upload a data file from the user into the browser. Now i need to get some data from my server, which are stored in a sqlite database. Usally in Flask i would query the data in the route and pass it as an object to the render_template function, then i have access to that object in the jinja template. is there a way to interject somewhere in this proces to get the data to the python interpreter or does it come down to make a request towards the server, serialize the data and download a json file? What woule be a efficient approach?
Andrea Giammarchi 03/05/2024 14:17
you fetch (as REST) that result and deal with it as JSON would be my answer ... all through pyscript.fetch, of course.
Avatar
chris.laffra 03/05/2024 15:08
You can also store the data when rendering the Jinja template and store it in a variable inside a <script> tag. Then, from Python, once it is alive, you can access the variable from the "pyscript.window.data". You may need to convert, so storing as json-encoded data is a safe route.
15:09
Including the data in the HTML is only if you really care a lot about having to call your server twice, instead of once. Using a REST call is much cleaner.
15:11
If you use LTK, you can use the "ltk.get" function to load the data. An example is given in https://pyscript.github.io/ltk/?tab=3, which loads a CSS files and renders it. You can load JSON encoded data as well, of course.
Avatar
Avatar
Andrea Giammarchi
please let us know if that MR fixed it (but I've tried it and tested it, it fixed it for me).
Thank you very much! It works offline now (thank you!) but only if i set the config file like this - which i think is ok for my use case: { "interpreter": "http://localhost:8000/micropython/micropython.mjs" } If i use relative path it will not work: { "interpreter": "/micropython/micropython.mjs" } and the console will show an error: (edited)
18:08
Console errors if using relative path for the interpreter
Avatar
elliot0x01 05/05/2024 13:52
is it possible to display images on the micropython terminal? if so, can someone give an example code
Avatar
elliot0x01 05/05/2024 15:00
why does datetime don't work print(datetime.datetime.now())
Avatar
Puppy of Doom 05/05/2024 16:53
Hi, I'm having issues with loading packages in offline mode. I'm following this guide: https://docs.pyscript.net/2024.4.1/user-guide/offline/ It works as expected until i try to load pyodide packages. If I include a package in the py-config (or via a .toml) as below: ######## <py-config> interpreter = "/pyodide/pyodide.mjs" packages = ["pandas"] </py-config> ########## i get the follwong errors in my browser console: ##### Loading micropip, packaging pyodide.asm.js:9:94945 None of the โ€œsha256โ€ hashes in the integrity attribute match the content of the subresource. The computed hash is โ€œSU+NQGcG92vsNzGIylQUG6NOUrq+u0hNrZh4vdHz+RE=โ€. 127.0.0.1:5000 None of the โ€œsha256โ€ hashes in the integrity attribute match the content of the subresource. The computed hash is โ€œgfxclnbCoeEhvGbfMLvSZyznt8GC3Jt0JYJ1DKzCy08=โ€. 127.0.0.1:5000 Failed to load micropip, packaging pyodide.asm.js:9:95309 I have tried a http.server and to serve the index.html via a flask server. (edited)
16:56
Full browser console logs: Loading micropip, packaging pyodide.asm.js:9:94945 None of the โ€œsha256โ€ hashes in the integrity attribute match the content of the subresource. The computed hash is โ€œSU+NQGcG92vsNzGIylQUG6NOUrq+u0hNrZh4vdHz+RE=โ€. 127.0.0.1:5000 None of the โ€œsha256โ€ hashes in the integrity attribute match the content of the subresource. The computed hash is โ€œgfxclnbCoeEhvGbfMLvSZyznt8GC3Jt0JYJ1DKzCy08=โ€. 127.0.0.1:5000 Failed to load micropip, packaging pyodide.asm.js:9:95309 The following error occurred while loading micropip: pyodide.asm.js:9:95351 NetworkError when attempting to fetch resource. pyodide.asm.js:9:95404 The following error occurred while loading packaging: pyodide.asm.js:9:95351 NetworkError when attempting to fetch resource. pyodide.asm.js:9:95404 Uncaught (in promise) PythonError: Traceback (most recent call last): File "/lib/python311.zip/importlib/init.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "<frozen importlib._bootstrap>", line 1209, in _gcd_import File "<frozen importlib._bootstrap>", line 1181, in _find_and_load File "<frozen importlib._bootstrap>", line 1145, in _find_and_load_unlocked ModuleNotFoundError: The module 'micropip' is included in the Pyodide distribution, but it is not installed. You can install it by calling: await micropip.install("micropip") in Python, or await pyodide.loadPackage("micropip") in JavaScript See https://pyodide.org/en/stable/usage/loading-packages.html for more details. M pyodide.asm.js:9 new_error pyodide.asm.js:9 _pythonexc2js pyodide.asm.js:9 callPyObjectKwargs pyodide.asm.js:9 callPyObject pyodide.asm.js:9 apply pyodide.asm.js:9 apply pyodide.asm.js:9 pyimport pyodide.asm.js:9 Qt pyodide.js:124 pyodide.asm.js:9:86981
16:58
Here's the flask logs: 127.0.0.1 - - [05/May/2024 17:51:35] "GET / HTTP/1.1" 200 - 127.0.0.1 - - [05/May/2024 17:51:35] "GET /pyscript/core.js HTTP/1.1" 200 - 127.0.0.1 - - [05/May/2024 17:51:35] "GET /pyscript/core.css HTTP/1.1" 200 - 127.0.0.1 - - [05/May/2024 17:51:35] "GET /pyscript/core-D8cMs74K.js HTTP/1.1" 200 - 127.0.0.1 - - [05/May/2024 17:51:35] "GET /pyscript.toml HTTP/1.1" 200 - 127.0.0.1 - - [05/May/2024 17:51:35] "GET /favicon.ico HTTP/1.1" 404 - 127.0.0.1 - - [05/May/2024 17:51:35] "GET /pyscript/toml-CvAfdf9_.js HTTP/1.1" 200 - 127.0.0.1 - - [05/May/2024 17:51:35] "GET /pyscript/deprecations-manager-a4FMde84.js HTTP/1.1" 200 - 127.0.0.1 - - [05/May/2024 17:51:35] "GET /pyscript/error-4YRcRMnE.js HTTP/1.1" 200 - 127.0.0.1 - - [05/May/2024 17:51:35] "GET /pyscript/py-editor-B-r3otcd.js HTTP/1.1" 200 - 127.0.0.1 - - [05/May/2024 17:51:35] "GET /pyscript/py-terminal-BJ8xBTEO.js HTTP/1.1" 200 - 127.0.0.1 - - [05/May/2024 17:51:35] "GET /pyscript/xterm.css HTTP/1.1" 200 - 127.0.0.1 - - [05/May/2024 17:51:53] "GET /pyscript/core-D8cMs74K.js.map HTTP/1.1" 304 -
16:58
127.0.0.1 - - [05/May/2024 17:51:35] "GET /pyscript/xterm-DqawCVsv.js HTTP/1.1" 200 - 127.0.0.1 - - [05/May/2024 17:51:35] "GET /pyscript/xterm-readline-D247p8vq.js HTTP/1.1" 200 - 127.0.0.1 - - [05/May/2024 17:51:35] "GET /pyscript/xterm_addon-fit--gyF3PcZ.js HTTP/1.1" 200 - 127.0.0.1 - - [05/May/2024 17:51:35] "GET /pyscript/xterm_addon-web-links-Cnej-nJ6.js HTTP/1.1" 200 - 127.0.0.1 - - [05/May/2024 17:51:35] "GET /pyodide/pyodide.mjs HTTP/1.1" 200 - 127.0.0.1 - - [05/May/2024 17:51:35] "GET /pyodide/pyodide-lock.json HTTP/1.1" 200 - 127.0.0.1 - - [05/May/2024 17:51:35] "GET /pyodide/python_stdlib.zip HTTP/1.1" 200 - 127.0.0.1 - - [05/May/2024 17:51:35] "GET /pyodide/pyodide.asm.wasm HTTP/1.1" 200 - 127.0.0.1 - - [05/May/2024 17:51:35] "GET /pyodide/pyodide.asm.js HTTP/1.1" 200 - 127.0.0.1 - - [05/May/2024 17:51:37] "GET /pyodide/micropip-0.5.0-py3-none-any.whl HTTP/1.1" 200 - 127.0.0.1 - - [05/May/2024 17:51:37] "GET /pyodide/packaging-23.1-py3-none-any.whl HTTP/1.1" 200 -
Avatar
elliot0x01 06/05/2024 05:32
urllib not supported in mpy ? ๐Ÿค”
Avatar
Avatar
m28
Thank you very much! It works offline now (thank you!) but only if i set the config file like this - which i think is ok for my use case: { "interpreter": "http://localhost:8000/micropython/micropython.mjs" } If i use relative path it will not work: { "interpreter": "/micropython/micropython.mjs" } and the console will show an error: (edited)
Andrea Giammarchi 06/05/2024 08:13
we can look at that but the issue is basically that the Worker is out of a Blob URL and it has no files in it so that a fully qualified URL to point at the interpreter is always preferable ... true that with regular PyScript elements that should not be the case ... let's see if I can implement that dance in the PyEditor too (it's different bootstrap logic).
Avatar
Avatar
Andrea Giammarchi
we can look at that but the issue is basically that the Worker is out of a Blob URL and it has no files in it so that a fully qualified URL to point at the interpreter is always preferable ... true that with regular PyScript elements that should not be the case ... let's see if I can implement that dance in the PyEditor too (it's different bootstrap logic).
Andrea Giammarchi 06/05/2024 08:18
you know what ... this was a no-brainer https://github.com/pyscript/pyscript/pull/2050
Description This MR allows usage of relative to absolute paths to define the PyEditor interpreter via config. Changes resolve as fully qualified URL the interpreter, if present, before bootstrappi...
Avatar
Avatar
Puppy of Doom
Hi, I'm having issues with loading packages in offline mode. I'm following this guide: https://docs.pyscript.net/2024.4.1/user-guide/offline/ It works as expected until i try to load pyodide packages. If I include a package in the py-config (or via a .toml) as below: ######## <py-config> interpreter = "/pyodide/pyodide.mjs" packages = ["pandas"] </py-config> ########## i get the follwong errors in my browser console: ##### Loading micropip, packaging pyodide.asm.js:9:94945 None of the โ€œsha256โ€ hashes in the integrity attribute match the content of the subresource. The computed hash is โ€œSU+NQGcG92vsNzGIylQUG6NOUrq+u0hNrZh4vdHz+RE=โ€. 127.0.0.1:5000 None of the โ€œsha256โ€ hashes in the integrity attribute match the content of the subresource. The computed hash is โ€œgfxclnbCoeEhvGbfMLvSZyznt8GC3Jt0JYJ1DKzCy08=โ€. 127.0.0.1:5000 Failed to load micropip, packaging pyodide.asm.js:9:95309 I have tried a http.server and to serve the index.html via a flask server. (edited)
Andrea Giammarchi 06/05/2024 08:27
there is an example which has been successfully tested already ... https://docs.pyscript.net/2024.4.1/user-guide/offline/#local-pyodide-packages have you downloaded the pyodide version with all packages? have you tried npx static-handler --coi ./public/ to start the server?
Avatar
Avatar
Andrea Giammarchi
you know what ... this was a no-brainer https://github.com/pyscript/pyscript/pull/2050
Andrea Giammarchi 06/05/2024 09:05
this is already on npm so you can test it via 0.4.30 it solves already your case ... JS link https://cdn.jsdelivr.net/npm/@pyscript/core@0.4.30/dist/core.js with CSS https://cdn.jsdelivr.net/npm/@pyscript/core@0.4.30/dist/core.css (edited)
Avatar
If we were trying to make a PWA with session and indexed storage - Are there similarities ?
Avatar
Avatar
Neon22
If we were trying to make a PWA with session and indexed storage - Are there similarities ?
Andrea Giammarchi 06/05/2024 09:27
what do you mean? ๐Ÿค” ... also, why not IndexedDB? ๐Ÿค”
Avatar
Avatar
Andrea Giammarchi
there is an example which has been successfully tested already ... https://docs.pyscript.net/2024.4.1/user-guide/offline/#local-pyodide-packages have you downloaded the pyodide version with all packages? have you tried npx static-handler --coi ./public/ to start the server?
Puppy of Doom 06/05/2024 09:48
Thanks for the help! python3 -m http.server -d ./public/ <- Doesn't work npx static-handler --coi ./public/ <- Does work Not sure why that is tbh
Avatar
Avatar
Andrea Giammarchi
this is already on npm so you can test it via 0.4.30 it solves already your case ... JS link https://cdn.jsdelivr.net/npm/@pyscript/core@0.4.30/dist/core.js with CSS https://cdn.jsdelivr.net/npm/@pyscript/core@0.4.30/dist/core.css (edited)
Excellent.. i have just created a new offline project and everything works fine, including relative paths. Thank you for your time and effort.
Avatar
Avatar
Puppy of Doom
Thanks for the help! python3 -m http.server -d ./public/ <- Doesn't work npx static-handler --coi ./public/ <- Does work Not sure why that is tbh
Andrea Giammarchi 06/05/2024 13:40
you need special headers to enable SharedArrayBuffer as explained in the docs: https://docs.pyscript.net/2024.4.1/user-guide/workers/
Avatar
Avatar
m28
Excellent.. i have just created a new offline project and everything works fine, including relative paths. Thank you for your time and effort.
Andrea Giammarchi 06/05/2024 13:40
awesome, I've just asked to approve the MR so next release (coming soon) should have it sorted.
Avatar
Hello good people! I'm trying to work in OOP approach in pyscript ... not sure how to take user input as an object and connect the method defined within a Class with the py-click in HTML... any suggestion? (edited)
Avatar
Avatar
Moum
Hello good people! I'm trying to work in OOP approach in pyscript ... not sure how to take user input as an object and connect the method defined within a Class with the py-click in HTML... any suggestion? (edited)
Jeff Glass 06/05/2024 19:12
I'm not sure what you mean about taking using input as an object, but in terms of using instance methods with py-click, you can do somethng like: <script type="py"> class Foo(): def __init__(self, name): self.name = name def click(self, evt): print(f"{self.name} got a {evt.type} event!") f = Foo('Jeff') </script> <button py-click="f.click">Click Me</button>
19:12
The value of a py-*event attribute is just a name to look up and call in the global (main thread) namespace
Avatar
Avatar
Jeff Glass
I'm not sure what you mean about taking using input as an object, but in terms of using instance methods with py-click, you can do somethng like: <script type="py"> class Foo(): def __init__(self, name): self.name = name def click(self, evt): print(f"{self.name} got a {evt.type} event!") f = Foo('Jeff') </script> <button py-click="f.click">Click Me</button>
Hello Jeff, thanks for chiming in. So, this is the part of the function in produral approach where user input is being passed through input_text: def sent_represent(event): input_text = document.querySelector("#represent_sent") text = input_text.value ... output_div = document.querySelector("#output_represent") output_div.innerText = min_key (edited)
22:42
So, I'm trying to understand how to pass document.querySelector("#represent_sent") through the method ... as an event? Or like object_text = *Class*(document.querySelector("#represent_sent")) and then py-click = "object_text.sent_represent" in HTML? (edited)
Avatar
Avatar
Moum
So, I'm trying to understand how to pass document.querySelector("#represent_sent") through the method ... as an event? Or like object_text = *Class*(document.querySelector("#represent_sent")) and then py-click = "object_text.sent_represent" in HTML? (edited)
Andrea Giammarchi 07/05/2024 08:34
why don't you just object_text.addEventListener("click", create_proxy(object_text.method)) ?
Avatar
The demos are still usinf 2024.1 - at least in here: https://github.com/pyscript/examples It seems probable that you guys are no longer using this repo to populate the examples. I raised an issue a while ago and just updated it because the its a very old version of three.js and they changed how loading happens (17 releases behind). here:
Contribute to pyscript/examples development by creating an account on GitHub.
Avatar
Avatar
Andrea Giammarchi
why don't you just object_text.addEventListener("click", create_proxy(object_text.method)) ?
Hello ... not sure how and where to put that actually? If there's any demo project available to see and learn the syntax of the code, could please refer to that? Would be of great help.
Avatar
Avatar
Moum
Hello ... not sure how and where to put that actually? If there's any demo project available to see and learn the syntax of the code, could please refer to that? Would be of great help.
Andrea Giammarchi 08/05/2024 08:16
once you have a DOM Element reference the wohle DOM API just works out of the box
Avatar
Hi all, Can anyone please confirm if it is possible to pass a list from pyscript to respective html id and convert the list to separate html button
Avatar
Can anyone please help on the above ask
Avatar
Avatar
796279
Hi all, Can anyone please confirm if it is possible to pass a list from pyscript to respective html id and convert the list to separate html button
Not quite sure what you're asking for. Can you perhaps give more context? What does the list represent? What values does it contain? How is this related to an HTML button? More context = a better answer. ๐Ÿ‘๐Ÿš€โญ (edited)
Avatar
Can someone help me with this code why it's not displayed?
Avatar
Avatar
zobi
Can someone help me with this code why it's not displayed?
I forgot the call the function it works๐Ÿ‘
Avatar
Avatar
Jeff Glass
And finally - if you want to clear the terminal programmatically (from an external event or script), you can grab a reference to the terminal and clear it: <script type="py" terminal> print("hello, world") </script> <script type="py"> from pyscript import document def clearTerm(evt): terminal_element = document.querySelector("script[type='py'][terminal]") terminal_element.terminal.clear() </script> <button py-click="clearTerm">Click to Clear</button>
@Jeff Glass for me this code worked on browser but not in mobile.
Avatar
how do i send information between javascript inside a script tag, and a main.py file?
18:55
like if i have some text input, and then i want to check if a checkbox is checked with javascript, and if it is, then call a function in the main.py with the text input, and an argument for if the checkbox is checked or not
18:55
i've tried for hours but dont know how to do it, so if anyone could try to help, i would really appreciate it ๐Ÿ™
Avatar
Avatar
Otso
like if i have some text input, and then i want to check if a checkbox is checked with javascript, and if it is, then call a function in the main.py with the text input, and an argument for if the checkbox is checked or not
chris.laffra 13/05/2024 09:56
You could use PyScript/LTK at https://github.com/pyscript/ltk, See the live example with an "I love LTK" checkbox at https://pyscript.github.io/ltk/?tab=2
Avatar
SnowCutieOwO 13/05/2024 11:41
is there a jsDelivr website instead of pyscript.net/releases? I'm having problem when I try loading PyScript using this domain
Avatar
Avatar
chris.laffra
You could use PyScript/LTK at https://github.com/pyscript/ltk, See the live example with an "I love LTK" checkbox at https://pyscript.github.io/ltk/?tab=2
im more talking about comminucating between a main.py file, like getting information sent between the python file and javascript in the html
Avatar
And also is it possible to load the pyodide/pyscript wasm binary async, so that the website doesnt for example freeze css animated loading screens
Avatar
the main thing i want to know is how to call a python function which is in a main.py file, from javascript, thats all i need
Avatar
No one answered so i ditched pyscript for pyodide, and its working great
Avatar
Avatar
SnowCutieOwO
is there a jsDelivr website instead of pyscript.net/releases? I'm having problem when I try loading PyScript using this domain
Andrea Giammarchi 14/05/2024 15:57
โค๏ธ 1
Avatar
Facing a weird issue with pyscript terminal in a nextjs (react) app. Here is an overview of the setup:
  • I add a script tag with the packages (config) and the code using a useEffect when the page loads and remove it once component is unmounted.
  • But the main script tag for the pyscript cdn url is only added once and never removed. When i open the page with the component, navigate away and then go to the page again, I see 2 instances of the terminal and the python code no longer has access to dunder terminal object. I am providing my own div for the terminal and passing that div's id to the script's target During this navigation, the entire DOM is not reloaded. I think nextjs only replaces a part of the dom with the new page's html
I don't know the inner workings of pyscript, but the script tag's id keeps incrementing each time i navigate away and back to the page. Starts with id="py-w0" and increases to py-w2 and so on.
(edited)
Avatar
how can i use pyscript in pycharm to launch python script in html?
Avatar
Avatar
-1S
how can i use pyscript in pycharm to launch python script in html?
pyscript runs in the browser. you need html, css,py files. Suggest you try manually on pyscript.com first. pycharm is an IDE for authoring py files but not html or css or toml.
Avatar
Hi. I'm wondering if it is possible to pass generators from main thread to workers. I have code that looks like this in main def genx(): yield 'a' yield 'b' yield 'c' worker = PyWorker("./worker.py", worker_options) worker.sync.genx = genx and then in worker.py, I do for x in sync.genx(): but this blows up with an error TypeError: 'pyodide.ffi.JsProxy' object is not iterable
Avatar
Avatar
dmmiller
Hi. I'm wondering if it is possible to pass generators from main thread to workers. I have code that looks like this in main def genx(): yield 'a' yield 'b' yield 'c' worker = PyWorker("./worker.py", worker_options) worker.sync.genx = genx and then in worker.py, I do for x in sync.genx(): but this blows up with an error TypeError: 'pyodide.ffi.JsProxy' object is not iterable
Generators work just fine if I stay in one context, either main or in a worker. It's only when I try and sync them across that I run into an issue. It's probably not a good idea because of the overhead of marshalling data across, but I was playing with it to see.
17:47
Is there a way to connect to azure sql database through py-script in html?
Avatar
how do I get the pyweb module to play nice with VSCode? It doesn't appear I can install it in my venv, so VSCode has no concept that the module exists.
Avatar
Puppy of Doom 16/05/2024 08:40
Hi, has anyone successfully imported libsodium into their pyscript project? Been trying all kinds of solutions with no luck https://github.com/jedisct1/libsodium.js
libsodium compiled to Webassembly and pure JavaScript, with convenient wrappers. - jedisct1/libsodium.js
Avatar
Avatar
dmmiller
Generators work just fine if I stay in one context, either main or in a worker. It's only when I try and sync them across that I run into an issue. It's probably not a good idea because of the overhead of marshalling data across, but I was playing with it to see.
Andrea Giammarchi 16/05/2024 10:52
have you tried to from pyscript import ffi and then worker.sync.genx = ffi.create_proxy(genx) ? I wonder if that would be smart enough to instrument the returned callback to deal with genreators ... otherwise it seems like a pyodide proxy limitation hard for us to bypass or solve.
Avatar
Avatar
Andrea Giammarchi
have you tried to from pyscript import ffi and then worker.sync.genx = ffi.create_proxy(genx) ? I wonder if that would be smart enough to instrument the returned callback to deal with genreators ... otherwise it seems like a pyodide proxy limitation hard for us to bypass or solve.
Andrea Giammarchi 16/05/2024 10:54
and yes, like you know and stated already, generators work just fine within the same realm (world / thread) but crossing boundaries there might be problematic or not yet handled properly by the interpreters (haven't tried MicroPython though, but having mpy on the worker is not really a common use case, pyodide is way more common)
Avatar
Avatar
Andrea Giammarchi
have you tried to from pyscript import ffi and then worker.sync.genx = ffi.create_proxy(genx) ? I wonder if that would be smart enough to instrument the returned callback to deal with genreators ... otherwise it seems like a pyodide proxy limitation hard for us to bypass or solve.
Let me try that and I'll let you know. Thanks for the suggestion
Avatar
blackbooks 16/05/2024 20:49
I am wondering how to best develop my PyScript Script in PyCharm as part of a Flask Application: Specifically i am wondering how to develop in way which lets me test and debug my code preferably without Restarting and Reloading the flask page everytime i change something and be able to set breakpoints. I am wondering if it would be possible or even a good idea to try to mirror the pyscript code in a different folder and develop and debug there and only start to move the modules to PyScript when i am sure they are roughly doing what i want. Is there some advice or best practice here?
Avatar
Hey I copied the example on https://docs.pyscript.net/2024.5.2/user-guide/editor/ in a html file and tried to open it, the code editors get displayed, however I can not run it. Am I missing something or is the example outdated? <!doctype html> <html> <head> <!-- Recommended meta tags --> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width,initial-scale=1.0"> <!-- PyScript CSS --> <link rel="stylesheet" href="https://pyscript.net/releases/2024.5.2/core.css"> <!-- This script tag bootstraps PyScript --> <script type="module" src="https://pyscript.net/releases/2024.5.2/core.js"></script> </head> <body> <script type="py-editor"> import sys print(sys.version) </script> <script type="mpy-editor"> import sys print(sys.version) a = 42 print(a) </script> </body> </html>
Avatar
Avatar
Yasin
Hey I copied the example on https://docs.pyscript.net/2024.5.2/user-guide/editor/ in a html file and tried to open it, the code editors get displayed, however I can not run it. Am I missing something or is the example outdated? <!doctype html> <html> <head> <!-- Recommended meta tags --> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width,initial-scale=1.0"> <!-- PyScript CSS --> <link rel="stylesheet" href="https://pyscript.net/releases/2024.5.2/core.css"> <!-- This script tag bootstraps PyScript --> <script type="module" src="https://pyscript.net/releases/2024.5.2/core.js"></script> </head> <body> <script type="py-editor"> import sys print(sys.version) </script> <script type="mpy-editor"> import sys print(sys.version) a = 42 print(a) </script> </body> </html>
Jeff Glass 16/05/2024 21:56
If you open the developer console in your browser, do you see any errors?
22:11
Yea I see these errors (edited)
22:14
I just opened the html page in a browser
Avatar
I also got this one a bit later
22:58
which is weird because the src files should be correct
Avatar
Avatar
Andrea Giammarchi
and yes, like you know and stated already, generators work just fine within the same realm (world / thread) but crossing boundaries there might be problematic or not yet handled properly by the interpreters (haven't tried MicroPython though, but having mpy on the worker is not really a common use case, pyodide is way more common)
I tried it with ffi.create_proxy on the generator and then sync.genx() on the other side and it does not work.
Avatar
Avatar
Yasin
Click to see attachment ๐Ÿ–ผ๏ธ
Andrea Giammarchi 17/05/2024 08:42
you are running a server that serves correct headers, yes? 'cause you cannot open the .html file out of the box, you need a localhost. Try with npx mini-coi . in that folder and reach the localhost url. If you name the file index.html you will see everything working, as I just did: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="https://pyscript.net/releases/2024.5.2/core.css"> <script type="module" src="https://pyscript.net/releases/2024.5.2/core.js"></script> </head> <body> <script type="py-editor"> import sys print(sys.version) </script> <script type="mpy-editor"> import sys print(sys.version) a = 42 print(a) </script> </body> </html> (edited)
Avatar
Does Pyscript support importing methods from other files? Currently, I'm trying to import methods from other files but its not detecting them for some reason and I keep getting a ModuleNotFound Error. The main python file being run is being picked up and I'm trying to access timeutils.py from schedule.py and I'm trying the following import: from assets.pyfiles.utils.timeutils import ( get_utc_time, get_local_time, get_next_update_time, get_time_until_next_update, format_time_string, ) (edited)
08:55
This is the file structure for reference
Avatar
Avatar
Aditya
Does Pyscript support importing methods from other files? Currently, I'm trying to import methods from other files but its not detecting them for some reason and I keep getting a ModuleNotFound Error. The main python file being run is being picked up and I'm trying to access timeutils.py from schedule.py and I'm trying the following import: from assets.pyfiles.utils.timeutils import ( get_utc_time, get_local_time, get_next_update_time, get_time_until_next_update, format_time_string, ) (edited)
have you got it in your toml file ?
Avatar
Avatar
Neon22
have you got it in your toml file ?
Am I supposed to add all the python files to the toml file? I thought that was only for files that python could access like csvs, etc?
Avatar
@Neon22 Solved it with adding it to my toml file, thank you!
๐Ÿ‘ 1
Avatar
Avatar
Andrea Giammarchi
you are running a server that serves correct headers, yes? 'cause you cannot open the .html file out of the box, you need a localhost. Try with npx mini-coi . in that folder and reach the localhost url. If you name the file index.html you will see everything working, as I just did: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="https://pyscript.net/releases/2024.5.2/core.css"> <script type="module" src="https://pyscript.net/releases/2024.5.2/core.js"></script> </head> <body> <script type="py-editor"> import sys print(sys.version) </script> <script type="mpy-editor"> import sys print(sys.version) a = 42 print(a) </script> </body> </html> (edited)
thank you it really fixed my issues
00:00
now I can play around ๐Ÿ™‚
Avatar
Hi all! Is there a way to cache resources already downloaded by Pyodide? Each time my pyscript page reloads the whole process starts again and Iโ€™m trying to reduce initialisation time. Donโ€™t know if Iโ€™m doing something wrong. (edited)
Avatar
Avatar
Diseph
Hi all! Is there a way to cache resources already downloaded by Pyodide? Each time my pyscript page reloads the whole process starts again and Iโ€™m trying to reduce initialisation time. Donโ€™t know if Iโ€™m doing something wrong. (edited)
Andrea Giammarchi 19/05/2024 11:06
I believe Pyodide tries its best to cache resources but the bootstrap time for WASM can't be cached, it's going to be a new botstrap each time. We recommend running Pyodide in workers though so at least the main thread is not blocked while bootstrapping (in case you aren't using a worker already).
Avatar
@Andrea Giammarchi thank you, Iโ€™ll try that.
Avatar
@Fabio Here's the PR that I mentioned when I met you today around lunch. https://github.com/pyscript/pyscript/pull/2065
Description Add a config option to optionally provide index_urls that can override the default python package index url. Changes A new config option called index_urls that can provide a list of URL...
โค๏ธ 1
๐Ÿ™ 1
Avatar
Avatar
Amjith
@Fabio Here's the PR that I mentioned when I met you today around lunch. https://github.com/pyscript/pyscript/pull/2065
That's awesome ๐Ÿ‘ Thank you
Avatar
I couldn't get the test to pass and I see that it is continuing to use the default PyPI url.
22:26
I'd love to get your help (when you have a chance) in finishing up the PR.
Avatar
blackbooks 20/05/2024 20:32
Hi there, i was able to create all my logic for the clientside data evaluation in sqlalchemy and have now my results in a json format provided by pyscript. I now want to display the results to the user in a table. On the flask server-side i would do it with a jinja2 template like so: <table class="table table-hover table-striped" data-filter-control="true" id="rctable"> <thead> <tr> <th scope="col">Frage</th> <th scope="col">Anzahl</th> <th scope="col">รœbermittlung</th> </tr> </thead> <tbody> {% for item in json_data.query%} <tr> <td>{{ item.Field_Label }}</td> <td>{{ item.Con_Result}}</td> <td> {% if item.transfer ==True %} <input type="checkbox" id="transfer" value=" True " checked> {% else %} <input type="checkbox" id="transfer" value=" false" unchecked> {% endif %} </td> </tr> {% endfor %} </tbody> </table> add a datatable plugin at the end: html <script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.13.1/js/jquery.dataTables.js"></script> <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.13.1/css/jquery.dataTables.css"> <script>$(document).ready(function(){ $('#rctable').DataTable(); }); </script> ` But now i am on the clients side and i am wondering how best to proceed. 1. Would it be possible to load the jinja2 template engine into the pyscript environment and make it usable there? Is it even a reasonable idea, or unjustified effort given the existing possibilites of pyscript? 2. Should i simply try to do this with the dom-manipulation tools provided by pyscript? 3. Save the effort and create a REST API to send the data to the server and proceed from there as usual?
Avatar
Avatar
blackbooks
Hi there, i was able to create all my logic for the clientside data evaluation in sqlalchemy and have now my results in a json format provided by pyscript. I now want to display the results to the user in a table. On the flask server-side i would do it with a jinja2 template like so: <table class="table table-hover table-striped" data-filter-control="true" id="rctable"> <thead> <tr> <th scope="col">Frage</th> <th scope="col">Anzahl</th> <th scope="col">รœbermittlung</th> </tr> </thead> <tbody> {% for item in json_data.query%} <tr> <td>{{ item.Field_Label }}</td> <td>{{ item.Con_Result}}</td> <td> {% if item.transfer ==True %} <input type="checkbox" id="transfer" value=" True " checked> {% else %} <input type="checkbox" id="transfer" value=" false" unchecked> {% endif %} </td> </tr> {% endfor %} </tbody> </table> add a datatable plugin at the end: html <script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.13.1/js/jquery.dataTables.js"></script> <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.13.1/css/jquery.dataTables.css"> <script>$(document).ready(function(){ $('#rctable').DataTable(); }); </script> ` But now i am on the clients side and i am wondering how best to proceed. 1. Would it be possible to load the jinja2 template engine into the pyscript environment and make it usable there? Is it even a reasonable idea, or unjustified effort given the existing possibilites of pyscript? 2. Should i simply try to do this with the dom-manipulation tools provided by pyscript? 3. Save the effort and create a REST API to send the data to the server and proceed from there as usual?
I had a go with either jinja/jinga2/jingax early on. It almost worked. probably a bit easier now. You need the one without any C in it.
Avatar
christy heaton 21/05/2024 00:22
Hello! This app worked when I made it last month, but now when I run it I get TypeError: Can't create an SSLContext object without an ssl module https://pyscript.com/@cheaton/2024-eclipse/latest Any ideas? (edited)
Avatar
Avatar
christy heaton
Hello! This app worked when I made it last month, but now when I run it I get TypeError: Can't create an SSLContext object without an ssl module https://pyscript.com/@cheaton/2024-eclipse/latest Any ideas? (edited)
Jeff Glass 21/05/2024 14:57
I believe a few of the headers on PyScript.com have been adjusted recently, though in theory for the betterโ€ฆ does it work if you change to using PyScirpt 2024.5.2 ?
Avatar
Erkenbrandcxvii 21/05/2024 20:27
I'm a student making an app that takes weather data from two local stations (one is an airport) and I get the same error when I try requests.get() with either website: /lib/python3.11/site-packages/urllib3/connectionpool.py:1101: InsecureRequestWarning: Unverified HTTPS request is being made to host 'aviationweather.gov'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#tls-warnings warnings.warn(Error occurred: ('Connection aborted.', HTTPException("Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'https://aviationweather.gov/api/data/metar?ids=KSLC&format=json'.")) Here is the method where the error occurs: def make_request(self, url, tries=1): response = None try: response = requests.get(url, timeout=self.wait, verify=True) response.raise_for_status() except requests.exceptions.RequestException as err: print(f"Error occurred: {err}") except Exception as err: print(f"An unexpected error occurred: {err}") if tries > 0: time.sleep(self.wait) self.make_request(url, tries-1) else: print("Requests failed after 5 attempts, giving up...") return response Has anyone seen an error like this before? I assume it has to do with my implementation of requests, unless there's something in the html I'm supposed to do (edited)
Avatar
blackbooks 21/05/2024 21:33
Hello again, I have some trouble setting up the terminal on the web page according to the docs: PyScript Terminal. I have: <script id="my_script" type="mpy" terminal worker> import code code.interact() </script> and further down <script> const myterm = document.querySelector("#my_script"); await myterm.process('print("Hello world!")'); </script> But i get the following errors: Uncaught SyntaxError: await is only valid in async functions and the top level bodies of modules (at terminal:122:5) and the Error: Unable to use SharedArrayBuffer due insecure environment error, when i use the suggested solution with the mini-coi.js file and properly reference its file: {% block header_start %} <script src={{ url_for('PyScriptDB.static', filename='mini-coi.js') }} scope="./static/"></script> {% endblock %} the page tries endlessly to load and reload, without throwing an error. What is the issue here?
Avatar
Avatar
Jeff Glass
I believe a few of the headers on PyScript.com have been adjusted recently, though in theory for the betterโ€ฆ does it work if you change to using PyScirpt 2024.5.2 ?
christy heaton 22/05/2024 04:04
It works with 2024.5.2. Thanks!
๐ŸŽ‰ 1
Avatar
elliot0x01 22/05/2024 12:57
Anyone know, how can I overcome SharedArray issue when embedding my website in an iframe? I am using mini-coi.js and my webapp at sub,domain,com works fine but when I am trying to iframe the page into a hugo static site it's causing issues, I guess I need to have mini coi on main domain ?
Avatar
Avatar
elliot0x01
Anyone know, how can I overcome SharedArray issue when embedding my website in an iframe? I am using mini-coi.js and my webapp at sub,domain,com works fine but when I am trying to iframe the page into a hugo static site it's causing issues, I guess I need to have mini coi on main domain ?
Jeff Glass 22/05/2024 15:00
Andrea will probably be the best to answer when heโ€™s back next week - heโ€™s off on a little R&R!
Avatar
Avatar
elliot0x01
Anyone know, how can I overcome SharedArray issue when embedding my website in an iframe? I am using mini-coi.js and my webapp at sub,domain,com works fine but when I am trying to iframe the page into a hugo static site it's causing issues, I guess I need to have mini coi on main domain ?
Andrea Giammarchi 22/05/2024 16:02
not sure that would solve for iframe, it's likely that you need a plethora of iframe dedicated config features to allow SAB and more ... see any PyScript .com related project to see how we enable iframes to contain more security related features (sorry I can't remember from the top of my head) ... the list can be huge, the specific one for subdomains might be smaller ... out of curiosity though, if you get to control those domains and servers behind, I suggest you add the right headers directly via servers sent headers, not via mini-coi all over the place.
Avatar
Avatar
Andrea Giammarchi
not sure that would solve for iframe, it's likely that you need a plethora of iframe dedicated config features to allow SAB and more ... see any PyScript .com related project to see how we enable iframes to contain more security related features (sorry I can't remember from the top of my head) ... the list can be huge, the specific one for subdomains might be smaller ... out of curiosity though, if you get to control those domains and servers behind, I suggest you add the right headers directly via servers sent headers, not via mini-coi all over the place.
Andrea Giammarchi 22/05/2024 16:03
if you'd like to dig more into iframe's policy: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe#allow
The HTML element represents a nested browsing context, embedding another HTML page into the current one.
Avatar
Avatar
Neon22
I had a go with either jinja/jinga2/jingax early on. It almost worked. probably a bit easier now. You need the one without any C in it.
blackbooks 22/05/2024 21:08
Thanks a lot i tried it with jinja and it was quite easily setup in the local environment and i can now use it for the site manipulation
Avatar
I've built something in Pyscript but while pyodide is loading packages, the website becomes unresponsive, is there a way to mitigate this to keep the website responsive while pyodide loads the packages?
Avatar
Avatar
blackbooks
Thanks a lot i tried it with jinja and it was quite easily setup in the local environment and i can now use it for the site manipulation
would love to see how you use it. cheers...
Avatar
Avatar
Aditya
I've built something in Pyscript but while pyodide is loading packages, the website becomes unresponsive, is there a way to mitigate this to keep the website responsive while pyodide loads the packages?
Jeff Glass 23/05/2024 14:34
Iโ€™d recommend adding the worker attribute to your PyScript tag, to load the interpreter in a worker thread and prevent blocking the pageโ€™s main thread. There are a couple of considerations around headers there - see the docs for more info
Avatar
elliot0x01 24/05/2024 07:05
I have a project based on jekyll static site, and I am trying to add pyscript terminal to one of the markdown page. Everything works fine just when I try to use "worker" for the terminal, the shared array issue arises even though I have properly set mini-coi.js in my assets folder. I am doing something like this, probably something to do with the chirpy theme I am using? ๐Ÿค” Can anyone suggest what can be the best solution to have my terminal work in the markdown
Avatar
elliot0x01 24/05/2024 07:45
Probably add the minicoi.js after the html file is generated by jekyll ?
Avatar
Avatar
Aditya
I've built something in Pyscript but while pyodide is loading packages, the website becomes unresponsive, is there a way to mitigate this to keep the website responsive while pyodide loads the packages?
chris.laffra 24/05/2024 09:21
You can use a worker to load the packages and do the work. That would happen asynchronously. The main thread could actually run MicroPython in that case. I do something similar in PySheets. The LTK kitchensink also uses a worker that way. See https://github.com/pyscript/ltk/blob/main/examples/pubsub.py#L9
LTK is a little toolkit for writing UIs in PyScript - pyscript/ltk
Avatar
Avatar
chris.laffra
You can use a worker to load the packages and do the work. That would happen asynchronously. The main thread could actually run MicroPython in that case. I do something similar in PySheets. The LTK kitchensink also uses a worker that way. See https://github.com/pyscript/ltk/blob/main/examples/pubsub.py#L9
elliot0x01 24/05/2024 12:43
Workers only work when server sets CORS, COOP, COEP headers Will this approach work if I am relying on mini-coi.js and have no controls over headers ? (edited)
Avatar
chris.laffra 24/05/2024 13:48
Did you read the link under that message? https://jeff.glass/post/whats-new-pyscript-2023-11-1/
What's new and what's changed in the newest PyScript release
Avatar
blackbooks 24/05/2024 14:12
Hi there, i have a table in which i load into my website with a pyscript function. On the server side i slap the DataTable plugin on these tables, but this obviously doesn't work so easily here: <script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.13.1/js/jquery.dataTables.js"></script> <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.13.1/css/jquery.dataTables.css"> <script>$(document).ready(function(){ $('#rctable').DataTable(); }); </script> it is no problem to insert this code into the html, but that doesn't mean the browser will apply the code. How do i load this dynamically into my page and make the browser execute the code?
Avatar
Avatar
chris.laffra
Did you read the link under that message? https://jeff.glass/post/whats-new-pyscript-2023-11-1/
elliot0x01 24/05/2024 14:37
Done now ๐Ÿ™‚
Avatar
fixingbrokenrobots 25/05/2024 00:55
Hi everyone. I've been playing with Pyscript for about a year but this is my first time in the Discord. I'm working on a little app that searches a dataframe , returns a list of news articles, then sends a query + these articles to gpt-4o so the AI can summarize them. The summary returned by the OpenAI API is then displayed on the page. The problem I'm encountering is that the API request keeps timing out. Ideally I'd have a 1min+ timeout as making the exact same request in a Jupyter notebook takes many seconds. I've tried adjusting timeout based on info from OpenAI forums but this has had no effect on the problem. So now I'm wondering if there's something in the way that Pyscript handles requests that's causing the issue? Here's the function making the request: userElement = js.document.getElementById('brief') OPENAI_API_KEY = userElement.value.strip() client = OpenAI(api_key=OPENAI_API_KEY, timeout=60.0, max_retries=3) # Convert similar_items to string format global similar_items similar_items_str = "" for index, row in similar_items.iterrows(): similar_items_str += f"Title: {row['Title']} Summary: {row['Summary']} Note: {row['Note']}" payload = f"Using "Please generate a 500 word narrative summary of the following information: '''{similar_items_str}'''" chat_completion = client.chat.completions.create( messages=[ { "role": "user", "content": payload } ], model="gpt-4o", ) # Log the request data for debugging console.log(f"Data: {messages}") # Display the response in the summarybrief div Element('summarybrief').element.innerHTML = chat_completion
Avatar
Hi everyone I use Pyscript for an webpage on which users can run python code in a terminal. For that I use the PyEditor plugin. How do I get the compiling results or the errors. Is this in any way possible?
Avatar
Hello, all! I have a Python package I'd like to use in other projects in PyScript (a little frontend framework). From the PyScript docs, I'm seeing: https://docs.pyscript.net/2024.5.2/user-guide/configuration/#packages
If you need pure Python modules for MicroPython, you have two further options: Use the files option to manually copy the source code for a package onto the file system. Use a URL referencing a MicroPython friendly package instead of PyPI package name.
I have a regular Python package and I'm not sure how to convert it to a package MicroPython's runtime can use. It's Pure Python, but several files. The MicroPython docs don't seem to mention a package format: https://docs.micropython.org/en/latest/reference/packages.html What are my best options for a simple way to include a whole package? I'm happy to put it on a CDN somewhere, but how can I include several files as a package? (Eg, a .zip would be great). PyScript's docs mention wheel formats, but not for MicroPython, so I assume that's out?
20:21
Avatar
Avatar
bouncing
This is my project: https://github.com/kkinder/puepy
elliot0x01 27/05/2024 05:21
Wow this looks promising, is the framework complete or work in progress? For years I have struggled to put on a decent looking frontend LOL, would love to try this one ๐Ÿ™‚
Avatar
Avatar
bouncing
Hello, all! I have a Python package I'd like to use in other projects in PyScript (a little frontend framework). From the PyScript docs, I'm seeing: https://docs.pyscript.net/2024.5.2/user-guide/configuration/#packages
If you need pure Python modules for MicroPython, you have two further options: Use the files option to manually copy the source code for a package onto the file system. Use a URL referencing a MicroPython friendly package instead of PyPI package name.
I have a regular Python package and I'm not sure how to convert it to a package MicroPython's runtime can use. It's Pure Python, but several files. The MicroPython docs don't seem to mention a package format: https://docs.micropython.org/en/latest/reference/packages.html What are my best options for a simple way to include a whole package? I'm happy to put it on a CDN somewhere, but how can I include several files as a package? (Eg, a .zip would be great). PyScript's docs mention wheel formats, but not for MicroPython, so I assume that's out?
Andrea Giammarchi 27/05/2024 06:28
have you read this F.A.Q. section? https://docs.pyscript.net/2024.5.2/faq/#packaging-pointers specially the .zip one
Avatar
Avatar
elliot0x01
I have a project based on jekyll static site, and I am trying to add pyscript terminal to one of the markdown page. Everything works fine just when I try to use "worker" for the terminal, the shared array issue arises even though I have properly set mini-coi.js in my assets folder. I am doing something like this, probably something to do with the chirpy theme I am using? ๐Ÿค” Can anyone suggest what can be the best solution to have my terminal work in the markdown
Andrea Giammarchi 27/05/2024 06:32
a service worker cannot be retrieved from subfolders, it has to be either in parent folders or current folder. This is a Service Worker spec/limitation. (edited)
Avatar
Avatar
bouncing
Hello, all! I have a Python package I'd like to use in other projects in PyScript (a little frontend framework). From the PyScript docs, I'm seeing: https://docs.pyscript.net/2024.5.2/user-guide/configuration/#packages
If you need pure Python modules for MicroPython, you have two further options: Use the files option to manually copy the source code for a package onto the file system. Use a URL referencing a MicroPython friendly package instead of PyPI package name.
I have a regular Python package and I'm not sure how to convert it to a package MicroPython's runtime can use. It's Pure Python, but several files. The MicroPython docs don't seem to mention a package format: https://docs.micropython.org/en/latest/reference/packages.html What are my best options for a simple way to include a whole package? I'm happy to put it on a CDN somewhere, but how can I include several files as a package? (Eg, a .zip would be great). PyScript's docs mention wheel formats, but not for MicroPython, so I assume that's out?
The LTK does it like that I think. in the toml file is a direct link to the github [files] "https://raw.githubusercontent.com/pyscript/ltk/main/ltk/ltk.js" = "ltk/ltk.js" "https://raw.githubusercontent.com/pyscript/ltk/main/ltk/ltk.css" = "ltk/ltk.css"
Avatar
Avatar
Neon22
The LTK does it like that I think. in the toml file is a direct link to the github [files] "https://raw.githubusercontent.com/pyscript/ltk/main/ltk/ltk.js" = "ltk/ltk.js" "https://raw.githubusercontent.com/pyscript/ltk/main/ltk/ltk.css" = "ltk/ltk.css"
Andrea Giammarchi 27/05/2024 11:12
with the .zip it'd be "https://raw.githubusercontent.com/pyscript/ltk/main/ltk.zip" = "./*" instead, where the zip contains the ltk folder with anything else you like in it
Avatar
Avatar
Andrea Giammarchi
a service worker cannot be retrieved from subfolders, it has to be either in parent folders or current folder. This is a Service Worker spec/limitation. (edited)
elliot0x01 27/05/2024 12:45
Yeah figured it, thank you Andrea
๐Ÿ‘ 1
Avatar
Avatar
Neon22
The LTK does it like that I think. in the toml file is a direct link to the github [files] "https://raw.githubusercontent.com/pyscript/ltk/main/ltk/ltk.js" = "ltk/ltk.js" "https://raw.githubusercontent.com/pyscript/ltk/main/ltk/ltk.css" = "ltk/ltk.css"
@Andrea Giammarchi @Neon22 Aha! I looked at the main docs, but not the FAQ. My bad. Awesome, thanks for your help. That worked!
๐Ÿฅณ 1
Avatar
Avatar
bouncing
This is my project: https://github.com/kkinder/puepy
very cool. Great idea. Please enable SVG if possible. Here's an LTK example with a lot of linked interactions. IWBNI Puepy could enable this kind of interaction without too much difficulty ๐Ÿ™‚
Avatar
Avatar
Neon22
very cool. Great idea. Please enable SVG if possible. Here's an LTK example with a lot of linked interactions. IWBNI Puepy could enable this kind of interaction without too much difficulty ๐Ÿ™‚
Thanks! You mean support svg tags, for inline graphics definitions? I had actually tinkered with that, though it does get a little complex because you have to jump through some hoops to get XML namespaces to work right when you create elements in the DOM. I didn't know enough about XML namespaces to be sure I knew what I was doing when I tinkered with it...
Avatar
Avatar
Neon22
very cool. Great idea. Please enable SVG if possible. Here's an LTK example with a lot of linked interactions. IWBNI Puepy could enable this kind of interaction without too much difficulty ๐Ÿ™‚
Is this really all you need? # this is mandatory to display svg properly if tag == "svg": node.setAttribute("xmlns", _svg_ns)
Avatar
Avatar
bouncing
Thanks! You mean support svg tags, for inline graphics definitions? I had actually tinkered with that, though it does get a little complex because you have to jump through some hoops to get XML namespaces to work right when you create elements in the DOM. I didn't know enough about XML namespaces to be sure I knew what I was doing when I tinkered with it...
Yes it is tricky - which is why (I think) the LTK uses jquery for a lot of that work as it wraps a lot of the quirks in a common interface.
Avatar
Avatar
bouncing
Is this really all you need? # this is mandatory to display svg properly if tag == "svg": node.setAttribute("xmlns", _svg_ns)
If you look in the code for that demo - you wil see an svg.py file which helps with doing all that and a to_dom method which does the Namespace stuff f you need to add SVG live vs as innerHTML. So for dynamic changes vs build it once and use it (not changing or adding anything). I find I always need to add stuff dynamically and that most frameworks fail at that point. Don't forget you can also look at the ltk source (edited)
Avatar
Avatar
Neon22
If you look in the code for that demo - you wil see an svg.py file which helps with doing all that and a to_dom method which does the Namespace stuff f you need to add SVG live vs as innerHTML. So for dynamic changes vs build it once and use it (not changing or adding anything). I find I always need to add stuff dynamically and that most frameworks fail at that point. Don't forget you can also look at the ltk source (edited)
Thanks, I actually didn't know about LTK. It looks like it's a PyScript project, but not mentioned in the docs. It has some similar goals to what I'm doing. I'll definitely be looking at its code.
Avatar
Avatar
bouncing
Thanks, I actually didn't know about LTK. It looks like it's a PyScript project, but not mentioned in the docs. It has some similar goals to what I'm doing. I'll definitely be looking at its code.
It was originally (and still is) by Chris Laffra. But the pyscript group has added it to their repo in some fashion. Probably because it works so well. Its a great middle step to a reactive UI and the code I wrot eto make my stuff work is very hacky and could be designed better if I knew more about how Javascript worked but I'm a python guy with lofty goals ๐Ÿ™‚
bouncing started a thread. 28/05/2024 10:39
Avatar
blackbooks 28/05/2024 23:05
Hi there, i am trying to activate some piece of javascript code by clicking a button but i am not having much luck: so i have table i want to use the datatable plugin on, this is my script: <script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.13.1/js/jquery.dataTables.js"></script> <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.13.1/css/jquery.dataTables.css"> <script id="activate_datatable" type="text/javascript"> function activate_datatable(){ $('#results_table').DataTable(); } </script> and this my function in pyscript: def activate_datatable(event): output_div = document.querySelector('#datatable_ok') script_content = document.getElementById('activate_datatable').text output_div.innerText = script_content window.eval(script_content) the script content gets shown, but window.eval doesnt seem to do anything, no error prompts either?
Avatar
Avatar
blackbooks
Hi there, i am trying to activate some piece of javascript code by clicking a button but i am not having much luck: so i have table i want to use the datatable plugin on, this is my script: <script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.13.1/js/jquery.dataTables.js"></script> <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.13.1/css/jquery.dataTables.css"> <script id="activate_datatable" type="text/javascript"> function activate_datatable(){ $('#results_table').DataTable(); } </script> and this my function in pyscript: def activate_datatable(event): output_div = document.querySelector('#datatable_ok') script_content = document.getElementById('activate_datatable').text output_div.innerText = script_content window.eval(script_content) the script content gets shown, but window.eval doesnt seem to do anything, no error prompts either?
Andrea Giammarchi 29/05/2024 10:35
you are just evaluating a function, you are not invoking it ... but you don't need neither approaches, you can just from pyscript import window and then window.activate_datatable()
10:35
that function is global there
Avatar
Why some packages get installed in type="py" but not type="mpy"?
Avatar
Function print goes to terminal.How can I display function print on DOM?
Avatar
Avatar
zobi
Why some packages get installed in type="py" but not type="mpy"?
Andrea Giammarchi 30/05/2024 08:36
two different worlds, interpreters, languages and most modules won't work out of the box in both projects.
๐Ÿ‘ 1
Avatar
Avatar
zobi
Function print goes to terminal.How can I display function print on DOM?
Andrea Giammarchi 30/05/2024 08:37
you return the value, you don't print it
๐Ÿ‘ 2
08:37
print is by design ends up in the terminal, that's the whole purpose of the terminal (edited)
Avatar
Avatar
Andrea Giammarchi
you are just evaluating a function, you are not invoking it ... but you don't need neither approaches, you can just from pyscript import window and then window.activate_datatable()
blackbooks 30/05/2024 10:55
That works rather well, thank you.
Avatar
hey, does technical documentation in sort of the style of the python docs exist? Like, if I go here https://docs.python.org/3/library/time.html I can see all the functions of the time library. Specifically, I want to see something like this for pydom. In the pyscript docs, it refers you to the user guide, but I can't seem to find anything this detailed. https://docs.pyscript.net/2024.5.2/user-guide/dom/ for example doesn't list the .text property of Elements. Thanks in advance (edited)
This module provides various time-related functions. For related functionality, see also the datetime and calendar modules. Although this module is always available, not all functions are available...
Avatar
Avatar
ableman
hey, does technical documentation in sort of the style of the python docs exist? Like, if I go here https://docs.python.org/3/library/time.html I can see all the functions of the time library. Specifically, I want to see something like this for pydom. In the pyscript docs, it refers you to the user guide, but I can't seem to find anything this detailed. https://docs.pyscript.net/2024.5.2/user-guide/dom/ for example doesn't list the .text property of Elements. Thanks in advance (edited)
This is ongoing work... @Fabio is working on it and we should have something more concrete very soon. ๐Ÿ‘
Avatar
Avatar
ntoll
This is ongoing work... @Fabio is working on it and we should have something more concrete very soon. ๐Ÿ‘
Yes. Trying to make time to merge that work, that has been basically for forever... I've also added a docs PR. With that said, the ask seems more around having reference docs for the pydom module and we don't really have reference docs for the pyscript and pyweb modules. Imho, we should add them.
Avatar
Yeah, reference docs sounds right. I also vote for adding them :).
16:20
Eventually, when we get pydom and pyweb docs, these should probably move from the user guide and be put into a top level "API" section of the docs.
16:20
(or something, to be confirmed after chatting with folks)
Avatar
Is there a protocol for making a PR for docs if I have an idea for a change? should I make an issue first? should I just make a fork and do it? (edited)
Avatar
specifically, I want to make a PR to remove a bunch of, IMO unhelpful, filler words a la https://justsimply.dev/
Avatar
Epic_Ginger 02/06/2024 22:16
Can anyone help me set up Pyscript? Every video tutorial shows a different screen than I'm getting when searching up pie script.com
Avatar
Avatar
Epic_Ginger
Can anyone help me set up Pyscript? Every video tutorial shows a different screen than I'm getting when searching up pie script.com
just go to https://pyscript.com/ and start an account. Then clone/copy an example and away you go.
Avatar
Epic_Ginger 02/06/2024 23:40
@Neon22 Oh ok, that makes more sense. Thank you so much! This is due Tuesday so I may be in a bit of a time crunch ๐Ÿ˜…
Avatar
Traceback (most recent call last): File "/lib/python3.10/_pyodide/_base.py", line 460, in eval_code .run(globals, locals) File "/lib/python3.10/_pyodide/_base.py", line 306, in run coroutine = eval(self.code, globals, locals) File "<exec>", line 3, in <module> ModuleNotFoundError: No module named 'discord' I'm getting this error trying to import discord. In the TOML file, there is packages = ["discord.py"] (also tried with packages = ["discord"] but same issue). So does it not support Discord bots? I want the user to click a button to send a message to my Discord account with a Bot
08:41
Or is there another way to send me a message (somewhat) that a user clicked the button?
Avatar
Also, here: https://docs.pyscript.net/2024.3.1/user-guide/builtins/ it says Pyscript has a fetch function, but when I import it, this error occurs: ImportError: cannot import name 'fetch' from 'pyscript' (/home/pyodide/pyscript/__init__.py)
Avatar
@Rito Pit make sure you're using the latest version of PyScript (check the <script> tags in your head of the HTML file... latest version is 2024.5.2 although we expect to cut a new release sometime mid-week). (edited)
Avatar
I do. So now I have: A button: <button class="guildenter" py-click="join()">Unisciti alla Gilda</button> My main.py file: from pyscript import fetch async def join(): payload = { "content": "THE_MESSAGE" } headers = { "Authorization": "MY_BOT'S_TOKEN" } await fetch( "MY_URL", method="POST", headers=headers, body="Utente registrato" ) When I click the button, which appears, nothing happens
09:43
Also <head> has: <link rel="stylesheet" href="https://pyscript.net/releases/2024.5.2/pyscript.css" /> <link rel="stylesheet" href="elements.css"> <script defer src="https://pyscript.net/releases/2024.5.2/pyscript.js"></script>
Avatar
I seriously need help
09:56
New project, just adding to the default code: <button py-click="find()">Fetch</button> in HTML def find(): print("found") in main.py Now the button seems perma-clicked and can't be clicked
09:59
I tried with find, find(), main.find, main.find(), nothing at all works
09:59
@ntoll
09:59
Sorry for the ping
10:00
can you link to this project please, just so I can take a look..?
Avatar
FWIW, it should just be py-click="find"
Avatar
As you can see it's just a blank, test project, with just that button in it
Avatar
Yup. So I have it working.
๐Ÿ™ 1
10:03
You need to do two things:
10:04
1. Put the button after the reference to the Python script, otherwise the function you call in the button doesn't exist when the button is encountered.
10:04
2. The event handler takes a single argument (representing the event). So your function should be def find(event): (or something like it).
Avatar
Hmm
10:05
I did but the button still isn't clickable
Avatar
Also, remove the terminal
10:05
You can see the output of print in the browser console.
Avatar
I see, thank you, now I'll try writing the POST function within this new project and I'll let you know if it works
Avatar
๐Ÿ‘ Please do. And best of luck!
Avatar
Avatar
ntoll
๐Ÿ‘ Please do. And best of luck!
Error 400 bad request. I used: payload = { "content": "Utente registrato" } headers = { "Authorization": "MY TOKEN" } res = await fetch( "LINK TO THE DISCORD CHANNEL", method="POST", headers=headers, payload=payload ) print(res)
Avatar
Right, what was the message returned with the 400 response (if any).
10:21
Remember, you're requesting from a browser, not requests. So all the browser CORS sandboxing and isolation limitations come into play (they're not there when using requests). (edited)
Avatar
Avatar
ntoll
Right, what was the message returned with the 400 response (if any).
Just OST <MY URL> 400 (Bad Request)
Avatar
OK. And if you do the same thing with requests in normal Python..?
10:23
Also, the response headers might help too.
10:23
\400 response headers.
Avatar
Avatar
ntoll
OK. And if you do the same thing with requests in normal Python..?
This works just fine, it sends the message properly
Avatar
Right. So I suspect it's the CORS problem, explained in the docs link I pasted above.
10:26
If you're using PyScript.com you may need to use the API proxy service for this sort of request.
10:26
There's literally nothing PyScript can do since this sort of CORS related behaviour is built into the browser, and we can't change it.
Avatar
Do you know any other way to have a message sent with other services (email and so on)?
Avatar
Avatar
ntoll
If you're using PyScript.com you may need to use the API proxy service for this sort of request.
Because it seems Discord won't allow me to, I tried importing the discord.py module but Pyscript doesn't find it (edited)
Avatar
I'm not sure what you mean. Import discord from where?
Avatar
In main.py, import discord, and in the TOML file, packages = ["discord.py"]
Avatar
Right... but from where do you get discord.py..?
Avatar
A Python wrapper for the Discord API
10:35
Locally I pip install it
Avatar
Right, but it still won't work properly because of the CORS issue.
Avatar
I see
Avatar
We can't do anything about it. The browser is in charge when we make outgoing HTTP requests, and it enforces CORS security.
10:37
The best way to get around it is to use the API proxy built into PyScript.com.
10:38
@martin @FabioRosado is the API proxy documented anywhere, so @Rito Pit can go take a look?
Avatar
Avatar
ntoll
We can't do anything about it. The browser is in charge when we make outgoing HTTP requests, and it enforces CORS security.
Any other service for sending a message/email that doesn't have CORS?
10:52
btw when I try my bot's token, the error is 401, when I try mine, the error is 400
10:52
(I won't do self botting or the sort, it's just a test in my private server's channel) (edited)
Avatar
@Rito Pit I'm not sure. It just depends upon the way the server to which you're sending a message is configured.
Avatar
Hello, is it possible to set a dark theme for the pyeditor ?
Avatar
Avatar
ntoll
@martin @FabioRosado is the API proxy documented anywhere, so @Rito Pit can go take a look?
FabioRosado 03/06/2024 12:21
We don't have it documented yet, we will be working on it this week
๐Ÿ‘ 2
๐Ÿ‡น 1
๐Ÿ‡พ 1
Avatar
@m28 in the next release of PyScript you should be able to get a reference to the editor instance on the DOM and be able to change the style in that way.
Avatar
Is it possible to store data persistently on a json or txt file in your Pyscript editor?
13:45
So that one can see the full list of users whose nickname is on the file
Avatar
So... with os.listdir() printing results in console it seems my db.json is there, but when I try to access it, it says No such file or directory
Avatar
Avatar
Rito Pit
So... with os.listdir() printing results in console it seems my db.json is there, but when I try to access it, it says No such file or directory
Jeff Glass 03/06/2024 14:06
Can you share a code snippet of how you're accessing the db?
Avatar
Avatar
Jeff Glass
Can you share a code snippet of how you're accessing the db?
Sure: for file in os.listdir(): print(file) jsonfile = f"/db.json" print(jsonfile) with open(jsonfile, "r") as f: db = json.load(f) db["partecipanti"].append("Master di <Servant_class>") with open(jsonfile, "w") as f: json.dump(db, f)
Avatar
Avatar
Rito Pit
Sure: for file in os.listdir(): print(file) jsonfile = f"/db.json" print(jsonfile) with open(jsonfile, "r") as f: db = json.load(f) db["partecipanti"].append("Master di <Servant_class>") with open(jsonfile, "w") as f: json.dump(db, f)
Jeff Glass 03/06/2024 15:53
Shouldn't it be "./db.json?"
15:53
Otherwise you're looking for the file in the root directory
Avatar
Avatar
Jeff Glass
Shouldn't it be "./db.json?"
Got the same error
Avatar
Jeff Glass 03/06/2024 15:54
Huh....
Avatar
I tried with: db.json, ./db.json, /db,json
15:55
I want the json to be that of the server so that users can sign their username there
15:56
If there is any other way apart json I'll switch to that one
Avatar
Jeff Glass 03/06/2024 15:56
Writing to that JSON file won't automatically update it on the server, it will just update the local in-browser-memory copy...
Avatar
Avatar
Jeff Glass
Writing to that JSON file won't automatically update it on the server, it will just update the local in-browser-memory copy...
Oh, I see, so I won't go for that
๐Ÿ‘ 1
Avatar
Avatar
Rito Pit
Oh, I see, so I won't go for that
Jeff Glass 03/06/2024 15:56
Still odd it wouldn't let you open the file, to be honest
Avatar
Yeah it's listed there but it won't open
Avatar
Jeff Glass 03/06/2024 15:57
How did you write that file?
Avatar
It's on the same directory on the server (pyscript.com site) as main.py
Avatar
Jeff Glass 03/06/2024 15:58
In the file browser there, is the file called db.json or literally /db.json?
15:58
(Slash if you're comfortable, can you share the pyscript.com project? Either to the server or DM me?)
15:59
It sounds like we could have either a PyScript bug or a PyScript.com bug... or something else
Avatar
Jeff Glass 03/06/2024 16:03
Ah, you'll want to add this to pyscript.toml:
16:03
[files] "db.json" = "db.json"
16:04
Basically, the files on the left-hand panel are the files that are present on the server, but PyScript still needs to load them into its virtual file system on the client side so Python knows how to find this. This is what the [files] key does. https://docs.pyscript.net/2024.5.2/user-guide/configuration/#files
Avatar
Thank you. That said, of course the file itself on the server hasn't changed, as you were telling me, so I was wondering, is there any way of writing data to the server?
Avatar
Jeff Glass 03/06/2024 16:09
On PyScript.Com specifically... I don't think there is. But it would be worth asking in the #โ•ฐโ•ธpyscript-dot-com channel where their devs lurk - PSDC adds some functionality on top of open source PyScript and I'm not sure familiar with all of it
Avatar
Avatar
ableman
Is there a protocol for making a PR for docs if I have an idea for a change? should I make an issue first? should I just make a fork and do it? (edited)
Hey hey... sorry... missed this question. Please create an issue! ๐Ÿ‘ All feedback and contributions most welcome!
Avatar
Random question: Is there a way to programmatically do something similar to js_modules in the config? I want to load a javascript module dynamically at runtime without specifying it upfront in the config.
Avatar
@Andrea Giammarchi ^^^
14:41
@bouncing with the next release of PyScript, you'll be able to do it with Python modules. (I realise that doesn't answer your question, but, you know, it's cool). ๐Ÿ™‚
Avatar
Well that'll be nice too actually
14:42
I can think of some hacky ways of injecting javascript live at runtime, but I'm hoping for a not-hacky way
Avatar
Best talk to web yoda @Andrea Giammarchi about that. He's all over not-hacky ways to get stuff done. ๐Ÿ˜„
Avatar
@ntoll thanks!
14:47
I'll await the web yoda
๐Ÿคฃ 1
Avatar
Avatar
bouncing
Random question: Is there a way to programmatically do something similar to js_modules in the config? I want to load a javascript module dynamically at runtime without specifying it upfront in the config.
Andrea Giammarchi 04/06/2024 15:16
sorry I've misssed this ... in pyodide you can import js and then await js.import_("module.js") but AFAIK micropython doesn't have the same convention for reserved words ... edit ... actually that doesn't work anymore in pyodide neither (edited)
15:19
so the easiest way is to have <script>const dynamicImport = name => import(name)</script> on the page and then: from pyscript import window module = await window.dynamicImport("module.js")
Avatar
hmmmm
Avatar
Andrea Giammarchi 04/06/2024 15:22
import and from are reserved words in Python so Array.from_ (IIRC) is what Pyodide came up as solution (imho a bit ugly but it works) but there's no import_ counterpart on the global context.
Avatar
Avatar
bouncing
hmmmm
Andrea Giammarchi 04/06/2024 15:24
although ... why do you need that? with worker attribute all JS modules are loaded lazily, only when you import these, so you can have thousand of modules in your config and, as long as that's a worker PyScript executing it, you won't see any difference. On the main though, we can't make it lazy so on main (no worker attribute) you need that workaround
Avatar
The reason I want to do it is that I'm writing what's more or less a library, so I don't want to require consumers of the library to do all kinds of configuration upfront to use it.
15:26
I want them to add one line, {"files": {...}} and be done
Avatar
Andrea Giammarchi 04/06/2024 15:26
and this library won't have any js module needed in the config? because nothing stops you to have your js module exposing a dynamic import utility ... as long as you don't use import in Python everything is fine.
Avatar
Well, I need to add my first js module, but I'm hoping to avoid requiring that users add it to the config.
Avatar
Andrea Giammarchi 04/06/2024 15:28
on the other hand, your Python code could also do this: from pyscript import window # if you want modules always on main js_import = window.Function('return name => import(name)')() await window.js_import("module.js").then(window.console.log) (edited)
Avatar
Is that better than something like js.eval()?
15:29
though that's a Python keyword...
Avatar
Andrea Giammarchi 04/06/2024 15:30
I don't know, you could use eval too ... eval is contextual so I always try not to use it
15:30
but you got the workaround from python code.
Avatar
makes sense
15:30
thanks for your help!
๐Ÿ‘ 1
Avatar
Jeff Glass 04/06/2024 15:31
Not that you should necessarily use JS eval, but pyodide.code.run_js is a wrapper around eval that works from Python (edited)
Avatar
Andrea Giammarchi 04/06/2024 15:31
btw, this works too (I think) js_import = window.Function('return name => import(name)')() (edited)
Avatar
@Jeff Glass oh nice
15:34
@Andrea Giammarchi That is a lot cleaner
๐Ÿฅณ 1
Avatar
Andrea Giammarchi 04/06/2024 15:43
maybe one day we can also provide a pyscript.js_import too that won't need eval or Function from our users ... @ntoll thoughts?
15:44
actually we could have same API for pyscript.py_import(one, or_more, module) and pyscript.js_import(one, or_more, module) that both resolve asynchronously as modules tuple
15:44
will ask about it during the call
Avatar
That would be handy.
15:54
especially if it could take URLs that are on the webassembly filesystem.
15:55
If you import ./whatever.js in the context of JavaScript's runtime, it imports it relative to wherever PyScript was served from, not relative to your psuedo filesystem.
Avatar
Avatar
bouncing
especially if it could take URLs that are on the webassembly filesystem.
Andrea Giammarchi 04/06/2024 16:03
you don't need that to import in python ... the import is for packages and .whl files in python, the JS import can't be in the vFS as that vFS is not reachable through JS (edited)
16:03
also to put those in the vFS in the first place these need to be part of the config files so in that case I am not sure why one wouldn't just use js_modules ... but maybe we're crossing topics here ๐Ÿค”
Avatar
I guess we are, yeah
16:16
I would use js_modules, if there were a way to bundle all my requirements up in one. So project a can do something like... { "import_config": "my_project.json" } Then in my_project.json, I could specify js_modules.
16:16
The goal was really just to require only one entry in the files section to be able to use the project -- not that important, just a nicety.
Avatar
Avatar
bouncing
The goal was really just to require only one entry in the files section to be able to use the project -- not that important, just a nicety.
Andrea Giammarchi 04/06/2024 16:26
I am still not sure I am following ... once you have dynamic import exposed, instead of needing eval like workaround, are you OK for the JS side of affairs? (edited)
Avatar
Yeah, you answered my questions for sure. I'm just musing that it would be nice to sort of "merge" or "include" PyScript configs, which would have negated the need to begin with.
16:28
It's essentially a problem of dependency management. If you add my package as a dependency, I'd like that to be enough; no need to also add js_modules that my package depends on. But it's not the end of the world.
16:30
Anyway, I don't mean to go down a rabbit hole with that... Thanks again for your help
Avatar
I'm trying to get a simple page working w/ a MicroPython REPL. I'm able to get output from code printed out in a terminal div, and can get input prompts and such. But I'd like a full REPL so I can explore the usage of various functions interactively. I see the MicroPython REPL example from the MicroPython Technical Preview, but it's pretty old and lots has changed since that was released. Getting a Pyodide REPL is straightforward enough, but are there any good examples of how to get a MicroPython REPL going?
Avatar
@jburnett it's all explained here: https://docs.pyscript.net/2024.5.2/user-guide/terminal/ See especially the section about: import code code.interact() Finally, we're making a new release of PyScript later today (this evening Euro-time). It'll have a major update to the MicroPython REPL, along with more docs (once released). All feedback most welcome. ๐Ÿ‘
Avatar
Ah, that works now! I had tried using that earlier, but I think I was referencing an earlier version of PyScript that didn't support code.interact(). Just tried it w/ 2024.5.2 and it works like a charm. Thanks!
12:23
And I'm definitely interested to see the other improvements coming!
Avatar
blackbooks 07/06/2024 09:54
I have a problem regarding the certificates when usying PyScript. I am sending data from the pyscript environment to a data collection server (REDCap) using the PyCap library as an abstraction layer to the REDCap-API. I can send data and also receive it, however i get InsecureRequestWarning. Prompting the Requests from my local system is not a problem. So it seems to me that PyScript does not get access to the certificate Pool of the Browser. Is there a way to configure that? Hard to imagine i am the first stumbling over this.
Avatar
elliot0x01 08/06/2024 11:04
mini coi stopped working suddenly ? ๐Ÿค”
Avatar
elliot0x01 08/06/2024 11:54
Probably something on my website broke with the latest update ๐Ÿค”
Avatar
elliot0x01 08/06/2024 12:08
Ahhh, input() seems to be broken with the latest release ? (edited)
Avatar
I'm running into two issues when trying to provide an environment config in a simple test app. I get an error when I try to provide a config file for any MicroPython script, and I also get an error when I try to provide a config for any worker, regardless of if it's using Python or MicroPython. The test script just has a call to print() so it's not interacting w/ the UI. For now it's not actually using anything loaded from the config file, to remove that as a possible source of issues. It works fine when: 1. Using MicroPython not in a worker (with no config) 2. Using Pyodide not in a worker, with the config 3. Using Pyodide in a worker but not providing the config option But whenever I combine MicroPython or a Pyodide worker with a config option, I get this error in the console: (edited)
16:08
(Expanded the first error)
Avatar
Ah, found the problem. I was using an example .toml file from an example project, and the file format seems to have changed a little. Changing the TOML file to the apparently newer format works in the cases I listed above as problems.
16:27
Just odd that the older format still worked in some specific cases, but not in others
Avatar
Phelsong (Josh) 10/06/2024 15:47
Did you guys really just yoink pyweb/pydom and not even leave a footnote? amegablobsweats (apparently its next build) (edited)
Avatar
What is the best way to get code completion for pyscript in visual studio code. Are there type stubs for pyscript or is it best to just have a local copy installed?
Avatar
Phelsong (Josh) 10/06/2024 15:59
copy pyscript/pyscript.core/src/stdlib/pyscript/ to the root of your project
๐Ÿ˜ฎ 1
16:01
Avatar
Avatar
Phelsong (Josh)
copy pyscript/pyscript.core/src/stdlib/pyscript/ to the root of your project
Thank you ๐Ÿ‘Œ
Avatar
If you want to script this, you can do something like git clone -q https://github.com/pyscript/pyscript.git tmp-pyscript && \ cp -r tmp-pyscript/pyscript.core/src/stdlib/pyscript ./pyscript && \ rm -rf tmp-pyscript
Avatar
This approach falls apart quite quickly though, if you do e.g. from pyscript import document element = document.getElementById("some_elem") element.innerHTML = "LOL" you will not get any type hints on the getElementById function or the element variable itself, for that you'd need a custom .pyi file or something (edited)
Avatar
Phelsong (Josh) 11/06/2024 19:37
main is also not the same as release. like right now, pyweb now pyscript.web
Avatar
Hi all, I have this template in my HTML: <template id="jinja-template"> <div>{{ name }}</div> </template> My intention is to extract <div>{{ name }}</div> out of it. Here is my code: from pyscript import document template_element = document.getElementById("jinja-template") template_content = template_element.innerHTML print(f'jinja_template_str => {template_content}') but this returns <div></div> (i.e. gets rid of the {{ name }} placeholder) Does anyone know how i can get the raw value with the placeholder? Many thanks in advance.
Avatar
Avatar
arvapo
Hi all, I have this template in my HTML: <template id="jinja-template"> <div>{{ name }}</div> </template> My intention is to extract <div>{{ name }}</div> out of it. Here is my code: from pyscript import document template_element = document.getElementById("jinja-template") template_content = template_element.innerHTML print(f'jinja_template_str => {template_content}') but this returns <div></div> (i.e. gets rid of the {{ name }} placeholder) Does anyone know how i can get the raw value with the placeholder? Many thanks in advance.
Andrea Giammarchi 12/06/2024 08:31
I can't reproduce it ... no issue whatsoever ... which version of PyScript are you using?
08:32
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="https://pyscript.net/releases/2024.6.1/core.css"> <script type="module" src="https://pyscript.net/releases/2024.6.1/core.js"></script> <script type="mpy"> from pyscript import document template_element = document.getElementById("jinja-template") template_content = template_element.innerHTML print(f'jinja_template_str => {template_content}') </script> </head> <body> <template id="jinja-template"> <div>{{ name }}</div> </template> </body> </html>
๐Ÿ‘ 1
08:33
this works to me ... if your jinga is pre-processing that output before landng on the HTML page then it's not about PyScript at all
Avatar
Avatar
Andrea Giammarchi
I can't reproduce it ... no issue whatsoever ... which version of PyScript are you using?
I am using 2024.5.2
Avatar
Avatar
Andrea Giammarchi
this works to me ... if your jinga is pre-processing that output before landng on the HTML page then it's not about PyScript at all
Ah yeah, I think you are right. When i save your code in a .html file and open it directly in Chrome it works, but when i serve the same html with Flask, it doesn't. I think Flask is doing the pre-processing you are talking about because it also uses jinja! ๐Ÿค” My intention is to deliver the raw template to the browser and get pyscript to do the jinja template rendering. I might need to find a way around Flask rendering the template on the server side.
Avatar
Avatar
arvapo
Ah yeah, I think you are right. When i save your code in a .html file and open it directly in Chrome it works, but when i serve the same html with Flask, it doesn't. I think Flask is doing the pre-processing you are talking about because it also uses jinja! ๐Ÿค” My intention is to deliver the raw template to the browser and get pyscript to do the jinja template rendering. I might need to find a way around Flask rendering the template on the server side.
blackbooks 12/06/2024 10:55
I did this recently, i can provide the Code if you can wait and/or remind me . Earliest is in 8 hours.
Avatar
Avatar
arvapo
Ah yeah, I think you are right. When i save your code in a .html file and open it directly in Chrome it works, but when i serve the same html with Flask, it doesn't. I think Flask is doing the pre-processing you are talking about because it also uses jinja! ๐Ÿค” My intention is to deliver the raw template to the browser and get pyscript to do the jinja template rendering. I might need to find a way around Flask rendering the template on the server side.
Andrea Giammarchi 12/06/2024 11:32
I added a facebook button to my page by copying/pasting the code they supply on their website. It looks like this: "http://www.facebook.com/dialog/feed?app_id={{fbapp_id}}&link={{link_url}}...
โค๏ธ 1
Avatar
Avatar
Andrea Giammarchi
Yep. It certainly does help. This is exactly what I need to do. Thank you very much ๐Ÿ‘
๐Ÿ‘ 1
Avatar
Avatar
blackbooks
I did this recently, i can provide the Code if you can wait and/or remind me . Earliest is in 8 hours.
Yeah. Absolutely. I would like to see your approach too. No rush though so whenever you can. Thanks mate.
Avatar
hi team, under an older version of pyscript I was able to use pyfetch to fetch json data from a url. How do we do this in the newer version using <script type="module" src="https://pyscript.net/releases/2023.11.1/core.js"></script> am confused with urequests in micropython and looking for any working http request solution, thanks in advance
16:54
Works the same way in Pyodide and MicroPython. It's just a shim around the browser's own fetch API.
Avatar
Avatar
arvapo
Yeah. Absolutely. I would like to see your approach too. No rush though so whenever you can. Thanks mate.
blackbooks 12/06/2024 16:57
here is how i setup jinja in the pyscript environment: first load jinja into your environment and create a template folder to be copied into the environment along with some html templates { "packages": [ "Jinja2" ], "files": { "{STATIC_FOLDER}": "http://localhost:5000/wizard/pyscript/static/", "{TEMPLATES}": "templates", "{STATIC_FOLDER}/jinja.py": "jinja.py", "{STATIC_FOLDER}{TEMPLATES}/query_results.html": "./{TEMPLATES}/query_results.html", "{STATIC_FOLDER}{TEMPLATES}/redcap_reroute.html": "./{TEMPLATES}/redcap_reroute.html" } } then in my jinja.py i configure functions to setup the jinja2 environment: import os from jinja2 import Environment, FileSystemLoader, select_autoescape def setup_jinja_env(): root_dir = os.path.dirname(__file__) template_folder = os.path.join(root_dir, 'templates') new_env = Environment( loader=FileSystemLoader(template_folder), autoescape=select_autoescape(["html", "xml"]), ) return new_env def _render_template(template_name_or_list, jinja_env, **context): template = jinja_env.get_template(template_name_or_list) return template.render(**context) and finally use them at the top of my main.py file: from jinja import setup_jinja_env, _render_template jinja_env = setup_jinja_env() def render_template(template_name_or_list, **context): return _render_template(template_name_or_list, jinja_env, **context) Now i can use the templates similiar to how i use them in flask. Keep in mind though, that you don't have access to all the flask environment variables you usually can access in the templates like current_user but only the context you have explicitly passed to the template. Hope that helps (edited)
๐Ÿ‘ 1
Avatar
Avatar
blackbooks
here is how i setup jinja in the pyscript environment: first load jinja into your environment and create a template folder to be copied into the environment along with some html templates { "packages": [ "Jinja2" ], "files": { "{STATIC_FOLDER}": "http://localhost:5000/wizard/pyscript/static/", "{TEMPLATES}": "templates", "{STATIC_FOLDER}/jinja.py": "jinja.py", "{STATIC_FOLDER}{TEMPLATES}/query_results.html": "./{TEMPLATES}/query_results.html", "{STATIC_FOLDER}{TEMPLATES}/redcap_reroute.html": "./{TEMPLATES}/redcap_reroute.html" } } then in my jinja.py i configure functions to setup the jinja2 environment: import os from jinja2 import Environment, FileSystemLoader, select_autoescape def setup_jinja_env(): root_dir = os.path.dirname(__file__) template_folder = os.path.join(root_dir, 'templates') new_env = Environment( loader=FileSystemLoader(template_folder), autoescape=select_autoescape(["html", "xml"]), ) return new_env def _render_template(template_name_or_list, jinja_env, **context): template = jinja_env.get_template(template_name_or_list) return template.render(**context) and finally use them at the top of my main.py file: from jinja import setup_jinja_env, _render_template jinja_env = setup_jinja_env() def render_template(template_name_or_list, **context): return _render_template(template_name_or_list, jinja_env, **context) Now i can use the templates similiar to how i use them in flask. Keep in mind though, that you don't have access to all the flask environment variables you usually can access in the templates like current_user but only the context you have explicitly passed to the template. Hope that helps (edited)
Thanks for taking the time to share this. Cheers
Avatar
streetlight 13/06/2024 07:44
Hi everyone, this is my first time trying PyScript While I follow exactly the tutorial in Beginner Guide (Polyglot project https://docs.pyscript.net/2024.6.1/beginning-pyscript/), With correct file naming and running the website withhttp-server at localhost://8000 I still encounter error, my website doesnt function (edited)
07:45
It will start loading with an inactive button and uneditable input (edited)
07:45
After clicking it the website will crash completely
07:50
May I know how to fix this? Any help would be appreciated.
Avatar
๐Ÿ‘‹ @pocoo Hmmm.... lemme see if I can reproduce. It'd be helpful to know the OS and browser you're using.
Avatar
@streetlight I'm on Linux, using Firefox (also tested with Chrome). It works for me. Context like OS and browser would be very helpful. Also, any output in the browser console would be welcome too. Finally, do you have any plugins or add-ons to your browser that could be interfering..? Just want to make sure I've covered all the bases here. TL;DR - I want to try to reproduce your problem... then I can dig. ๐Ÿ‘
11:26
Also, you mention you're serving with http-server - I'm guessing you mean python -m http.server ..? If so, that's exactly the same setup as me.
Avatar
Avatar
streetlight
Hi everyone, this is my first time trying PyScript While I follow exactly the tutorial in Beginner Guide (Polyglot project https://docs.pyscript.net/2024.6.1/beginning-pyscript/), With correct file naming and running the website withhttp-server at localhost://8000 I still encounter error, my website doesnt function (edited)
try this server instead https://github.com/python/cpython/blob/main/Tools/wasm/wasm_webserver.py if you also host the wasm files (edited)
Avatar
hi team, when I run pyfetch using an older version of pyscript my bokeh code works but when running under the newest version I get a runtime coroutine error. I have tried awaiting my function but still cannot get the syntax right. Thanks for your help. Please see my code at this link, https://rickd.s3.us-east-2.amazonaws.com/pyfetch+example.txt
Avatar
Avatar
Rick
hi team, when I run pyfetch using an older version of pyscript my bokeh code works but when running under the newest version I get a runtime coroutine error. I have tried awaiting my function but still cannot get the syntax right. Thanks for your help. Please see my code at this link, https://rickd.s3.us-east-2.amazonaws.com/pyfetch+example.txt
Andrea Giammarchi 13/06/2024 15:28
I did update everything in your example and this works for me: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>get json from s3</title> <link rel="stylesheet" href="https://pyscript.net/releases/2024.6.1/core.css"> <script type="module" src="https://pyscript.net/releases/2024.6.1/core.js"></script> <script src="https://cdn.bokeh.org/bokeh/release/bokeh-3.4.1.min.js"></script> <script src="https://cdn.bokeh.org/bokeh/release/bokeh-gl-3.4.1.min.js"></script> <script src="https://cdn.bokeh.org/bokeh/release/bokeh-widgets-3.4.1.min.js"></script> <script src="https://cdn.bokeh.org/bokeh/release/bokeh-tables-3.4.1.min.js"></script> <script src="https://cdn.bokeh.org/bokeh/release/bokeh-mathjax-3.4.1.min.js"></script> </head> <body> <p>getting python code and py fetching remote AWS S3 json data...</p> <div id="chart"></div> <py-config>packages = ["bokeh"]</py-config> <script type="py" async> from bokeh.plotting import figure from bokeh.embed import json_item from pyscript import window, fetch, ffi Bokeh = window.Bokeh async def get_data(): data = await fetch('https://rickd.s3.us-east-2.amazonaws.com/04.json').json() xAxis=[i['time'] for i in data] #grab s3 data using loop yAxis=[i['Quantity'] for i in data] #grab s3 data using loop p = figure(x_range=(0, 10), y_range=(0, 10), width = 700, height = 600) p.line(xAxis, yAxis, legend_label="data from S3", line_width=2) await Bokeh.embed.embed_item(ffi.to_js(json_item(p, "chart"))) await get_data() </script> </body> </html> (edited)
15:29
There you have latest PyScript and latest Bokeh and I think you don't need the ugly json back and forward thing anymore with our ffi
Avatar
Andrea Giammarchi 13/06/2024 15:45
btw, if you are wondering what is that from pyscript import window, fetch, ffi Bokeh = window.Bokeh about, it's a way to retrieve Bokeh from the main thread, no matter if you add worker to the <script type="py"> which I also suggest at it bootstraps without ever blocking the main thread (edited as the time seems to be similar in both cases, just measured) (edited)
Avatar
Avatar
Andrea Giammarchi
btw, if you are wondering what is that from pyscript import window, fetch, ffi Bokeh = window.Bokeh about, it's a way to retrieve Bokeh from the main thread, no matter if you add worker to the <script type="py"> which I also suggest at it bootstraps without ever blocking the main thread (edited as the time seems to be similar in both cases, just measured) (edited)
wow, thanks, I can understand ffi and fetch vs pyfetch but still trying to wrap my head around window.Bokeh, need to understand that one better. Also Fyi, this code does not work using type="mpy" in script tag as it cannot find module named bokeh
Avatar
Avatar
Rick
wow, thanks, I can understand ffi and fetch vs pyfetch but still trying to wrap my head around window.Bokeh, need to understand that one better. Also Fyi, this code does not work using type="mpy" in script tag as it cannot find module named bokeh
i fear bokeh is only made for cpython not micropython (mpy) (edited)
๐Ÿ‘ 1
Avatar
Avatar
Rick
wow, thanks, I can understand ffi and fetch vs pyfetch but still trying to wrap my head around window.Bokeh, need to understand that one better. Also Fyi, this code does not work using type="mpy" in script tag as it cannot find module named bokeh
Andrea Giammarchi 13/06/2024 16:35
pyodide packages usually don't work outof the box in MicroPython, they need to be pure Python wheels but also you need to provide <mpy-config> instead of <py-config> as multiple interpreters can run on the same page and should not interfere with each other setup
16:37
the window on the main thread is just an indirection for js ... basically import js and from pyscript import window on the main are the same thing but if you use a worker attribute and you land JS code on the page import js will give you the worker global context, not the main thread one, so you won't find any Bokeh in there at all as that's on the main page. from pyscript import window in there will still give you full access to the main thread even from a worker, so it's always the right global main reference to use and it makes code portable between main and workers.
Avatar
Avatar
Andrea Giammarchi
pyodide packages usually don't work outof the box in MicroPython, they need to be pure Python wheels but also you need to provide <mpy-config> instead of <py-config> as multiple interpreters can run on the same page and should not interfere with each other setup
hey thanks for the easier explanation and the reference re pyscript.window, now I get it !
Avatar
Avatar
pmp-p
i fear bokeh is only made for cpython not micropython (mpy) (edited)
I was hoping to use mpy after reading Jeff Glass' recent article which stated that mpy is much faster, just fyi <script type="py"> takes much longer to load than the old <py-script> tag in the old version of pyscript (in a bokeh app) (edited)
Avatar
Avatar
Rick
I was hoping to use mpy after reading Jeff Glass' recent article which stated that mpy is much faster, just fyi <script type="py"> takes much longer to load than the old <py-script> tag in the old version of pyscript (in a bokeh app) (edited)
Andrea Giammarchi 13/06/2024 17:33
please don't blame the messanger ... we are using latest Pyodide but polyscript (the WASM bootstrapper behind the scene) is blazing fast and it's hard to measure it being really any slower than raw loadPyodide when there's no config around, these do almost the same thing. Classic PyScript itself was 5X if not 10X heaveier on the JS side, if not more, but then again it used an older version of Pyodide so it's oranges to apples comparison. (edited)
Avatar
Avatar
Rick
I was hoping to use mpy after reading Jeff Glass' recent article which stated that mpy is much faster, just fyi <script type="py"> takes much longer to load than the old <py-script> tag in the old version of pyscript (in a bokeh app) (edited)
Jeff Glass 13/06/2024 17:34
I think I take it for granted that folks' understand the differences between CPython/Pyodide and Micropython ๐Ÿ˜… They're essentially two separate languages with a lot of the same syntax but very different foundations. In general, packages for one will not work for the other. Pure Python that has minimal-to-no reliance on the standard library may, but beyond that, probably not
โค๏ธ 1
Avatar
Avatar
Rick
I was hoping to use mpy after reading Jeff Glass' recent article which stated that mpy is much faster, just fyi <script type="py"> takes much longer to load than the old <py-script> tag in the old version of pyscript (in a bokeh app) (edited)
Jeff Glass 13/06/2024 17:39
Also, depending on exactly how far back in time you go - Pyodide/PyScript upgraded Bokeh from v 2.x to 3.x with the release of PyScript 2023.3.1, which could also be the source of performance changes compared to much older versions of PyScript
๐Ÿ‘ 1
Avatar
Andrea Giammarchi 13/06/2024 17:40
imagine my hint to solve the issue is on latest 3.4.1 of that library that indeed might have added discrepancies in terms of performance
17:41
my measurements were around 3 seconds to show the graph though, considering the runtime bokeh dependencies to solve too, that ain't even too bad
Avatar
Avatar
Jeff Glass
Also, depending on exactly how far back in time you go - Pyodide/PyScript upgraded Bokeh from v 2.x to 3.x with the release of PyScript 2023.3.1, which could also be the source of performance changes compared to much older versions of PyScript
I am still using bokeh 2.4 and not version 3 because 2.4 is faster
Avatar
Andrea Giammarchi 13/06/2024 17:42
reload page to graph was 10 seconds though ... that's not super cool but a lot happens behind the network scene too
Avatar
Avatar
Jeff Glass
I think I take it for granted that folks' understand the differences between CPython/Pyodide and Micropython ๐Ÿ˜… They're essentially two separate languages with a lot of the same syntax but very different foundations. In general, packages for one will not work for the other. Pure Python that has minimal-to-no reliance on the standard library may, but beyond that, probably not
thanks for weighing in Jeff
Avatar
Avatar
Rick
I am still using bokeh 2.4 and not version 3 because 2.4 is faster
Andrea Giammarchi 13/06/2024 17:43
that version broke my fix though ... it was complaining all over something renderers was not available, I had to switch to its latest and I suggest you keep the pace with this module's updates too
Avatar
Avatar
Rick
thanks for weighing in Jeff
Jeff Glass 13/06/2024 17:44
If you're in the latest version of PyScript and you're pining Bokeh 2.4, you're likely loading straight from PyPI and not from the prebuilt Pyodide wheels, which are pinned to a specific release in each release of PyScript/Pyodide, and all bets are off
Avatar
Avatar
Andrea Giammarchi
that version broke my fix though ... it was complaining all over something renderers was not available, I had to switch to its latest and I suggest you keep the pace with this module's updates too
I do have bokeh version 3 working on an older version of pyscript (edited)
Avatar
Jeff Glass 13/06/2024 17:45
I see your example code... you're loading Bokeh 3 on the Python side but Bokeh 2 on the JS side, since PyScript 2024.1.1 loads Bokeh 3 if unpinned
Avatar
Avatar
Rick
I do have bokeh version 3 working on an older version of pyscript (edited)
Andrea Giammarchi 13/06/2024 17:45
I was talking about your request for help ... that used 2.x something ... but then again, my example works everywhere I could test ๐Ÿฅณ
Avatar
Avatar
Jeff Glass
I see your example code... you're loading Bokeh 3 on the Python side but Bokeh 2 on the JS side, since PyScript 2024.1.1 loads Bokeh 3 if unpinned
Andrea Giammarchi 13/06/2024 17:46
OK, that explains to me a lot
Avatar
Avatar
Andrea Giammarchi
I was talking about your request for help ... that used 2.x something ... but then again, my example works everywhere I could test ๐Ÿฅณ
your example is perfect for my immediate needs, thanks again....mpy is only on my wishlist for now..ps I will be teaching this to my students (edited)
Avatar
Andrea Giammarchi 13/06/2024 17:48
something to keep in mind and document too though ... Pyodide and PyScript updating Pyodide keep going, people with unpinned versions of modules finding themselves unable to run their code after updates is indeed a footgun we should explain in docs (if we haven't done or highlighted that already)
17:48
@ntoll ^^^
Avatar
Jeff Glass 13/06/2024 17:48
That's a very good point!
17:49
I wonder if Bokeh provides their cdn resources as ESM... maybe we can rewrite that example without the additional script tags pointing to bokeh and use js_modules instead
Avatar
Andrea Giammarchi 13/06/2024 17:49
they don't last time I've checked (1 hour ago)
Avatar
Jeff Glass 13/06/2024 17:49
โ˜น๏ธ
17:49
Well dang
Avatar
Avatar
Andrea Giammarchi
something to keep in mind and document too though ... Pyodide and PyScript updating Pyodide keep going, people with unpinned versions of modules finding themselves unable to run their code after updates is indeed a footgun we should explain in docs (if we haven't done or highlighted that already)
yes code gets deprecated fast...I am on it !
Avatar
Andrea Giammarchi 13/06/2024 17:50
people not familiar with JS publishing UMD or CommonJS modules in their own CDNs are surprisingly frequent to me these days .. I keep re-packaging everything, including Emscripten vFS stuff
Avatar
I sure hope nothing happens to Bokeh 2.4
Avatar
Andrea Giammarchi 13/06/2024 17:53
it's sealed in time, that's how versioning works ... so you should have a packages = [...] that points at those pinned version of the module you expect both on Python and JS side
17:54
looking at your text file, that's bokeh@2.4.2
17:54
as that reflects what you land on the main thread
Avatar
Avatar
Andrea Giammarchi
it's sealed in time, that's how versioning works ... so you should have a packages = [...] that points at those pinned version of the module you expect both on Python and JS side
it may be sealed in time but I worry that bokeh gets broken when pyscript changes (edited)
Avatar
Andrea Giammarchi 13/06/2024 17:55
that's not what we are saying ... we don't get to control packages versioning, you need to do so
Avatar
Jeff Glass 13/06/2024 17:55
Yeah.... the trick is Pyodide breaks pinning this way. Each version of Pyodide vendors exactly one pinned version of the packages they distribute. So if you pin Bokeh 3.4.1 in Pyodide 0.26, you get the version from the Pyodide CDN. If Pyodide 0.27 upgrades to Bokeh 3.5 and you upgrade your Pyodide/PyScript version, you'll suddenly be pulling 3.4.1 from PyPI instead, and things break
Avatar
Andrea Giammarchi 13/06/2024 17:55
if you don't pin your packages next pyodide update will use next bokeh and your code might break.
17:55
we really need to add a F.A.Q. around this in our docs!
๐Ÿ‘€ 1
Avatar
Avatar
Andrea Giammarchi
that's not what we are saying ... we don't get to control packages versioning, you need to do so
ok
Avatar
Avatar
Andrea Giammarchi
if you don't pin your packages next pyodide update will use next bokeh and your code might break.
I will be writing an article about this on dev.to (edited)
Avatar
Jeff Glass 13/06/2024 17:57
Please do!
17:57
If you want eyes on any drafts, we'd be happy give it a look over, if that's helpful
๐Ÿ‘ 1
Avatar
Avatar
Jeff Glass
If you want eyes on any drafts, we'd be happy give it a look over, if that's helpful
thanks Jeff, my job is to be able to explain all this in plain english to a rank beginner. I have been putting off writing due to all the recent changes, otherwise my topic becomes a dinausor version and whats worse is that readers in a few months might experience broken code....that is very embarrassing for me (edited)
Avatar
Jeff Glass 13/06/2024 18:01
I feel ya! PyScript (and Python on the Web / Pyodide / Emscripten / Wasm) is a really fast-moving target these days
Avatar
Avatar
Jeff Glass
I feel ya! PyScript (and Python on the Web / Pyodide / Emscripten / Wasm) is a really fast-moving target these days
sadly I think it is too fast for the general public as they are already lost...not meant to be a dig
Avatar
Andrea Giammarchi 13/06/2024 18:04
Me and @JeffersGlass had a very lengthy conversation in Discord that started here: https://discord.com/channels/972017612454232116/972020206538997822/1250818968466362378 It's clear people using...
Avatar
Avatar
Rick
sadly I think it is too fast for the general public as they are already lost...not meant to be a dig
Andrea Giammarchi 13/06/2024 18:08
PyScript in the last year changed a lot and we did everything possible to inform users out of our User guide or F.A.Q. section but it's clear we haven't put enough effort to inform users how packages versioning work. This is not even a "fast moving pace" topic, this is how the entirety of the SW industry moves forward: you trust that version is going to work for your need? PIN IT and that's why package-lock.json got introduced in the JS world and why Pyodide itself has a package-lock bootstrap feature too so that if you expect certain packages to work, those packages frozen in time will guarantee they will work, with all pros and possible bugs they have, in your project. (edited)
18:12
So this is not a PyScript issue, rather a lack of understanding that packages that are still maintained will upgrade with sem-ver (maybe?) breaking changes hinted in the sem-ver convention itself, but that's what you get from software that's still "alive". All I am saying is that I can relate to your frustration around your stuff suddenly broken, but you've seen that just moving to an updated stack everything works so that it's not really our fault your stuff broke, it's how the whole SW stack works. I hope this reasons well to you.
Avatar
Andrea Giammarchi 13/06/2024 18:25
@Rick if worth anything to you, the current state of the project is that it's super stable, it gives you all the primitives you need (see my HTML file being 2X smaller than your previous one thanks to changes we made last months) so if you think PyScript changed too much you are right, I hear you, but the goal was indeed to make it more stable after a first round of feedback so that current state is meant to be a rock solid foundation to build anything you like and hopefully without further breaking changes. I know this might sound "blah-blah" to you but I assure you this is what we all think behind the project. (edited)
Avatar
Jeff Glass 13/06/2024 18:29
Tangentially but related - when I asked Hood (one of the Pyodide maintainers) how the WASM summit went at PyCon, he said something to the effect of (and I Iโ€™m paraphrasing): โ€œThank goodness we didnโ€™t get bogged down in talking about packagingโ€. The web packaging story is indeed pretty ad-hoc, and itโ€™s limiting reproducibility in general. Not a problem we caused per se, but itโ€™s the lake weโ€™re swimming in
Avatar
Avatar
Andrea Giammarchi
So this is not a PyScript issue, rather a lack of understanding that packages that are still maintained will upgrade with sem-ver (maybe?) breaking changes hinted in the sem-ver convention itself, but that's what you get from software that's still "alive". All I am saying is that I can relate to your frustration around your stuff suddenly broken, but you've seen that just moving to an updated stack everything works so that it's not really our fault your stuff broke, it's how the whole SW stack works. I hope this reasons well to you.
thanks for that, I am not that worried about how packages and versioning works and changes, it is up to me to explain it plainly to the beginner and also to emphasize its importance (and also to keep reminding them so they don't, in turn, break code). Just because I may not break code does not mean that the next developer down the line won't break his extended code. (edited)
Avatar
Avatar
Andrea Giammarchi
@Rick if worth anything to you, the current state of the project is that it's super stable, it gives you all the primitives you need (see my HTML file being 2X smaller than your previous one thanks to changes we made last months) so if you think PyScript changed too much you are right, I hear you, but the goal was indeed to make it more stable after a first round of feedback so that current state is meant to be a rock solid foundation to build anything you like and hopefully without further breaking changes. I know this might sound "blah-blah" to you but I assure you this is what we all think behind the project. (edited)
this is great news, thank you !!
Avatar
blackbooks 13/06/2024 18:55
Sorry to bump this up, but i really would like to know if there is a way to let the pyscript Environment get access to the Certificate Pool of the Browser. Original Post https://discord.com/channels/972017612454232116/972020206538997822/1248560756480540722 (edited)
Avatar
Avatar
ntoll
@streetlight I'm on Linux, using Firefox (also tested with Chrome). It works for me. Context like OS and browser would be very helpful. Also, any output in the browser console would be welcome too. Finally, do you have any plugins or add-ons to your browser that could be interfering..? Just want to make sure I've covered all the bases here. TL;DR - I want to try to reproduce your problem... then I can dig. ๐Ÿ‘
streetlight 15/06/2024 07:47
I am hosting html file, I use MS Edge, and no console output is shown ๐Ÿ˜ฆ
Avatar
Riiiight. How are you hosting the file? Directly from the filesystem, via localhost? etc...
10:09
My suggestion: 1. Start a local web server to serve your index.html. Use python -m http.server 2. Visit localhost:8000 to view the page. 3. Report what happens.
Avatar
AAAAAAAAAAAAAAAAAAAAAAAAA 17/06/2024 01:43
I have a question, if I have multiple input boxes with the same ID and call that in with document.querySelector(), do I get a list or something else entirely?
01:44
If it isn't a list, then how would I go about making it a list?
Avatar
AAAAAAAAAAAAAAAAAAAAAAAAA 17/06/2024 03:06
For those who like to see the code I wanna work with, here. The attachment is the HTML code. I want everything with the ID "stats" to be in a list so that way I can get the sum of the list. (edited)
03:10
I wanna do something like this: sP_text = document.querySelector("#stats") sP = sum(sP_text.value) output_div = document.querySelector("#point") Potal = pTotal(event)-sP if Potal<0: output_div.innerText = "You are pushing your Point limit." return output_div.innerText = Potal
03:10
But it isn't giving me anything but the first input box.
03:11
For example, if I put 15 for all of the text's, it will only recognize the first 15 and ignore the rest.
Avatar
Avatar
AAAAAAAAAAAAAAAAAAAAAAAAA
I have a question, if I have multiple input boxes with the same ID and call that in with document.querySelector(), do I get a list or something else entirely?
Andrea Giammarchi 17/06/2024 08:21
by definition, the id in HTML has to be unique.
08:22
use 3 different ids or use a class name instead and document.querySelectorAll('.stats') to then loop over each stat and do the math.
Avatar
Avatar
AAAAAAAAAAAAAAAAAAAAAAAAA
For example, if I put 15 for all of the text's, it will only recognize the first 15 and ignore the rest.
id must be totally unique field on eth page. use a class to refer to more than one. then when you select elements with that class you will get a list. id is one only. Learn html/css etc at https://www.w3schools.com/html/default.asp
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
Avatar
How to call two functions from same onclick in pyscript?
Avatar
Avatar
zobi
How to call two functions from same onclick in pyscript?
Andrea Giammarchi 17/06/2024 12:20
you mean on the HTML ? you create a third function that invokes those two functions ... otherwise with @when you do ... well, the same, you create a listener that invokes two functions. If you want to pass through addEventListener manually though, you just add 2 listeners.
12:22
truth to be told, as we just accept function names, without arguments or anything else possible, we might as well one day enable a comma separated list of names to allow multiple listeners per same event. This might leagt to conflicting operations though (such as event.stopImmediatePropagation() or other things one of the two listeners might do) and it looks to me an extremely edge case very easy to solve in userland when/if necessary.
Avatar
Avatar
Andrea Giammarchi
you mean on the HTML ? you create a third function that invokes those two functions ... otherwise with @when you do ... well, the same, you create a listener that invokes two functions. If you want to pass through addEventListener manually though, you just add 2 listeners.
Yes HTML. Something like the following https://stackoverflow.com/questions/16025138/call-two-functions-from-same-onclick .How to write in Pyscript?
HTML & JS How do I call 2 functions from one onclick event? Here's my code <input id ="btn" type="button" value="click" onclick="pay() cls()"/> the two functions being pay() and cls(...
Avatar
Avatar
zobi
Yes HTML. Something like the following https://stackoverflow.com/questions/16025138/call-two-functions-from-same-onclick .How to write in Pyscript?
Andrea Giammarchi 18/06/2024 16:23
yeah, that's covered in there already https://stackoverflow.com/a/16025172 ... we don't evaluate on the JS side and you are telling a Python interpreter what function to call, JS syntax won't work there and no eval happens neither so ... the answer is: combine two calls into one. The details behind blindly calling two functions per single event is also overwhelming because you need to understand the event cycle so ... use just one function if you really need to do two things within a single click.
HTML & JS How do I call 2 functions from one onclick event? Here's my code <input id ="btn" type="button" value="click" onclick="pay() cls()"/> the two functions being pay() and cls(...
Avatar
Avatar
Andrea Giammarchi
yeah, that's covered in there already https://stackoverflow.com/a/16025172 ... we don't evaluate on the JS side and you are telling a Python interpreter what function to call, JS syntax won't work there and no eval happens neither so ... the answer is: combine two calls into one. The details behind blindly calling two functions per single event is also overwhelming because you need to understand the event cycle so ... use just one function if you really need to do two things within a single click.
I tried the following.I want to clear everything and display something on DOM.https://_12.pyscriptapps.com/noisy-scene/latest/
Avatar
Avatar
zobi
I tried the following.I want to clear everything and display something on DOM.https://_12.pyscriptapps.com/noisy-scene/latest/
Andrea Giammarchi 18/06/2024 20:06
you had:
  • a button before the body ... in an HTML page any UI element should be within the body
  • the script inside a <p> tag ... you were destroying every PyScript layout with that innerHTML on that element ... why was that?
  • the script with terminal attribute (which is optional) exposes a terminal reference where you can clear it
I don't know if this fork satisfies your intents https://pyscript.com/@agiammarchi/noisy-scene-copy/latest but it's very very hard to understand what you are trying to do in terms of expectations or goals so it's a bit of a blind guessing to help but there were a few gotchas in there.
(edited)
20:08
a pyscript or mpyscript script also exposes a target which is where stuff happens ... the terminal exposes a detach or desotry (can't remember, that's XTerm API) to remove it ... so unless I understand your initial goal and your final one, I am afraid I cannot help further.
Avatar
This is exactly what I wanted.Thanks for helping.I will see how I can fix it in my code.
๐Ÿฅณ 1
Avatar
Avatar
zobi
This is exactly what I wanted.Thanks for helping.I will see how I can fix it in my code.
how can I add append=False for exec(a.string) ? If I write display(exec(a.string) ,append=False) it displays none and display(exec(a.string) ,append=True) gives me multiple displays? (edited)
Avatar
Avatar
zobi
how can I add append=False for exec(a.string) ? If I write display(exec(a.string) ,append=False) it displays none and display(exec(a.string) ,append=True) gives me multiple displays? (edited)
Andrea Giammarchi 20/06/2024 08:22
again, an example of what you are trying to do would help ... but in general I have no idea what exec returns and if it's None I am not sure what you think it should display?
Avatar
@zobi please tell us what you are trying to achieve.
Avatar
For exec("some code") on button click I was trying how can I display it only once.like display("hello",append=False).I found a solution in https://stackoverflow.com/. ๐Ÿ‘ (edited)
Stack Overflow | The Worldโ€™s Largest Online Community for Developers
Avatar
Avatar
zobi
For exec("some code") on button click I was trying how can I display it only once.like display("hello",append=False).I found a solution in https://stackoverflow.com/. ๐Ÿ‘ (edited)
Andrea Giammarchi 20/06/2024 14:17
link please? I mean ... the link to the solution? Thanks!
Avatar
I am famous...@andrea asking code for the first time.https://_12.pyscriptapps.com/shy-leaf/latest/ blindly copied from stackflow can't explain it.It says exec always returns none instead of the actual return value and this code is the alternative to get the actual return value. https://stackoverflow.com/questions/33409207/how-to-return-value-from-exec-in-function (edited)
I try: def test(w,sli): s = "'{0}'{1}".format(w,sli) exec(s) return s print test("TEST12344","[:2]") its return 'TEST12344'[:2] How to return value from exec in function
Avatar
Avatar
zobi
I am famous...@andrea asking code for the first time.https://_12.pyscriptapps.com/shy-leaf/latest/ blindly copied from stackflow can't explain it.It says exec always returns none instead of the actual return value and this code is the alternative to get the actual return value. https://stackoverflow.com/questions/33409207/how-to-return-value-from-exec-in-function (edited)
Andrea Giammarchi 21/06/2024 11:53
watch out I am not sure that would work with MicroPython but if that's not a concern then it kinda makes sense, it's still not clear to me why you need all that instead of a runtime.runPython or similar
Avatar
Yes it's not supporting MicroPython. runtime.runPython I never heard about that?My Textarea code supports MicroPython.What's the concern with using textarea?
Avatar
Hello guys, Is it possible to configure my PyEditor inline? I want to make a PyEditor that can plot figures using matplotlib but the way my project is set up I can not configure using other files. My component is being called as a static webpage so I need to find a way to call the config file either inline or bundle it in the same static webpage. The new PyEditor needs an explicit config attribute. I'd appreciate if there is anything you could point me to (edited)
Avatar
@aayush underneath PyEditor is CodeMirror (https://codemirror.net/). We explain how it all works and fits together here: https://docs.pyscript.net/2024.6.2/user-guide/editor/ If this isn't enough for you, it would be really helpful for us if you could tell us what precisely it is you want to configure. Then, depending on the feasibility of what you're trying to do, we might be able to fix/enable/document things so your request becomes an enhancement! Of course, caveats apply: is it possible, how much effort is involved, is it likely to be a much sought after enhancement etc... But the key thing to do (as you've already started to do) is to engage..! ๐Ÿ‘ ๐Ÿš€
Avatar
Avatar
ntoll
@aayush underneath PyEditor is CodeMirror (https://codemirror.net/). We explain how it all works and fits together here: https://docs.pyscript.net/2024.6.2/user-guide/editor/ If this isn't enough for you, it would be really helpful for us if you could tell us what precisely it is you want to configure. Then, depending on the feasibility of what you're trying to do, we might be able to fix/enable/document things so your request becomes an enhancement! Of course, caveats apply: is it possible, how much effort is involved, is it likely to be a much sought after enhancement etc... But the key thing to do (as you've already started to do) is to engage..! ๐Ÿ‘ ๐Ÿš€
Hey thanks for the response! I want to run code with external libraries like numpy, pandas, matplotlib etc using the PyScript PyEditor. The PyEditor currently needs an explicit config attribute, I just want to know if there is a way to provide this config inline as a json or can I only link to .toml and .json files. The docs don't really elaborate on that.
Avatar
@aayush good question. As you know, you have a choice of JSON or TOML (see: https://docs.pyscript.net/2024.6.2/user-guide/configuration/#toml-or-json). Because JSON is built into the browser, it's "faster" than TOML (because we have to download a TOML parser IIRC - and the TOML just parses it to a JavaScript object anyway). You could also just inline your JSON (but not TOML) settings as the value of the config attribute (see: https://docs.pyscript.net/2024.6.2/user-guide/configuration/#file-or-inline). But to be honest, the currently idiomatic approach is to stick config into a separate file and reference that from config. ๐Ÿ‘ Keep asking qustions. Happy to help.
Avatar
Avatar
ntoll
@aayush good question. As you know, you have a choice of JSON or TOML (see: https://docs.pyscript.net/2024.6.2/user-guide/configuration/#toml-or-json). Because JSON is built into the browser, it's "faster" than TOML (because we have to download a TOML parser IIRC - and the TOML just parses it to a JavaScript object anyway). You could also just inline your JSON (but not TOML) settings as the value of the config attribute (see: https://docs.pyscript.net/2024.6.2/user-guide/configuration/#file-or-inline). But to be honest, the currently idiomatic approach is to stick config into a separate file and reference that from config. ๐Ÿ‘ Keep asking qustions. Happy to help.
Thank you for being so patient with my questions, I really appreciate it. I have tried passing the .json as the value of config attribute but it doesn't seem to be working for me. It works if type="py" but if I pass a .json inline as the config for type="py-editor" it stops working and I get an ever loading editor like this.
17:33
17:35
This is the piece of code, I hope this helps. I understand referencing the config as a file is a better approach but that is not an option for me of the way my project is setup.
Avatar
Avatar
aayush
This is the piece of code, I hope this helps. I understand referencing the config as a file is a better approach but that is not an option for me of the way my project is setup.
Andrea Giammarchi 24/06/2024 08:57
py-editor is a custom type a part, it does not inherit, consider, or even parse, a py-config at all ... in your page you have no type="py" and just a type="py-editor" but there is also a py-config which will be ignored ... it's not clear to me what you are trying to do but having a link instead of a screenshot to your pyscript .com attempt (or anything else you are using) might help further.
Avatar
@aayush happy to help. Basically, what @Andrea Giammarchi just asked.
Avatar
@ntoll @Andrea Giammarchi what I essentially want to do it to be able to run the code inside <script type=py> in <script type=py-editor> creating an editor where user can type the code and see their outputs. I am sorry if the sreen-shot confused you, this is the link to PyScript .com, https://pyscript.com/@aayush05/run-ltk-in-browser/latest. I am able to do that when I provide config attribute to the py-editor as separate file but when I do it as an inline json like in the link above I get an ever loading editor. Once again, thank you for being so patient with the questions. (edited)
Avatar
Avatar
aayush
@ntoll @Andrea Giammarchi what I essentially want to do it to be able to run the code inside <script type=py> in <script type=py-editor> creating an editor where user can type the code and see their outputs. I am sorry if the sreen-shot confused you, this is the link to PyScript .com, https://pyscript.com/@aayush05/run-ltk-in-browser/latest. I am able to do that when I provide config attribute to the py-editor as separate file but when I do it as an inline json like in the link above I get an ever loading editor. Once again, thank you for being so patient with the questions. (edited)
Andrea Giammarchi 24/06/2024 14:44
finally I see what you mean ... that's a feature request for PyEditor as indeed it doesn't understand raw JSON like PyScript custom scripts do: https://github.com/pyscript/pyscript/blob/main/pyscript.core/src/plugins/py-editor.js#L39-L46
14:47
@aayush issue filed, fix is trivial, please "stay tuned" https://github.com/pyscript/pyscript/issues/2109 ๐Ÿ˜
We never thought about enabling just JSON as config attribute value, we just assumed the best way to go was to point at a config file: https://github.com/pyscript/pyscript/blob/main/pyscript.core/s...
Avatar
Avatar
Andrea Giammarchi
@aayush issue filed, fix is trivial, please "stay tuned" https://github.com/pyscript/pyscript/issues/2109 ๐Ÿ˜
Andrea Giammarchi 24/06/2024 15:32
this has been addressed and tested ... if you'd like to adventure into "canary" feedback, use this link instead of the official release one to see it working: https://cdn.jsdelivr.net/npm/@pyscript/core@0.4.52/dist/core.js (with its .css counterpart, but no changes in there)
โค๏ธ 1
Avatar
Avatar
Andrea Giammarchi
this has been addressed and tested ... if you'd like to adventure into "canary" feedback, use this link instead of the official release one to see it working: https://cdn.jsdelivr.net/npm/@pyscript/core@0.4.52/dist/core.js (with its .css counterpart, but no changes in there)
Andrea Giammarchi 24/06/2024 15:35
I've also cloned your test page and it works to me: https://pyscript.com/@agiammarchi/run-ltk-in-browser-copy-14992/latest
Avatar
Hi. I'm trying to use Python on my laptop (3.12.3) to pickle an object (a Trie of a million words) and then use pickle.load on my pyoxide worker (running 2024.6.2 pyscript) to load the Trie object from the binary file. I am able to load the file, but when I call pickle.load, I get an error ModuleNotFoundError: No module named 'public' on the call to pickle.load. I've imported pickle at the top of my file and it seems to work fine, no errors about not finding that module. It seems to happen on the call to pickle.load. Am I missing an import or a dependency in my pyodide set up? Any other ideas? This is the first module of things I'd consider part of python that I've run into an issue with. I have no issues using re, typing, collections, etc.
Avatar
@dmmiller hmm... that's a funny one. Feels like a Pyodide problem (upstream of us) rather than PyScript specific.
Avatar
Avatar
ntoll
@dmmiller hmm... that's a funny one. Feels like a Pyodide problem (upstream of us) rather than PyScript specific.
Yea, seems likely. Just happened to get to you first. Is there a discord or support for pyodide?
Avatar
Raising a GitHub issue is perhaps the first thing I'd do (or at least check nobody else has had anything similar). AFAICT from their docs and a quick Google - it should "just work" โ„ข๏ธ ๐Ÿ˜‰
๐Ÿ‘ 1
Avatar
Avatar
Andrea Giammarchi
this has been addressed and tested ... if you'd like to adventure into "canary" feedback, use this link instead of the official release one to see it working: https://cdn.jsdelivr.net/npm/@pyscript/core@0.4.52/dist/core.js (with its .css counterpart, but no changes in there)
thanks! I can reproduce it and works fine for me too!
18:36
thanks for all the help
๐Ÿ‘ 1
Avatar
Is there a way to set attributes and properties on DOM elements via pydom? (similar to using jQuery's .attr() and .prop() methods)
Avatar
Puppy of Doom 25/06/2024 21:41
Is it possible to use pyscript.storage outside of async functions? The docs use await like below: from pyscript import storage

Each store must have a meaningful name.

store = await storage("my-storage-name") But AFAIK await can only be used inside an async, which i'd like to avoid if possible
Avatar
Avatar
Puppy of Doom
Is it possible to use pyscript.storage outside of async functions? The docs use await like below: from pyscript import storage

Each store must have a meaningful name.

store = await storage("my-storage-name") But AFAIK await can only be used inside an async, which i'd like to avoid if possible
window.localStorage. setItem/getItem is synchronous but size is quite limited
Avatar
Avatar
Puppy of Doom
Is it possible to use pyscript.storage outside of async functions? The docs use await like below: from pyscript import storage

Each store must have a meaningful name.

store = await storage("my-storage-name") But AFAIK await can only be used inside an async, which i'd like to avoid if possible
Andrea Giammarchi 26/06/2024 07:39
if you have async attribute in your script you also have top-level await (edited)
Avatar
Avatar
Andrea Giammarchi
if you have async attribute in your script you also have top-level await (edited)
Puppy of Doom 26/06/2024 09:36
Thanks Andrea, I'm trying to avoid async as I have lots of sync code i'm trying to port over. I enjoyed this related discussion: https://discord.com/channels/972017612454232116/1202066677593026601/1202704968696594513 Correct me if wrong, but to use python.storage or own-rolled IndexedDB shim requires async functions. Either one rewrites all the functions that touch python.storage to be async as well, or one uses asyncio to call async python.storage from sync functions. Many consider this last approach to not be robust and when I tried it asyncio in pyodide does not seem to behave in the same way as in regular python. Could off-loading all the IndexedDB operations to a worker (bootstrapped once, i hope) be a better approach?
Avatar
Avatar
pmp-p
window.localStorage. setItem/getItem is synchronous but size is quite limited
Puppy of Doom 26/06/2024 09:39
Thanks, size constraint is a bit of an issue. But I think localstorage will have to do for the PoC I'm working on, and hope that IndexedDB tooling will develop over time
Avatar
Avatar
Puppy of Doom
Thanks Andrea, I'm trying to avoid async as I have lots of sync code i'm trying to port over. I enjoyed this related discussion: https://discord.com/channels/972017612454232116/1202066677593026601/1202704968696594513 Correct me if wrong, but to use python.storage or own-rolled IndexedDB shim requires async functions. Either one rewrites all the functions that touch python.storage to be async as well, or one uses asyncio to call async python.storage from sync functions. Many consider this last approach to not be robust and when I tried it asyncio in pyodide does not seem to behave in the same way as in regular python. Could off-loading all the IndexedDB operations to a worker (bootstrapped once, i hope) be a better approach?
Andrea Giammarchi 26/06/2024 09:51
only the creation of the storage is async, everything else is sync so ... I am not following. You need to load in memory IndexedDB content to then have a sync storage and IndexedDB is async, no matter what.
09:53
store = await storage("my-storage-name") # now all operations are sync store["key"] = { "some": "value" } print(store["key"]) del store["key"]
09:54
if you have sync code it will work without any issue on top-level await ... it changes nothing for your code, it allows you to await outside functions ... that's it. Use async attribute in your script and you would have no issue.
09:55
dare I say it was probably a mistake to not enable top-level await by default in PyScript as more and more APIs from the Web are inevitably async ... food for thoughts @ntoll ?
Avatar
Avatar
Andrea Giammarchi
only the creation of the storage is async, everything else is sync so ... I am not following. You need to load in memory IndexedDB content to then have a sync storage and IndexedDB is async, no matter what.
Puppy of Doom 26/06/2024 11:06
I misunderstood. Thanks so much! I tried writing a IDB interface before this last update adding pyscript.storage and suffice to say i had some async/sync issues with get functions returning pyodide tasks instead of values.
11:06
For anybody reading this the async script tag is something like below in index.html: <script type="py" src="./main.py" config="./pyscript.toml" async></script>
11:09
Just out of curiosity, python.storage mirrors IndexedDB in memory, right?
11:09
And async script tag doesn't have any drawbacks?
Avatar
Avatar
Andrea Giammarchi
dare I say it was probably a mistake to not enable top-level await by default in PyScript as more and more APIs from the Web are inevitably async ... food for thoughts @ntoll ?
I was thinking the same thing this morning when I saw @Puppy of Doom 's comment
Avatar
Avatar
Puppy of Doom
Just out of curiosity, python.storage mirrors IndexedDB in memory, right?
Andrea Giammarchi 26/06/2024 11:27
we decided to provide a dictionary that reflects what's been stored in the IndexedDB to make it easy and simple to reason about for our users. PyScript inevitably use a lot of RAM due Viartual FileSystem. WASM buffers and whatnot ... ideally we would love for IDB to not require async things but it's not possible. Even an sqlite3 based solution would inevitably run in memory plus it won't be persistent as IndexedDB is. There are workarounds for that too but then again, sqlite3 via WASM would still kinda need a lot of RAM to work with. Latest module uses an Opaque Persistent FileSystem Web feature available to all browsers and I might have a look if that's also a suitable solution for our needs, but at that point it'd be a good case to just have sqlite3 as is, not just a key/value store so ... these are not mutually exclusive to me, yet current pyscript.storage feels easy, fast, and natural to use. We keep things in sync behind the scene too but if paranoid about it you can eventually, once in a while, use await store.sync() and be sure that will persist next time you visit that page/project. I hope this clarifies a bit and explain contrains and decisions around it.
Avatar
Avatar
ntoll
I was thinking the same thing this morning when I saw @Puppy of Doom 's comment
Andrea Giammarchi 26/06/2024 11:28
honestly, with MicroPython now fully onboard and working well with top-level await I think using by default runPythonAsync behind the scene might be a welcome feature. I don't know if there's anything ugly, greedier, slower, or problematic behind Pyodide and MicroPython scene but, if that's not the case, I think having even less to explain around async attribute or top level await would be beneficial (big imho here).
Avatar
Yeah, it feels like we need to propose a change so folks get visibility of this issue. (Note: it's not an issue... I think you and I [correctly] see the async attribute as surpless to requirements, and we should just tacitly make it that way... assuming no performance gotchas).
Avatar
hey hey! is there anyone who used console with pyscript (prints that usually appear in console to show on webpage, either in console format or paragraphs)?
12:52
i developed a script that usually run for few hours and prints steps one by one and i wonder if i can make it appear on my webpage (im developing a fullstack project)
13:06
That's basically what's going on on the https://pyscript.net homepage. ๐Ÿ˜‰
PyScript is a platform for Python in the browser.
Avatar
yeah thats it, mb ๐Ÿ˜„ thanks (edited)
Avatar
Avatar
ntoll
Yeah, it feels like we need to propose a change so folks get visibility of this issue. (Note: it's not an issue... I think you and I [correctly] see the async attribute as surpless to requirements, and we should just tacitly make it that way... assuming no performance gotchas).
Andrea Giammarchi 26/06/2024 13:55
I wrote a "what if" proposal for the next major / breaking (not really) version of PyScript here: https://github.com/pyscript/pyscript/discussions/2111 (edited)
Background I know we use CalVer for versioning but, behind the scene, PyScript uses semver convention to land on npm in its "canary" or "developer preview" fashion. Maybe not ev...
Avatar
any idea why it cant find module in terminal? i have imports in .py file that i execute also i tried to import module by <py-env>
Avatar
hi team, I am having trouble importing boto3 package into pyscript. It definately works in python and is installed correctly. I am getting the following error, thanks in advance for helping File "/lib/python3.12/site-packages/botocore/utils.py", line 39, in <module> import botocore.httpsession File "/lib/python3.12/site-packages/botocore/httpsession.py", line 22, in <module> from urllib3.util.ssl_ import ( ImportError: cannot import name 'ssl' from 'urllib3.util.ssl_' (/lib/python3.12/site-packages/urllib3/util/ssl_.py) I know urllib package does not work in pyscript. it seems that urllib is being called from boto3 (edited)
Avatar
can someone please help me with this example. on the left is example how it should be formated (which i dont really understand) and on the right is my formating (i want to run plan.py which is located in folder apps) (edited)
Avatar
Avatar
Shaman
can someone please help me with this example. on the left is example how it should be formated (which i dont really understand) and on the right is my formating (i want to run plan.py which is located in folder apps) (edited)
Andrea Giammarchi 26/06/2024 16:00
you forgot src ...
16:01
<script id="my_script" src="./plan.py" type="mpy" terminal worker> (edited)
16:02
I have the feeling you need to do something else though
Avatar
Avatar
Andrea Giammarchi
I have the feeling you need to do something else though
yeah same
16:03
adding src doesnt help
16:06
honestly i never used js so i may lack knowledge about it. but i managed to execute my python files with button before. i guess terminal is a bit tricky
16:08
cuz this for example works perfectly
Avatar
Avatar
Shaman
cuz this for example works perfectly
Andrea Giammarchi 26/06/2024 16:25
you forgot type in there, and everything else I've said ... you also likely never need pyfetch in our current offer
16:26
I am not asking you to put a live demo due API ... but if you could reduce your scope and just publish what you are trying to do, we might help better.
16:26
minimal example, that's it
Avatar
Avatar
Andrea Giammarchi
you forgot type in there, and everything else I've said ... you also likely never need pyfetch in our current offer
all i want is actually having terminal on a page and with a click of a button to execute my plan.py file through the terminal
Avatar
Avatar
Shaman
all i want is actually having terminal on a page and with a click of a button to execute my plan.py file through the terminal
Andrea Giammarchi 26/06/2024 16:37
can you write a minimal expectation example of that?
Avatar
Avatar
Andrea Giammarchi
can you write a minimal expectation example of that?
aight, so i have a script that does automated activities on blockchain for me. when i run a script in my terminal it looks like shown in screenshot. what i want is to connect my script to frontend (to have terminal like that visible on my site)
Avatar
Avatar
Shaman
aight, so i have a script that does automated activities on blockchain for me. when i run a script in my terminal it looks like shown in screenshot. what i want is to connect my script to frontend (to have terminal like that visible on my site)
Andrea Giammarchi 26/06/2024 16:42
I am still struggling to understand what you are after ... can you put a minimal example on pyscript.com so I can eventually suggest anything around your code? (edited)
Avatar
Avatar
Andrea Giammarchi
I am still struggling to understand what you are after ... can you put a minimal example on pyscript.com so I can eventually suggest anything around your code? (edited)
so this is my pyscript code
16:48
16:48
what i want is to run my .py in that terminal
16:48
automaticly when the page opens (edited)
Avatar
Avatar
Shaman
aight, so i have a script that does automated activities on blockchain for me. when i run a script in my terminal it looks like shown in screenshot. what i want is to connect my script to frontend (to have terminal like that visible on my site)
so i get prints like this (this what it looks like when i run my .py in vscodes terminal for example) (edited)
Avatar
Najmul Sharif 26/06/2024 19:57
hello everybody . how are you ? is it possible to integrate pyscript to my django project alternative to JavaScript ?
Avatar
i managed to open my .py file with console and it looks like it has a problem loading module web3. I googled a bit and found on some forums, that web3 isnt possible to run through pyscript. Can anyone confirm that statement, or should there be a way to run it? ๐Ÿ™‚
Avatar
@Shaman I've just taken a look around the project's source code and notice that they use Python client libraries that ultimately sit on top of sockets (aiohttp for example). Because PyScript runs in the browser, and for very sensible security reasons, you're only able to make network connections that use the browser's networking capabilities (i.e. http and web-socket connections). If these folks have a well organised code base, they should be able to extract the Python native things that the browser doesn't support, and replace them with browser based equivalents (see: https://docs.pyscript.net/2024.6.2/user-guide/builtins/#pyscriptfetch for example). HOWEVER (READ THIS!), PyScript has the ability to make use of native JavaScript libraries such as web3.js.... (https://web3js.readthedocs.io/en/v1.10.0/index.html) - However, I notice that their docs say you may need the MetaMask (MetaMask) plugin for your browser, for this to work. Details of interacting with JavaScript modules from PyScript can be found here: https://docs.pyscript.net/2024.6.2/user-guide/dom/#working-with-javascript
23:22
Hope this helps.
Avatar
Hello Does PyScript provide a function that can be called from JS to execute Python code and return the output (print output)? something like: let output = PyS.excutePythonCode("print('Hello, world')")
Avatar
hi team, I am having trouble importing boto3 package into pyscript. It definately works in python and is installed correctly. I am getting the following error, thanks in advance for helping File "/lib/python3.12/site-packages/botocore/utils.py", line 39, in <module> import botocore.httpsession File "/lib/python3.12/site-packages/botocore/httpsession.py", line 22, in <module> from urllib3.util.ssl import ( ImportError: cannot import name 'ssl' from 'urllib3.util.ssl' (/lib/python3.12/site-packages/urllib3/util/ssl_.py) I know urllib package does not work in pyscript. it seems that urllib is being called from boto3
Avatar
Puppy of Doom 28/06/2024 11:47
Does pyscript.storage have cursor operations? IndexedDB has them but it's not mentioned in the pyscript docs
Avatar
I'm using 2024.6.2 and I have the terminal option added <script type="py" src="main.py" config="pyscript_pyo.toml" terminal></script>so I can see any print statements when I am editing the code and running it on pyscript.com. This is working.But if the code errors - there is no error message in the terminal(used to be) and there is no error message in the Chrome console(used to be). I am using py not mpy and I have only main - no workers. How do I get error messages to console or terminal ?
Avatar
Hi ๐Ÿ‘‹ I am trying to change the content of an input element that gets created from an HTML template. The element has an attribute id = "status_msg". I can access said attribute via pydom['#status_msg'] or document.getElementById('status_msg'). I can change its value using the .value attribute, but not using .content nor .html. Given this little context, can anyone see what I am doing wrong? I tried using 2024.1.1 as well as 2024.6.2. My issue with .value is that it seems to be for text and I would like to store HTML code in the input element. Thanks ๐Ÿ™ (edited)
Avatar
Have you tried .innerHTML? I think that's how you usually do it, both in Pyscript and JavaScript
Avatar
Hey @Dagusiu! Yes, I've tried .innerHTML as well. It's strange though, I am able to modify the input element itself using .outerHTML... cannot really make sense of it
Avatar
Wait, what exactly are you trying to do here? An <input> of type text doesn't store HTML, it only stores plain text. And to get the text written in it, you can read/write the value property (this works the same in JS and Pyscript)
21:10
.innerHTML can be used to modify e.g. a <div>
21:12
So, if you want an HTML object where you can programmatically place arbitrary HTML code in it using .innerHTML, it should be a <div>, not an input
Avatar
True. More context is needed: I want to show emojis in the input element. If I copy and paste it from the clipboard into the input element, it gets shown correctly as a rendered emoji. If I set the emoji (html encoding) via .value, it does not get rendered but shown as the html encoded chars. That's the issue I am trying to solve.
Avatar
Najmul Sharif 01/07/2024 10:25
i need help . after install pyscript package in my pycharm ! it's showing this error . how to solve it ? . Thank you
10:27
or my pyscript version 0.2.5 from pip .. but currently released 6.2
Avatar
chris.laffra 01/07/2024 13:56
Hi @Najmul Sharif Najmuil. PyScript is not just another Python module you can run locally. It only runs inside a web page. The best way to get started is to use https://pyscript.com and create a sample project there. Once you understand how it works there, you can replicate a similar setup in Intellij or VS Code.
Avatar
Najmul Sharif 02/07/2024 08:31
hi mr @chris.laffra Thank you for your reply . your advised is helpful for me . but my questions is that , i install the pyscript package from "pip install pyscript" and try to import this below function and write this code from the . pyscript documentation site . the screentshot is hear . but the " from pyscript import document " is not imported . i just want to say that how to solve this error . i try to import it to the main.py file . thank you
Avatar
I'm looking to create a PyEditor instance at runtime, by creating a <script> DOM element, setting its type to py-editor, and adding it to the DOM. Any special steps I need to do?
08:37
Also: is there a way to disable the run button and instead just get an update on every change for "automatic" running?
08:38
That would be helpful for a little thing I'm tinkering with to let you edit PuePy pages live.
Avatar
Avatar
Najmul Sharif
hi mr @chris.laffra Thank you for your reply . your advised is helpful for me . but my questions is that , i install the pyscript package from "pip install pyscript" and try to import this below function and write this code from the . pyscript documentation site . the screentshot is hear . but the " from pyscript import document " is not imported . i just want to say that how to solve this error . i try to import it to the main.py file . thank you
chris.laffra 02/07/2024 10:59
When you run "pip install pyscript", you install command line tools for PyScript, that help you create PyScript applications. See https://pypi.org/project/pyscript/. This is NOT the PyScript runtime, but helps you create an HTML page that can then run on top of PyScript. Like I said, start at https://pyscript.com to see how it works. This is how I got started myself when PyScript was all new to me as well.
Command Line Interface for PyScript
Avatar
Avatar
Neon22
I'm using 2024.6.2 and I have the terminal option added <script type="py" src="main.py" config="pyscript_pyo.toml" terminal></script>so I can see any print statements when I am editing the code and running it on pyscript.com. This is working.But if the code errors - there is no error message in the terminal(used to be) and there is no error message in the Chrome console(used to be). I am using py not mpy and I have only main - no workers. How do I get error messages to console or terminal ?
Andrea Giammarchi 02/07/2024 15:36
you file a bug with a minimal reproducible code that shows the issue and we'll work on it, that's usually the easiest and fastest way ๐Ÿ™‚
Avatar
KleberKadanus 03/07/2024 02:41
Algum brasileiro?
Avatar
The newest version of chrome is providing a notice that in future versions of chrome third party cookies will be blocked. Specifically, it is noting "__cf_bm" for the pyscript.net domain. Is this cookie necessary for pyscript to work? Just making sure this isn't a problem in the future.
Avatar
Avatar
Ben Smith
The newest version of chrome is providing a notice that in future versions of chrome third party cookies will be blocked. Specifically, it is noting "__cf_bm" for the pyscript.net domain. Is this cookie necessary for pyscript to work? Just making sure this isn't a problem in the future.
It seems like the cookie might have something to do with CloudFlare.
Avatar
Andrea Giammarchi 03/07/2024 16:43
we don't use cookies in PyScript (code) ... but I am sure our IT is well aware of the issue and will take precautions to be sure stuff will just work on PyScript .com and .net too
16:44
thanks for the headsup though, appreciated!
Avatar
Avatar
Ben Smith
The newest version of chrome is providing a notice that in future versions of chrome third party cookies will be blocked. Specifically, it is noting "__cf_bm" for the pyscript.net domain. Is this cookie necessary for pyscript to work? Just making sure this isn't a problem in the future.
You don't need cookies for PyScript to work. AFAICT the cookie is something left over from when the domain was first put together. I'll take a look and remove it if possible.
Avatar
Avatar
Neon22
I'm using 2024.6.2 and I have the terminal option added <script type="py" src="main.py" config="pyscript_pyo.toml" terminal></script>so I can see any print statements when I am editing the code and running it on pyscript.com. This is working.But if the code errors - there is no error message in the terminal(used to be) and there is no error message in the Chrome console(used to be). I am using py not mpy and I have only main - no workers. How do I get error messages to console or terminal ?
I'm getting the code errors both in console and terminal.
Avatar
Avatar
Andrea Giammarchi
watch out I am not sure that would work with MicroPython but if that's not a concern then it kinda makes sense, it's still not clear to me why you need all that instead of a runtime.runPython or similar
I think if return works then display doesn't seem to work both are interlinked.
Avatar
Hi there coders, can someone provide a very plain english explanation as to why under the newest version of pyscript we don't import pyodide or asyncio anymore? and also why we use fetch now vs pyfetch, thanks...i need to explain this to my students and readers because what is out there seems to be way over their heads.
Avatar
@RickD it's because PyScript now supports multiple Python interpreters (Pyodide and MicroPython). As a result we've simplified and unified things such that it all just works the same in both interpreters. Hence fetch rather than pyfetch and the async attribute for the script tags (although you can still import asyncio if you need it). You can also still import pyodide namespaces... they're still there, but your code won't be so portable. Hope this makes sense.
Avatar
Where are you able to find the pyscript package to use 'import pyscript'?
Avatar
@Benno it's built into PyScript itself, no need to install it.
Avatar
Avatar
ntoll
@Benno it's built into PyScript itself, no need to install it.
also when i want to use it in vscode? because there is no syntax + import error
Avatar
Hmm... well, the raw Python source code it in GitHub: https://github.com/pyscript/pyscript/tree/main/pyscript.core/src/stdlib/pyscript but I guess you need to tell VSCode that the pyscript namespace is part of the framework, not a module in your local filesystem.
Avatar
alright thank you
Avatar
Avatar
ntoll
@RickD it's because PyScript now supports multiple Python interpreters (Pyodide and MicroPython). As a result we've simplified and unified things such that it all just works the same in both interpreters. Hence fetch rather than pyfetch and the async attribute for the script tags (although you can still import asyncio if you need it). You can also still import pyodide namespaces... they're still there, but your code won't be so portable. Hope this makes sense.
@ntoll WOW thanks that didn't hurt, a nice easy explanation for those of us who came late to the party (edited)
๐Ÿš€ 1
Avatar
Can i pass Python code directly to Pyscript from Javascript code without the use of script tag?
Avatar
@m28 yes you definitely can. It's the weekend and I'm just quickly checking messages before travelling to EuroPython. On Monday I hope @Andrea Giammarchi can just point you in the right direction.... but the TL;DR will be instantiating a JS class and using it to interact with the Python interpreter of choice (but the details escape me ATM and I don't have time to look 'em up). Note to @Andrea Giammarchi - we should document this in docs.pyscript.net - as part of the "Working with JavaScript" section. ๐Ÿ‘
Avatar
Hi, I have a question on micropython temrinal in web worker. Is it possible to control a worker created with <script type="mpy" terminal worker>, e.g. stop its execution/restart it? To be more precise, I'd like to know if it's possible to stop the executed code (e.g. an infinite loop) from JS?
Avatar
@jbdod I suspect that will break the terminal. What exactly are you trying to do?
Avatar
I m trying to create a mini editor, and since the repl is in a webworker (which is super cool because it doesnโ€™t block the UI), I m trying to make a button to stop the worker when there is an infinite loop
Avatar
One way i was thinking was trying to exit with a soft reset (https://docs.micropython.org/en/latest/reference/repl.html#soft-reset) and then relaunching with a code.interact(), but i don't know if there is a way to trigger this from main thread
Avatar
Not if it's in an infinite loop. I wonder if you could get a reference to the worker's underlying JavaScript object/proxy and call terminate on it..? @Andrea Giammarchi probably has better context to advise on this.
Avatar
I'm working on trying to get things running offline. I have an app that uses a python script on the main thread. I am able to use the docs at https://docs.pyscript.net/2024.7.1/user-guide/offline/ to get it loading from my own copy of pyscript/pyodide. That python script however kicks off a PyWorker like so worker = PyWorker('python/worker.py', worker_options) where worker_options looks like this worker_options = ffi.to_js({ "type" : "pyodide", "config": { "files" : { "./python/decorators/makefilter.py" : "decorators/makefilter.py", ... When I run this, it loads pyodide.mjs from https://cdn.jsdelivr.net/pyodide/v0.26.1/full/pyodide.mjs. I tried specifying an interpreter like i did in the main toml file as a field in the config, but it seems to ignore it. If I leave off the type, it doesn't work. I need to pass dynamic config because I am also passing additional configuration to my worker. It is possible I could specify the config with a toml file, and then try and load data in the worker thread, but ideally, i'd be able in the config to specify the interpreter just like in my main toml file to get the main thread script running.
Avatar
It looks like in the /dist/core-Di0g8mUD.js there is an object that maps pyodide to https://cdn.jsdelivr.net/pyodide/v${e}/full/pyodide.mjs. I was able to modify that code in the dist to point to my local pyodide.mjs and make it work, but it seemed brittle and error prone since I'd want to reference an environmnent variable as to whether i was running on localhost or the actual server endpoint.
Avatar
It looks like this code is originally coming from https://github.com/pyscript/polyscript/blob/main/esm/interpreter/pyodide.js
Polyscript - PyScript single core to rule them all - pyscript/polyscript
Avatar
CyberneticDaisy 09/07/2024 08:38
pyscript.web.elements question: is there a simple way to set an attribute like aria-label or data-tooltip on an html element? or do I need to dig into _dom_element? I've made it work okay using _dom_element but that prevents me from using the nice declarative style of creating elements
Avatar
Avatar
dmmiller
I'm working on trying to get things running offline. I have an app that uses a python script on the main thread. I am able to use the docs at https://docs.pyscript.net/2024.7.1/user-guide/offline/ to get it loading from my own copy of pyscript/pyodide. That python script however kicks off a PyWorker like so worker = PyWorker('python/worker.py', worker_options) where worker_options looks like this worker_options = ffi.to_js({ "type" : "pyodide", "config": { "files" : { "./python/decorators/makefilter.py" : "decorators/makefilter.py", ... When I run this, it loads pyodide.mjs from https://cdn.jsdelivr.net/pyodide/v0.26.1/full/pyodide.mjs. I tried specifying an interpreter like i did in the main toml file as a field in the config, but it seems to ignore it. If I leave off the type, it doesn't work. I need to pass dynamic config because I am also passing additional configuration to my worker. It is possible I could specify the config with a toml file, and then try and load data in the worker thread, but ideally, i'd be able in the config to specify the interpreter just like in my main toml file to get the main thread script running.
Andrea Giammarchi 09/07/2024 09:02
hve you tried passing interpreter there?
Avatar
Avatar
ntoll
Not if it's in an infinite loop. I wonder if you could get a reference to the worker's underlying JavaScript object/proxy and call terminate on it..? @Andrea Giammarchi probably has better context to advise on this.
Andrea Giammarchi 09/07/2024 09:04
if you are running the terminal in a worker and you have an infinite loop you should be able to Ctrl+C and get out of it ... otherwise, from the worker reference (through the script or direct) you can always worker.terminate() and kill it ... then you need to bootstrap it again, eventually. This is part of Web Workers API though, nothing really too magic from our side.
09:08
actually ... if the infinite loop is running within the worker already, Ctrl+C might not be captured so terminate() it is.
Avatar
Avatar
CyberneticDaisy
pyscript.web.elements question: is there a simple way to set an attribute like aria-label or data-tooltip on an html element? or do I need to dig into _dom_element? I've made it work okay using _dom_element but that prevents me from using the nice declarative style of creating elements
CyberneticDaisy 09/07/2024 10:22
I might use jinja2 templates in pyscript instead of building everything with pyscript.web. I'll see what fits my needs better
Avatar
Avatar
CyberneticDaisy
I might use jinja2 templates in pyscript instead of building everything with pyscript.web. I'll see what fits my needs better
check out puepy which is reactive and can use jinja2 https://github.com/kkinder/puepy
Python+Webassembly Frontend Framework via PyScript - kkinder/puepy
Avatar
Avatar
Neon22
check out puepy which is reactive and can use jinja2 https://github.com/kkinder/puepy
CyberneticDaisy 09/07/2024 10:26
ooh i did actually stumble across that a few hours ago, will look into it further thanks
Avatar
Avatar
CyberneticDaisy
I might use jinja2 templates in pyscript instead of building everything with pyscript.web. I'll see what fits my needs better
chris.laffra 09/07/2024 10:51
I write all my PyScript UIs in Python, not HTML/JavaScript. See https://github.com/pyscript/ltk
LTK is a little toolkit for writing UIs in PyScript - pyscript/ltk
Avatar
CyberneticDaisy 09/07/2024 10:52
ooh that looks like it would do what I want, thanks!
Avatar
Avatar
Andrea Giammarchi
hve you tried passing interpreter there?
I have. It seems to have no effect. If I don't specify the type as pyodide, nothing works. If I do specify it, it seems to ignore interpreter as a value.
Avatar
Avatar
dmmiller
I have. It seems to have no effect. If I don't specify the type as pyodide, nothing works. If I do specify it, it seems to ignore interpreter as a value.
Andrea Giammarchi 09/07/2024 12:32
sorry ... it might be version instead for some reason ... please give it a try, and outside the config
Avatar
Avatar
Andrea Giammarchi
sorry ... it might be version instead for some reason ... please give it a try, and outside the config
That doesn't see to work for me either : still doesn't attempt to download.
Avatar
Avatar
dmmiller
That doesn't see to work for me either : still doesn't attempt to download.
@Andrea Giammarchi I was able to make it work by patching the node_modules/@pycsript/core/dist/core-Di0g8mUD.js file changing module:(e="0.26.1")=>https://cdn.jsdelivr.net/pyodide/v${e}/full/pyodide.mjs`` to module:(e="0.26.1")=>${globalThis.origin}/pyodide/pyodide.mjs``
Avatar
Avatar
dmmiller
@Andrea Giammarchi I was able to make it work by patching the node_modules/@pycsript/core/dist/core-Di0g8mUD.js file changing module:(e="0.26.1")=>https://cdn.jsdelivr.net/pyodide/v${e}/full/pyodide.mjs`` to module:(e="0.26.1")=>${globalThis.origin}/pyodide/pyodide.mjs``
This felt a bit dicey, but it did work. I then serve that data up just as recommended in the Use Offline section of help
Avatar
Avatar
dmmiller
This felt a bit dicey, but it did work. I then serve that data up just as recommended in the Use Offline section of help
Andrea Giammarchi 09/07/2024 14:56
you can't trust that name for that module ... it's gonna be different every single time after releases ... we need to allow that with procedural workers so please file an issue in PyScript as I am sure you can use offline workers but maybe we forgot to pass that detail through the PyWorker instance itself ... from JS module everything looks fine to me.
Avatar
Avatar
Andrea Giammarchi
you can't trust that name for that module ... it's gonna be different every single time after releases ... we need to allow that with procedural workers so please file an issue in PyScript as I am sure you can use offline workers but maybe we forgot to pass that detail through the PyWorker instance itself ... from JS module everything looks fine to me.
Yep, I figured it would probably change, but I was willing to do it to unblock right now. I'll file an issue as well.
๐Ÿ™ 1
Avatar
Avatar
Andrea Giammarchi
you can't trust that name for that module ... it's gonna be different every single time after releases ... we need to allow that with procedural workers so please file an issue in PyScript as I am sure you can use offline workers but maybe we forgot to pass that detail through the PyWorker instance itself ... from JS module everything looks fine to me.
Opened https://github.com/pyscript/pyscript/issues/2117 to track it. Thanks for your help!
Checklist I added a descriptive title I searched for other issues and couldn't find a solution or duplication I already searched in Google and didn't find any good information or help What ...
Avatar
Avatar
dmmiller
Opened https://github.com/pyscript/pyscript/issues/2117 to track it. Thanks for your help!
Andrea Giammarchi 09/07/2024 15:58
no, thank you !!! I'll have a look later today or tomorrow morning first thing!
Avatar
PhantomsByte 09/07/2024 18:52
hey attention everyone can i use python pip in pyscript if yes then how?
Avatar
for how i"d hope to see https://peps.python.org/pep-0723 soon if not already there
This PEP specifies a metadata format that can be embedded in single-file Python scripts to assist launchers, IDEs and other external tools which may need to interact with such scripts.
Avatar
Avatar
PhantomsByte
hey attention everyone can i use python pip in pyscript if yes then how?
chris.laffra 10/07/2024 10:21
Avatar
Can someone help me with the following code.I am struggling to add Py-script logo to the page.I tried the following but logo doesn't display.https://_12.pyscriptapps.com/pandas-copy-36373-copy/latest/
Avatar
Avatar
zobi
Can someone help me with the following code.I am struggling to add Py-script logo to the page.I tried the following but logo doesn't display.https://_12.pyscriptapps.com/pandas-copy-36373-copy/latest/
Andrea Giammarchi 10/07/2024 15:00
please repeat after me: anything visual on a Web page should never be inside a head element ... also the logo.png is nowhere in your repo, here a cloned example https://pyscript.com/@agiammarchi/pandas-copy-36373-copy-copy/latest (edited)
๐Ÿ‘ 1
15:04
if curious about details, when you have a PyScript element in your <head> it moves itself, or its target visual element, to the document.body ... a Web page has a <head> to explain intents but never to show layouts ... the body element is always what you are after for anything layout related. (edited)
15:06
in a parallel universe, the Web would be like: <!doctype html> <html> <instructions /> <view /> </html> consider those instructions your head element and the view your body tag and you won't have issues whatsoever later on. (edited)
15:08
maybe definition would've been more appropriate too ... instruction is a bit too generic ... <title> as example makes sense only in <head> elements, putting titles anywhere else makes no sense for the browsers ... maybe it works too, but at the search engine level, it's broken
๐Ÿ‘ 1
Avatar
Avatar
Andrea Giammarchi
actually ... if the infinite loop is running within the worker already, Ctrl+C might not be captured so terminate() it is.
Hi, sorry to bother again, but I'm having some trouble terminating from the webworker reference. I tried to follow the documentation (https://docs.pyscript.net/2024.7.1/api/#pyscriptworkers), and .terminate() has no effect: https://pyscript.com/@jbdod/stop-terminal-worker/latest. What's more, I don't think it's possible to list the workers running on a web page from JS, so I can't use the API directly from JS (Or is there a way to get the reference out of python?).
Avatar
Avatar
Andrea Giammarchi
no, thank you !!! I'll have a look later today or tomorrow morning first thing!
Closing the loop. Using your guidance on the issue (https://github.com/pyscript/pyscript/issues/2117) I was able to load a worker from a custom url rather than the default. The secret was to pass a fully qualified URL in as the version parameter. I had tried passing it relative and called interpreter as it is in the config for the main thread.
Avatar
Avatar
dmmiller
Closing the loop. Using your guidance on the issue (https://github.com/pyscript/pyscript/issues/2117) I was able to load a worker from a custom url rather than the default. The secret was to pass a fully qualified URL in as the version parameter. I had tried passing it relative and called interpreter as it is in the config for the main thread.
Andrea Giammarchi 10/07/2024 16:25
yeah, the PyWorker is not fully aligned there in terms of key/value expected pairs, but glad you solved there
Avatar
Avatar
jbdod
Hi, sorry to bother again, but I'm having some trouble terminating from the webworker reference. I tried to follow the documentation (https://docs.pyscript.net/2024.7.1/api/#pyscriptworkers), and .terminate() has no effect: https://pyscript.com/@jbdod/stop-terminal-worker/latest. What's more, I don't think it's possible to list the workers running on a web page from JS, so I can't use the API directly from JS (Or is there a way to get the reference out of python?).
Andrea Giammarchi 10/07/2024 16:28
try this example https://pyscript.com/@agiammarchi/stop-terminal-worker-copy/latest ... most notably:
  • you don't await worker.terminate() as that's not awaitable, it's a "kill" switch
  • the workers[name] convention is for utilities, it doesn't give you a reference to the worker itself ... to do so, you need to query that very same script node and access its xworker field to then terminate it. This is all food for thoughts on how we could simplify that terminate dance, but as it's not the main use case for workers, I hope you can deal with my revision of your example.
(edited)
Avatar
Thank you! I didn't know the reference was in xworker ๐Ÿ˜…
Avatar
Avatar
jbdod
Thank you! I didn't know the reference was in xworker ๐Ÿ˜…
Andrea Giammarchi 10/07/2024 16:38
we probably didn't specify it in our docs, all good though, we can improve those or think about a better convention! (edited)
16:40
dare I say adding that terminate method to the script itself might indeed fade away some complexity ... still we need to be clear that workers[name] give you access to utilities exported by that worker, not the worker itself, as that's a different API
Avatar
Hello everyone, I'm looking for some assistance. I have a translation API hooked up to my python script which sends translated text back to my HTML script after receiving a users input. I'm having an issue saving that translated text in my browsers local storage within the HTML script. I have tried using localStorage and sessionStorage but they do not work (I'm not sure if I was using them incorrectly). Any help will be greatly appreciated. Also I have blurred out things which are confidential or otherwise unnecessary.
Avatar
Avatar
Blank
Hello everyone, I'm looking for some assistance. I have a translation API hooked up to my python script which sends translated text back to my HTML script after receiving a users input. I'm having an issue saving that translated text in my browsers local storage within the HTML script. I have tried using localStorage and sessionStorage but they do not work (I'm not sure if I was using them incorrectly). Any help will be greatly appreciated. Also I have blurred out things which are confidential or otherwise unnecessary.
chris.laffra 10/07/2024 20:15
The way I use localStorage as "pyscript.window.localStorage.getItem(key)" and the corresponding "setItem" function.
Avatar
Ok I will try it out thanks.
Avatar
Suggestions on the friendliest way to give a user to input a DataFrame (or CSV data) into a PyScript page? (edited)
Avatar
trying to import the package PyRTF3. not working in toml as packages = [ "pyscript-ltk>=0.1.2", "emfs://wheels/PyRTF3-0.47.5-py3-none-any.whl"] or as packages = [ "PyRTF3"] the whll never gets past initialisation on pyscript.com. Any hints ?
Avatar
Avatar
chris.laffra
The way I use localStorage as "pyscript.window.localStorage.getItem(key)" and the corresponding "setItem" function.
Andrea Giammarchi 11/07/2024 08:54
remember we have a storage too which is persistent and more pythonic https://docs.pyscript.net/2024.7.1/api/#pyscriptstorage
Avatar
@Jeff Glass Would you mind modifying your download recipe for getting a png saved from a canvas ? I can't work out how to get the canvas.toDataURL() https://pyscript.recipes/latest/basic/file-download/
Avatar
Is there any possibility of having drag and drop in between the projects?
Avatar
Has anyone here had experience using pyscript-react? It's very undocumented and for some reason the hook usePyscript always returns null.
Avatar
Hey - I am making a project with two env on is a repl for a device and the other is one where the user using matplotlib is there a way to share env variables betweeen the two? My current plan was to throw it on the dom and read it.
Avatar
Avatar
Driver
Has anyone here had experience using pyscript-react? It's very undocumented and for some reason the hook usePyscript always returns null.
Andrea Giammarchi 19/07/2024 17:38
a good question on their projects ... happy to help there as we can though
Avatar
Avatar
Eric
Hey - I am making a project with two env on is a repl for a device and the other is one where the user using matplotlib is there a way to share env variables betweeen the two? My current plan was to throw it on the dom and read it.
Andrea Giammarchi 19/07/2024 17:40
if you trash anything on the main thread both projects will eventually be able to read that value ... that being said, you need a mechanism to notify other projects can read such value, and window.addEventListener or window.dispatchEvent there might help.
Avatar
elliot0x01 20/07/2024 10:06
Hi as we have rich and textualize for python, do we have something similar to be used when we are using micropython ?
10:06
Lightweight, pure-Python Text User Interface (TUI) widget toolkit with minimal dependencies. Dedicated to the Pycopy project. - pfalcon/picotui
10:07
Was wondering if there are better libraries than this which can help ship TUIs with micropython and pyscript
Avatar
Avatar
elliot0x01
Was wondering if there are better libraries than this which can help ship TUIs with micropython and pyscript
Avatar
elliot0x01 20/07/2024 17:18
Damnn these looks awesome but the question is will they work with micropython ?
Avatar
Avatar
elliot0x01
Damnn these looks awesome but the question is will they work with micropython ?
dunno but they can all work with pyodide/cpython-wasm with very few changes
Avatar
Hi. I am trying to create an bootstrap offcanvas object (https://getbootstrap.com/docs/5.0/components/offcanvas/#methods) in order to call its hide() method. What I am doing so far: from pyscript import document from js import bootstrap offcanvas_element = document.querySelector('#myOffcanvas') offcanvas = bootstrap.Offcanvas(offcanvas_element).new() However, calling new() causes error: pyodide.ffi.JsException: TypeError: Class constructor qn cannot be invoked without 'new' Any ideas why? (edited)
โœ… 1
Avatar
Kullanฤฑcฤฑ 22/07/2024 10:14
Can you dm me to help me write code in a game
Avatar
Has anyone tried writing actual web components (ie, CustomElementRegistry.define()) using PyScript?
Avatar
Hi guys, I've been trying to integrate a pyscript editor component into my project (pyzombis) to create ui lessons based on pyscript ltk. The idea is to eventually be able to let users write ltk code in the editor and the see the results (ui widgets) inside an output box.
21:23
Like in this image
21:27
this is the link to the PoC version: https://pyscript.com/@aayush05/late-star/latest
21:31
While this works fine but when I try to integrate it to my project like this I get a sharedarraybuffer error and an ever loading py-editor, I tried reading the FAQs and using minicoi but nothing really worked for me. It'll be really helpful if someone could point to me why this is happening.
21:35
The project I am trying to integrate this into is built using RuneStoneComponents, also in the past I have integrated a similar editor component using py-repl with an older pyscript version in the same project which works fine but I want to leverage ltk this time so I can not use that.
21:36
I'd appreciate any help, also please do let me know if you think there is a better way to achieve the same or if this is infeasible!
Avatar
chris.laffra 24/07/2024 09:53
@Andrea Giammarchi See this SharedArrayBuffer issue ^^^. Will you latest changes address issues like this, by "just making it work?"
Avatar
Avatar
aayush
Hi guys, I've been trying to integrate a pyscript editor component into my project (pyzombis) to create ui lessons based on pyscript ltk. The idea is to eventually be able to let users write ltk code in the editor and the see the results (ui widgets) inside an output box.
chris.laffra 24/07/2024 09:55
I created a quick demo on pyscript.com that uses LTK, MicroPython, and a live CodeMirror editor to edit code and make it reload in the UI immediately. See: https://pyscript.com/@laffra/ltk-live-editor/latest I have not yet looked at py-editor....
Avatar
Avatar
chris.laffra
@Andrea Giammarchi See this SharedArrayBuffer issue ^^^. Will you latest changes address issues like this, by "just making it work?"
Andrea Giammarchi 24/07/2024 11:21
without native support or explicit helper to fallback / polyfill synchronous Atomic.wait operations there is no DOM access in workers so ... yes and no: yes because it helps integrating with already used Service Workers (something mini-coi can't do) and no, it can't fix/patch automatically itself because the service worker related file MUST be installed and served via the server.
Avatar
Avatar
aayush
While this works fine but when I try to integrate it to my project like this I get a sharedarraybuffer error and an ever loading py-editor, I tried reading the FAQs and using minicoi but nothing really worked for me. It'll be really helpful if someone could point to me why this is happening.
Andrea Giammarchi 24/07/2024 11:23
I don't see <script src="mini-coi.js"></script> on top of your <head> ... once you have that, you should have no issues. Feel free to literally copy and paste this content into mini-coi.js then add that script that should NOT be a module and should be there before anything else.
Avatar
elliot0x01 26/07/2024 10:41
is pydom documentation available yet?
Avatar
Avatar
elliot0x01
is pydom documentation available yet?
Andrea Giammarchi 26/07/2024 12:07
it's pyscript.web now, if interested, and yes, it's docummented https://docs.pyscript.net/2024.7.1/api/#pyscriptweb
โค๏ธ 1
Avatar
Hi I'm importing these libraries and it's taking forever to load can someone please share a solution?
14:49
Avatar
Avatar
amogh
Hi I'm importing these libraries and it's taking forever to load can someone please share a solution?
chris.laffra 27/07/2024 19:43
When you open the Chrome devtools and go to the Network tab, you can see how long (and big) each package is. The very first time, you browser will need to cache the files, but future runs should go faster....
Avatar
chris.laffra 27/07/2024 21:22
Recently I have been seeing Pandas warnings. Is this a PyOdide or PyScript issue?
Avatar
Hi all! Is there a way currently to get VS Code (and other editors) intellisense working with the pyscript package? If not, is that something in the works or does anybody know of anyone working on that? This would be quite useful, though I can see why this might be a challenge... (edited)
Avatar
Avatar
Kenny
Hi all! Is there a way currently to get VS Code (and other editors) intellisense working with the pyscript package? If not, is that something in the works or does anybody know of anyone working on that? This would be quite useful, though I can see why this might be a challenge... (edited)
Andrea Giammarchi 29/07/2024 22:26
to be honest the reason we bootstrap our projects in PyScript .com with a main.py as <script src="..."> is that you get intellisense for Python out of the box. We need a plugin for VSCode/ium to do the same on <script type="py|mpy"> but currently that's not on the pipe ... unless you are referring to our pyscript module namespace, in which case we also would need a plugin for VSCode/ium but that might be even harder to bring in. (edited)
Avatar
@Andrea Giammarchi You wouldn't even need a VS Code plugin if we could just install a pypi package with either the pyscript core pure python code or stubs, right?
Avatar
Avatar
Kenny
@Andrea Giammarchi You wouldn't even need a VS Code plugin if we could just install a pypi package with either the pyscript core pure python code or stubs, right?
Andrea Giammarchi 29/07/2024 23:50
correct, I wasn't sure you were after the pyscript namespace / module or not ... that pure python wheel would need a lot of assumptions around the worker/non-worker DOM and JS magic in it though, it would be a dummy module in pypi and I am not sure that's a (welcomed) thing in there ๐Ÿค”
Avatar
Hmm... Fair enough. Seems like something we need a solution for long-term as the ecosystem grows, though. And making an extension for each IDE to be supported when they all support type stubs now seems... Not ideal to me ๐Ÿ˜ฌ
00:08
In PEP 561, it says you can distribute stubs separately with the syntax *-stubs, and pandas actually provides stubs in pypi (pandas-stubs). That's all the proof I need! ๐Ÿ˜‚ https://github.com/pandas-dev/pandas-stubs (edited)
Public type stubs for pandas. Contribute to pandas-dev/pandas-stubs development by creating an account on GitHub.
Avatar
chris.laffra 30/07/2024 16:54
"pip install pyscript-stubs" would be great to have for development in VS Code.
Avatar
HalwaHacker 31/07/2024 10:04
Does anyone know how to:
  • Create offline pyscript on chromebook
  • import the pyscript core onto html file
  • create python terminal
  • create a place to write micropip package install commands
(edited)
Avatar
๐Ÿ‘‹ @HalwaHacker offline PyScript, using PyScript in local html and creating a Python terminal are all explained in our docs https://docs.pyscript.net/ Specifically, look in the user guide section. Not sure what you mean by that last point. More context and a use case would be helpful so we can suggest something. ๐Ÿ‘ (edited)
Avatar
Avatar
chris.laffra
When you open the Chrome devtools and go to the Network tab, you can see how long (and big) each package is. The very first time, you browser will need to cache the files, but future runs should go faster....
Thanks but seems like it's loading for a different reason I'm getting the fetch at '.....' from origin '....' has been blocked by CORS policy error do you have any fix for that?
13:26
Avatar
Avatar
amogh
Click to see attachment ๐Ÿ–ผ๏ธ
Andrea Giammarchi 01/08/2024 09:05
if pypi does not want the internet to fetch from other domains or extensions we cannot really fix much ... and if we default to no-cors fetch operations we'll have other issues.
Avatar
Avatar
Andrea Giammarchi
if pypi does not want the internet to fetch from other domains or extensions we cannot really fix much ... and if we default to no-cors fetch operations we'll have other issues.
Andrea Giammarchi 01/08/2024 09:08
could you please try to put this script before anything else in your <head> element and see if that fixes the issue? <script> globalThis.fetch = ($ => (url, options) => $(url, { ...options, mode: 'no-cors' }))(fetch); </script>
Avatar
I want to run this by folks before I report as a bug. I'm seeing some odd behavior in 2024.8.1 that did not exist in the prior version. I'm getting "Unhandled Promise Rejection: Error: Object has already been destroyed For more information about the cause of this error, use pyodide.setDebug(true)" for calls from the JavaScript side to the Python side (example on the javascript side would be let r = await checkCommSystem();; checkCommSystem is a function on the Python side). On the python side this is how I'm setting up the communication between the two sides: Near the top: from pyscript import window as js At the end: js.startBootstrapWrapper = startBootstrapWrapper js.buildTableWrapper = buildTableWrapper js.calcMeansSDMW = calcMeansSDMW js.passFileData = passFileData js.getListData = getListData js.checkCommSystem = checkCommSystem Where each one of these items is a async function. Is this a bug or are we changing how we are supposed to setup the communication between the two sides? My python code is running in a worker. (edited)
Avatar
Avatar
Ben Smith
I want to run this by folks before I report as a bug. I'm seeing some odd behavior in 2024.8.1 that did not exist in the prior version. I'm getting "Unhandled Promise Rejection: Error: Object has already been destroyed For more information about the cause of this error, use pyodide.setDebug(true)" for calls from the JavaScript side to the Python side (example on the javascript side would be let r = await checkCommSystem();; checkCommSystem is a function on the Python side). On the python side this is how I'm setting up the communication between the two sides: Near the top: from pyscript import window as js At the end: js.startBootstrapWrapper = startBootstrapWrapper js.buildTableWrapper = buildTableWrapper js.calcMeansSDMW = calcMeansSDMW js.passFileData = passFileData js.getListData = getListData js.checkCommSystem = checkCommSystem Where each one of these items is a async function. Is this a bug or are we changing how we are supposed to setup the communication between the two sides? My python code is running in a worker. (edited)
Andrea Giammarchi 01/08/2024 16:00
if you could create a minimal example of what issue you are having we can investigate ... from your code I don't understand anything that is going on. Remember though, in Pyodide you need to create_proxy when you want callbacks to be kept alive and never be garbage collected ... the only hint I have is that you are using eval somewhere and the engine couldn't know what you are going to evaluate, so that the reference is marked as not needed anymore and removed from the running context. We do take care a lot about memory leaks, so I kinda prefer less leaks and breaking things than having unspotted leaks out there, if that makes sense.
Avatar
Avatar
Andrea Giammarchi
if you could create a minimal example of what issue you are having we can investigate ... from your code I don't understand anything that is going on. Remember though, in Pyodide you need to create_proxy when you want callbacks to be kept alive and never be garbage collected ... the only hint I have is that you are using eval somewhere and the engine couldn't know what you are going to evaluate, so that the reference is marked as not needed anymore and removed from the running context. We do take care a lot about memory leaks, so I kinda prefer less leaks and breaking things than having unspotted leaks out there, if that makes sense.
Andrea Giammarchi 01/08/2024 16:03
on the other hand, while this would demonstrate the underlying stack is working better than ever, I might indeed relax assignments to the gobal context but it's not clear to me why you would need all that. The logic there is that you assign things never used, referenced, retained, in your current code so that the Garbage Collector feels like "yeah, all these references can just go now as never needed out there" and it wouldn't be necessarily wrong. On the window maybe, but on window random references, I don't know.
16:04
at the same time ... how are you accessing those things later on? 'cause things landing on main also get tracked and if nothing uses these the GC again does its job ... so please provide a minimal example of the issue, and I'll look into it in no time.
Avatar
Andrea Giammarchi 01/08/2024 16:12
basically if you could add at the end js.thing = create_proxy(thing) where create_proxy comes from pyodide.ffy or even pyscript.ffi I think your issue should vanish.
Avatar
Andrea Giammarchi 01/08/2024 16:45
it'd be lovely if you could confirm it ... as a lot changed behind the scene but theoretically for good ๐Ÿ˜…
Avatar
Thanks so much for your help! Unfortunately, that didn't seem to fix it. I've created a minimal example (attached). I also have this example running at https://minimal-test-project-431218.uc.r.appspot.com
509 bytes
185 bytes
Avatar
Look Behind You 01/08/2024 20:19
Hi I am a beginner at using pyscript. I wanted to use an api from github and output into the website through pyscript. I had some trouble importing the modules necessary to make the code run. I was wondering how I can do that
Avatar
Avatar
Ben Smith
Thanks so much for your help! Unfortunately, that didn't seem to fix it. I've created a minimal example (attached). I also have this example running at https://minimal-test-project-431218.uc.r.appspot.com
Andrea Giammarchi 01/08/2024 20:56
ok ... this took a couple of indirect patches to actually understand what you are doing ... <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Test</title> <link rel="stylesheet" href="https://pyscript.net/releases/2024.8.1/core.css"> <script type="module" src="https://pyscript.net/releases/2024.8.1/core.js"></script> </head> <body> <script src="./onloadCode.js"></script> <script type="py" src="./startup.py" worker></script> </body> </html>
20:57
onloadCode.js being like this
20:57
window.addEventListener("py:all-done", async (event) => { console.log(await checkCommSystem()); });
20:58
and sturtup.py bein like this from pyscript import window as js def checkCommSystem(): return True js.checkCommSystem = checkCommSystem
21:00
now, this all works nicely ... but the moment you introduce asyncio (why?) and you do this: async def checkCommSystem(): return True
21:00
it's the moment nobody understand anymore what's going on ...
21:02
any callback trashed to the window global namespace understands callbacks, although there might be an issue with async callbacks but nothing throws on MicroPython side, it does on Pyodide side (edited)
21:07
I've no idea why previous version of PyScript worked, but I am interested into learning more why you need to export, as global callbacks, your current async logic. Anything invoked on the main thread is async by default, so probably you don't need that?
Avatar
Avatar
Look Behind You
Hi I am a beginner at using pyscript. I wanted to use an api from github and output into the website through pyscript. I had some trouble importing the modules necessary to make the code run. I was wondering how I can do that
Andrea Giammarchi 01/08/2024 21:11
a bit vague of a question ... anything to share we can hekp with?
Avatar
Thanks Andrea. The intent here was a minimal example. In my real app, which is statistics related, I have async functions with await within them. This makes the user interface responsive even when multiple different things might be triggered from the interface side.
Avatar
Avatar
Ben Smith
Thanks Andrea. The intent here was a minimal example. In my real app, which is statistics related, I have async functions with await within them. This makes the user interface responsive even when multiple different things might be triggered from the interface side.
Andrea Giammarchi 01/08/2024 21:13
I see ... I need to dig into why/what Pyodide returns in those cases and understand why our previous version worked at all before
Avatar
Avatar
Andrea Giammarchi
a bit vague of a question ... anything to share we can hekp with?
Look Behind You 01/08/2024 21:14
ah sorry i didnt word the question properly at all my bad!
๐Ÿ‘ 1
Avatar
Avatar
Andrea Giammarchi
I see ... I need to dig into why/what Pyodide returns in those cases and understand why our previous version worked at all before
Andrea Giammarchi 01/08/2024 21:15
somehow, nothing changed in there so far, so this is odd (edited)
Avatar
Look Behind You 01/08/2024 21:15
I am using this API https://github.com/spectrshiv/python-weather_gov and wanted to implement into pyscript
A free API wrapper for Weather.gov made in base Python. - spectrshiv/python-weather_gov
21:16
I was having trouble importing in some of the files since there are multiple files, but I have them in the same folder
21:16
I assumed If i just imported in the file that is outputting the code it would be fine, but it keeps erroring out saying a modulenotfound error on weather_gov
21:17
I used py_config to help with this issue, and also imported json but now it is not even outputting a website like it used too
Avatar
Avatar
Look Behind You
I am using this API https://github.com/spectrshiv/python-weather_gov and wanted to implement into pyscript
Andrea Giammarchi 01/08/2024 21:17
any chance you can put a basic exampe in pyscript.com?
Avatar
Look Behind You 01/08/2024 21:18
sure let me try that
21:19
do i dm you the link
21:19
or how would I go about this
Avatar
Andrea Giammarchi 01/08/2024 21:19
also sorry folks, I am going to sleep soon, don't feel ignored if I am not answering anymore
Avatar
Look Behind You 01/08/2024 21:20
thank you for answering so quickly
21:20
yep it is still not outputting anything either
21:20
it was outputting something at least on vscode (edited)
Avatar
Andrea Giammarchi 01/08/2024 21:21
what live servers?
Avatar
Look Behind You 01/08/2024 21:21
sorry the website
21:21
not live servers meant vscode mb
Avatar
Avatar
Look Behind You
or how would I go about this
Andrea Giammarchi 01/08/2024 21:22
DM me the link if super secret, just post the URL if not
21:24
21:24
here is the error on my side
21:24
i removed the json part so now its working somewhat fine
Avatar
Andrea Giammarchi 01/08/2024 21:28
your demo has a whole package in it, I am not sure it's going to work (edited)
21:29
anyway, sorry folks, I am done for today
Avatar
Look Behind You 01/08/2024 21:29
thank you for the help
Avatar
Andrea Giammarchi 01/08/2024 21:29
keep posting and I will try to help tomorrow
Avatar
Look Behind You 01/08/2024 21:29
Are there any other alternatives I could use other than pyscript?
Avatar
Thank you @Andrea Giammarchi for your excellent support!
Avatar
you probably only need the files under weather_gov. You will also need to point to them in the toml and import them into your main.py. As a working example to look at consider the ltk example on pyscript.com. It pulls in the files it needs from github directly rather than host locally. also the toml is a good example of how to do that and the main.py shows the import. - ltk on pyodide example in here: - https://pyscript.com/@laffra (edited)
Avatar
Hmmm. Can't work out how to do it. There are no examples in doc about importing a local module like this. Tried a few variations on editing the three files under weather_gov and importing and the toml and its not clear. I hope someone who knows will be able to reply and maybe add a bit to the docs.
๐Ÿฅบ 2
Avatar
not to deter you from that specific library but there is one available on pypi which appears to be python only and might work with a packages setup in toml file. - https://pypi.org/project/python-weather/ in toml packages = ["python-weather"]
A free and asynchronous weather API wrapper made in Python, for Python.
Avatar
Look Behind You 02/08/2024 07:32
ok thank you @Neon22 i will look into this
Avatar
Avatar
Ben Smith
Thanks so much for your help! Unfortunately, that didn't seem to fix it. I've created a minimal example (attached). I also have this example running at https://minimal-test-project-431218.uc.r.appspot.com
Andrea Giammarchi 02/08/2024 10:01
OK, my previous investigation was actually wrong ... but now I have a working solution that also gives you results, it doesn't just run the task ... check this out: <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width,initial-scale=1.0"> <link rel="stylesheet" href="https://pyscript.net/releases/2024.8.1/core.css"> <script type="module" src="https://pyscript.net/releases/2024.8.1/core.js"></script> </head> <body> <script> addEventListener('py:done', async () => { console.log({ result: await test() }); }); </script> <script type="py" worker> from pyscript import window async def test(): print("test") return True # enforce pyodide async resolution when # the attached method is an async def def attach_async(fn): from pyscript import window, RUNNING_IN_WORKER if not RUNNING_IN_WORKER: return fn from pyscript.ffi import create_proxy cb = create_proxy(fn) def invoker(*args, **kw): p = window.Promise.withResolvers() cb(*args, **kw).add_done_callback(lambda _:p.resolve(_.result())) return p.promise return invoker window.test = attach_async(test) </script> </body> </html>
10:01
nothing like this was previously part of our code base so I really struggle to understand how this could've worked before ... the callback returns a coroutine (PyodideFuture or Task) that has no then in it so that the callback would've resolved without the actual result. As apparently there's no way to run_until_complete in a blocking way (no matter what I do, no matter if async) and pyodide.ffi.run_sync was breaking too, that dance is needed to ensure that the task runs and once it's done it's result is passed to the returned promise which is understood by the main and awaited. If older PyScript (before current changes) was working it's worth investigating what the hell is going on because I don't know or understand why it was working before (or why it wouldn't now). At least there is a workaround for this though
Avatar
Avatar
Andrea Giammarchi
OK, my previous investigation was actually wrong ... but now I have a working solution that also gives you results, it doesn't just run the task ... check this out: <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width,initial-scale=1.0"> <link rel="stylesheet" href="https://pyscript.net/releases/2024.8.1/core.css"> <script type="module" src="https://pyscript.net/releases/2024.8.1/core.js"></script> </head> <body> <script> addEventListener('py:done', async () => { console.log({ result: await test() }); }); </script> <script type="py" worker> from pyscript import window async def test(): print("test") return True # enforce pyodide async resolution when # the attached method is an async def def attach_async(fn): from pyscript import window, RUNNING_IN_WORKER if not RUNNING_IN_WORKER: return fn from pyscript.ffi import create_proxy cb = create_proxy(fn) def invoker(*args, **kw): p = window.Promise.withResolvers() cb(*args, **kw).add_done_callback(lambda _:p.resolve(_.result())) return p.promise return invoker window.test = attach_async(test) </script> </body> </html>
Andrea Giammarchi 02/08/2024 10:14
in MicroPython the callback is way simpler: # enforce micropython async resolution when # the attached method is an async def def attach_async(fn): from pyscript import window, RUNNING_IN_WORKER if not RUNNING_IN_WORKER: return fn def invoker(*args, **kw): async def promise(resolve, _): resolve(await fn(*args, **kw)) return window.Promise.new(promise) return invoker
Avatar
Hey Andrea! Thank you! You are the best. I'm one of the people running a conference for the next two days so I probably won't get a chance to try it until Sunday. All I can say on it working on prior versions is that it does (and has for quite a few versions). The production version of my app is at https://projectassessment.app. All of the functions called from the js side are async. (edited)
Avatar
Avatar
Ben Smith
Hey Andrea! Thank you! You are the best. I'm one of the people running a conference for the next two days so I probably won't get a chance to try it until Sunday. All I can say on it working on prior versions is that it does (and has for quite a few versions). The production version of my app is at https://projectassessment.app. All of the functions called from the js side are async. (edited)
Andrea Giammarchi 02/08/2024 16:35
I will check next week what canged ... we did update to latest Pyodide and I am not excluding that might be the reason ... but also we did a lot of refactoring behind the scene for better perf/maintainability when it comes to workers ... I hope it's not in there as it would be weird because everything else just works without issues. In the worst case scenario, we will provide in our ffi a way to attach async listeners safely in either Pyodide or MicroPython which might actually be the best approach to me ... less magic involved. Promises and Future (coroutines) are pretty different beasts
Avatar
Marc Doc Madsen 02/08/2024 17:52
Hi. I'm trying to reduce the boilerplate code I need to write to use PyScript with Panel for example at pyscript.com. I believe the solution is a plugin. I would like to install some python packages in the plugin. For example packages = [ "https://cdn.holoviz.org/panel/wheels/bokeh-3.5.0-py3-none-any.whl", "https://cdn.holoviz.org/panel/wheels/panel-1.5.0b2-py3-none-any.whl" ] I believe I should use one of the Main Hooks. How would I use a hook to install some package onReady or onBeforeRun? This is the solution I'm trying to refactor https://marcskovmadsen.pyscriptapps.com/panel-plugin-poc/v2/
Avatar
Avatar
Andrea Giammarchi
I will check next week what canged ... we did update to latest Pyodide and I am not excluding that might be the reason ... but also we did a lot of refactoring behind the scene for better perf/maintainability when it comes to workers ... I hope it's not in there as it would be weird because everything else just works without issues. In the worst case scenario, we will provide in our ffi a way to attach async listeners safely in either Pyodide or MicroPython which might actually be the best approach to me ... less magic involved. Promises and Future (coroutines) are pretty different beasts
Thanks Andrea! I was able to implement a version of what you suggested in my development version this morning. It works! Two questions (probably related): 1. Is there a pyodide version difference between the 2024.8.1 and 2024.7.1? 2. Should I backport the fix to my production version (which is running 2024.5.2)? My old implementation method worked in version 2024.7.1 (thus question 1); this might narrow down why this worked before. For question 2, I'm not crazy about something working but for a unknown reason. This makes me think it is 'flaky' but I just haven't detected it. (As an aside, I'm currently on 2024.5.2 in production because of this warning - https://github.com/pyodide/pyodide/issues/4840 - which is fixed in https://github.com/pyodide/pyodide/pull/4893). (edited)
๐Ÿ› Bug Pyodide is resulting in Pandas sending out a depreciation warning: <exec>:5: DeprecationWarning: [Error] Pyarrow will become a required dependency of pandas in the next major release of...
GitHub is where people build software. More than 100 million people use GitHub to discover, fork, and contribute to over 420 million projects.
Avatar
I'm pretty sure I'm being a real bone-head, but for some reason, my pyscript code which I load this way <script type="py" src="/pyscript/c_and_c_client.py" config="/pyscript/c_and_c_client.toml"></script> Doesn't seem to reload on a Shift F5. Currently the entire content of that file is.... def main(): print("I'm here!") main() But when I reload I get this error ImportError: cannot import name 'console' from 'pyscript' I was trying to do from pyscript import console But when that gave me the error above I decided to cut down my code and rebuild slowly so I could track down what I was doing that was wrong. Also is the above the "correct" way to launch pyscript code?
Avatar
OK I am completely freaking out now as it is now working and reloading on every "Shift-Reload"
23:10
So now I'm back to the question I originally came here to ask.... PythonError: Traceback (most recent call last): File "/lib/python312.zip/_pyodide/_base.py", line 522, in eval_code .run(globals, locals) ^^^^^^^^^^^^^^^^^^^^ File "/lib/python312.zip/_pyodide/_base.py", line 356, in run coroutine = eval(self.code, globals, locals) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "<exec>", line 1, in <module> ImportError: cannot import name 'console' from 'pyscript' (/home/pyodide/pyscript/__init__.py) why?
Avatar
I am not sure but it could be that there is nowhere for the print to go. Try adding terminal in your scrip tag like so: <script type="py" src="..." config="..." terminal></script> I believe these days they might recommend using display() instead of print which defaults to adding text to the dom. https://docs.pyscript.net/2024.8.1/user-guide/first-steps/
Avatar
Avatar
Christian Clauss
Answered my own question: Running pytest in https://pyscript.com was merely a question of calling pytest.main() which I found at https://docs.pytest.org/en/stable/how-to/usage.html#calling-pytest-from-python-code To run the pytests background worker connected to a terminal do: <script type="py" config="./pyscript.toml" terminal worker> import pytest pytest.main() </script> All the file naming for pytest discover, plus the __init__.py files, plus the [files] section of pyscript.toml are required but it work for me! Also see: https://pyscript.com/@antocuni/pytest-in-pyterminal (edited)
CyberneticDaisy 04/08/2024 10:36
I've got pytest working in pyscript, as described here. It would be preferable to have it in color, however it's obvious that this method https://pyscript.com/@antocuni/pytest-in-pyterminal is very much deprecated and I don't know whether there's any current know recipe for a pyscript terminal that supports colour
Avatar
CyberneticDaisy 04/08/2024 13:02
Actually I think I'll try getting JUnitXML output and convert that to an html report instead of using the terminal, which would get unwieldy with many tests
Avatar
CyberneticDaisy 04/08/2024 13:19
No, even better I'll add a log handler which makes the HTML to show test results (with ltk) live, otherwise there won't be feedback until the test suite is complete edit: no, pytest doesn't log, it just captures other logging. instead i use a pytest_runtest_makereport hook (edited)
Avatar
Hi Anyone knows why this produces an error on Firefox with version 8.1 but not on 7.1? <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width,initial-scale=1" /> <title>๐Ÿฆœ Polyglot - Piratical PyScript</title> <!-- <link rel="stylesheet" href="https://pyscript.net/releases/2024.7.1/core.css"> <script type="module" src="https://pyscript.net/releases/2024.7.1/core.js"></script> --> <link rel="stylesheet" href="https://pyscript.net/releases/2024.8.1/core.css"> <script type="module" src="https://pyscript.net/releases/2024.8.1/core.js"></script> </head> <body> <script type="py" worker> print("runs") </script> </script> </body> </html> when using pyscript 2024.8.1 i get TypeError: oe.withResolvers is not a function but only when using the worker, in 2024.7.1 both variants work.
Avatar
Avatar
pattu
Hi Anyone knows why this produces an error on Firefox with version 8.1 but not on 7.1? <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width,initial-scale=1" /> <title>๐Ÿฆœ Polyglot - Piratical PyScript</title> <!-- <link rel="stylesheet" href="https://pyscript.net/releases/2024.7.1/core.css"> <script type="module" src="https://pyscript.net/releases/2024.7.1/core.js"></script> --> <link rel="stylesheet" href="https://pyscript.net/releases/2024.8.1/core.css"> <script type="module" src="https://pyscript.net/releases/2024.8.1/core.js"></script> </head> <body> <script type="py" worker> print("runs") </script> </script> </body> </html> when using pyscript 2024.8.1 i get TypeError: oe.withResolvers is not a function but only when using the worker, in 2024.7.1 both variants work.
Not sure about the version difference you are asking for but prints goto terminal so it has to be <script type="py" terminal worker>
Avatar
Avatar
pattu
Hi Anyone knows why this produces an error on Firefox with version 8.1 but not on 7.1? <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width,initial-scale=1" /> <title>๐Ÿฆœ Polyglot - Piratical PyScript</title> <!-- <link rel="stylesheet" href="https://pyscript.net/releases/2024.7.1/core.css"> <script type="module" src="https://pyscript.net/releases/2024.7.1/core.js"></script> --> <link rel="stylesheet" href="https://pyscript.net/releases/2024.8.1/core.css"> <script type="module" src="https://pyscript.net/releases/2024.8.1/core.js"></script> </head> <body> <script type="py" worker> print("runs") </script> </script> </body> </html> when using pyscript 2024.8.1 i get TypeError: oe.withResolvers is not a function but only when using the worker, in 2024.7.1 both variants work.
Andrea Giammarchi 05/08/2024 09:01
I just tried this in all browsers and I read uns in devtools <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width,initial-scale=1" /> <title>๐Ÿฆœ Polyglot - Piratical PyScript</title> <!-- <link rel="stylesheet" href="https://pyscript.net/releases/2024.7.1/core.css"> <script type="module" src="https://pyscript.net/releases/2024.7.1/core.js"></script> --> <link rel="stylesheet" href="https://pyscript.net/releases/2024.8.1/core.css"> <script type="module" src="https://pyscript.net/releases/2024.8.1/core.js"></script> </head> <body> <script type="py" worker> print("runs") </script> </body> </html> you need to run a server that enables special headers to use workers at their full potentials.
09:02
also your HTML had 2 closing scripts so the page was not well formed
Avatar
Avatar
zobi
Not sure about the version difference you are asking for but prints goto terminal so it has to be <script type="py" terminal worker>
Andrea Giammarchi 05/08/2024 09:05
but prints goto terminal so it has to be <script type="py" terminal worker>
this is not accurate ... prints goes to devtools console if the terminal attribute is missing ... https://developer.chrome.com/docs/devtools/open
All of the ways that you can open Chrome DevTools.
Avatar
Avatar
rhacer
So now I'm back to the question I originally came here to ask.... PythonError: Traceback (most recent call last): File "/lib/python312.zip/_pyodide/_base.py", line 522, in eval_code .run(globals, locals) ^^^^^^^^^^^^^^^^^^^^ File "/lib/python312.zip/_pyodide/_base.py", line 356, in run coroutine = eval(self.code, globals, locals) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "<exec>", line 1, in <module> ImportError: cannot import name 'console' from 'pyscript' (/home/pyodide/pyscript/__init__.py) why?
Andrea Giammarchi 05/08/2024 09:14
there is no console to import from pyscript ... you can import window and then use window.console to use native JS consle otherwise you can just use print
Avatar
Avatar
Ben Smith
Thanks Andrea! I was able to implement a version of what you suggested in my development version this morning. It works! Two questions (probably related): 1. Is there a pyodide version difference between the 2024.8.1 and 2024.7.1? 2. Should I backport the fix to my production version (which is running 2024.5.2)? My old implementation method worked in version 2024.7.1 (thus question 1); this might narrow down why this worked before. For question 2, I'm not crazy about something working but for a unknown reason. This makes me think it is 'flaky' but I just haven't detected it. (As an aside, I'm currently on 2024.5.2 in production because of this warning - https://github.com/pyodide/pyodide/issues/4840 - which is fixed in https://github.com/pyodide/pyodide/pull/4893). (edited)
Andrea Giammarchi 05/08/2024 10:00
I found the culprit ... it's in coincident ... I was not awaiting results as apparently I did before so mystery solved.
Avatar
Avatar
Andrea Giammarchi
I found the culprit ... it's in coincident ... I was not awaiting results as apparently I did before so mystery solved.
Andrea Giammarchi 05/08/2024 10:03
oddly enough, I actually was not explicitly awaiting anything before so it's before that was working "by accident" but glad I've found the issue.
Avatar
Avatar
Ben Smith
Thanks Andrea! I was able to implement a version of what you suggested in my development version this morning. It works! Two questions (probably related): 1. Is there a pyodide version difference between the 2024.8.1 and 2024.7.1? 2. Should I backport the fix to my production version (which is running 2024.5.2)? My old implementation method worked in version 2024.7.1 (thus question 1); this might narrow down why this worked before. For question 2, I'm not crazy about something working but for a unknown reason. This makes me think it is 'flaky' but I just haven't detected it. (As an aside, I'm currently on 2024.5.2 in production because of this warning - https://github.com/pyodide/pyodide/issues/4840 - which is fixed in https://github.com/pyodide/pyodide/pull/4893). (edited)
Andrea Giammarchi 05/08/2024 10:30
the fix has been published on npm as https://cdn.jsdelivr.net/npm/@pyscript/core@0.5.2/dist/core.js (and .css counter-part) ... it'd be great if you could confirm everything works again as expected, thank you. /cc @ntoll ... PR here https://github.com/pyscript/pyscript/pull/2136
Description Turns out methods / functions attached to window proxy were not awaited and this was not playing well with coroutines / tasks.

example

from pyscript import window async def test(): ...
Avatar
Avatar
Andrea Giammarchi
the fix has been published on npm as https://cdn.jsdelivr.net/npm/@pyscript/core@0.5.2/dist/core.js (and .css counter-part) ... it'd be great if you could confirm everything works again as expected, thank you. /cc @ntoll ... PR here https://github.com/pyscript/pyscript/pull/2136
Yup, everything works using the code without the attach_async function wrapper. Thanks Andrea for looking into this so quick. My question to you: you noted that this worked by accident. Is it a best practice then to have the wrapper function in place even if it works without it?
Avatar
Avatar
Ben Smith
Yup, everything works using the code without the attach_async function wrapper. Thanks Andrea for looking into this so quick. My question to you: you noted that this worked by accident. Is it a best practice then to have the wrapper function in place even if it works without it?
Andrea Giammarchi 05/08/2024 12:15
it didn't work by accident before, it worked because the promise was consumed before leaving the worker global context but it was done on a different code path https://github.com/WebReflection/coincident/blob/v1/esm/index.js#L149 and I am not sure it was returning the correct result neither ... although I haven't investigated too much the old coincident code as that's ... well, the old one, te new one is easier to reason about and maintain, indeed it didn't take too long to figure out where the async clalback was not consumed, before it was in the mix of the "evrything" that was passing either on main or workers through the same shared code, which worked well, but it was harder to branch out in terms of logic or pin-point what should've been different, like in this specific case. Latest patches only the main-calling-worker use case and, as that's always asynchronous anyway, it's safe to assume it was the right fix to do. It's also possible that it was in here https://github.com/WebReflection/coincident/blob/main/src/window/main.js#L91 , without awaiting it, that the thing was not instantly consumed but at the same time that happens on main, while the await should happen whenever the worker utility is defined, which is in the worker indeed. In short, I am confident the current patch "just works" and it makes sense too. Before? ... I am not super sure why or how that worked but it clearly did, and that's why I wanted to rewrite coincident, to be sure about what happens ๐Ÿ˜‰
An Atomics based Proxy to simplify, and synchronize, Worker related tasks. - WebReflection/coincident
An Atomics based Proxy to simplify, and synchronize, Worker related tasks. - WebReflection/coincident
Avatar
Avatar
Andrea Giammarchi
there is no console to import from pyscript ... you can import window and then use window.console to use native JS consle otherwise you can just use print
Thank you.
Avatar
Avatar
Andrea Giammarchi
but prints goto terminal so it has to be <script type="py" terminal worker>
this is not accurate ... prints goes to devtools console if the terminal attribute is missing ... https://developer.chrome.com/docs/devtools/open
Can you share an example related to this because it didn't work for me.
Avatar
Avatar
zobi
Can you share an example related to this because it didn't work for me.
Andrea Giammarchi 05/08/2024 14:58
you write this in your index.html page https://discord.com/channels/972017612454232116/972020206538997822/1269928297618214943 and you open devtools to read the printed value in console.
Avatar
Avatar
Andrea Giammarchi
it didn't work by accident before, it worked because the promise was consumed before leaving the worker global context but it was done on a different code path https://github.com/WebReflection/coincident/blob/v1/esm/index.js#L149 and I am not sure it was returning the correct result neither ... although I haven't investigated too much the old coincident code as that's ... well, the old one, te new one is easier to reason about and maintain, indeed it didn't take too long to figure out where the async clalback was not consumed, before it was in the mix of the "evrything" that was passing either on main or workers through the same shared code, which worked well, but it was harder to branch out in terms of logic or pin-point what should've been different, like in this specific case. Latest patches only the main-calling-worker use case and, as that's always asynchronous anyway, it's safe to assume it was the right fix to do. It's also possible that it was in here https://github.com/WebReflection/coincident/blob/main/src/window/main.js#L91 , without awaiting it, that the thing was not instantly consumed but at the same time that happens on main, while the await should happen whenever the worker utility is defined, which is in the worker indeed. In short, I am confident the current patch "just works" and it makes sense too. Before? ... I am not super sure why or how that worked but it clearly did, and that's why I wanted to rewrite coincident, to be sure about what happens ๐Ÿ˜‰
Thanks you so much for the detailed explanation.
Avatar
Avatar
Ben Smith
Thanks you so much for the detailed explanation.
Andrea Giammarchi 05/08/2024 15:19
we're planning to release soon so that you should have no issue whatsoever from 2024.8.2 and live happily ever after, at least that's our hope ๐Ÿ™‚
Avatar
Avatar
Andrea Giammarchi
we're planning to release soon so that you should have no issue whatsoever from 2024.8.2 and live happily ever after, at least that's our hope ๐Ÿ™‚
Do you know if the next release will also update pyodide to 0.26.2?
Avatar
Avatar
Andrea Giammarchi
you write this in your index.html page https://discord.com/channels/972017612454232116/972020206538997822/1269928297618214943 and you open devtools to read the printed value in console.
It shows nothing that's the reason I asked. https://_12.pyscriptapps.com/summer-recipe/latest/
Avatar
Avatar
Ben Smith
Do you know if the next release will also update pyodide to 0.26.2?
Andrea Giammarchi 05/08/2024 16:00
the current already has it, so it's there to stay?
16:01
2024.8.2 is out though, enjoy the ride ๐Ÿฅณ
Avatar
Avatar
zobi
It shows nothing that's the reason I asked. https://_12.pyscriptapps.com/summer-recipe/latest/
Andrea Giammarchi 05/08/2024 16:02
what do you mean?
Avatar
Avatar
Andrea Giammarchi
what do you mean?
worker supports user inputs but why is it showing error?
Avatar
Avatar
zobi
worker supports user inputs but why is it showing error?
Andrea Giammarchi 06/08/2024 07:27
there is no prompt in Workers, you can use input though ... but that's unrelated with the devtools not showing logs?
07:28
also, if you really want prompt you need to use the window.prompt one
07:28
but that's unnecessary, as there is input there.
Avatar
Awesome. My stuff works with 2024.8.2.
๐Ÿ‘ 1
Avatar
Hi all, I'm using py-repl and I'm trying to execute a python code when the page is loaded, but it doesn't work. I read something about the DOMContentLoaded but I got the following error: ncaught TypeError: Cannot read properties of null (reading 'value') at window.onload (index.html:24:56), how can I fix this error?
Avatar
Avatar
xalex
Hi all, I'm using py-repl and I'm trying to execute a python code when the page is loaded, but it doesn't work. I read something about the DOMContentLoaded but I got the following error: ncaught TypeError: Cannot read properties of null (reading 'value') at window.onload (index.html:24:56), how can I fix this error?
The version you are using is VERY old. Please try 2024.8.2 Docs here: https://docs.pyscript.net/2024.8.1/user-guide/first-steps/ also consider using pyscript.com as its easy setup and you can clone examples
๐Ÿ‘ 2
Avatar
Andrea Giammarchi 07/08/2024 12:58
to whom it might concern, there was an issue about Python blocking the main thread when doing heavy computation and the solution was to detach via asyncio that heavy computation so that the DOM has a chance to "tick" and show changes. I am sure this would help anyone else not using worker out there so the solution is here for history sake: https://github.com/pyscript/pyscript/issues/2135#issuecomment-2271260217
Checklist I added a descriptive title I searched for other issues and couldn't find a solution or duplication I already searched in Google and didn't find any good information or help What ...
Avatar
Hello everyone, little question about syntax, when we use the fetch function (which is async) we dont have to wrap it into a async function, so my lsp is considering it as wrong, is there something to do to avoid this ?
19:23
Same type of question but how can we get type completion etc when using pyscript, because even if we import it in the file, the package is not present in our dev folder
Avatar
Avatar
Loic
Hello everyone, little question about syntax, when we use the fetch function (which is async) we dont have to wrap it into a async function, so my lsp is considering it as wrong, is there something to do to avoid this ?
Andrea Giammarchi 07/08/2024 20:50
we run top-level await by default now, what is your concern?
Avatar
Avatar
Loic
Same type of question but how can we get type completion etc when using pyscript, because even if we import it in the file, the package is not present in our dev folder
Andrea Giammarchi 07/08/2024 20:50
for that we need to work out how to export our module as pip /cc @ntoll
Avatar
fixingbrokenrobots 08/08/2024 02:24
I've had a Pyscript page running for months (release 2024.05.02). Today I discovered that the page was broken. It was failing with the console error: Uncaught (in promise) PythonError: Traceback (most recent call last): File "/lib/python3.11/site-packages/micropip/_commands/install.py", line 146, in install raise ValueError( ValueError: Can't find a pure Python 3 wheel for: 'jiter<1,>=0.4.0' Since it worked perfectly before, something must've changed, but I'm having difficulty figuring out what and how to fix it. Any help would be greatly appreciated. Here's the page for reference https://rstory.io/vectorSearch.html
Avatar
it could not find a py3-none-any wheel
02:51
my wild guess is one of your package now rely on jiter and did not previously before lastest release
Avatar
fixingbrokenrobots 08/08/2024 04:51
Do you know how I might identify which package and correct the problem? As I'm not using jiter is there a way to force skip the install?
Avatar
grab them all and have a look to their requirements, also various pip deps tree tools avail on pypi (edited)
Avatar
@fixingbrokenrobots yeah, what @pmp-p says... but also, it's good practice to pin your requirements to specific versions, just so this situation (requirements creep) doesn't happen. Of course, packages upon which you rely will be updated, but by updating the pinned versions, you're doing such updates consciously rather than tacitly (as has happened here). FWIW, this is also why we stopped making latest a version of PyScript, because we want folks to pin their PyScript version (especially since, given the current growth stage of the project, things are changing fast). Change is hard. I know exactly how this feels, and you have my deepest sympathy when dealing with this... ๐Ÿ™‚
10:03
@Loic TL;DR we need to create some sort of type stub file for the pyscript namespace (see: https://microsoft.github.io/pyright/#/type-stubs)
Description
10:05
We're going to be updating our API "geography" over the coming months (see: https://github.com/pyscript/pyscript/discussions/2131), but I hope such a stub file can fall out of this work.
In yesterday's community call we discussed the names in and structure of the API we provide in the pyscript namespace. It is currently mostly flat and very crowded. Clearly we need to refactor ...
Avatar
Avatar
ntoll
@fixingbrokenrobots yeah, what @pmp-p says... but also, it's good practice to pin your requirements to specific versions, just so this situation (requirements creep) doesn't happen. Of course, packages upon which you rely will be updated, but by updating the pinned versions, you're doing such updates consciously rather than tacitly (as has happened here). FWIW, this is also why we stopped making latest a version of PyScript, because we want folks to pin their PyScript version (especially since, given the current growth stage of the project, things are changing fast). Change is hard. I know exactly how this feels, and you have my deepest sympathy when dealing with this... ๐Ÿ™‚
fixingbrokenrobots 08/08/2024 13:39
Thanks it looks like an openai update was the culprit. I already pin my PyScript version. Can I also specify package versions when I bring them in?
Avatar
Avatar
Andrea Giammarchi
to whom it might concern, there was an issue about Python blocking the main thread when doing heavy computation and the solution was to detach via asyncio that heavy computation so that the DOM has a chance to "tick" and show changes. I am sure this would help anyone else not using worker out there so the solution is here for history sake: https://github.com/pyscript/pyscript/issues/2135#issuecomment-2271260217
CyberneticDaisy 08/08/2024 13:41
A couple of days ago I did something similar. I was adding GUI elements in response to each completed PyTest test. The changes to the DOM are only shown on test completion. My solution was to run a single test at a time and call js.getAnimationFrame to call back to run the next test when the DOM has ticked
๐Ÿ‘ 1
Avatar
Avatar
CyberneticDaisy
A couple of days ago I did something similar. I was adding GUI elements in response to each completed PyTest test. The changes to the DOM are only shown on test completion. My solution was to run a single test at a time and call js.getAnimationFrame to call back to run the next test when the DOM has ticked
Andrea Giammarchi 08/08/2024 13:56
that works too ... here the result was 100% Python driven though, hopefully a hint/help for whoever will encounter the same issue in the future
Avatar
Avatar
fixingbrokenrobots
Thanks it looks like an openai update was the culprit. I already pin my PyScript version. Can I also specify package versions when I bring them in?
Avatar
Avatar
Andrea Giammarchi
we run top-level await by default now, what is your concern?
is it a correct syntax for classic python to use async at top level ?
19:37
Because I get syntax error when writting this
Avatar
Avatar
Andrea Giammarchi
that works too ... here the result was 100% Python driven though, hopefully a hint/help for whoever will encounter the same issue in the future
CyberneticDaisy 09/08/2024 02:17
yup. unfortunately I don't think that would work for me unless I can find a way to make PyTest plugins run async. there's plugins to make tests and fixtures async, but I don't have a clear enough grasp of async and PyTest internals to see how to design something that would work
Avatar
Enjoyable Pointing 09/08/2024 05:36
I'm trying to use pyscript inside nextjs, I would like to download and build from npm and distribute pyscript library with the rest of my app code, as opposed to including the cdn. It looks like in latest nextjs version top level async/await is disallowed in client components. Pyscript seems to be doing top level async/await stuff when it's imported, so it's giving me a few errors related to that. Is there a way to use pyscript inside nextjs without using the cdn?
Avatar
Avatar
Enjoyable Pointing
I'm trying to use pyscript inside nextjs, I would like to download and build from npm and distribute pyscript library with the rest of my app code, as opposed to including the cdn. It looks like in latest nextjs version top level async/await is disallowed in client components. Pyscript seems to be doing top level async/await stuff when it's imported, so it's giving me a few errors related to that. Is there a way to use pyscript inside nextjs without using the cdn?
Andrea Giammarchi 09/08/2024 09:31
PyScript is based on standard ECMAScript behavior and it's a module and it should work as a module. I think you have an issue with your bundler not understanding Web standards or how JS code in a module should run so while there's no work-around the fact we use and need/want a module for PyScript, its lazy load abilities, config parsing, bootstrap, and whatnot, you are going to have the same issue with any other ECMAScript module so it feels better to suggest you to change or fix your bundler or simply put the pyscript dist folder somewhere nothing can touch it and import { things } from './assets/pyscript/dist/core.js'; in your project as part of the HTML so that you won't have bundlers on your way.
Avatar
Avatar
Andrea Giammarchi
PyScript is based on standard ECMAScript behavior and it's a module and it should work as a module. I think you have an issue with your bundler not understanding Web standards or how JS code in a module should run so while there's no work-around the fact we use and need/want a module for PyScript, its lazy load abilities, config parsing, bootstrap, and whatnot, you are going to have the same issue with any other ECMAScript module so it feels better to suggest you to change or fix your bundler or simply put the pyscript dist folder somewhere nothing can touch it and import { things } from './assets/pyscript/dist/core.js'; in your project as part of the HTML so that you won't have bundlers on your way.
Andrea Giammarchi 09/08/2024 09:34
to clarify, even https://esm/run/pyscript@core witohut explicit /dist/core.js target is broken because bundlers tend to do "way too smart" things and in our case, like in many other cases out there where things are not just the usual "hello world" but a more complex architecture/hierarchy of dependencies, everything breaks "like a charm" but it's always bundlers fault ... or bundlers not being properly configured to ignore things that are not supposed to be bundled around. (edited)
Avatar
Look Behind You 09/08/2024 19:34
when doing a from bs4 import BeautifulSoup in the toml would i specify BeautifulSoup, or bs4 or as the package I put down
Avatar
Look Behind You 09/08/2024 19:41
also does html text not run on pyscript, what are supported datatypes that I would be able to print and see on the website?
19:42
I trying to run a webscraping program, and wanted to see the content of the scraped text from beautifulsoup
Avatar
Look Behind You 09/08/2024 19:52
is there a limit to how many packages i can put in the toml, after I put more than two(I am using re, bs4, and requests) packages the pyscript preview just turns blank Here is what I have: https://f835d02d-509c-4dec-93b3-3d7053c15eaf.pyscriptapps.com/crimson-night/ (edited)
Avatar
@Look Behind You if you look in the browser console you'll see this error message from Pyodide: ValueError: Can't find a pure Python 3 wheel for: 're' See: https://pyodide.org/en/stable/usage/faq.html#why-can-t-micropip-find-a-pure-python-wheel-for-a-package We explain this in our docs here: https://docs.pyscript.net/2024.8.2/faq/#python-packages
Avatar
hey guys can anyone help me out with how to display a plot using sympy python from sympy.plotting import plot from sympy.abc import x # Create a plot p = plot(x**2, show=False) # Display the plot display(p) this returns only the plot object but not the plot : <sympy.plotting.plot.Plot object at 0x3242648> (edited)
Avatar
Hi, I'm new to Pyscript. I was reading this https://docs.pyscript.net/2024.8.2/user-guide/dom/#pydom. The pydom link does not work anymore. Is that because pydom now is deprecated and we should use pyscript.web instead?
Avatar
Another question: In the worker interaction example given in https://docs.pyscript.net/2024.8.2/user-guide/workers/#worker-interactions, when the worker call greeting = sync.hello("PyScript"). Is the body of the hello() function executed in the worker thread or in the main thread?
Avatar
Hello, I literally just started with pyscript. The process of installing packages is eluding me. I can't find a page that explains it.
Avatar
Oh, I see, modules have to be installed through script.
09:27
Maybe it's a good idea to use a loading screen of some kind then. Can the main thread communicate with worker threads?
Avatar
! artemsnite 16/08/2024 14:07
hello to everyone! I'm new to pyscript, my website with terminal is empty on macos with safari. another issue is that input() doesn't work in telegram's new inapp web browser my test website: https://artemsniteterminal.vercel.app/
14:08
@ if help pls ๐Ÿ™
14:12
and how to check if browser supports terminal?
Avatar
I was trying to get some stock quotes from yahoo finance usign pyscript "fetch" as in https://parvizo.pyscriptapps.com/fetch-example-with-various-results/ pyscript "fetch" seems inconsistent. Using fetch I get the following results: # Returns 403 !! # url = 'pyscript.net' # Works ok. Return results # url = 'https://pyscript.net' # Works ok. Return results # url = 'https://rickd.s3.us-east-2.amazonaws.com/04.json' # Returns 403 # url = 'badURL' # Returns 403 # url = 'www.yahoo.com' # pyodide.ffi.JsException # url = 'https://www.yahoo.com/' # Returns 403 # url = 'query1.finance.yahoo.com/v8/finance/chart/LDOS?interval=1d' # pyodide.ffi.JsException # url = 'https://query1.finance.yahoo.com/v8/finance/chart/LDOS?interval=1d' For all the above URLs (except 'badURL') using chrome browser, I get the results as I am expecting. (edited)
Avatar
Hi there! Trying to find out if OpenCV examples/demos are available for PyScript?
Avatar
Avatar
Akbartus
Hi there! Trying to find out if OpenCV examples/demos are available for PyScript?
there's a opencv-python which is included in pyodide. Search this discord for opencv for examples and links. could be a bit old though. probably need to update
๐Ÿ‘ 1
Avatar
Hello, Im looking for a way to publish a package which should be installed via package.json file (for micropython backend) do you know how to do that ?
Avatar
Hello, how can I specify packages I want to use with micropython from a normal URL ?
Avatar
I think you have to just manually add the relevant files using the config file.
17:49
Don't quote me on that though.
Avatar
If I understand correctly, the default css sheet is optional? But can be used as a base for a custom sheet?
Avatar
Avatar
Akbartus
Hi there! Trying to find out if OpenCV examples/demos are available for PyScript?
Alas openCV is written in C. There is a python wrapper which enables us to call opencv functions from python but there is no wasm compile of opencv and so you can't use it in pyscript - which requires a python or wasm package so it can run in the browser. Consider if PIL will do what you need (Pillow really). or scikit, Examples in the pyscript examples use a number of scientific packages that are written entirely in python
๐Ÿ‘ 1
Avatar
why am I getting this error Traceback (most recent call last): File "/lib/python312.zip/pyodide/base.py", line 522, in eval_code .run(globals, locals) ^^^^^^^^^^^^^^^^^^^^ File "/lib/python312.zip/_pyodide/_base.py", line 356, in run coroutine = eval(self.code, globals, locals) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "<exec>", line 2, in <module> ImportError: cannot import name 'create_proxy' from 'pyodide' (/lib/python312.zip/pyodide/__init.py)
Avatar
Avatar
Neon22
Alas openCV is written in C. There is a python wrapper which enables us to call opencv functions from python but there is no wasm compile of opencv and so you can't use it in pyscript - which requires a python or wasm package so it can run in the browser. Consider if PIL will do what you need (Pillow really). or scikit, Examples in the pyscript examples use a number of scientific packages that are written entirely in python
Thank you for your clarification. I was able to create examples using pyscript and opencv-python and official documentation (https://docs.opencv.org/4.x/d6/d00/tutorial_py_root.html). Although some functionalities are not supported (but not the ones which are crucial). Also found some codes in this channel so thank you for help. If there are those who are interested in PyScript + OpenCV check recently created repo: https://github.com/akbartus/PyScript-Examples/tree/main/3_opencv_example. It includes examples of document scanner, qr reader, feature matching and many more created with PyScript! (edited)
โค๏ธ 1
Avatar
Avatar
! artemsnite
hello to everyone! I'm new to pyscript, my website with terminal is empty on macos with safari. another issue is that input() doesn't work in telegram's new inapp web browser my test website: https://artemsniteterminal.vercel.app/
! artemsnite 19/08/2024 12:04
bump.
Avatar
Avatar
! artemsnite
bump.
Have you tried it on pyscript.com to see if its a config or safari issue ?
Avatar
Avatar
Neon22
Have you tried it on pyscript.com to see if its a config or safari issue ?
! artemsnite 19/08/2024 12:08
I will, thanks
Avatar
I'm a newbie to PyScript. I'm trying to import w2ui but I get this error:
14:43
TypeError: class constructors must be invoked with 'new'
14:43
Any ideas what I'm doing wrong?
14:45
Just for clarity I'm using mpy
Avatar
OK, I think I got it: w2ui.w2grid.new(to_js({})) ๐Ÿ™ƒ
Avatar
Avatar
-oLo-
OK, I think I got it: w2ui.w2grid.new(to_js({})) ๐Ÿ™ƒ
Yes - new is used when making js things from eth python side
Avatar
Avatar
ntoll
@CyberneticDaisy so, (once again), I feel your pain. Also, I've gotta say you're turning the awesomeness dial up to a solid 11 with a namespace like floofbytes - that had me giggling like a 2yo. ๐Ÿคฃ Is there any way you can share this code so I (or others - @Andrea Giammarchi for instance) could have a poke around to see what's going on..? If you stick everything on PyScript.com we can easily clone your project and make our own private mess. Quite understand if this isn't possible, but we're kinda flying blind here in terms of supporting you. ๐Ÿ‘ Another possibility is to drop into a video call here on discord and you can show us around / demo the problem and we can try to figure it out. I'm in the UK and when the US wakes up I'm usually in meetings with US colleagues so mornings (UK time - but not this morning or tomorrow) are usually good for ad hoc pairing. (edited)
CyberneticDaisy 24/08/2024 08:35
https://discord.com/channels/972017612454232116/973505371685658624/1276806686924869634 you're welcome to take a look at my progress on FloofBytes
Avatar
Antonio Zamora 25/08/2024 17:55
I hope this helps other PyScript beginners. It took me several days to find out how to get "numpy" to work. I had no problem running Python, but I got stuck on PyScript. I finally set up a simple test program, and after much research, I found that "pyscript.toml" needed the line 'packages = ["matplotlib"]' (edited)
Avatar
Avatar
Antonio Zamora
I hope this helps other PyScript beginners. It took me several days to find out how to get "numpy" to work. I had no problem running Python, but I got stuck on PyScript. I finally set up a simple test program, and after much research, I found that "pyscript.toml" needed the line 'packages = ["matplotlib"]' (edited)
Check out the examples and experiment at https://docs.pyscript.net/2024.8.2/examples/
Avatar
blackbooks 27/08/2024 12:49
I am sending and receiving data from my pyscript environment to a certified webserver. This prompts an InsecureRequestWarning InsecureRequestWarning: Unverified HTTPS request is being made to host 'redcap.med.tu-dresden.de'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#tls-warnings Is there a way to add the certificate verification to my environment, or via the browser or at least disable the warnings?
Avatar
I have a problem that seems solvable:
11:01
On the first load of my pyscript page, everything works, but on subsequent reloads, it seems that it fails to reinstall the external packages I use. A full reload fixes it, but I wouldn't want the end user to have to deal with this. Anything I can do?
11:01
I assume it is a caching issue.
Avatar
I also noticed that it only happens some of the time.
Avatar
Generally, after a full reload the next two or so reloads give me an error.
11:52
Obviously I do not want to force the end user to reload so what is going on and how can I avoid?
Avatar
chris.laffra 29/08/2024 15:41
Have you tried "Chrome Devtools > Network" to see what happens to the loads? Sounds silly, but maybe a reboot of your machine helps.
Avatar
That doesn't really make sense to me.
Avatar
For some reason using @when to subscribe to events doesn't always work. I've tried connecting to an id instead of the web object, but that still doesn't work. I'm sorry it's kind of hard to describe and I can't provide a code example right now. There are other situations where I can subscribe an event to a button (object, not id) using @when just fine. I'm not sure what is going on.
Avatar
Is there a universally accepted way to upload xlsx files using the Panel framework? In my current iteration of the code, my df object remains empty (I believe)

Initialize Dataframe

df = pd.DataFrame()

Create a function to handle file input changes

def handle_file_input(event): ย ย  global df ย ย  if event.new: ย ย ย ย ย ย  # Create a BytesIO object from the file content ย ย ย ย ย ย  file_content = io.BytesIO(event.new) ย ย ย ย ย ย  # Read the Excel file into a DataFrame ย ย ย ย ย ย  df = pd.read_excel(file_content, engine = 'openpyxl') ย ย ย ย ย ย  # Update the Tabulator with the DataFrame ย ย ย ย ย ย  tabulator.value = df ย ย ย ย ย ย  tabulator.visible = True

Create a Tabulator widget to display the DataFrame

tabulator = pn.widgets.Tabulator(df, height=450, width=600)

Initially hide the table

tabulator.visible = False

Create an upload widget, default to prompting for .xlsx files

upload = pn.widgets.FileInput(accept='.xlsx') upload.param.watch(handle_file_input, 'value')
(edited)
โค๏ธ 1
Avatar
Avatar
Douglife
Is there a universally accepted way to upload xlsx files using the Panel framework? In my current iteration of the code, my df object remains empty (I believe)

Initialize Dataframe

df = pd.DataFrame()

Create a function to handle file input changes

def handle_file_input(event): ย ย  global df ย ย  if event.new: ย ย ย ย ย ย  # Create a BytesIO object from the file content ย ย ย ย ย ย  file_content = io.BytesIO(event.new) ย ย ย ย ย ย  # Read the Excel file into a DataFrame ย ย ย ย ย ย  df = pd.read_excel(file_content, engine = 'openpyxl') ย ย ย ย ย ย  # Update the Tabulator with the DataFrame ย ย ย ย ย ย  tabulator.value = df ย ย ย ย ย ย  tabulator.visible = True

Create a Tabulator widget to display the DataFrame

tabulator = pn.widgets.Tabulator(df, height=450, width=600)

Initially hide the table

tabulator.visible = False

Create an upload widget, default to prompting for .xlsx files

upload = pn.widgets.FileInput(accept='.xlsx') upload.param.watch(handle_file_input, 'value')
(edited)
Have you tried the uploader in recipes. I am not sure of integration with Panel but maybe its helpful:
Avatar
I am trying to send an input from an html text box into a python program but for some reason the .lower() function is giving an attribute error
Avatar
What's the code and the error?
Avatar
Avatar
Manock
For some reason using @when to subscribe to events doesn't always work. I've tried connecting to an id instead of the web object, but that still doesn't work. I'm sorry it's kind of hard to describe and I can't provide a code example right now. There are other situations where I can subscribe an event to a button (object, not id) using @when just fine. I'm not sure what is going on.
I managed to find the solution to this, at the very least. ^^ (edited)
Avatar
1. Here is the function that index.html calls first, which initiates the program. 2. Here is the .lower() function. 3. Here is my index.html. 4. Error for some reason. (edited)
Avatar
chris.laffra 30/08/2024 19:51
@Jelly event handlers get passed an event object, not a string. You will have to extract the string value from the input element inside the event handler. Using something like "event.target.value" inside your begin function would work.
Avatar
hi, very new to pyscript, how do i import an image into my python code
Avatar
.maybe ./img input/ maybe also consider replacing the space with an underscore - not that you need to - just for clarity and to make your life easier. Looks like you're using opencv or PIL.
Avatar
yea PIL
06:33
neither the dot nor underscore resolves the issue
Avatar
chris.laffra 01/09/2024 10:52
@floaty The following recipe describes how the local file system works in PyScript: https://docs.pyscript.net/2024.5.2/faq/#file-system
Avatar
Avatar
floaty
yea PIL
Did you add the image path in the files section in .toml file?
Avatar
Avatar
zobi
Did you add the image path in the files section in .toml file?
i didn't, because i don't know how to do that i guess it doesn't matter if my files aren't going to be shareable though...
Avatar
Is there a way in PyScript to intercept error messages, or outputs to stderr, so that I can parse it and give the user a more human-readable error message? This would be for teaching kids python, so I want to introduce them in a much friendlier way before exposing the real error messages.
Avatar
Avatar
scott
Is there a way in PyScript to intercept error messages, or outputs to stderr, so that I can parse it and give the user a more human-readable error message? This would be for teaching kids python, so I want to introduce them in a much friendlier way before exposing the real error messages.
Maybe sys.excepthook could be a way to approach this? Something like: import sys import random def nudge(exc_type, instance, traceback): msg = random.choice([ "Whoops! That didn't go so well", "Ouch! Python didn't like that", "Hmmmm..., something weird happened", ]) suggest = random.choice([ "Try looking at", "I think the problem is around", "Something smells funny around", ]) frame = traceback.tb_frame lineno = frame.f_lineno from pathlib import Path source_file = Path(frame.f_code.co_filename).read_text() source = source_file.splitlines()[lineno-2:lineno+1] print(msg) print(f"{suggest} line {frame.f_lineno}:\n") print("\n".join(f" {lineno + i - 1}: {line}" for i, line in enumerate(source))) sys.excepthook = nudge # do something exceptional x = 1/0
07:06
^^ this works for code run in the console, you'd need to adapt the code inspection parts to PyScript
Avatar
Avatar
scott
Is there a way in PyScript to intercept error messages, or outputs to stderr, so that I can parse it and give the user a more human-readable error message? This would be for teaching kids python, so I want to introduce them in a much friendlier way before exposing the real error messages.
chris.laffra 02/09/2024 11:16
I intercept both "print" and "window.console.log" in PySheets as follows: https://github.com/PySheets/pysheets/blob/c4cf3576d9ce3792225bda4196b93e8bc1b69e86/src/static/state.py#L93
Source for PySheets. Contribute to PySheets/pysheets development by creating an account on GitHub.
11:18
@scott as @ptmcg points out, it may be better to capture the error higher up. I do that as well in PySheets, just by wrapping the execution of the code with a try-except clause.
๐Ÿ‘ 1
Avatar
Avatar
ptmcg
Maybe sys.excepthook could be a way to approach this? Something like: import sys import random def nudge(exc_type, instance, traceback): msg = random.choice([ "Whoops! That didn't go so well", "Ouch! Python didn't like that", "Hmmmm..., something weird happened", ]) suggest = random.choice([ "Try looking at", "I think the problem is around", "Something smells funny around", ]) frame = traceback.tb_frame lineno = frame.f_lineno from pathlib import Path source_file = Path(frame.f_code.co_filename).read_text() source = source_file.splitlines()[lineno-2:lineno+1] print(msg) print(f"{suggest} line {frame.f_lineno}:\n") print("\n".join(f" {lineno + i - 1}: {line}" for i, line in enumerate(source))) sys.excepthook = nudge # do something exceptional x = 1/0
Exactly what I was looking for and hoping to find. Thank you!
Avatar
Avatar
ptmcg
Maybe sys.excepthook could be a way to approach this? Something like: import sys import random def nudge(exc_type, instance, traceback): msg = random.choice([ "Whoops! That didn't go so well", "Ouch! Python didn't like that", "Hmmmm..., something weird happened", ]) suggest = random.choice([ "Try looking at", "I think the problem is around", "Something smells funny around", ]) frame = traceback.tb_frame lineno = frame.f_lineno from pathlib import Path source_file = Path(frame.f_code.co_filename).read_text() source = source_file.splitlines()[lineno-2:lineno+1] print(msg) print(f"{suggest} line {frame.f_lineno}:\n") print("\n".join(f" {lineno + i - 1}: {line}" for i, line in enumerate(source))) sys.excepthook = nudge # do something exceptional x = 1/0
I wrote the following code in a <script type="py-editor">: import sys def nudge(exc_type, instance, traceback): frame = traceback.tb_frame print(f'File: {frame.f_code.co_filename}') print(f'Line: {frame.f_lineno}') sys.excepthook = nudge x = 1/0 The output div shows: File: /lib/python312.zip/_pyodide/_base.py Line: 596 Error The error is in red. So the line number is not informative, nor is the filename. I'd really like to not show the red error as well. Is there any way to get relevant information from the error and not display "Error"?
Avatar
@scott I believe so, but I've just got back from holiday so the information isn't right at my fingertips. I believe there's a way to turn off the red error box (and just emit to console.log). @Andrea Giammarchi will definitely know...
Avatar
chris.laffra 02/09/2024 15:45
@scott Use try-except around your code, so that the error is not printed by default. Or, catch the print calls using something like "builtins.print = lambda *args: None"?
Avatar
Avatar
ntoll
@scott I believe so, but I've just got back from holiday so the information isn't right at my fingertips. I believe there's a way to turn off the red error box (and just emit to console.log). @Andrea Giammarchi will definitely know...
I like the error output going to the editor-output div, just not the red Error word. I want students to see the simplified error message. Is there any way to get better line numbers and filenames (for tabbed editors)?
Avatar
Hmm... good question. We're mostly just passing on the error message from Python itself (or MicroPython).
16:11
To be clear, MicroPython isn't as verbose as "regular" Python (i.e. Pyodide).
Avatar
chris.laffra 02/09/2024 16:13
@scott I would definitely try your code on MicroPython. My intuition says it will not work as "traceback" is only implemented barebones there.
Avatar
Avatar
chris.laffra
@scott Use try-except around your code, so that the error is not printed by default. Or, catch the print calls using something like "builtins.print = lambda *args: None"?
Where would I put the try-except? I'm using <script type="py-editor"> with editor.process(editor.code) Would the process function throw an exception if the code had an error? Separately, I can't get the process function to work. I get the following error in the browser console: pyodide.asm.js:10 Unable to use `window` or `document` -> https://docs.pyscript.net/latest/faq/#sharedarraybuffer
Avatar
chris.laffra 02/09/2024 16:34
You will want to import window and document from the pyscript module?
Avatar
Avatar
chris.laffra
You will want to import window and document from the pyscript module?
of course I do that
๐Ÿ‘ 1
16:36
from pyscript import document def submit(event): editor = document.querySelector('#editor') print(f'code: {editor.code}') editor.process(editor.code) The code gets printed to the console
16:36
but process gives me the above error
Avatar
chris.laffra 02/09/2024 16:36
In that case, try to extract the text from the editor and run it yourself using "exec(...)"?
16:37
Then you can control how errors are handled
16:37
That's what I would try.
16:38
That is how PySheets runs the cell functions. I actually run them in a worker, as I do not want to block the UI in case a user writes "while True: do_something()"
Avatar
Avatar
chris.laffra
That's what I would try.
ok, I'll try exec() instead of process()
17:58
I am curious though, is editor.process(editor.code) broken, or am I missing something?
Avatar
How can I import local files? Typically, I do something like: import mylib mylib.myfunc() where mylib.py is in the same directory as the current file. But this doesn't work in PyScript.
Avatar
Avatar
scott
I wrote the following code in a <script type="py-editor">: import sys def nudge(exc_type, instance, traceback): frame = traceback.tb_frame print(f'File: {frame.f_code.co_filename}') print(f'Line: {frame.f_lineno}') sys.excepthook = nudge x = 1/0 The output div shows: File: /lib/python312.zip/_pyodide/_base.py Line: 596 Error The error is in red. So the line number is not informative, nor is the filename. I'd really like to not show the red error as well. Is there any way to get relevant information from the error and not display "Error"?
Andrea Giammarchi 03/09/2024 09:03
we don't get to decide what the interpreter returns as error ... as example, if you use mpy-editor instead with the exact same code you have Line 8 which is where the error happens, then you have: Error: Traceback (most recent call last): File "", line 8, in AttributeError: 'module' object has no attribute 'excepthook' which is definitively better but then again, it's not us interfering with what the error provides in terms of details. That being said, if you want to disable the red errors you can add this CSS toyour page: py-editor-output span, mpy-editor-output span { display: none; }
Avatar
Avatar
scott
How can I import local files? Typically, I do something like: import mylib mylib.myfunc() where mylib.py is in the same directory as the current file. But this doesn't work in PyScript.
you need to look at the help and examples on pyscript.com. - https://docs.pyscript.net/2024.8.2/beginning-pyscript/
Avatar
Do coroutines get turned into promises on the js side? Does that mean that calling it on the js side will automatically start it?
Avatar
Avatar
Manock
Do coroutines get turned into promises on the js side? Does that mean that calling it on the js side will automatically start it?
Andrea Giammarchi 03/09/2024 11:15
you still need to await those but the easiest way to go is to define async functions instead of orchestrating manually coroutines as that gets easily translated on the interpreter side and it's more aligned with JS pattern around async ... also latest PyScript enables top level await out of the box, just in case this detail is interesting or useful. (edited)
Avatar
Avatar
scott
How can I import local files? Typically, I do something like: import mylib mylib.myfunc() where mylib.py is in the same directory as the current file. But this doesn't work in PyScript.
Andrea Giammarchi 03/09/2024 11:20
[files] "./mylib.py" = "./"
Avatar
Avatar
Andrea Giammarchi
you still need to await those but the easiest way to go is to define async functions instead of orchestrating manually coroutines as that gets easily translated on the interpreter side and it's more aligned with JS pattern around async ... also latest PyScript enables top level await out of the box, just in case this detail is interesting or useful. (edited)
I'm right now experimenting with asyncio.create_task(). That does mean I cannot wait for the task to complete from a non-async context. If there's anything I want to do once the task completes I'd have to use the callback. Which I suppose is fine'ish.
Avatar
I thought I could await a promise object in python but I guess not?
Avatar
Nevermind, I can, the error was something completely unrelated.
Avatar
Avatar
Manock
I'm right now experimenting with asyncio.create_task(). That does mean I cannot wait for the task to complete from a non-async context. If there's anything I want to do once the task completes I'd have to use the callback. Which I suppose is fine'ish.
Andrea Giammarchi 03/09/2024 13:27
if your Python runs in a worker script you have all sync-like APIs you like, that's how the whole project works already, it's non-blocking from a worker, if you ask for async stuff on the main thread.
Avatar
My python script runs synchronously in the main thread. And I see no value in running it in a worker. It only does async operations when a button is clicked.
13:35
create_task() seems to do everything I need. I just need to remember to put everything I need inside the task.
Avatar
Avatar
Neon22
you need to look at the help and examples on pyscript.com. - https://docs.pyscript.net/2024.8.2/beginning-pyscript/
I had read that page previously but didn't see anything about importing local files
Avatar
Avatar
Andrea Giammarchi
[files] "./mylib.py" = "./"
This worked, thank you @Andrea Giammarchi
๐Ÿ‘ 1
Avatar
Avatar
Manock
I'm right now experimenting with asyncio.create_task(). That does mean I cannot wait for the task to complete from a non-async context. If there's anything I want to do once the task completes I'd have to use the callback. Which I suppose is fine'ish.
Andrea Giammarchi 03/09/2024 14:55
if it helps, I've used it in here (bottom of the code example) and it worked great: https://github.com/pyscript/pyscript/issues/2135#issuecomment-2271260217
Checklist I added a descriptive title I searched for other issues and couldn't find a solution or duplication I already searched in Google and didn't find any good information or help What ...
Avatar
Used what?
15:12
Oh, create task. Yeah it works for me.
Avatar
I'm having a difficult time with the most basic operation in PyScript, executing Python code. I'm creating a web-based platform to learn Python. The PyScript Python editor (https://docs.pyscript.net/2024.8.2/user-guide/editor/) works great, as do all the examples in the documentation for <script type="mpy">, <script type="py">, <py-script>, and <mpy-script> tags. But I need students to be able to submit code to be tested by my unit tests, in addition to running their code. For that, I need to be able to programmatically execute the Python code in the editor, either from JavaScript or within Python. Unfortunately, I can't get editor.process(editor.code) (https://docs.pyscript.net/2024.8.2/user-guide/editor/#read-write-execute) to work as mentioned in a post above. @chris.laffra suggested using exec() instead of process(). The output goes directly to the console and there's no result (what the code returns after the last statement). Is there a way to get process() working or can I intercept exec() 's output and retrieve the result?
Avatar
Avatar
scott
I'm having a difficult time with the most basic operation in PyScript, executing Python code. I'm creating a web-based platform to learn Python. The PyScript Python editor (https://docs.pyscript.net/2024.8.2/user-guide/editor/) works great, as do all the examples in the documentation for <script type="mpy">, <script type="py">, <py-script>, and <mpy-script> tags. But I need students to be able to submit code to be tested by my unit tests, in addition to running their code. For that, I need to be able to programmatically execute the Python code in the editor, either from JavaScript or within Python. Unfortunately, I can't get editor.process(editor.code) (https://docs.pyscript.net/2024.8.2/user-guide/editor/#read-write-execute) to work as mentioned in a post above. @chris.laffra suggested using exec() instead of process(). The output goes directly to the console and there's no result (what the code returns after the last statement). Is there a way to get process() working or can I intercept exec() 's output and retrieve the result?
Andrea Giammarchi 03/09/2024 15:54
there is no single minimal reproducible example of what you are after there ... so it's very hard to answer ... but ... if editor.exec works, it looks like editor.process should too ... are you also changing the editor code before asking it to evaluate anything?
Avatar
Avatar
Andrea Giammarchi
there is no single minimal reproducible example of what you are after there ... so it's very hard to answer ... but ... if editor.exec works, it looks like editor.process should too ... are you also changing the editor code before asking it to evaluate anything?
Andrea Giammarchi 03/09/2024 15:56
also worth saying there is not such thing as editor.exec in our code ...
Avatar
Avatar
Andrea Giammarchi
there is no single minimal reproducible example of what you are after there ... so it's very hard to answer ... but ... if editor.exec works, it looks like editor.process should too ... are you also changing the editor code before asking it to evaluate anything?
The minimal reproducible example is at the link https://docs.pyscript.net/2024.8.2/user-guide/editor/#read-write-execute Here are the mre files I used exec, not editor.exec
547 bytes
186 bytes
Avatar
Avatar
scott
The minimal reproducible example is at the link https://docs.pyscript.net/2024.8.2/user-guide/editor/#read-write-execute Here are the mre files I used exec, not editor.exec
Andrea Giammarchi 03/09/2024 17:36
this example uses both ... which one is working and which one is not? a PyScript .com page with this example would be great too!
17:37
also please consider using an async listener and await editor.process as we switched to async by default so these cases must await the result
17:38
that is await editor.process(editor.code) ... something we should definitively change in our docs for clarity sake
Avatar
Avatar
Andrea Giammarchi
also please consider using an async listener and await editor.process as we switched to async by default so these cases must await the result
I did also try await editor.process()
Avatar
Avatar
Andrea Giammarchi
this example uses both ... which one is working and which one is not? a PyScript .com page with this example would be great too!
ahh, sorry. So exec works, but only outputs to console and doesn't give a return result. editor.process() never works
Avatar
Andrea Giammarchi 03/09/2024 17:39
also worth saying in a button that is not meant to submit you should event.preventDefault() too
๐Ÿ‘ 1
Avatar
chris.laffra 03/09/2024 17:49
Hi @scott, Here is a working version, although I must say I would rather use LTK, but did not want to change too much in your example ๐Ÿค“ https://pyscript.com/@laffra/lingering-mud/latest?files=mre.py,index.html (edited)
Avatar
Thank you @chris.laffra . Now I see what you previously meant by replacing the definition of print. What's LTK? I also see that editor.process(editor.code) also doesn't work in your example. Is editor.process() simply broken? Or the documentation for PyScript needs to be updated?
Avatar
Avatar
scott
Thank you @chris.laffra . Now I see what you previously meant by replacing the definition of print. What's LTK? I also see that editor.process(editor.code) also doesn't work in your example. Is editor.process() simply broken? Or the documentation for PyScript needs to be updated?
Avatar
is there a way to change the url of a project so that it does not include the username once deployed on pyscriptapps?
Avatar
Avatar
Douglife
is there a way to change the url of a project so that it does not include the username once deployed on pyscriptapps?
excellent question - no idea - try asking on pyscript-do-com channel
โค๏ธ 1
Avatar
When I click generate, nothing happens (first picture is not the full code). How do I properly send input from the textbox in pic 2 to the string variable in pic 1? (edited)
07:30
Pic 3 (edited)
Avatar
blackbooks 05/09/2024 08:57
Something broke my PyScript Application. I now get the error during the download of an environment package: Can't find a pure Python 3 wheel for: 'pycap', it says here, that this means there is no python wheel however in the project page there is clearly a wheel file. So i am at a loss. This all worked fine a few weeks ago.
PyCap: Python interface to REDCap
Avatar
Avatar
blackbooks
Something broke my PyScript Application. I now get the error during the download of an environment package: Can't find a pure Python 3 wheel for: 'pycap', it says here, that this means there is no python wheel however in the project page there is clearly a wheel file. So i am at a loss. This all worked fine a few weeks ago.
do you have "brotli", "brotlicffi", "nh3" modules working ? (edited)
Avatar
blackbooks 05/09/2024 10:27
not that i know of, not sure if one of them is buried among the package dependencies:, this tit eh env.json: { "packages": [ "SQLAlchemy", "Jinja2", "python_dateutil", "PyCap" ], "files": { "{STATIC_FOLDER}": "http://localhost:5000/wizard/pyscript/static/", "{TEMPLATES}": "templates", "{STATIC_FOLDER}/db_config.py": "db_config.py", "{STATIC_FOLDER}/models.py": "models.py", "{STATIC_FOLDER}/invoice.py": "invoice.py", "{STATIC_FOLDER}/invoicequery.py": "invoicequery.py", "{STATIC_FOLDER}/jinja.py": "jinja.py", "{STATIC_FOLDER}/pycap_ext.py": "pycap_ext.py", "{STATIC_FOLDER}{TEMPLATES}/query_results.html": "./{TEMPLATES}/query_results.html", "{STATIC_FOLDER}{TEMPLATES}/redcap_reroute.html": "./{TEMPLATES}/redcap_reroute.html" } }
Avatar
blackbooks 05/09/2024 11:52
Found the issue: The PyCap wheel is only valid until Python version 3.11, without a specific interpreter PyScript seems to take the most recent stable python version 3.12. i set added: "interpreter": "3.11" and it works fine now, i guess the maintainers of the PyCap Package need to update their wheel.
Avatar
@blackbooks yeah, these things can take time to filter through. A friendly issue / PR fix on their repository may prod them into action (but remember they may also be volunteers and might not have the bandwidth right now) ๐Ÿ‘
Avatar
MatttNguyen 07/09/2024 02:19
help
Avatar
Avatar
MatttNguyen
help
we can't see any code. is it: from pyscript import document ??
Avatar
MatttNguyen 07/09/2024 06:04
yep
Avatar
MatttNguyen 07/09/2024 06:16
at least that's the first line of the file
06:19
@Neon22
06:20
is bro dead
Avatar
chris.laffra 07/09/2024 11:16
Where are you running this code? This does not look like you ran it inside a browser, but from the command line after manually installing using "pip install pyscript"? If so, use https://pyscript.com to get started and see how PyScript applications are hosted and run.
Avatar
Avatar
chris.laffra
Where are you running this code? This does not look like you ran it inside a browser, but from the command line after manually installing using "pip install pyscript"? If so, use https://pyscript.com to get started and see how PyScript applications are hosted and run.
MatttNguyen 07/09/2024 14:31
I ran it using localhost and yes, I used pip install
14:36
btw the examples at pyscript.com uses pyweb soo what?
Avatar
chris.laffra 07/09/2024 15:34
PyScript is not installed using "pip install", More details can be found here: https://docs.pyscript.net/2024.8.2/
Avatar
blackbooks 07/09/2024 19:54
I am sending data from the pyscript environment to a secure server and encounter a InsecureRequestWarning with the warning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised.See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings InsecureRequestWarning) I could probably simply disable the warning: import requests from requests.packages.urllib3.exceptions import InsecureRequestWarning requests.packages.urllib3.disable_warnings(InsecureRequestWarning) But prefer to solver the issue, so i thought the certificates are missing so i tried to add them like this: import certifi import os os.environ['REQUESTS_CA_BUNDLE'] = certifi.where() But this didnt help, so i dug through the libraries and if i understand correctly the requests library already uses urrlib3 and the certifi package for the certification. So the PyScript Environment has the needed certificates. There is also a check if the proxy is verified. Could this be the root of the issue? Any ideas how to proceed? Debugging this is a nightmare. (edited)
Avatar
Hey, folks! I'm just starting with pyscript, and believe I've found a package that is not supported (cmd2). cmd2's wheel is downloaded successfully from PyPI, but when it requests for pypy.org/simple/pyperclip (a dependency from cmd2), it makes no progress or raises any error on the console. I see that pyperclip doesn't distribute a wheel, and from its name it must rely on native capabilities to access the clipboard. What options do you recommend to move forward?
23:33
Also, it'd have been good to get a message that the package is not supported, I had to find out by elimination that the issue was on requesting cmd2.
Avatar
I have a problem. I want to experiment with micropython, but every time I try to do so, it just fails to load the page.
Avatar
I have seen scripts that work, but I do not understand what they do different. I'm just trying to do a basic Hello, World!
11:26
And Pyodide isn't doing this for me.
11:33
Although the pages that do work use an older version of pyscript.
11:33
But this is weird. I'm not sure what is happening.
11:33
If it helps, I'm using Brave.
Avatar
Avatar
chris.laffra
PyScript is not installed using "pip install", More details can be found here: https://docs.pyscript.net/2024.8.2/
MatttNguyen 08/09/2024 11:52
well what about this
Avatar
Avatar
Manock
I have seen scripts that work, but I do not understand what they do different. I'm just trying to do a basic Hello, World!
chris.laffra 08/09/2024 14:14
Chromium v128 is broken for WASM and workers, mostly hitting MicroPython and workers. If you are on Chrome, Brave, or Edge, you likely run Chromium v128. You can switch temporarily to Chrome Canary, or Safari, or Firefox and try again. This Chromium bug has wide issues, includng on my own PySheets app at https://pysheets.app
Avatar
Avatar
MatttNguyen
well what about this
chris.laffra 08/09/2024 14:15
What about that? it does not pip install pyscript, does it?
Avatar
Avatar
chris.laffra
Chromium v128 is broken for WASM and workers, mostly hitting MicroPython and workers. If you are on Chrome, Brave, or Edge, you likely run Chromium v128. You can switch temporarily to Chrome Canary, or Safari, or Firefox and try again. This Chromium bug has wide issues, includng on my own PySheets app at https://pysheets.app
How long has this been going on for? Do they know about it?
Avatar
chris.laffra 08/09/2024 14:20
Yes, since Sept 3. Fix is in v129 and v130. It may also be backported to v128. See https://issues.chromium.org/issues/361611791
Avatar
Avatar
chris.laffra
What about that? it does not pip install pyscript, does it?
MatttNguyen 08/09/2024 14:22
then how am i supposed to install that ๐Ÿ˜ญ
Avatar
chris.laffra 08/09/2024 14:23
Use Safari, Firefox, or Chrome Canary for the next day or two...
14:26
I am not trying to minimize the impact this has on the PyScript community. @Andrea Giammarchi has raised the issue with Chrome team immediately when the issue was noticed around Sept 3/4. Concurrency issues like these are really hard to test and sometimes these fall through the cracks. As a community we also have to raise the seriousness of this problem with the Chromium project, possible helping create a test case that would detect this problem. It is kind of weird that in Chrome you cannot say, please go back to v127, or skip to v129....
Avatar
? I mean I just wanted to know if the issue was known. ๐Ÿ˜›
Avatar
chris.laffra 08/09/2024 14:27
It basically caused PySheets to be unusable for "normal" Chrome/Edge users for 5 days now...
Avatar
Avatar
Manock
? I mean I just wanted to know if the issue was known. ๐Ÿ˜›
chris.laffra 08/09/2024 14:28
I was responding to Matt's question on how to install v130 ๐Ÿค“
14:29
But they were asking a different question.
Avatar
Ah, I got confused. Because it's related to the same issue right?
Avatar
chris.laffra 08/09/2024 14:30
Please read the getting started docs at https://pyscript.net. In short, PyScript is installed using a small JavaScript library that then bootstraps the Python VM and all the packages you need to run your application.
Avatar
Also I think the question was how to install pyscript. To which the answer is by including it in your web page, right?
Avatar
chris.laffra 08/09/2024 14:31
We should use threads for discussion to avoid these confusion (edited)
Avatar
Also if they do it locally they have to also run a local server.
Avatar
chris.laffra 08/09/2024 14:45
Correct, That is how PySheets works. You can "pip install pysheets-app" and then just run "pysheets". This starts a local server to serve up the HTML, CSS, JS, and Python files. See https://github.com/PySheets/pysheets?tab=readme-ov-file#installation (edited)
Source for PySheets. Contribute to PySheets/pysheets development by creating an account on GitHub.
14:46
Or you git clone the project and run "python3 src/pysheets.js", which is an easier way if you want to make changes to PySheets.
Avatar
It does look like updates are in Brave's pipeline, but it looks like the pipeline for browsers can be pretty complicated.
๐Ÿ‘ 1
Avatar
Hi, I'm new to PyScript and I can't get a simple statement to run with out error and it works elsewhere. Any ideas? me = input("Who are you? ") print ("you are", me)
Avatar
Avatar
MatttNguyen
then how am i supposed to install that ๐Ÿ˜ญ
pyscript runs in the browser. to load packages in the browser you need to specify which ones you want to load in the toml file (the one you mention in your config part of the script tag). In all the examples that load packages - they define which packages and what they're called in teh toml file. Once again - running pyscript in a browser - not in some env on your local machine.
Avatar
Avatar
imphil
Hi, I'm new to PyScript and I can't get a simple statement to run with out error and it works elsewhere. Any ideas? me = input("Who are you? ") print ("you are", me)
The best possible hing you can do - which wil lsave yo a lot of time and confusion - is to use pyscript.com and clone an example and adjust your local copy to see how everything works. And the docs of course.
Avatar
Avatar
Neon22
pyscript runs in the browser. to load packages in the browser you need to specify which ones you want to load in the toml file (the one you mention in your config part of the script tag). In all the examples that load packages - they define which packages and what they're called in teh toml file. Once again - running pyscript in a browser - not in some env on your local machine.
MatttNguyen 09/09/2024 05:46
ok thanks but for example in my case what should i put in the toml file
Avatar
Avatar
MatttNguyen
ok thanks but for example in my case what should i put in the toml file
It appears you are using the example provided by @ntoll which uses the pirate module. His example is here: https://pyscript.com/@ntoll/piratical/v5?files=main.py,pyscript.json,index.html In his case he has used a json file instead of toml file as his config format. In the help it explains the difference syntax between the two formats. You should use the one you are comfortable with. If both are new to you - I suggest toml as almost all the examples use toml format. The help for toml vs json is on this page: https://docs.pyscript.net/2024.8.2/user-guide/configuration/ The rest of the help is very linked on left. Clone the repo and experiment with this and the other examples (edited)
Avatar
Still trying to get my feet wet with pyscript, sadly time doesn't often allow, but it is at the moment, so... async def main(): response = await fetch("http://localhost:8000/get_uuid") if response.ok: data = response.text() else: display(response.status, target="myoutput") display(f"UUID: {data}", target="myoutput", append=False) Why do I not get anything displayed in my "myoutput" div? I know that the endpoint being called has been called successfully. but it feels like that display call never gets called. <div id="myoutput">my output</div> <script type="py" config="/pyscript/c_and_c_client.toml" target="myoutput"> import c_and_c_client c_and_c_client.main() </script> <py-script> from pyscript import display display("Hello all you people", target="myoutput", append=False) </py-script> That py-script block works as expected. The console reports: <PyodideFuture pending> and I'm unsure what that means. (edited)
Avatar
Chris Laffra 11/09/2024 10:22
Do you have a link to a pyscript.com example others can try?
Avatar
I mean I can at least ask one question. Is it ok for target to be 'myoutput' and not '#myoutput'?
10:47
Ok I checked and that's not it, sorry.
10:48
Uh
10:48
There's one other question.
10:48
Can you just call an async function like that without awaiting it?
10:49
Oh, I just noticed the part about the pyodidefuture pending.
10:50
Yeah you're calling your main() function without using await.
Avatar
Oh I forgot what I came here for.
12:42
What is the best way for my script to know if it's running in pyodide or micropython?
Avatar
Avatar
Manock
What is the best way for my script to know if it's running in pyodide or micropython?
Andrea Giammarchi 11/09/2024 15:38
import sys if 'MicroPython' in sys.version: print('mpy') else: print('py')
Avatar
hello ๐Ÿ™‚ i'm trying pyscript for the first time and i don't really understand how to get started, is there any chance someone could help me? i have a html page on a web server, on this page i've added pyscript like this : <link rel="stylesheet" href="https://pyscript.net/releases/2024.9.1/core.css"> <script type="module" src="https://pyscript.net/releases/2024.9.1/core.js"></script> in the same folder, i created a run.py script that imports libraries like this : import cv2 as cv from cv2geojson import find_geocontours, export_annotations import numpy as np and finally in my html i added the following script element : <script type="mpy" src="./run.py" config='{"packages":["cv2","cv2geojson","numpy"]}'></script> now when i open my page, i get a CORS error : micropython.mjs:1 Access to XMLHttpRequest at 'https://micropython.org/pi/v2/package/py/cv2/latest.json' from origin 'https://super-duper.fr' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. I'm not sure what i'm doing wrong, any help is appreciated. Thanks ๐Ÿ™‚
Avatar
i tried to switch the script type from "mpy" to "py", but i get a different error. Maybe openCV library is not supported by pyscript? do i have a way to check if that's the case?
Avatar
Avatar
Flamby
hello ๐Ÿ™‚ i'm trying pyscript for the first time and i don't really understand how to get started, is there any chance someone could help me? i have a html page on a web server, on this page i've added pyscript like this : <link rel="stylesheet" href="https://pyscript.net/releases/2024.9.1/core.css"> <script type="module" src="https://pyscript.net/releases/2024.9.1/core.js"></script> in the same folder, i created a run.py script that imports libraries like this : import cv2 as cv from cv2geojson import find_geocontours, export_annotations import numpy as np and finally in my html i added the following script element : <script type="mpy" src="./run.py" config='{"packages":["cv2","cv2geojson","numpy"]}'></script> now when i open my page, i get a CORS error : micropython.mjs:1 Access to XMLHttpRequest at 'https://micropython.org/pi/v2/package/py/cv2/latest.json' from origin 'https://super-duper.fr' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. I'm not sure what i'm doing wrong, any help is appreciated. Thanks ๐Ÿ™‚
You need to mention your packages in .toml file or inside the <py-config></py-config> tag ,and opencv-python is the package for importing cv2. check the following link https://_12.pyscriptapps.com/mute-cake/latest/
Avatar
thanks a lot, works fine ! i was confused with all the different config options.
Avatar
Another newbie question, i'm reading a tutorial on files that tells me to: -have a file input in the HTML -in the python script add an onChange listener this way : def _img2geojson(e): myFile = e.target.files.item(0) .... img2geojson = create_proxy(_img2geojson) document.getElementById("inputFile").addEventListener("change", img2geojson) the thing i do not understand is how to use a function that requires a file path. For example with opencv library i would open an image with the command imread("/path/img.png"). In my pyscript i only have a file object, i don't have a path to give to imread function. Is there a way to deal with this?
Avatar
Chris Laffra 15/09/2024 09:42
@Flamby All your Python code runs in a sandbox environment inside the browser. There is a virtual file system in the browser, but that is not the same as your local file system. To find out more, go to https://pyscript.net , click on Documentation, and in the search bar on top, search for "file". You will see the FAQ entry on File System: https://docs.pyscript.net/2024.9.1/faq/#file-system
PyScript is a platform for Python in the browser.
Avatar
ah, thanks a lot, i'll read that ๐Ÿ‘
Avatar
so, i understand how to save my input file in the virtual file system, then process it. At the end, my result is stored in an output file, still located in the virtual file system. I would like to use this output file in my javascript, and unless i'm mistaken i have not seen this in the FAQ. in the download file section of the FAQ, there is a line tmp = window.URL.createObjectURL(file) i imagine this would make my file somehow accessible from the window object, but i'm not sure where this is created or how to use it in a javascript function. By the way, thanks everyone for your answers, it's very helpfull.
Avatar
How can I connect my sql server database to py-script?sql server is desktop and pyscript is on browser how should i connect both?๐Ÿค” ๐Ÿค”
Avatar
Avatar
zobi
How can I connect my sql server database to py-script?sql server is desktop and pyscript is on browser how should i connect both?๐Ÿค” ๐Ÿค”
you could try embed sqlite3 or pglite into your app, or use an async db client and put a websocket frontend (eg websockify) in front of db server ( TCP )
11:19
i think sqlite3 is already bundled in pyodide
Avatar
Avatar
pmp-p
you could try embed sqlite3 or pglite into your app, or use an async db client and put a websocket frontend (eg websockify) in front of db server ( TCP )
I don't want to create my database.I just want to connect and get the database to pyscript.I can connect to sql server from jupyternotebook,in the same way can i connect to sql server from pyscript?I don't think sqlite3 will get the SQL server database to pyscript and i have no idea about the other technical terms you have mentioned.
Avatar
well connecting a webpage to a sql db server is purely a technical issue
๐Ÿ‘ 1
Avatar
Chris Laffra 16/09/2024 17:18
@zobi When a Jupyter notebook connects to a database, it is not the webpage that connects, but the Jupyter kernel that runs somewhere else. This kernel can make any network connections, including your database server. If you want to do the same from PyScript you have to understand how web security works, in particular CORS. In short, you need to go through the webserver to make random network calls from the PyScript code running in the browser. The server you use to host your index.html can act as a trampoline and make any network call on your behalf. PySheets does exactly that. The source can be found on github.
Avatar
I went through the PySheets.How are you able to import packages in pysheets? I see no packages mentioned in .toml file but still it imports pandas package?
Avatar
elliot0x01 18/09/2024 13:43
what is the best approach to build a TUI with miropython and pyscript ?
Avatar
Avatar
elliot0x01
what is the best approach to build a TUI with miropython and pyscript ?
may i know the reason not using cpython which provides a lot of TUI toolkit choices ?
Avatar
Avatar
pmp-p
may i know the reason not using cpython which provides a lot of TUI toolkit choices ?
elliot0x01 18/09/2024 14:34
loadtime, using cpython takes a huge load time initially
14:34
mpy takes fraction of seconds to load
Avatar
true it takes 7 seconds to load pyytermtk or textual+rich but that may be a bit better to use than picotui / nanotui (edited)
Avatar
Avatar
elliot0x01
mpy takes fraction of seconds to load
pytermtk loads under 4 seconds it is not that bad
Avatar
elliot0x01 18/09/2024 14:57
Thanks @pmp-p , let me try and benchmark all four ๐Ÿ™‚
Avatar
The guy behind nanotui looks vaguely familiar... ๐Ÿ˜‰
Avatar
@pmp-p - I just tried it, and encountered problems. Traceback (most recent call last): File "<stdin>", line 2, in <module> File "nanotui/__init__.py", line 2, in <module> ImportError: no module named '__future__' https://pyscript.com/@ntoll/chewytui/
Avatar
elliot0x01 18/09/2024 16:49
Tried pyscript after a long time, not sure why this is not working ๐Ÿค” did I do something wrong?
16:50
I remember doing this way with earlier versions of pyscript
Avatar
Code looks good. What's in main.py ? What browser is this? Try updating if not the latest.
Avatar
Avatar
ntoll
Code looks good. What's in main.py ? What browser is this? Try updating if not the latest.
elliot0x01 18/09/2024 18:42
simple print("hello world")
18:42
It's firefox, earlier used to work fine
Avatar
Hmm... I'm on firefox and it works for me. I wonder if you have plugins or something unique to your setup?
19:48
I'm intrigued...
Avatar
Avatar
ntoll
@pmp-p - I just tried it, and encountered problems. Traceback (most recent call last): File "<stdin>", line 2, in <module> File "nanotui/__init__.py", line 2, in <module> ImportError: no module named '__future__' https://pyscript.com/@ntoll/chewytui/
i think i've misplaced the micropython one ( that runs board side) this code looks like the one i used to run on msys2 python3.4 ๐Ÿ˜ฌ
Avatar
@pmp-p I'd love to see this working in PyScript! ๐ŸŽ‰
Avatar
i'll give a look to it then
Avatar
Avatar
zobi
I went through the PySheets.How are you able to import packages in pysheets? I see no packages mentioned in .toml file but still it imports pandas package?
Chris Laffra 19/09/2024 05:11
That's a good question to ask on the PySheets server: https://discord.gg/7gJ8xGfz
Avatar
Avatar
ntoll
@pmp-p I'd love to see this working in PyScript! ๐ŸŽ‰
it almost worked with bun (i've pushed a test) but on browser it really need os.read and os.write (edited)
07:54
emscripten stdin/stdout buffering is just wrong maybe https://github.com/jerch/browser-fakepty https://github.com/xloem/emterm (edited)
Avatar
Avatar
ntoll
Hmm... I'm on firefox and it works for me. I wonder if you have plugins or something unique to your setup?
elliot0x01 19/09/2024 12:19
not really
12:19
Infact it used to work for me as well
12:19
But not working as of now
Avatar
elliot0x01 19/09/2024 12:26
Only terminal works fine but using terminal worker won't work
12:27
Also if I use an input statment without using worker, then a prompt box is created to enter the input
12:27
๐Ÿค”
12:29
Changing pyscript version to https://pyscript.net/releases/2024.5.2/core.js works (edited)
Avatar
elliot0x01 19/09/2024 12:44
Getting this error with pytermtk @pmp-p ๐Ÿค” anyways I think textual-rich works pretty fine and easiest to implement but takes a long time to load
12:45
Any idea why I am not getting the colors ๐Ÿค” : (edited)
Avatar
Avatar
elliot0x01
Any idea why I am not getting the colors ๐Ÿค” : (edited)
you need to force them, it happens when destination fd is not recognized as a TTY (edited)
Avatar
cool i did put a comment for that ๐Ÿ˜„ # for Rich color output os.environ["FORCE_COLOR"] = "1"
Avatar
Avatar
pmp-p
cool i did put a comment for that ๐Ÿ˜„ # for Rich color output os.environ["FORCE_COLOR"] = "1"
elliot0x01 19/09/2024 14:05
Thank you so much
14:08
Since picotui is not found Package not found: https://micropython.org/pi/v2/package/py/picotui/latest.json 2f0c965c-eb37-46cf-a1e9-ffabaaa9e930:2:7273 Package may be partially installed 2f0c965c-eb37-46cf-a1e9-ffabaaa9e930:2:7273 What is the best way to use it? Do I copy all the python files in my project directory ?
Avatar
Avatar
elliot0x01
Since picotui is not found Package not found: https://micropython.org/pi/v2/package/py/picotui/latest.json 2f0c965c-eb37-46cf-a1e9-ffabaaa9e930:2:7273 Package may be partially installed 2f0c965c-eb37-46cf-a1e9-ffabaaa9e930:2:7273 What is the best way to use it? Do I copy all the python files in my project directory ?
it won't work without os.read / os.write i'm sorry i mislead you, i was using picotui and nanotui on wapy (a special fork) not stock micropython
Avatar
Avatar
pmp-p
it won't work without os.read / os.write i'm sorry i mislead you, i was using picotui and nanotui on wapy (a special fork) not stock micropython
elliot0x01 19/09/2024 14:18
Ohhh okay no issues ๐Ÿ˜…
14:19
So my only options are rich and pytermtk right ?
Avatar
probably
14:25
maybe this one too https://github.com/salt-die/batgrl but unsure about pyodide support
Avatar
elliot0x01 19/09/2024 14:54
Thanks
Avatar
I am trying to open new tab using the following code. import webbrowser webbrowser.open_new_tab("https://www.stackoverflow.com") It works in my mobile but not the laptop?What can be the reason? (edited)
Avatar
i am trying to use the pyscript.web. I see I can append to an element, but how do i remove all the previous children in order to replace rather than append result = pw.page.find("#result_div")[0] try: res = g.query(query_string) result.append(res_to_table(res)) except Exception as e: ...
Avatar
Avatar
N0m15
i am trying to use the pyscript.web. I see I can append to an element, but how do i remove all the previous children in order to replace rather than append result = pw.page.find("#result_div")[0] try: res = g.query(query_string) result.append(res_to_table(res)) except Exception as e: ...
Hey hey... you could just result.replaceChildren() right..?
Avatar
Avatar
ntoll
Hey hey... you could just result.replaceChildren() right..?
yes. i had to use res_to_table(res)._dom_element to get it to work though. (edited)
Avatar
Hmm.... the replaceChildren() call should be passed through to the underlying ._dom_element automagically. I'll take a look.
Avatar
JTS.Dawnstar[] 26/09/2024 04:05
How can I change the starting dimensions of a <script type="py-editor"> widget? At the moment no matter what I do it always starts out tiny and only gets larger if you type in it
Avatar
Avatar
JTS.Dawnstar[]
How can I change the starting dimensions of a <script type="py-editor"> widget? At the moment no matter what I do it always starts out tiny and only gets larger if you type in it
are you using the css ?
Avatar
amphicoelias. 26/09/2024 09:35
Is there an equivalent to JS' "this" in functions? I'm trying to use the @when decorator with multiple targets, which seems perfectly possible, but then how do I target the clicked element inside the function? @when("click", ".tile") def click_tile(event): # need to know which div was clicked here
Avatar
@amphicoelias. it'll be something like event.target
09:47
the event is a JsProxy object that represents the "standard" JS event object.
Avatar
Avatar
Neon22
are you using the css ?
JTS.Dawnstar[] 26/09/2024 18:22
yeah, I've defined width and height in a class selector
Avatar
Avatar
JTS.Dawnstar[]
yeah, I've defined width and height in a class selector
the terminal dimensions are defined directly. E.g. ltk.window.document.currentScript.terminal.resize(60, 12) I am not familiar with the py-editor but maybe look for some direct handles
Avatar
Avatar
Neon22
the terminal dimensions are defined directly. E.g. ltk.window.document.currentScript.terminal.resize(60, 12) I am not familiar with the py-editor but maybe look for some direct handles
JTS.Dawnstar[] 26/09/2024 20:54
awesome, I'll see if I can get something working
Avatar
Avatar
JTS.Dawnstar[]
awesome, I'll see if I can get something working
JTS.Dawnstar[] 27/09/2024 01:03
update: turns-out the fix was to contain the <script type="py-editor"> in a div tag and then stylize the div tag instead
Avatar
@JTS.Dawnstar[] ๐Ÿ‘
Avatar
Hi everyone, I have a question that I've been trying to solve on my own for a while but wasn't able to. Basically I've written something like a python tutorial with blocks of texts (contained in <pre> tags) followed by some py-editor tags for code demonstration and the opportunity to test something that has been explained earlier. At the end of the tutorial, it gets more advanced and tries to teach how to use something like pandas and that is where the issue occurs. My py-editor blocks use the "env" attribute and I've tried to have pandas installed like one can do with the script type="py" tags. For some reason it doesn't seem to work there, am I doing something wrong or is this something that is not yet supported?
Avatar
Weird, I saw my message twice so I tried to remove one but it removed both. Anyway, how do I move a dataframe between pyworkers?
Avatar
Floppypants 02/10/2024 12:44
Hello. I'm trying to run this code in a project on Pyscript.com and getting an error. This code works on normal Python 3.11. import datetime x=datetime.now() print(x.strftime("%Y-%m-%d")) print(x.strftime("%Y-%m-%d %H:%M:%S")) The error is something like "AttributeError: module datetime does not have an attribute 'now'". But this works in normal Python on my PC. How do I format dates and times to my liking? (edited)
Avatar
Avatar
Floppypants
Hello. I'm trying to run this code in a project on Pyscript.com and getting an error. This code works on normal Python 3.11. import datetime x=datetime.now() print(x.strftime("%Y-%m-%d")) print(x.strftime("%Y-%m-%d %H:%M:%S")) The error is something like "AttributeError: module datetime does not have an attribute 'now'". But this works in normal Python on my PC. How do I format dates and times to my liking? (edited)
Chris Laffra 02/10/2024 13:32
Are you using MicroPython? That Python runtime has a minimal set of Python libraries and reduced APIs, in order to launch fast on embedded devices. PyScript uses that to get fast launch times. You may want to try PyOdide for full Python compatibility?
Avatar
Avatar
Chris Laffra
Are you using MicroPython? That Python runtime has a minimal set of Python libraries and reduced APIs, in order to launch fast on embedded devices. PyScript uses that to get fast launch times. You may want to try PyOdide for full Python compatibility?
Floppypants 02/10/2024 15:08
How do I know which Python I'm using? I only see "Mode: Python". I can't see a way to copy the full error message from the Pyscript.com output console/preview pane. And I don't see anything about that in the Project Settings (gear icon). (edited)
Avatar
Hey yall. Working on a tool that grabs data from Salesforce and then uses Pandas to view data in different ways. Imports the imports the simple_salesforce library and works perfectly! I've got an error that I can't seem to figure out. Just trying to get the value from a textbox using the Element() funtion. inputValue = Element("quotesNums") I get the following error: NameError: name 'Element' is not defined I'm pretty sure Element imports automatically but I even tried importing it and i still get the error. I've used the "To-do" demo app to help me with this and it works perfectly.
Avatar
Avatar
Floppypants
How do I know which Python I'm using? I only see "Mode: Python". I can't see a way to copy the full error message from the Pyscript.com output console/preview pane. And I don't see anything about that in the Project Settings (gear icon). (edited)
Chris Laffra 02/10/2024 17:13
Can you share your pyscript.com URL?
Avatar
Avatar
Floppypants
Hello. I'm trying to run this code in a project on Pyscript.com and getting an error. This code works on normal Python 3.11. import datetime x=datetime.now() print(x.strftime("%Y-%m-%d")) print(x.strftime("%Y-%m-%d %H:%M:%S")) The error is something like "AttributeError: module datetime does not have an attribute 'now'". But this works in normal Python on my PC. How do I format dates and times to my liking? (edited)
Try importing "from datetime import datetime" then it will work.
Avatar
Avatar
Floppypants
How do I know which Python I'm using? I only see "Mode: Python". I can't see a way to copy the full error message from the Pyscript.com output console/preview pane. And I don't see anything about that in the Project Settings (gear icon). (edited)
See the info box on the type attribute in this section of the docs: https://docs.pyscript.net/2024.9.2/api/#pyscriptconfig
07:28
TL;Dr... pyscript.config now has a type attribute that returns either "mpy" or "py" to indicate the interpreter in which the code is running. ๐Ÿ‘
Avatar
Is it possible to get the current PyScript version at runtime?
๐Ÿ‘† 1
Avatar
Well, you could access the src of the script tag in the head that's used to grab PyScript itself, and then parse the path in the src for teh version number. ๐Ÿคท
Avatar
Avatar
ntoll
TL;Dr... pyscript.config now has a type attribute that returns either "mpy" or "py" to indicate the interpreter in which the code is running. ๐Ÿ‘
Floppypants 03/10/2024 18:26
My interpreter is "py". Thank you. I wrote to Anaconda, they replied, and I got it working.
๐ŸŽ‰ 2
Avatar
can I use pyscript along with vanilla script tags in svelte app - has anyone seen it work?
06:25
I have some domain specific logic in pure python - I am not experienced enough in js to port it confidently
Avatar
Avatar
psi2kgcm
can I use pyscript along with vanilla script tags in svelte app - has anyone seen it work?
Chris Laffra 07/10/2024 09:24
Yes that should work. Easiest to experiment with this would be to go to https://pyscript.com (edited)
๐Ÿ‘ 2
Avatar
Avatar
elliot0x01
Also if I use an input statment without using worker, then a prompt box is created to enter the input
Andrea Giammarchi 07/10/2024 12:40
there's no way around it ... we either block the whole main thread without workers or you use a worker ... the page can't provide inputs if it's waiting for inputs
Avatar
Avatar
Pemo2023
Is it possible to get the current PyScript version at runtime?
Andrea Giammarchi 07/10/2024 12:41
is it about the interpreter version or the PyScript version? 'cause these are not related
Avatar
Avatar
psi2kgcm
can I use pyscript along with vanilla script tags in svelte app - has anyone seen it work?
Andrea Giammarchi 07/10/2024 12:43
Svelte is a pre-processor ... if that doesn't work it means Svelte processes also scripts that don't belong to Svelte logic. I haven't tried though but if Chris says it should work, I believe it will.
Avatar
@psi2kgcm its about the PyScript version. Its part of the url but I wonder if there is an attribute
Avatar
Avatar
Pemo2023
@psi2kgcm its about the PyScript version. Its part of the url but I wonder if there is an attribute
Andrea Giammarchi 08/10/2024 15:48
the issue with a version is that Pyscript can be a custom one or one provided locally which URL contains no version and neither does the underlying code ... may I ask what's the version requirement about?
15:52
to clarify, here the clarification around "version about what?"
  • you can provide your own interpreters, those links don't necessarily contain a version number
  • Pyodide version? MicroPython version? strictly PyScript version? these are all different and not related ever to each other
  • features detection might be the best "version" resolution you can get ... you try/catch recent APIs to retrieve the behavior you can expect from that version
15:56
so ... what are you looking for when you think you need a specific version?
15:56
the interpreters provide their own version, our API around those also might help retrieving exposed functionalities. (edited)
15:56
I hope this makes somehow sense to you.
Avatar
damson2505 08/10/2024 20:12
Hello! I've been trying to translate matplotlib interactive plots that I do on notebooks to my web browser and that's how I found out about pyscript. The "problem" I've been having is that I am using matplotlib widgets, and in order for the plots to be interactive and not a picture I need to change matplotlibs backend. I tried the html5 backend for pyodide but it doesn't seem to be mantained. Is there an easy translation or should I interface with html widgets instead of python?
Avatar
Avatar
Andrea Giammarchi
the issue with a version is that Pyscript can be a custom one or one provided locally which URL contains no version and neither does the underlying code ... may I ask what's the version requirement about?
OK, I see, a version number info wouldn't make much sense. I am using PyScript for a couple of weeks now and have often experienced that something from a PyScript tutorial or from the official examples did not work for me. So I was looking for an equvialent to python --version. So what I would like to have is the version of the PyScript that runs my script. (edited)
Avatar
Avatar
Pemo2023
OK, I see, a version number info wouldn't make much sense. I am using PyScript for a couple of weeks now and have often experienced that something from a PyScript tutorial or from the official examples did not work for me. So I was looking for an equvialent to python --version. So what I would like to have is the version of the PyScript that runs my script. (edited)
Andrea Giammarchi 09/10/2024 08:39
if you find our tutorials outdated or not working, please file an issue because we want to fix that!
Avatar
Avatar
damson2505
Hello! I've been trying to translate matplotlib interactive plots that I do on notebooks to my web browser and that's how I found out about pyscript. The "problem" I've been having is that I am using matplotlib widgets, and in order for the plots to be interactive and not a picture I need to change matplotlibs backend. I tried the html5 backend for pyodide but it doesn't seem to be mantained. Is there an easy translation or should I interface with html widgets instead of python?
Andrea Giammarchi 09/10/2024 09:16
are we talking worker or main thread? the html5 backend should work at least on main thread, IIRC
Avatar
I understand that when passing values between workers, they 'must be serializable', but exactly what do we mean by serializable? Do we mean to json?
๐Ÿ‘ 1
Avatar
I noticed that between version 2024.6.1 and 2024.7.1 of core.js the pydom module is no longer part of pyweb so from pyweb import pydom results to an ModuleNotFoundError error. Is this correct and if so where is pydom now? (edited)
Avatar
Is a python dict serializable or do I need to convert it to a js object first?
Avatar
I found the change from pydom to pyscript.web in the repo ('move pydom and elements from pyweb to pyscript.web', #2092) - but how does the import looks like? An example please... (the examples on https://www.pyscript.com still use pydom from pyweb) (edited)
๐Ÿ™„ 2
Avatar
No need for an example, its all in the docs of course, I just didn't see that I have to choose the right pyscript version in the header list๐Ÿ˜Ÿ (edited)
๐Ÿ‘€ 1
๐Ÿ‘ 1
Avatar
Yeah, so the docs are versioned. ๐Ÿ˜‰
10:19
The examples need updating. Thank you for the prompt.
Avatar
It took me some time to find this out... may be google search directed me to an older version of the doc and I did not realize it
Avatar
Avatar
Andrea Giammarchi
if you find our tutorials outdated or not working, please file an issue because we want to fix that!
My impression is that many tutorials on blogs or online magazine are from 2023 - the golden rule of the Internet, never update your blog entries;)
Avatar
Avatar
Manock
Is a python dict serializable or do I need to convert it to a js object first?
Andrea Giammarchi 10/10/2024 12:38
we convert/transform references out of the box in most cases but whenever you want to be sure, from pyscript import to_js and to_js(python_ref)
Avatar
Andrea Giammarchi 10/10/2024 12:52
to be honest, with MicroPython you'll rarely have any issue passing Python dictionaries around, or lists/tuples, Pyodide with dictionaries passes a weird Map like proxy, which most of the time works but it's not strictly a JS literal counterpart. to_js from pyscript ensures you convert to literals, no Proxies or exotic objects hard to understand from JS APIs.
Avatar
help: why does output = page["#output"][0] output.html = outputText shows no output in the browser? output is the id of a div and outputText contains text and a display(output.html) outputs the text. So the html attribute has a value. I am using 2024.9.2. It looks like the object page["#output][0] returns is a pyscript.web.divobject that has no attributes like html or content. (edited)
Avatar
Unable to add Py script import to anaconda toolbox Add-ins?
Avatar
Making an app (with micropython) playing with lots of html elements. Initially planned each to be covered in a class instance. But while testing reached a limit around 35k instances where run out the memory. Keeping elements purely in their jsproxies expectedly doesnโ€™t reach this limit maintaining millions of them. At end of the day decided to keep only important ones in instances of python class, but still needed something to work easy and lazy on the rest /showing in the ide their methods etc./ So the lame yet helpful solution was to create this dummy class shown on the image. So now jsproxies are much more fun to work with. PP. @property will make it act as getter/setter (edited)
Avatar
@Ridensium ooh... MicroPython shouldn't run out of memory like that. Perhaps report that (along with an example of the behaviour) to Damien over in MicroPython..? Or if it's more convenient for you, do the same (reproducable example) but as a ticket in PyScript and we'll ping/liaise with Damien.
18:45
Nice find..! ๐Ÿ™‚ And thank you for reporting it. If we don't know about this stuff, we can't fix it. ๐Ÿ‘
Avatar
Avatar
ntoll
@Ridensium ooh... MicroPython shouldn't run out of memory like that. Perhaps report that (along with an example of the behaviour) to Damien over in MicroPython..? Or if it's more convenient for you, do the same (reproducable example) but as a ticket in PyScript and we'll ping/liaise with Damien.
Will report that in PyScript ๐Ÿค“
๐Ÿ‘ 1
Avatar
Russell Keith-Magee 12/10/2024 06:37
Hi folks - I've been doing an update to BeeWare's PyScript support prior to a new release, and I've hit a discrepancy in behavior between whatever version is being served by latest/pyscript.net, and what is served by releases/2024.10.1/core.js.
06:37
In latest, stdout and stderr are both redirected to the javascript console. However, under 2024.10.1, stdout goes to the javascript console, but errors go to the DOM. I've seen a bunch of references to this being the behavior of an error plugin that is enabled by default, and that adding a plugins=["!error"] declaration to the config can reverse this behavior... but this doesn't appear to do anything for me.
06:37
Sample gist with 3 different approaches to setting the config: https://gist.github.com/freakboy3742/ed3073fcffd6d92bb374bcf85301f320 (edited)
06:39
In all three, This is normal content goes to the javascript console; but This is error content is added as content in red in the DOM.
06:39
I'm seeing this in both Chrome and Safari, so it doesn't appear to be browser specific.
06:39
Any ideas?
Avatar
Russell Keith-Magee 12/10/2024 06:50
Making matters worse - if I change the code to <script type="mpy">, the content that is printed to the DOM is each individual ASCII value of the text, one line per value (so, "84", "104", "105", "115", "32", ...) (edited)
06:54
In both cases, the error content is also being printed to the javascript console; I just can't disable the "write to DOM" behavior.
Avatar
Virtuoso bug spotting @Russell Keith-Magee ๐Ÿค—. Can you bundle up ^^^ into an issue over on the PyScript repos? (Just so we can track things and so others may find the issue on the open web). Thank you! cc @Andrea Giammarchi (edited)
Avatar
Russell Keith-Magee 12/10/2024 07:37
Ok - just wanted to confirm this wasn't expected behavior, or me holding it wrong ๐Ÿ™‚
โค๏ธ 1
Avatar
Definitely a bug.
07:39
Also, MicroPython emitting ASCII numbers instead of actual characters is rather hilarious.
Avatar
Russell Keith-Magee 12/10/2024 08:11
@ntoll On a related note - Do you publish release notes? Or any kind of migration guide or backwards compatibility notes? As someone not actively tracking the day-to-day development of PyScript, it's difficult to work out exactly what has changed in each release, and what might need an update from Briefcase/Toga.
Avatar
Avatar
Russell Keith-Magee
@ntoll On a related note - Do you publish release notes? Or any kind of migration guide or backwards compatibility notes? As someone not actively tracking the day-to-day development of PyScript, it's difficult to work out exactly what has changed in each release, and what might need an update from Briefcase/Toga.
Yes, every release (mentioned in #announcements ) lives at a URL like this one: https://pyscript.net/releases/2024.10.1/
Avatar
Russell Keith-Magee 12/10/2024 09:08
Ah - you've using actual Github releases for the release notes. Missed the one obvious place to look for them.
๐Ÿคฃ 1
Avatar
I would like to add an SVG image to a canvas and then manipulate that SVG on the canvas. I can get the canvas context no problem, and I get the SVG document no problem, but when I try and add the SVG to the Canvas, I am told that my image is not a valid Image type. I'm sure I need to use Image() of some sort , but my fumbling around has not resulted in success yet.
Avatar
Avatar
rhacer
I would like to add an SVG image to a canvas and then manipulate that SVG on the canvas. I can get the canvas context no problem, and I get the SVG document no problem, but when I try and add the SVG to the Canvas, I am told that my image is not a valid Image type. I'm sure I need to use Image() of some sort , but my fumbling around has not resulted in success yet.
I don't have an exact solution for you but you need to set the src of an image to be the svg. I have not yet succeeded in modifying the following recipe (which works for text files (utf-8 encoding) to base64 encoding required for images. https://pyscript.recipes/2024.5.2/basic/file-download/ and use an approach similar to this https://stackoverflow.com/questions/27230293/how-to-draw-an-inline-svg-in-dom-to-a-canvas If you get it working I would love to know how to do it. Good luck
Well, I need some help about convert .svg file/image to .png file/image... I have a .svg image displayed on my page. It is saved on my server (as a .png file). I need to convert it to a .png file on
09:50
So the route is to create the svg. draw it into the newly made Image() then do canvas.drawImage into the 2d context of the canvas. In pyscript I use the svg.py you can find here to create all my svg elements. - https://pyscript.com/@neon22/working-copy/latest?files=svg.py
09:51
This works well for inserting into the dom but for a canvas you need to go through an image as you can't draw svg directly onto the canvas (sadly)
Avatar
Avatar
Neon22
I don't have an exact solution for you but you need to set the src of an image to be the svg. I have not yet succeeded in modifying the following recipe (which works for text files (utf-8 encoding) to base64 encoding required for images. https://pyscript.recipes/2024.5.2/basic/file-download/ and use an approach similar to this https://stackoverflow.com/questions/27230293/how-to-draw-an-inline-svg-in-dom-to-a-canvas If you get it working I would love to know how to do it. Good luck
Thank you very much. I've been cobbling bits and pieces together and feel I'm getting closer. I appreciate the pointers.
๐ŸŽ‰ 2
Avatar
for those wanting help on how to build an interactive stock market app using Pyscript and Bokeh, I offer three code examples here at https://dev.to/rickdelpo1/interactive-stock-market-sp-500-line-chart-using-bokeh-python-js-pyscript-and-a-movable-angle-finder-for-trend-line-analysis-1k6a
Is the stock market overvalued in 2024? Thanks to a Python library called Bokeh we can chart the...
๐Ÿ‘ 1
Avatar
Avatar
Russell Keith-Magee
Ok - just wanted to confirm this wasn't expected behavior, or me holding it wrong ๐Ÿ™‚
Andrea Giammarchi 14/10/2024 12:27
thanks for the issue but to be clear, we don't match latest at all because latest does not exist anymore since very long time ago. We never granted feature parity with latest over the last year so while the mpy bug is something to look at and fix, we won't necessarily match latest because we had different goals with modern PyScript. /cc @ntoll
Avatar
Russell Keith-Magee 14/10/2024 12:29
Totally understood that parity with latest isnโ€™t necessarily a goal; but โ€œdonโ€™t write to DOMโ€ should exist as an option somehow. It looks like it might have been an option in 2024.4.*, but itโ€™s not clear that it is an option any more. (edited)
Avatar
Andrea Giammarchi 14/10/2024 12:30
if there is a config with !error that indeed should indicate no error is print on the DOM. If you can provide a minmal example case in our repo we'll have a look if that's not the case and fix it, same gose for mpy printing out ASCII chars ... we did change mpy behavior "recently" to allow REPL mode and its now handling chars in a different way but that might be terminal dependent, which is why a minimal error case would be ideal. Thanks again for helping us helping you ๐Ÿ™
Avatar
Russell Keith-Magee 14/10/2024 12:32
I opened a ticket over the weekend with a minimal example. https://github.com/pyscript/pyscript/issues/2220
โค๏ธ 1
12:33
But also - all references to !error in the docs appear to have been removed, so if thatโ€™s still an option, thatโ€™s a notable omission.
Avatar
Hello. I am new here --and a complete beginner in python. I have started using pyscript as it seems the best way of learning how to use python without changing my hosting provider. My first isuue is that I can't get what seems to be a very simple python command -- print ("Hello World") -- to work as I was expecting. "Hello World" appears in the browser console, but not in the browser window. I have tried adding '"print('Content-type: text/html')" first, but that too just appears in the console. Any help will be welcome. Thanks.
Avatar
Avatar
Russell Keith-Magee
But also - all references to !error in the docs appear to have been removed, so if thatโ€™s still an option, thatโ€™s a notable omission.
Andrea Giammarchi 15/10/2024 18:32
yeah, that was some "left-over" that didn't ... left ... over ... I'm on the case, will try to find a reasonable answer tomorrow, if that's OK.
Avatar
Avatar
Mark L
Hello. I am new here --and a complete beginner in python. I have started using pyscript as it seems the best way of learning how to use python without changing my hosting provider. My first isuue is that I can't get what seems to be a very simple python command -- print ("Hello World") -- to work as I was expecting. "Hello World" appears in the browser console, but not in the browser window. I have tried adding '"print('Content-type: text/html')" first, but that too just appears in the console. Any help will be welcome. Thanks.
This is a problem that all new users of pyscript encounter. Basically we are used to print being used to showing us stuff. But in a browser its different because there is a dom to deal with. So pyscript has added a display() function which works a litle closer to what you might expect. If you want to use print() you can expose the terminal in yoru config like: <script type="py" src="main.py" config="pyscript_pyo.toml" terminal></script> but this is only for py. But will not workfor mpy (micropython interpreter which is faster). OR you can add this to the top of your main.py file and use print (maybe just for quick debugging, its not a useful solution (which is display()) import ltk def print(*args): ltk.find("body").append(" ".join(str(a) for a in args), "<br>")
22:03
Also your life will be much easier by looking at the help, using pyscript.com as a testing location, cloning the examples there, etc... https://docs.pyscript.net/2024.10.1/ https://pyscript.com
22:05
@ntoll - is there a faq link we can point new users to? needs to talk about py,mpy,print,pyscript.com,help pages, recipes.
Avatar
Good question.
Avatar
Avatar
Neon22
This is a problem that all new users of pyscript encounter. Basically we are used to print being used to showing us stuff. But in a browser its different because there is a dom to deal with. So pyscript has added a display() function which works a litle closer to what you might expect. If you want to use print() you can expose the terminal in yoru config like: <script type="py" src="main.py" config="pyscript_pyo.toml" terminal></script> but this is only for py. But will not workfor mpy (micropython interpreter which is faster). OR you can add this to the top of your main.py file and use print (maybe just for quick debugging, its not a useful solution (which is display()) import ltk def print(*args): ltk.find("body").append(" ".join(str(a) for a in args), "<br>")
Thank you. I will study what you have said and experiment. ๐Ÿ™‚
Avatar
So I'm working with pandas dataframes. And I also want to use web workers. So I wonder, what is the best way to pass a dataframe from one worker to another? If I serialize it to json, some stuff might be lost like the dtypes.
Avatar
I do have some ideas to experiment with, but maybe there is just a regular option.
Avatar
Avatar
Manock
So I'm working with pandas dataframes. And I also want to use web workers. So I wonder, what is the best way to pass a dataframe from one worker to another? If I serialize it to json, some stuff might be lost like the dtypes.
Chris Laffra 19/10/2024 21:32
I am just curious: What is your use case for sending dataframes between workers? Marshalling and sending (large amounts of) data between workers can be quite slow.
Avatar
I mean, the use case is "I want to find out if there is a gain to be made here".
23:46
At the very least, the source files need to reach the worker somehow (which isn't a dataframe yet, but it's a similar problem I'm trying to solve).
23:48
Also aren't there these 'transferable objects'?
Avatar
I am trying to get content from another source to my tabs. How can I get? Can someone help?https://_12.pyscriptapps.com/bold-darkness/latest/
Avatar
Hi, I'm currently developing a web app that will allow students to enter and execute code. A lot of the programs they need to write include input() statements, and many of these loop these inputs until a condition based on the input value is met. It seems like PyScript doesn't handle this sort of input() due to how it runs, but is there a workaround or method I can use to allow PyScript to "start and stop" mid-execution to wait for the user to input?
Avatar
Avatar
Kyal
Hi, I'm currently developing a web app that will allow students to enter and execute code. A lot of the programs they need to write include input() statements, and many of these loop these inputs until a condition based on the input value is met. It seems like PyScript doesn't handle this sort of input() due to how it runs, but is there a workaround or method I can use to allow PyScript to "start and stop" mid-execution to wait for the user to input?
you could await an async user input (edited)
12:26
and if on desktop they want to await at toplevel, there's the python3 -m asyncio async repl
Avatar
Avatar
pmp-p
you could await an async user input (edited)
If I do this, do I need to add await before each input (use a regex to replace "input" with "await input")?
Avatar
Avatar
Kyal
Hi, I'm currently developing a web app that will allow students to enter and execute code. A lot of the programs they need to write include input() statements, and many of these loop these inputs until a condition based on the input value is met. It seems like PyScript doesn't handle this sort of input() due to how it runs, but is there a workaround or method I can use to allow PyScript to "start and stop" mid-execution to wait for the user to input?
anfroholic 21/10/2024 12:38
You could also just make an 'input' html element. Then onchange or whatever event you want to trigger on, can just pop some function
12:40
One of the best things about pyscript is javascript ;) -# can't believe I just said that lol
๐Ÿ˜… 1
Avatar
Avatar
Kyal
If I do this, do I need to add await before each input (use a regex to replace "input" with "await input")?
i think you should teach student why an I/O is async and why it should be awaited
12:45
( and because regex would fail, so you would have to setup js worker to run the code etc ... )
Avatar
Avatar
pmp-p
i think you should teach student why an I/O is async and why it should be awaited
Maybe if every student was older, but the youngest students are 6. I also need the code to match what's taught in-person otherwise we'd need to change everything we have outside the web app
๐Ÿ‘ 1
Avatar
anfroholic 21/10/2024 12:47
I may be projecting, but the whole thing about students, makes me think that things are intended to be something simple. I just posted a talk in #โ•ฐโ•ธchat about python generators (the core of asyncio) but in the 'app' that's running the talk has a series of 'repl promts' that are just html elements that just feed into an eval()
12:49
Maybe play with it a little bit @Kyal. Is something like this what you're seeking to do? There's also buttons that will 'inject their contents' into the terminal and execute
Avatar
I was originally trying the PyScript editor https://docs.pyscript.net/2024.4.1/user-guide/editor/ since something like this is what I was looking for (just with the ability to support inputs too, as when I tried entering code with inputs it froze). An example of a program that didn't work isstop = False while stop == False: user_input = input("Enter a value") if user_input == "!": stop = True else: print(user_input)
Avatar
Avatar
anfroholic
Maybe play with it a little bit @Kyal. Is something like this what you're seeking to do? There's also buttons that will 'inject their contents' into the terminal and execute
I had a look but I don't think it's exactly what I'm looking for, unless I can use asyncio to pause execution when an input() is reached. I might try some sort of await to see if it works, or see if I can figure out if the 'input' HTML element can be used
Avatar
anfroholic 21/10/2024 13:17
Ah, yeah. I forgot that was a thing. Concurrency is just plain hard... good luck!
๐Ÿ˜ข 1
Avatar
My least favourite module during my degree has come back to haunt me ๐Ÿ˜…
Avatar
Avatar
Kyal
My least favourite module during my degree has come back to haunt me ๐Ÿ˜…
anfroholic 21/10/2024 13:40
I guess I have no idea what you're really doing. But I'll say that I am the opposite of a purist. I don't think your students will be writing while stop == False: user_input = input("Enter a value") if user_input == "!": stop = True else: print(user_input) So none of that actually matters from my opinion. If you think of this more like an HTML document that just has some kind of python backend/server you'll get this awaiting by virtue of what it is. I am a huge fan of domain specific languages as doing the things they are good at is super easy and straightforward. a basic example may look like this <script> function doit(value){ console.log(value) } </script> some sample code here <input type="text" onchange="doit(this.value)"> then in your pyscript you have. ...and I can't remember the exact syntax anymore it may have changed since the last time i looked from pyscript import js def pydoit(value): print(f'pyscript: {value}) js.doit = pydoit # this will override the original js function (which may not be needed in the first place) Then you just replace functions in various places for whatever you're looking to do (edited)
Avatar
quantumjim 21/10/2024 17:23
Hi all. I just wondered if I could ask a quick question to see if my use case is viable. Basically I want to make a website where people can upload and then run a Python file. Could Pyscript be used to do that?
Avatar
run python like replit ( on a real server) or on in the client browser (pyscript) ? (edited)
Avatar
Avatar
pmp-p
run python like replit ( on a real server) or on in the client browser (pyscript) ? (edited)
quantumjim 22/10/2024 10:16
in the client browser
Avatar
Avatar
quantumjim
in the client browser
then yes eg see https://pyscript.com/
Avatar
Avatar
pmp-p
then yes eg see https://pyscript.com/
quantumjim 22/10/2024 10:20
Ah, that's great. Thanks!
Avatar
@quantumjim your use case is one of the main features of PyScript. docs.pyscript.net is where to start for the technical details.
๐Ÿ‘ 1
Avatar
Avatar
quantumjim
Hi all. I just wondered if I could ask a quick question to see if my use case is viable. Basically I want to make a website where people can upload and then run a Python file. Could Pyscript be used to do that?
Chris Laffra 22/10/2024 22:58
Take a look at https://pysheets.app, it runs your Python code in the Browser, in a worker to be precise. (edited)
๐Ÿ‘ 1
Avatar
Hi all, pyscript can attach and fire custom events on specific dom elements (attaching with @where(element, event name) and creating using window.createEvent.new) but didnโ€™t found in documentation how to send data/message within the event. Tried when creating the event to put as argument the latter but without success. For now it works except sending that data/message on event. Anyone knows how to do it? (edited)
Avatar
How to update username in pyscript.com?
Avatar
Avatar
Ridensium
Hi all, pyscript can attach and fire custom events on specific dom elements (attaching with @where(element, event name) and creating using window.createEvent.new) but didnโ€™t found in documentation how to send data/message within the event. Tried when creating the event to put as argument the latter but without success. For now it works except sending that data/message on event. Anyone knows how to do it? (edited)
I use the proxy in the ltk (and make my UIs in the ltk) E.g. ltk.Input(measure).on("change", ltk.proxy(lambda event: change_param(event, uniqueid))).attr("id",uniqueid).addClass("measure")
๐Ÿ‘Œ 1
Avatar
Suppose I want to toggle an element between hidden and not hidden, is there an easy way to do this in pyscript? Thanks in advance
Avatar
Avatar
ableman
Suppose I want to toggle an element between hidden and not hidden, is there an easy way to do this in pyscript? Thanks in advance
assuming you make a class like: .gone { display: none; } Then you could toggle that class on and off for that dom element. To get the element you can use jquery (especially if you are using the ltk for your UI (as jquery is included) or any kind of dom selector to get the element you wish to toggle.
Avatar
thanks, I've been trying to use the "hidden" HTML keyword, css style seems more likely to succeed
Avatar
yes. have to agree. css means the browser does the work and its well tested. For more UI stuff and selectors. check out the docs and the pyscript.web section on this page: https://docs.pyscript.net/2024.10.2/api/
Avatar
Hi guys, everything ok? I'm explore the Pyscript creating interactive slide to a presentation. I started this project cloning some project in pyscript.com. My doubt is if it`s possible to export this project to a folder in my local machine. The idea is open the project on Visual Studio Code, running local and store the files in Github repository too. Im trying to see some option related with that in pyscript.com, but I didn't found.... Anyone can help me with that?
Avatar
@iury integration with GitHub is definitely on the roadmap for pyscript.com. I guess @Martin or @almart could tell you more? (They're devs on pyscript.com)
โค๏ธ 1
Avatar
Avatar
ntoll
@iury integration with GitHub is definitely on the roadmap for pyscript.com. I guess @Martin or @almart could tell you more? (They're devs on pyscript.com)
Hmmmm Understood, so for now it's not possible, probably if I want, I need to copy and paste each file content manually in my local machine, right?
Avatar
Avatar
iury
Hmmmm Understood, so for now it's not possible, probably if I want, I need to copy and paste each file content manually in my local machine, right?
yes that is what I am currently doing. It will be a nice feature when it comes. very manual right now... ๐Ÿ™‚
๐Ÿ‘ 1
Avatar
Avatar
ableman
Suppose I want to toggle an element between hidden and not hidden, is there an easy way to do this in pyscript? Thanks in advance
Chris Laffra 31/10/2024 11:46
Here is how you can do this easily in PyScript's LTK library: https://pyscript.github.io/ltk/. With LTK you can also inject CSS style sheets very easily.
Avatar
Good Night guys. I'm trying to render the diagrams inside of Pyscript. Is there some tool can help to build something like this https://diagrams.mingrammer.com/docs/getting-started/installation in Pyscript? Some diagram as a code integrated with Pyscript? (edited)
It requires Python 3.6 or higher, check your Python version first.
Avatar
JefeThePug 01/11/2024 02:41
Hey. Is it possible to load up python modules from my github repo in raw mode to be used in the pyscript code? Ideally I'd like to be able to do: main.pyimport helper import boardbut helper.py and board.py not existing in the root file system but instead read from github. I've tried to do it in my config TOML with no success... name = "Name" description = "..." packages = ["numpy", "requests"] [files] "helper" = "https://raw.githubusercontent.com/path/to/helper.py" "board" = "https://raw.githubusercontent.com/path/to/board.py" I've tried a bunch of combinations with quotes around helper/board, without, with .py on the end, with ./ at the start... none work. (edited)
Avatar
Avatar
JefeThePug
Hey. Is it possible to load up python modules from my github repo in raw mode to be used in the pyscript code? Ideally I'd like to be able to do: main.pyimport helper import boardbut helper.py and board.py not existing in the root file system but instead read from github. I've tried to do it in my config TOML with no success... name = "Name" description = "..." packages = ["numpy", "requests"] [files] "helper" = "https://raw.githubusercontent.com/path/to/helper.py" "board" = "https://raw.githubusercontent.com/path/to/board.py" I've tried a bunch of combinations with quotes around helper/board, without, with .py on the end, with ./ at the start... none work. (edited)
That is the right approach. As an example - have a look at the ltk starter examples. Goto the page linked below and scroll down to "getting started" where you will find both micropython and pyodide examples of using the ltk - which comes from a github account like yours. Check the toml files for each example on pyscript,com - https://github.com/pyscript/ltk
LTK is a little toolkit for writing UIs in PyScript - pyscript/ltk
๐Ÿ‘ 3
Avatar
Avatar
Neon22
That is the right approach. As an example - have a look at the ltk starter examples. Goto the page linked below and scroll down to "getting started" where you will find both micropython and pyodide examples of using the ltk - which comes from a github account like yours. Check the toml files for each example on pyscript,com - https://github.com/pyscript/ltk
JefeThePug 01/11/2024 08:17
OMG I can't believe I didnt just try reversing the order of things on the = sign... ๐Ÿคฆโ€โ™‚๏ธ Thank you so much!
๐Ÿ‘ 1
Avatar
pliroforiki 03/11/2024 19:05
Hi @everyone, I would like to run code after the PyREPL has been run. I tried onAfterRun hook but does not seem to work. Can you please help me? This is the simple project: https://pliroforiki.pyscriptapps.com/pyrepls/
Avatar
Avatar
pliroforiki
Hi @everyone, I would like to run code after the PyREPL has been run. I tried onAfterRun hook but does not seem to work. Can you please help me? This is the simple project: https://pliroforiki.pyscriptapps.com/pyrepls/
not sure what all these are doing: <script type="text/javascript" nonce="71340758f62d4234aed13772c61" src="//local.adguard.org?ts=1730283868492&amp;type=content-script&amp;dmn=pyscript.com&amp;url=https%3A%2F%2Fpyscript.com%2Fapi%2Fcontent%2F1cbafd21-3208-4424-a703-82fae09c0d1e%252Fbb6f3633-2f1d-447e-9321-630ce990a267%252Flatest%252Findex.html&amp;app=opera.exe&amp;css=3&amp;js=1&amp;rel=1&amp;rji=1&amp;sbe=1"></script> and you need to be using a modern versin of pyscript. Your link is very old and not in use anymore. Try: <!-- Import PyScript --> <link rel="stylesheet" href="https://pyscript.net/releases/2024.10.2/core.css"> <script type="module" src="https://pyscript.net/releases/2024.10.2/core.js"></script>
Avatar
Avatar
Neon22
not sure what all these are doing: <script type="text/javascript" nonce="71340758f62d4234aed13772c61" src="//local.adguard.org?ts=1730283868492&amp;type=content-script&amp;dmn=pyscript.com&amp;url=https%3A%2F%2Fpyscript.com%2Fapi%2Fcontent%2F1cbafd21-3208-4424-a703-82fae09c0d1e%252Fbb6f3633-2f1d-447e-9321-630ce990a267%252Flatest%252Findex.html&amp;app=opera.exe&amp;css=3&amp;js=1&amp;rel=1&amp;rji=1&amp;sbe=1"></script> and you need to be using a modern versin of pyscript. Your link is very old and not in use anymore. Try: <!-- Import PyScript --> <link rel="stylesheet" href="https://pyscript.net/releases/2024.10.2/core.css"> <script type="module" src="https://pyscript.net/releases/2024.10.2/core.js"></script>
pliroforiki 04/11/2024 07:07
I tried the links you said and now it does not run at all. See โ†’ https://pliroforiki.pyscriptapps.com/pyrepls/ I take error. But the thing is the onAfterRun hook that I would like to run a function. How I do this on my plugin? (edited)
Avatar
Avatar
pliroforiki
I tried the links you said and now it does not run at all. See โ†’ https://pliroforiki.pyscriptapps.com/pyrepls/ I take error. But the thing is the onAfterRun hook that I would like to run a function. How I do this on my plugin? (edited)
I don't know much about Plugins sorry. However the error message says "ImportError: cannot import name 'Plugin' from 'pyscript' (/home/pyodide/pyscript/init.py)" so I don't think you can import Plugins from there. I do see there is some documentation about plugins here: https://docs.pyscript.net/2024.10.2/user-guide/plugins/ I hope this helps
Avatar
Avatar
pliroforiki
I tried the links you said and now it does not run at all. See โ†’ https://pliroforiki.pyscriptapps.com/pyrepls/ I take error. But the thing is the onAfterRun hook that I would like to run a function. How I do this on my plugin? (edited)
Andrea Giammarchi 04/11/2024 14:21
not sure I am helping much but hooks are really a core functionality thing that work only on JS side when you pre-import the module and instrument PyScript to do different things. If anyone is trying hooks in any other way, after PyScript landed, or via Python, I think I have bad news around that ๐Ÿฅฒ
14:23
anyone can check our plugins directory to see how we instrument hooks Ahead of Time (AOT) before things are even parsed in the wild. We also removed hooks from our docs because it's super advanced topic and, humbling down a bit, maybe not in its best state of affairs, even if it these worked extremely well to date to pollute PyScript core with anything we needed.
Avatar
Avatar
Andrea Giammarchi
anyone can check our plugins directory to see how we instrument hooks Ahead of Time (AOT) before things are even parsed in the wild. We also removed hooks from our docs because it's super advanced topic and, humbling down a bit, maybe not in its best state of affairs, even if it these worked extremely well to date to pollute PyScript core with anything we needed.
pliroforiki 04/11/2024 15:24
what I would like to do is to print somethings based on the result printed on the REPL terminal after RUN button is pressed. How I could hook to that? I need a basic hook or a plugin? Is there a sequence diagram to see when it runs?
Avatar
Avatar
pliroforiki
what I would like to do is to print somethings based on the result printed on the REPL terminal after RUN button is pressed. How I could hook to that? I need a basic hook or a plugin? Is there a sequence diagram to see when it runs?
Do you mean: The user has typed something (code) into the repl, hit run, and there is some output. You want to get hold of the output and do something with it ? Or do you mean that you have initiated some code in the repl and the user's action is to hit run. You then want to know that the program has run because you're going to do something next (like add more code) ? Or something lse? Have you looked at the py-editor ?
Avatar
Avatar
Neon22
Do you mean: The user has typed something (code) into the repl, hit run, and there is some output. You want to get hold of the output and do something with it ? Or do you mean that you have initiated some code in the repl and the user's action is to hit run. You then want to know that the program has run because you're going to do something next (like add more code) ? Or something lse? Have you looked at the py-editor ?
pliroforiki 04/11/2024 21:22
Hi, I need the first sequence of actions. 1. student writes code in REPL โ†’ 2. Hits run โ†’ 3. I run check result code after RUN to check solution of student.
Avatar
Avatar
pliroforiki
Hi, I need the first sequence of actions. 1. student writes code in REPL โ†’ 2. Hits run โ†’ 3. I run check result code after RUN to check solution of student.
I have seen people discussing this usecase on this discord forum. Doing some searching may be useful to you. I did find this about the py-editor which seems to do most of what you want: https://docs.pyscript.net/2024.10.2/user-guide/editor/ Also a special donkey process now exists (starts in JS) which allows you to execute the code without any startup. You copy the user's code entry to the Donkey and then shows the results. This might also be a useful approach ? The donkey is halfway down this page (so #ref sorry) https://docs.pyscript.net/2024.10.2/api/ and here is an example of its use (you can call it to execute user stuff) - https://pyscript.com/@neon22/pyscript-donkey-copy/latest?files=main.py,index.js Maybe someone who has done this already could chime in ? (edited)
๐Ÿ’ฏ 1
Avatar
imsomebodys 05/11/2024 02:55
how exactly do i interact with sockets are they done in pyscript or js
Avatar
imsomebodys 05/11/2024 03:26
by sockets i mean websockets i know regular sockets have been removed
03:27
or not added
Avatar
some info on pyscript's websockets is here: https://docs.pyscript.net/2024.10.2/api/
Avatar
JefeThePug 05/11/2024 10:18
Has anyone used pyscript with NLTK?
Avatar
No idea. Why not try it and report back?
Avatar
LucianoRamalho 05/11/2024 20:51
Hi, all! If I am creating an HTML page from scratch, how do I specify that I want to use MicroPython as the runtime for PyScript?
20:52
The docs say "The value of the interpreter option should be a valid version number for the Python interpreter you are configuring, or a fully qualified URL to a custom version of the interpreter." (https://docs.pyscript.net/2023.11.2/user-guide/configuration/)
20:52
That did not help me ๐Ÿ˜‰
20:54
Perhaps what I need is the URL for the MicroPython runtime to put in the <script> tag?
20:54
Where do I find that?
Avatar
Avatar
LucianoRamalho
Hi, all! If I am creating an HTML page from scratch, how do I specify that I want to use MicroPython as the runtime for PyScript?
you need to use type="mpy" for micropython instead of type="py" . <script type="mpy" src="main.py"></script> this is from the docs at https://docs.pyscript.net/2024.10.2/user-guide/first-steps/
๐Ÿ‘ 1
Avatar
is there a javascript event that gets fired once pyscript is fully initialized?
Avatar
Avatar
Jeff Glass
Possibly the cleanest way would be to use a Plugin which implements the afterStartup() method https://github.com/pyscript/pyscript/blob/25809660efab80046d75314b14c08ff2c5d6194e/pyscriptjs/src/main.ts#L197
Is this still valid or is there native functionality now?
Avatar
found this in the df example. Looks to be what im looking for. addEventListener('py:ready', () => loading.close());
23:38
Never mind thats not working. document.addEventListener('py:ready', function () {
Avatar
Avatar
Neon22
I have seen people discussing this usecase on this discord forum. Doing some searching may be useful to you. I did find this about the py-editor which seems to do most of what you want: https://docs.pyscript.net/2024.10.2/user-guide/editor/ Also a special donkey process now exists (starts in JS) which allows you to execute the code without any startup. You copy the user's code entry to the Donkey and then shows the results. This might also be a useful approach ? The donkey is halfway down this page (so #ref sorry) https://docs.pyscript.net/2024.10.2/api/ and here is an example of its use (you can call it to execute user stuff) - https://pyscript.com/@neon22/pyscript-donkey-copy/latest?files=main.py,index.js Maybe someone who has done this already could chime in ? (edited)
pliroforiki 06/11/2024 07:03
thanks a lot! I ll look into all these useful links! Do you mind if I bug you a little bit more on that matter?
Avatar
Avatar
Neon22
I have seen people discussing this usecase on this discord forum. Doing some searching may be useful to you. I did find this about the py-editor which seems to do most of what you want: https://docs.pyscript.net/2024.10.2/user-guide/editor/ Also a special donkey process now exists (starts in JS) which allows you to execute the code without any startup. You copy the user's code entry to the Donkey and then shows the results. This might also be a useful approach ? The donkey is halfway down this page (so #ref sorry) https://docs.pyscript.net/2024.10.2/api/ and here is an example of its use (you can call it to execute user stuff) - https://pyscript.com/@neon22/pyscript-donkey-copy/latest?files=main.py,index.js Maybe someone who has done this already could chime in ? (edited)
pliroforiki 06/11/2024 10:29
I see this (https://docs.pyscript.net/2024.10.2/user-guide/editor/) that suits me but gives error. Do you know where I can report bugs like these? (edited)
Avatar
Avatar
pliroforiki
I see this (https://docs.pyscript.net/2024.10.2/user-guide/editor/) that suits me but gives error. Do you know where I can report bugs like these? (edited)
github pyscript
Avatar
Avatar
Quint
found this in the df example. Looks to be what im looking for. addEventListener('py:ready', () => loading.close());
Andrea Giammarchi 06/11/2024 12:11
there are other events, including py:all-done
Avatar
Avatar
Andrea Giammarchi
there are other events, including py:all-done
Thanks ill give that a try. Are a list of these event available any where?
Avatar
Avatar
Quint
Thanks ill give that a try. Are a list of these event available any where?
didnt work :(. this is the example im working with. <script> document.addEventListener('DOMContentLoaded', function () { console.log('DOM !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'); document.addEventListener('py:all-done', function () { console.log('PY !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'); }); }); </script>
20:39
DOM prints but PY log never gets called. (edited)
Avatar
Chris Laffra 06/11/2024 20:52
At the top of your main Python script you can send any event you want to the document. Like so? js.window.dispatchEvent(js.window.Event.new("python-done")); (edited)
๐Ÿ‘ 1
Avatar
how to import pyscript into vs code
Avatar
Avatar
Quint
Thanks ill give that a try. Are a list of these event available any where?
Andrea Giammarchi 07/11/2024 09:53
Avatar
Avatar
FM_1700
how to import pyscript into vs code
pyscript runs only in the browser. You can develop in whatever suits you. Please expand your question
Avatar
hey guys, how do you install packages i'm trying to use beautiful soup, but there is not shell, so I can't do pip install bs4. What is the method for this on pyscript?
01:04
and if there is a shell, could you point me towards it? thaks
Avatar
Avatar
Aistun
hey guys, how do you install packages i'm trying to use beautiful soup, but there is not shell, so I can't do pip install bs4. What is the method for this on pyscript?
via py-config tags in your HTML. <py-config> packages = ["pandas"] </py-config> (edited)
Avatar
thank you
๐Ÿ‘ 1
Avatar
Avatar
Aistun
thank you
Avatar
JefeThePug 08/11/2024 03:22
It says nltk is a supported library in the pyscript docs but does anyone know what corpora are supported? I'm struggling to do nltk.download("words") no matter if I try it the default way or unzipping the actual words.zip into my project. It can't find it
Avatar
@JefeThePug ๐Ÿ‘‹ I don't NLTK that well, but I imagine the docs will tell you where on the local FS (i.e. the FS in the browser) you should but the corpora. Then ensure you use the PyScript config options WRT files / zips to put it in the right place (see: https://docs.pyscript.net/2024.10.2/user-guide/configuration/#files) Hope this helps and please let us know how you get on..!
Avatar
imsomebodys 10/11/2024 02:35
I wrote a program that uses canvas how do i do animation in pyscript
02:37
from pyscript import document, window from pyscript.ffi import create_proxy class Canvas: def __init__(self, id, width=64*9, height=64*9): self.canvas = document.querySelector(id) self.ctx = self.canvas.getContext('2d') self.canvas.height = height self.canvas.width = width self.y = 100 self.set_bg_color('black') self.animation() def clear(self, color='black', pos=(0,0), size=None): x,y = pos if size: width, height = size else: width, height = (self.canvas.width, self.canvas.height) self.ctx.fillStyle = color self.ctx.clearRect(x,y,width,height) def set_bg_color(self, color): self.ctx.fillStyle = color self.ctx.fillRect(0, 0, self.canvas.width, self.canvas.height) def animation(self): self.ctx.fillStyle = 'red' self.ctx.fillRect(100, self.y, 100, 100) self.y += 1 self.clear() proxy = create_proxy(self.animation) window.requestAnimationFrame(proxy) i get this error Uncaught PythonError: TypeError: Canvas.animation() takes 1 positional argument but 2 were given
22:04
Is there any easy way to change the theme in the py-editor? I understand that it uses CodeMirror and I feel like it shouldn't be too hard to use a different code mirror theme, but I don't see a way other than cloning down the pyscript project and changing it there. Surely, I can just override the css somehow, but I am a... back end engineer.
Avatar
It seems that there should be a way to access a CodeMirror object from js at runtime, but I can't find it. For usability, it would be really nice I found could add a tag like theme to the py-editor div and it loaded that theme. (edited)
Avatar
Hi Everyone, I am trying to find the best way to stop a script running in an editor. I'm bootstrapping the environment using <script type="mpy-editor" config="pyscript.toml" env="env" src="init.py" setup></script> <script type="mpy-editor" env="env" target="editor" id="client-script"> is there a way to get the worker the editor is running within the setup stage somehow and call worker.terminate() when needed? (tie it into a button click handler?) Sorry, I am new, and it seems a bit confusing... (edited)
Avatar
Avatar
xcorat
Hi Everyone, I am trying to find the best way to stop a script running in an editor. I'm bootstrapping the environment using <script type="mpy-editor" config="pyscript.toml" env="env" src="init.py" setup></script> <script type="mpy-editor" env="env" target="editor" id="client-script"> is there a way to get the worker the editor is running within the setup stage somehow and call worker.terminate() when needed? (tie it into a button click handler?) Sorry, I am new, and it seems a bit confusing... (edited)
Ok, I used pyscript.window to add a global stop_flag to test for it within script. But I still would like to stop it when it's unresponsive without having to refresh the page...
Avatar
Avatar
xcorat
Hi Everyone, I am trying to find the best way to stop a script running in an editor. I'm bootstrapping the environment using <script type="mpy-editor" config="pyscript.toml" env="env" src="init.py" setup></script> <script type="mpy-editor" env="env" target="editor" id="client-script"> is there a way to get the worker the editor is running within the setup stage somehow and call worker.terminate() when needed? (tie it into a button click handler?) Sorry, I am new, and it seems a bit confusing... (edited)
Andrea Giammarchi 13/11/2024 08:15
the script bootstrapping the editor will have an xworker attached where you can invoke terminate() any time you need.
Avatar
I am getting the following exception and I'm unsure why. PyScript calls a function in a python file I load in a script tag. It's obviously loaded successfully as it calls the function. My server logs tell me that the endpoint was called successfully... INFO: 127.0.0.1:49266 - "GET /get_uuid HTTP/1.1" 200 OK Traceback (most recent call last): File "/lib/python312.zip/_pyodide/_base.py", line 597, in eval_code_async await CodeRunner( File "/lib/python312.zip/_pyodide/_base.py", line 413, in run_async await coroutine File "<exec>", line 5, in <module> File "/home/pyodide/c_and_c_client.py", line 14, in get_uuid response = await fetch("http://localhost:8000/get_uuid") ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/lib/python312.zip/pyodide/webloop.py", line 108, in callback r = onrejected_(e) ^^^^^^^^^^^^^^ File "/lib/python312.zip/pyodide/webloop.py", line 102, in onrejected_ raise x pyodide.ffi.JsException: TypeError: Failed to fetch
Avatar
Chris Laffra 17/11/2024 23:04
@rhacer: What do you see in the DevTools "Network" tab? (edited)
Avatar
Hi Pyscripters! I have a question related to what @JefeThePug was trying to do a few weeks ago. I have some compiled 100MB+ files on my github release page (https://github.com/bowersd/otw/releases/tag/v.0.1.0-alpha), and I would like to fetch them only if the user indicates they want them. My problem is that I can't seem to get the files from the URL, and even if I did, I can't get my downstream function to see that they have been loaded. In more detail... A simplified version of my main.py script looks like this: import pyscript from pyodide.http import pyfetch what_to_use = ["default_file_specified_in_TOML"] async def custom_load(event): x = pyscript.document.querySelector("#user_option").value value_mappings = {"some": "collection", "of": "values", "mapped": "to_urls"} if x: nu_file = await pyfetch(value_mappings[x]) what_to_use.append(nu_file) def do_it(event): for f in what_to_use: foo(f, data_specified_by_user) #process the data_specified_by_user using f My index.html file has buttons with the py-click option for both the do_it() function (to be clicked when the user specifies data) and the custom_load() function (to be clicked when the user customizes options). These functions work as intended. EXCEPT:
  • I am getting 404 not found errors
  • the do_it() function operates only on the value of what_to_use that was specified outside of custom_load() Addressing some sideshows:
  • I am using pyscript version 2024.1.1 . I tried updating to 2024.10.2 and the resulting chain of failed imports was too much of a headache to fix at the current time *I also tried using open_url() and fetch() I am happy to use whichever one is best
  • The files are big enough that it seems like a waste to fetch them on page load by specifying them with my config TOML ... but if that is the best way, then I'll do it.
  • The files are too big for github to host them, so I can't keep them in my repo.
(edited)
Avatar
Avatar
sautedman
Hi Pyscripters! I have a question related to what @JefeThePug was trying to do a few weeks ago. I have some compiled 100MB+ files on my github release page (https://github.com/bowersd/otw/releases/tag/v.0.1.0-alpha), and I would like to fetch them only if the user indicates they want them. My problem is that I can't seem to get the files from the URL, and even if I did, I can't get my downstream function to see that they have been loaded. In more detail... A simplified version of my main.py script looks like this: import pyscript from pyodide.http import pyfetch what_to_use = ["default_file_specified_in_TOML"] async def custom_load(event): x = pyscript.document.querySelector("#user_option").value value_mappings = {"some": "collection", "of": "values", "mapped": "to_urls"} if x: nu_file = await pyfetch(value_mappings[x]) what_to_use.append(nu_file) def do_it(event): for f in what_to_use: foo(f, data_specified_by_user) #process the data_specified_by_user using f My index.html file has buttons with the py-click option for both the do_it() function (to be clicked when the user specifies data) and the custom_load() function (to be clicked when the user customizes options). These functions work as intended. EXCEPT:
  • I am getting 404 not found errors
  • the do_it() function operates only on the value of what_to_use that was specified outside of custom_load() Addressing some sideshows:
  • I am using pyscript version 2024.1.1 . I tried updating to 2024.10.2 and the resulting chain of failed imports was too much of a headache to fix at the current time *I also tried using open_url() and fetch() I am happy to use whichever one is best
  • The files are big enough that it seems like a waste to fetch them on page load by specifying them with my config TOML ... but if that is the best way, then I'll do it.
  • The files are too big for github to host them, so I can't keep them in my repo.
(edited)
About the 404 errors, I thought I just needed to replace https://github.com/bowersd/otw/releases/download/v.0.1.0-alpha/foo with https://raw.githubusercontent.com/bowersd/otw/releases/download/v.0.1.0-alpha/foo (at least something like that seems to work here: https://pyscript.com/@examples/folium/latest?files=README.md,pyscript.toml,main.py), but that seems to be incorrect. What is the right way to do this?
Avatar
Avatar
sautedman
About the 404 errors, I thought I just needed to replace https://github.com/bowersd/otw/releases/download/v.0.1.0-alpha/foo with https://raw.githubusercontent.com/bowersd/otw/releases/download/v.0.1.0-alpha/foo (at least something like that seems to work here: https://pyscript.com/@examples/folium/latest?files=README.md,pyscript.toml,main.py), but that seems to be incorrect. What is the right way to do this?
I had trouble with this also but found this hint (which works for me): # - to find rawgithub url - append `?raw=true` to url in browser # and use resolved url which will come from raw.githubusercontent.com I.e. just navigate to the url you want to use and put that on the end and hit return = rawgithub url (edited)
Avatar
@sautedman there's always: https://docs.pyscript.net/2024.10.2/api/#pyscriptpy_import (although this function is likely to be renamed/move in the API in future releases).
Avatar
Avatar
Chris Laffra
@rhacer: What do you see in the DevTools "Network" tab? (edited)
I'm not sure exactly what I should be looking at, so taking a guess.
Avatar
Avatar
rhacer
I'm not sure exactly what I should be looking at, so taking a guess.
Chris Laffra 18/11/2024 14:42
That is the Network tab, indeed. Do you see the URL contents you were expecting? Then you know the problem is somewhere in the Python stock.
Avatar
I see a call to get_uuid, and that it returned OK.
14:43
That matches what my server logs say.
14:45
It feels like it's trying to fetch something else, but I'm not sure what that something else would be as the request is not logged.
Avatar
Hi, was anyone able to get one of the UI libs like streamlit working with pyscript? I barely remember that there was a example code for a service worker intercepting api requests for one of the python api libs, but can't find it anymore. (Maybe that was in pyodide repo). Thanks in advance!
Avatar
Avatar
Neon22
I had trouble with this also but found this hint (which works for me): # - to find rawgithub url - append `?raw=true` to url in browser # and use resolved url which will come from raw.githubusercontent.com I.e. just navigate to the url you want to use and put that on the end and hit return = rawgithub url (edited)
That's a really useful trick, thanks @Neon22! (I will note that Chrome and Firefox do not render the ?raw=true address into another address, but they do go to a raw page). Ultimately, it looks like compiled binaries on a release page do not have a raw page version to access, and GitHub blocks direct calls to the download link due to a CORS error. It looks like I will need to put my files elsewhere, or make users download the files themselves and manually upload them to my pyscript site. I will get to work on that. Any suggestions welcome!
Avatar
Avatar
ntoll
@sautedman there's always: https://docs.pyscript.net/2024.10.2/api/#pyscriptpy_import (although this function is likely to be renamed/move in the API in future releases).
@ntoll sincerely, thank you for your input. I suspect that py_import() will not work, since these are not .py files.
Avatar
Avatar
sautedman
That's a really useful trick, thanks @Neon22! (I will note that Chrome and Firefox do not render the ?raw=true address into another address, but they do go to a raw page). Ultimately, it looks like compiled binaries on a release page do not have a raw page version to access, and GitHub blocks direct calls to the download link due to a CORS error. It looks like I will need to put my files elsewhere, or make users download the files themselves and manually upload them to my pyscript site. I will get to work on that. Any suggestions welcome!
I am not sure what kind of files you are looking to load. But the rawgithub url allows me to refer to it in the toml file and therefore load it with an open(). e.g. for an image file: [files] "https://raw.githubusercontent.com/Neon22/pyweaving/wif-missing-warp/src/pyweaving/data/pyweaving-logo-1-84sq.png" = "logo-1-84sq.png" allows me to reference it inside the python code as "./logo-1-84sq.png" If you are looking to load js/css files then the ltk uses this approach: [files] "https://raw.githubusercontent.com/pyscript/ltk/main/ltk/ltk.js" = "ltk/ltk.js" "https://raw.githubusercontent.com/pyscript/ltk/main/ltk/ltk.css" = "ltk/ltk.css" as well as having this also in the toml file: packages = [ "pyscript-ltk"] then you just import ltk in your python file. But not sure what kind of task you are trying to do.
Avatar
Avatar
Neon22
I am not sure what kind of files you are looking to load. But the rawgithub url allows me to refer to it in the toml file and therefore load it with an open(). e.g. for an image file: [files] "https://raw.githubusercontent.com/Neon22/pyweaving/wif-missing-warp/src/pyweaving/data/pyweaving-logo-1-84sq.png" = "logo-1-84sq.png" allows me to reference it inside the python code as "./logo-1-84sq.png" If you are looking to load js/css files then the ltk uses this approach: [files] "https://raw.githubusercontent.com/pyscript/ltk/main/ltk/ltk.js" = "ltk/ltk.js" "https://raw.githubusercontent.com/pyscript/ltk/main/ltk/ltk.css" = "ltk/ltk.css" as well as having this also in the toml file: packages = [ "pyscript-ltk"] then you just import ltk in your python file. But not sure what kind of task you are trying to do.
@Neon22 Well, since you asked, I am applying compiled finite state machines to user-supplied text :). To be specific: (edited)
Use a morphological analyzer to enhance text.
Tools for applying linguistic analyzers to text, and checking the output. The end goal is producing glossaries, lemmatizations, or interlinearizations. - bowersd/textAnalysis
This is a set of 6 finite state transducers, for analyzing Ojibwe, both the syncopated Nishnaabemwin dialects and the unsyncopated Anishinaabemowin dialects. Most of the development focus has been ...
Avatar
Avatar
sautedman
@Neon22 Well, since you asked, I am applying compiled finite state machines to user-supplied text :). To be specific: (edited)
For my more pressing problem, the gist of it is that I set things up like this: foo = [] def bar(event): for o in values_specified_by_users_on_the_html_side: foo.append(o) def baz(event): for f in foo: print(f) I assumed that when a user clicked a button with py-click=bar specified, foo would get updated, and then when they clicked a button with py-click=baz specified, the updated value of foo would be accessed. But it isn't. Only the initial value of foo is used inside baz. UPDATE: I had been thinking that I was being clever and avoiding having to tangle with async and await (probably inspired by this discussion of just putting user data straight into a python data structure https://github.com/pyscript/pyscript/discussions/1146). Ultimately, could an approach like that work to add the results of three optional upload buttons to a containing python data structure, and then use that data structure? Or do I need to base my approach off of @Jeff Glass's demo here (https://jeff.glass/post/pyscript-image-upload/) ? At this late hour I am having trouble seeing where I should stash the various optional uploaded files before ultimately using them in my main function. Very many thanks to anyone who looks at this ๐Ÿ™‚ (edited)
How to upload images into Pyscript and work with them in Pillow in-browser
Neon22 started a thread. 19/11/2024 12:14
Avatar
Avatar
sautedman
For my more pressing problem, the gist of it is that I set things up like this: foo = [] def bar(event): for o in values_specified_by_users_on_the_html_side: foo.append(o) def baz(event): for f in foo: print(f) I assumed that when a user clicked a button with py-click=bar specified, foo would get updated, and then when they clicked a button with py-click=baz specified, the updated value of foo would be accessed. But it isn't. Only the initial value of foo is used inside baz. UPDATE: I had been thinking that I was being clever and avoiding having to tangle with async and await (probably inspired by this discussion of just putting user data straight into a python data structure https://github.com/pyscript/pyscript/discussions/1146). Ultimately, could an approach like that work to add the results of three optional upload buttons to a containing python data structure, and then use that data structure? Or do I need to base my approach off of @Jeff Glass's demo here (https://jeff.glass/post/pyscript-image-upload/) ? At this late hour I am having trouble seeing where I should stash the various optional uploaded files before ultimately using them in my main function. Very many thanks to anyone who looks at this ๐Ÿ™‚ (edited)
Andrea Giammarchi 19/11/2024 16:11
if worth nothing, py- event listeners are deprecated
Avatar
@Andrea Giammarchi Great to know. Is there a place you can point me to where I can see the right way to do things?
Avatar
Avatar
sautedman
@Andrea Giammarchi Great to know. Is there a place you can point me to where I can see the right way to do things?
Andrea Giammarchi 19/11/2024 17:47
our docs around @when decorator might be a starter https://docs.pyscript.net/2024.10.2/api/#pyscriptwhen
๐Ÿ‘ 1
sautedman started a thread. 19/11/2024 18:01
Avatar
Running into unintuitive discord behavior: replying to an old thread does not bubble to "Present". So sharing (in most low-tech way possible) that I found a couple tricks to make matplotlib animations work in pyscript, see https://discord.com/channels/972017612454232116/1190210104574885928/1308816582465687562.
๐Ÿ‘ 1
Avatar
Hello, I have a file fetching question. I'm trying to build a simple dashboard for server status, which reads status files that are uploaded and dumped in the http root. The structure is roughly base/index.html base/includes/*.py base/includes/pkl/*.pkl I wasn't able to load the pkl files, and so I dumped the results of os.walk and it appears they just aren't on the virtual filesystem at all. The content of these files is just text, but the names are not static. It doesn't seem like includes/pkl/* is a legal files entry in config.json, is there another way? (edited)
Avatar
Avatar
FightMilk
Hello, I have a file fetching question. I'm trying to build a simple dashboard for server status, which reads status files that are uploaded and dumped in the http root. The structure is roughly base/index.html base/includes/*.py base/includes/pkl/*.pkl I wasn't able to load the pkl files, and so I dumped the results of os.walk and it appears they just aren't on the virtual filesystem at all. The content of these files is just text, but the names are not static. It doesn't seem like includes/pkl/* is a legal files entry in config.json, is there another way? (edited)
pyscript is only able to look at the browser's internal filesystem. Perhaps you could fetch the files from your server. See fetch in docs. on his page https://docs.pyscript.net/2024.10.2/api/
Avatar
Yes I understand that, hence 'virtual filesystem'. How to get dynamically named files into the virtual filesystem is the question.
Avatar
How come the following html fails with ImportError: no module named 'ulab' yet https://pyscript.net/tech-preview/micropython/repl.html lets me import ulab? <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width,initial-scale=1" /> <title>Test</title> <link rel="stylesheet" href="https://pyscript.net/releases/2024.10.2/core.css"> <script type="module" src="https://pyscript.net/releases/2024.10.2/core.js"></script> </head> <body> <script type="mpy">import ulab</script> </body> </html>
Avatar
Avatar
FightMilk
Yes I understand that, hence 'virtual filesystem'. How to get dynamically named files into the virtual filesystem is the question.
sorry - how about a zip file - which can be unzipped ?
Avatar
Avatar
Jan Burgy
How come the following html fails with ImportError: no module named 'ulab' yet https://pyscript.net/tech-preview/micropython/repl.html lets me import ulab? <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width,initial-scale=1" /> <title>Test</title> <link rel="stylesheet" href="https://pyscript.net/releases/2024.10.2/core.css"> <script type="module" src="https://pyscript.net/releases/2024.10.2/core.js"></script> </head> <body> <script type="mpy">import ulab</script> </body> </html>
you'll probably need to put it in your toml file. This sets up how pyscript sees files/packages. https://docs.pyscript.net/2024.10.2/user-guide/configuration/ then you can import them
Avatar
Avatar
Neon22
you'll probably need to put it in your toml file. This sets up how pyscript sees files/packages. https://docs.pyscript.net/2024.10.2/user-guide/configuration/ then you can import them
I tried that too but ulab is apparently not a package. tech-preview doesn't declare it
Avatar
Avatar
Jan Burgy
I tried that too but ulab is apparently not a package. tech-preview doesn't declare it
just had a look and upab apparently built in c. so I'm guessing that as python only packages can be imported - you'd need to use numpy. or maybe there is a python version of ulab somewhere...?
Avatar
But how come it works in the repl?
Avatar
yeah - you got me. Maybe someone responsible for pyscript can answer...? @ntoll ?
Avatar
@Jan Burgy Can you link me to examples that work, and that don't. When you say REPL, what precisely do you mean? (I'm looking for explicit and detailed steps to reproduce the behaviour you're reporting). TY.
Avatar
@ntoll of course. Here's what doesn't work: https://janburgy.pyscriptapps.com/micropython-ulab/ Here's what does work: https://pyscript.net/tech-preview/micropython/repl.html, type import ulab then hit enter.
Avatar
MicroPyScript: A test harness for multiple runtimes in PyScript - pyscript/MicroPyScript
MicroPython - a lean and efficient Python implementation for microcontrollers and constrained systems - micropython/micropython
Avatar
Yes. The tech preview was exactly that - a proof of concept I created to show something PyScript-ish could work with both MicroPython and Pyodide - in which I compiled MP with ulab. Treat that work as now digital archaeology. The official release of PyScript uses a build from the MicroPython project itself and, for space related reasons, doesn't include ulab. We're working with Damien to figure out how to easily provide bespoke builds.
23:25
(which could include such modules as ulab)
23:25
@Jan Burgy ^^^
Avatar
Where can I follow the progress of that conversation? Quite keen on the MP/ulab mix of feature and size
Avatar
Currently it's internal Anaconda, 'cos we need to figure out how and for what we pay Damien for his time. TL; DR: bureaucracy. Once that's done, it'll be in the open on PyScript's / MP's GitHub.
๐Ÿ‘ 1
Avatar
hello all; I'm having an issue with the pyscript.web module: I'm creating a table dynamically, and I need to define the rowspan attribute for some tds, but even though I'm passing the rowspan=... attribute to web.td(..., rowspan=...), the generated HTML doesn't have that attribute any suggestions on how to solve my issue?
Avatar
Avatar
Javier
hello all; I'm having an issue with the pyscript.web module: I'm creating a table dynamically, and I need to define the rowspan attribute for some tds, but even though I'm passing the rowspan=... attribute to web.td(..., rowspan=...), the generated HTML doesn't have that attribute any suggestions on how to solve my issue?
I ended up switching to document.createElement; that solved the issue and allowed me to set rowspan as needed
Avatar
@Javier can you raise an issue about this over on GitHub (helps us track the problem)..? Thank you!
Avatar
How can I understand micropython's poor performance on https://pyscript.com/@janburgy/weird-performance/latest. This is a silly dynamic programming leetcode question. Javascript runs so fast it's barely perceptible, type="py" runs fast enough and type="mpy" is slow. I am pre-allocating maxlen even though the algorithm doesn't require it because I read something about micropython and allocations. I replaced the generator expression by an inner loop but that didn't help. What else can it be? Is this just MarkdownContext { Depth = 1 } or worth an issue?
Avatar
@Jan Burgy OK... so you should get in touch with the MicroPython folks (Damien - creator of MicroPython - is a friend of the PyScript project). My suggestion is you raise an issue on their GitHub along with as much information needed to recreate the problem. Damien is v.interested in performance, so this is right up his street. ๐Ÿ‘ (And thanks for finding this!)
Avatar
Avatar
ntoll
@Jan Burgy OK... so you should get in touch with the MicroPython folks (Damien - creator of MicroPython - is a friend of the PyScript project). My suggestion is you raise an issue on their GitHub along with as much information needed to recreate the problem. Damien is v.interested in performance, so this is right up his street. ๐Ÿ‘ (And thanks for finding this!)
Port, board and/or hardware pyscript, unix MicroPython version MicroPython 78d017f on 2024-11-27; linux [GCC 12.2.0] version Reproduction Visit https://pyscript.com/@janburgy/weird-performance/late...
๐Ÿ‘ 1
Avatar
anfroholic 28/11/2024 06:33
I am trying to serve this from another local computer and keep getting Uncaught TypeError: crypto.randomUUID is not a function <anonymous> main.js:43 Can someone let me know what's going on? code here: <!DOCTYPE html> <html> <head> <link rel="stylesheet" href="https://pyscript.net/releases/2024.8.1/core.css"> <script type="module" src="https://pyscript.net/releases/2024.8.1/core.js"></script> </head> <body> <h1>hello world</h1> <script type="mpy"> print('hello world') </script> </body> </html>
Avatar
Avatar
anfroholic
I am trying to serve this from another local computer and keep getting Uncaught TypeError: crypto.randomUUID is not a function <anonymous> main.js:43 Can someone let me know what's going on? code here: <!DOCTYPE html> <html> <head> <link rel="stylesheet" href="https://pyscript.net/releases/2024.8.1/core.css"> <script type="module" src="https://pyscript.net/releases/2024.8.1/core.js"></script> </head> <body> <h1>hello world</h1> <script type="mpy"> print('hello world') </script> </body> </html>
not immediaely clear - but try using latest 2024.10.2 rather than 2024.8.1. Also can't print as no terminal in micropython. You could append to the dom. Have you tried any of the examples on pyscript.com ? Its a good place to experiment
Avatar
Avatar
anfroholic
I am trying to serve this from another local computer and keep getting Uncaught TypeError: crypto.randomUUID is not a function <anonymous> main.js:43 Can someone let me know what's going on? code here: <!DOCTYPE html> <html> <head> <link rel="stylesheet" href="https://pyscript.net/releases/2024.8.1/core.css"> <script type="module" src="https://pyscript.net/releases/2024.8.1/core.js"></script> </head> <body> <h1>hello world</h1> <script type="mpy"> print('hello world') </script> </body> </html>
Andrea Giammarchi 28/11/2024 15:47
The randomUUID() method of the Crypto interface is used to generate a v4 UUID using a cryptographically secure random number generator.
Avatar
Andrea Giammarchi 28/11/2024 15:49
Secure Content ... our stack runs only behind secure content because nothing works anymore with unsecure content on the Web ... meaning: if you are serving things from your own intranet (http:// without the s) you need to instrument your browser to trust that URL but this is not about PyScript, this is how the entirety of most recent Web APIs work.
Avatar
Avatar
Andrea Giammarchi
Secure Content ... our stack runs only behind secure content because nothing works anymore with unsecure content on the Web ... meaning: if you are serving things from your own intranet (http:// without the s) you need to instrument your browser to trust that URL but this is not about PyScript, this is how the entirety of most recent Web APIs work.
Andrea Giammarchi 28/11/2024 15:52
alternatively, if you are playing around with some IoT project or whatnot, there is a way to provide https:// (with the s) through local network, but I am afraid that also requires you explicitly adding, and taking care of, those home-made certificates, which is slightly out of this project scope/goal to help with.
Avatar
Avatar
Andrea Giammarchi
alternatively, if you are playing around with some IoT project or whatnot, there is a way to provide https:// (with the s) through local network, but I am afraid that also requires you explicitly adding, and taking care of, those home-made certificates, which is slightly out of this project scope/goal to help with.
Andrea Giammarchi 28/11/2024 15:55
worth adding, for readers sake, that localhost has a special place in browsers' heart ... make it any http://127.0.10.7:8080/ and goodbye secure related features of the Web.
15:55
or a http://192.168.27.3:8080 or any similar IP based approach
15:57
if there's anything to add, we evaluate Python at runtime and non-secure sources/origins could inject malicious code to do evil things so that I welcome the decision of the Web standards to prevent various kind of attacks by demanding https to work at all.
Avatar
anfroholic 28/11/2024 18:48
Thank you @Neon22, I originally had the latest version and kept downgrading to see if it would change anything Thank you @Andrea Giammarchi, I suspected that was the case. When things are actually local (both devices on the same lan), everything works fine even serving files from another device. for other readers sake In my case I'm trying to build a lockers/ storage/ access system, so am attempting to access this machine through tailscale and a bunch of other stuff, so am not surprised I am having issues. Thank you both!
Avatar
blackbooks 29/11/2024 09:32
Hi, I want to go productive with my project and try to harden security. 1. I am not sure how secure the python sandbox environment of PyScript is. Can malicious code in PyScript infect the hosting system? 2. I would like to specify my packages with hash values or is there some other way of certifiying libraries? 3. certificate verification fails when i connect to a https site from the PyScript environment but suceeds from the local environment, how can i get the verification process to work?
Avatar
Avatar
blackbooks
Hi, I want to go productive with my project and try to harden security. 1. I am not sure how secure the python sandbox environment of PyScript is. Can malicious code in PyScript infect the hosting system? 2. I would like to specify my packages with hash values or is there some other way of certifiying libraries? 3. certificate verification fails when i connect to a https site from the PyScript environment but suceeds from the local environment, how can i get the verification process to work?
Andrea Giammarchi 29/11/2024 09:38
One of the main goals/advantages of WASM is that it's secure by design. In PyScript case, you code runs on the browser anyway, so the server cannot be reached, affected, or modified by any mean. To pin dependencies, use the package_name==1.2.3 convention, where 1.2.3 is the exact version and package_name is numpy, matplotlib or any other package you like. That ensures you will use the exact same version in the future. Add the fact we have a micropip cache to guarantee further that, you should be fine?
Avatar
blackbooks 29/11/2024 09:45
Well there is also the option to load packages from a specified url, which could be used to host specific libraries which could be checked beforehand. But if I understand correctly you are saying this is not necessary and a simple version numbering suffices?
Avatar
Avatar
blackbooks
Hi, I want to go productive with my project and try to harden security. 1. I am not sure how secure the python sandbox environment of PyScript is. Can malicious code in PyScript infect the hosting system? 2. I would like to specify my packages with hash values or is there some other way of certifiying libraries? 3. certificate verification fails when i connect to a https site from the PyScript environment but suceeds from the local environment, how can i get the verification process to work?
Chris Laffra 29/11/2024 12:05
PyScript is a web app that uses WebAssembly. It is as equally secure as webapps that use WebAssembly are. Those are very secure. Web standards have emphasized sandbox security for decades. However, one specific thing to be careful about is how you handle API secrets or authentication tokens. If you save them in your Python code, they will get downloaded in clear-text and can be seen by any "man in the middle" attacker or anyone who knows how to open the browser's DevTools. This problem is not unique to PyScript and also applies to JavaScript secrets: https://softwareengineering.stackexchange.com/questions/194045/securely-storing-secret-data-in-a-client-side-web-application (edited)
I have this web application that is going to be all client-side technology (HTML, CSS, JavaScript/AngularJS, etc...). This web application is going to be interacting with REST API in order to acce...
๐Ÿ‘ 1
Avatar
Avatar
blackbooks
Well there is also the option to load packages from a specified url, which could be used to host specific libraries which could be checked beforehand. But if I understand correctly you are saying this is not necessary and a simple version numbering suffices?
Chris Laffra 29/11/2024 12:07
See how pyscript.com (a web app) handles secrets for PyScript applications it manages: https://pyscript.com/docs/api-proxies-and-secrets
Avatar
Avatar
Chris Laffra
See how pyscript.com (a web app) handles secrets for PyScript applications it manages: https://pyscript.com/docs/api-proxies-and-secrets
blackbooks 29/11/2024 12:21
Thank you, the first concern that was raised is that users could load malicous code onto their systems by loading unverified libraries or packages. As i understand this could only happen if the libraries themselves are compromised or someone changed my env.json file on my server, but at that point i have bigger security concerns. Also the PyScript environment can not inject malicious code directly onto the host system, so even if the libraries are compromised they are gone when tha tab gets closed, do i have this correct? I need to alleviate concerns at with my server admin...
Avatar
Chris Laffra 29/11/2024 12:23
@blackbooks The summary is: "PyScript security is identical to JavaScript security".
Avatar
Chris Laffra 29/11/2024 12:41
PyScript files are never installed on someone's system. They are downloaded into the browser's secure WASM sandbox, which has a virtual file system that is cleaned out as soon as the tab containing the app is closed.
Avatar
I want to still use JS for DOM manipulation but PyScript for any logic processing. Any examples of this? Im struggling to call on Python functions from the JS side.
Avatar
Avatar
Quint
I want to still use JS for DOM manipulation but PyScript for any logic processing. Any examples of this? Im struggling to call on Python functions from the JS side.
Chris Laffra 29/11/2024 13:44
Avatar
Yep, thanks came across the answer as well. window.process_data(filteredData); (edited)
Avatar
Avatar
blackbooks
Well there is also the option to load packages from a specified url, which could be used to host specific libraries which could be checked beforehand. But if I understand correctly you are saying this is not necessary and a simple version numbering suffices?
Andrea Giammarchi 29/11/2024 17:00
I am saying, we cache resources, if your sources are the same, resources will be the same ... foreign index_url as config field landed already, feel free to use it if needed.
Avatar
hi! i've added the headers required to make web workers work, but they instead made the page render the plain html as text, somehow. i also tried adding them to just the pyscript .py file rather than the html file, but that didn't help. what might i be doing wrong?
22:22
if it's helpful, here's the django views i'm using to serve the page and the script: class Page(TemplateView): template_name = "page.html" def get(self, request): response = render(request, self.template_name) response.headers = {"Access-Control-Allow-Origin": "*", "Cross-Origin-Opener-Policy": "same-origin", "Cross-Origin-Embedder-Policy": "require-corp", "Cross-Origin-Resource-Policy": "cross-origin"} return response class Script(View): def get(self, request): path = request.path.split('/')[-1].replace('..','') try: file = open(os.path.join(settings.BASE_DIR, f'goop/{path}'), 'r') return HttpResponse(file.read(), content_type='text/plain', headers={"Access-Control-Allow-Origin": "*", "Cross-Origin-Opener-Policy": "same-origin", "Cross-Origin-Embedder-Policy": "require-corp", "Cross-Origin-Resource-Policy": "cross-origin"}) except Exception as e: raise e
Avatar
oh! i solved it. the issue was i needed it on the script only, but also i needed to correctly use the "service-worker" tag rather than incorrectly typing "worker" there :)
Avatar
the current state of affairs with respect to DOM access seems to be kinda dire. if i'm inferring things correctly, it looks like pydom got replaced with pyscript.web somewhat recently, but the docs haven't been fully updated (e.g. bullet 2 at https://docs.pyscript.net/2024.11.1/user-guide/dom/ references pydom and the link is broken), and as far as i can see none of the pyscript.com examples use .web yet. is there any working example code using pyscript.web anywhere? i'm struggling to do even very basic things that the docs seem to suggest should work, like page.append(div("foo", id="#foo")) page["#foo"].textContent = "bar" adds foo successfully but does not change it to bar. document.getElementById("#foo").textContent = "bar" in the console works fine. (edited)
04:17
docs also don't mention anything about how to delete DOM elements
Avatar
this may not work but check the pyscript docs github. Some updates have been done but not yet 'published'
Avatar
Chris Laffra 01/12/2024 15:55
@personman have you considered PyScript LTK? It offers a (much) higher level of abstraction than the low-level pyscript.web HTML API. You may like the Reactive aspect, for instance. See example here: https://pyscript.github.io/ltk/?tab=3
Avatar
Avatar
Chris Laffra
@personman have you considered PyScript LTK? It offers a (much) higher level of abstraction than the low-level pyscript.web HTML API. You may like the Reactive aspect, for instance. See example here: https://pyscript.github.io/ltk/?tab=3
hadn't heard of it, but this looks useful, thanks!
๐Ÿ‘ 1
Avatar
Avatar
personman
hadn't heard of it, but this looks useful, thanks!
can agree its super useful. Currently my main goto for complex UIs.
โค๏ธ 1
Avatar
blackbooks 05/12/2024 11:09
Hi, i am sending data from the pyscript environment to a secure server and my ssl handshake fails. when using the urllib3 library. After many hours of trying to debug this, i found that the underlying ssl library [https://docs.python.org/3/library/ssl.html] is not available on webassembly. checking this in the environment shows that the package is indeed not available in the environment. The ssl library is deeply embedded in the package i am using for the data transfer, so i worry that i cant easily exchange it. How can i proceed?
Avatar
Avatar
blackbooks
Hi, i am sending data from the pyscript environment to a secure server and my ssl handshake fails. when using the urllib3 library. After many hours of trying to debug this, i found that the underlying ssl library [https://docs.python.org/3/library/ssl.html] is not available on webassembly. checking this in the environment shows that the package is indeed not available in the environment. The ssl library is deeply embedded in the package i am using for the data transfer, so i worry that i cant easily exchange it. How can i proceed?
Andrea Giammarchi 06/12/2024 09:22
Checklist I added a descriptive title I searched for other issues and couldn't find a solution or duplication I already searched in Google and didn't find any good information or help What ...
Avatar
Avatar
Andrea Giammarchi
blackbooks 06/12/2024 09:24
Thanks for the reply. So simply manually reloading the library might already solve the issue? I will try this out and report back.
Avatar
blackbooks 06/12/2024 09:35
Okay i can manually import the library and make it available in the environment. But for some reason the ssl handshake still fails in the environment giving me: error.js:15 /lib/python3.11/site-packages/urllib3/connectionpool.py:1101: InsecureRequestWarning: Unverified HTTPS request is being made to host 'redcap.med.tu-dresden.de'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#tls-warnings
Customizing Pool Behavior: The PoolManager class automatically handles creating ConnectionPool instances for each host as needed. By default, it will keep a maximum of 10 ConnectionPool instances. ...
Avatar
Avatar
blackbooks
Okay i can manually import the library and make it available in the environment. But for some reason the ssl handshake still fails in the environment giving me: error.js:15 /lib/python3.11/site-packages/urllib3/connectionpool.py:1101: InsecureRequestWarning: Unverified HTTPS request is being made to host 'redcap.med.tu-dresden.de'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#tls-warnings
Andrea Giammarchi 06/12/2024 12:19
I believe in my previous link there's more to it than ust import ssl as package ... please read related discussions too https://github.com/pyscript/pyscript/issues/1321#issuecomment-1486853057
Checklist I added a descriptive title I searched for other issues and couldn't find a solution or duplication I already searched in Google and didn't find any good information or help What ...
Avatar
blackbooks 06/12/2024 12:39
@Andrea Giammarchi Thank again, i read that but thought i might just try to import the library and can quickly check. tbh reading these threads i feel very much out of my depth.
The smtplib module does not work in Pyodide / Emscripten. The smtplib module uses blocking sockets. Browser environment only support asynchronous, non-blocking sockets. You also can't connect to arbitrary ports from a browser. You would need a helper process on the server side to emulate TCP sockets over WebSockets, see https://emscripten.org/docs/porting/networking.html
i have no access to the server i am sending my data to so this would be not an option. Is there a standard way to send data via a https connection from the browser environment?
(edited)
Avatar
Avatar
blackbooks
@Andrea Giammarchi Thank again, i read that but thought i might just try to import the library and can quickly check. tbh reading these threads i feel very much out of my depth.
The smtplib module does not work in Pyodide / Emscripten. The smtplib module uses blocking sockets. Browser environment only support asynchronous, non-blocking sockets. You also can't connect to arbitrary ports from a browser. You would need a helper process on the server side to emulate TCP sockets over WebSockets, see https://emscripten.org/docs/porting/networking.html
i have no access to the server i am sending my data to so this would be not an option. Is there a standard way to send data via a https connection from the browser environment?
(edited)
Andrea Giammarchi 06/12/2024 12:55
you can send data over HTTPS, heck the whole thing needs HTTPS to work but I don't know internals of your library and if it doesn't use normal HTTPS and it uses something else server-side-only, I am afraid you need some reverse-proxy or whatever other mean to send such data to your server + such server must accept browsers' requests from other domains.
Avatar
blackbooks 06/12/2024 13:07
yea funny thing is, that everything works fine in a standard python environment and even in the browser environment the data gets posted to the data collection server. its seems it is just the ssl handshake that fails but the data gets transmitted anyway.
Avatar
thondibrahmin 06/12/2024 23:24
Iโ€™m curious if pyscript can be used in electron apps? In particular if the use case is to build a desktop application but there are also python package dependencies that are needed.
Avatar
Avatar
thondibrahmin
Iโ€™m curious if pyscript can be used in electron apps? In particular if the use case is to build a desktop application but there are also python package dependencies that are needed.
Chris Laffra 06/12/2024 23:52
100%
Avatar
Avatar
Chris Laffra
100%
thondibrahmin 07/12/2024 00:57
Thanks. Any examples youโ€™re aware of. Iโ€™ll take a stab at it. Iโ€™m assuming that when pyscript is in the app, it will load packages by making some kind of request to a server and downloading the packages? Is it possible to bundle the python dependencies locally. Iโ€™m not familiar with the pyscript lifecycle and how the python to wasm conversion works when pyscript loads and downloads (edited)
Avatar
Avatar
thondibrahmin
Thanks. Any examples youโ€™re aware of. Iโ€™ll take a stab at it. Iโ€™m assuming that when pyscript is in the app, it will load packages by making some kind of request to a server and downloading the packages? Is it possible to bundle the python dependencies locally. Iโ€™m not familiar with the pyscript lifecycle and how the python to wasm conversion works when pyscript loads and downloads (edited)
This doesn't answer your question directly but open an account on pyscript.com, clone an example, see on RHS you can install locally. This makes a PWA on your system. Perhaps this can show you what you need ?
Avatar
Avatar
Neon22
This doesn't answer your question directly but open an account on pyscript.com, clone an example, see on RHS you can install locally. This makes a PWA on your system. Perhaps this can show you what you need ?
thondibrahmin 07/12/2024 04:11
Thank you. I also found this https://docs.pyscript.net/2024.11.1/user-guide/offline/, seems to be the way?
Avatar
Chris Laffra 07/12/2024 08:45
People have written Chrome extensions that use PyScript that are completely self contained.
Avatar
Avatar
thondibrahmin
Thank you. I also found this https://docs.pyscript.net/2024.11.1/user-guide/offline/, seems to be the way?
Yeah - I was pretty sure there was some docs but I did not take the time to look it up for you. That's them...
Avatar
Any reason im unable to update a noUIslider element? def update_date_range(self, date_range): print('Updating date range...') start_date, end_date = date_range start_ms = start_date.timestamp() * 1000 end_ms = end_date.timestamp() * 1000 self._date_range.noUiSlider.updateOptions({ 'range': { 'min': start_ms, 'max': end_ms }, 'start': [start_ms, end_ms] }, True) self._date_range.noUiSlider.set([start_ms, end_ms]) print('Completed!') (edited)
13:34
I'm able to update traditional element no problem as well as update the slider in JS.
Avatar
Chris Laffra 08/12/2024 15:10
@Quint It looks like you are passing a Python dict to a JavaScript function. Try wrapping it with to_js({ ... }) . See https://docs.pyscript.net/2024.3.2/user-guide/builtins/#pyscriptffito_js (edited)
Avatar
Avatar
Chris Laffra
@Quint It looks like you are passing a Python dict to a JavaScript function. Try wrapping it with to_js({ ... }) . See https://docs.pyscript.net/2024.3.2/user-guide/builtins/#pyscriptffito_js (edited)
Andrea Giammarchi 08/12/2024 15:14
from pyscript.ffi import to_js would be my pick as it automatically pass object literals instead of maps, but with latest Pyodide even those maps should work out of the box (in theory, not all APIs are happy about those).
๐Ÿ‘ 1
Avatar
Avatar
Andrea Giammarchi
from pyscript.ffi import to_js would be my pick as it automatically pass object literals instead of maps, but with latest Pyodide even those maps should work out of the box (in theory, not all APIs are happy about those).
Andrea Giammarchi 08/12/2024 15:16
not that I've added much to what Chris said already, the thing is that also pyodide has that method in its ffi plus IIRC it attaches that to every Python proxy so that I just wanted to underline the difference/disambiguation we do there. (edited)
Avatar
Chris Laffra 08/12/2024 15:28
I have been running into this so many times when switching between MicroPython and PyOdide and across various JavaScript APIs, that I defensively wrap all my JavaScript calls with to_js now ๐Ÿ˜ธ
Avatar
from pyodide.ffi import to_js def update_date_range(self, date_range): print('Updating date range...') start_date, end_date = date_range start_ms = start_date.timestamp() * 1000 end_ms = end_date.timestamp() * 1000 options = { 'range': { 'min': start_ms, 'max': end_ms }, 'start': [start_ms, end_ms] } js_options = to_js(options) self._date_range.noUiSlider.updateOptions(js_options, True) self._date_range.noUiSlider.set(to_js([start_ms, end_ms])) print('Completed!') (edited)
16:36
Got some movement with that.
16:38
Its updating now. Just not in the intended way, likely just a logic issue now. Thanks! (edited)
Avatar
Avatar
Chris Laffra
@Quint It looks like you are passing a Python dict to a JavaScript function. Try wrapping it with to_js({ ... }) . See https://docs.pyscript.net/2024.3.2/user-guide/builtins/#pyscriptffito_js (edited)
So the updateOptions is having issues and not updating the sliders range. set() updates the sliders handles no problem. self._date_range.noUiSlider.updateOptions(to_js({ 'range': { 'min': start_ms, 'max': end_ms } }), True) (edited)
19:19
this is how to update the range from the docs. button1.addEventListener('click', function () { updateSlider.noUiSlider.updateOptions({ range: { 'min': 20, 'max': 50 } }); });
Avatar
Finally got it working. options = {'range': {'min': 0, 'max': 200}} js_options = to_js(options, dict_converter=js.Object.fromEntries) (edited)
๐Ÿ‘ 2
Avatar
Avatar
Quint
Finally got it working. options = {'range': {'min': 0, 'max': 200}} js_options = to_js(options, dict_converter=js.Object.fromEntries) (edited)
FWIW - docs for that can be found in pyodide rather than pyscript. - https://pyodide.org/en/stable/usage/type-conversions.html#explicit-conversion-of-proxies Hard to know when to dounble up on docs, or just refer to the pyodide url in pyscript docs... @ntoll?
Avatar
Avatar
Quint
Finally got it working. options = {'range': {'min': 0, 'max': 200}} js_options = to_js(options, dict_converter=js.Object.fromEntries) (edited)
that's a real nice multirange slider. Can you share any code about making and controlling it from pyscript ?
Avatar
Avatar
Quint
from pyodide.ffi import to_js def update_date_range(self, date_range): print('Updating date range...') start_date, end_date = date_range start_ms = start_date.timestamp() * 1000 end_ms = end_date.timestamp() * 1000 options = { 'range': { 'min': start_ms, 'max': end_ms }, 'start': [start_ms, end_ms] } js_options = to_js(options) self._date_range.noUiSlider.updateOptions(js_options, True) self._date_range.noUiSlider.set(to_js([start_ms, end_ms])) print('Completed!') (edited)
Andrea Giammarchi 09/12/2024 08:48
it's from pyscript.ffi import to_js if you never want surprises (edited)
Avatar
Avatar
Andrea Giammarchi
it's from pyscript.ffi import to_js if you never want surprises (edited)
Pyscript.ffi was throwing module import errors in the browser on the newest version.
Avatar
Avatar
Quint
Pyscript.ffi was throwing module import errors in the browser on the newest version.
Andrea Giammarchi 09/12/2024 10:53
I just tried it both main and workers ... it's from pyscript.ffi import to_js no error, we also have integration tests so I am not sure what kind of error you are seeing or why
Avatar
Avatar
Quint
Pyscript.ffi was throwing module import errors in the browser on the newest version.
Please tell us the errors you are seeing.
Avatar
Chris Laffra 09/12/2024 16:12
I do not see the error either. But, you may see this error in VS Code when pylint does not know where to find the pyscript package. However, this code works fine for me in the browser in the context of PyScript (tested also on v2024.11.1).
Avatar
Avatar
ntoll
Please tell us the errors you are seeing.
I cant recreate the error now for some reason. Digging through the log this was the error. [pyexec] Python exception: Traceback (most recent call last): File "/home/pyodide/pyscript/_internal.py", line 104, in run_pyscript result = eval_code(code, globals=__main__.__dict__) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/lib/python311.zip/_pyodide/_base.py", line 468, in eval_code .run(globals, locals) ^^^^^^^^^^^^^^^^^^^^ File "/lib/python311.zip/_pyodide/_base.py", line 310, in run coroutine = eval(self.code, globals, locals) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "<exec>", line 5, in <module> ModuleNotFoundError: No module named 'pyscript.ffi' (edited)
00:44
simply changing pyscript.ffi to pyodide.ffi didnt throw any such error.
00:45
This was thrown in browser.
Avatar
Avatar
Quint
I cant recreate the error now for some reason. Digging through the log this was the error. [pyexec] Python exception: Traceback (most recent call last): File "/home/pyodide/pyscript/_internal.py", line 104, in run_pyscript result = eval_code(code, globals=__main__.__dict__) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/lib/python311.zip/_pyodide/_base.py", line 468, in eval_code .run(globals, locals) ^^^^^^^^^^^^^^^^^^^^ File "/lib/python311.zip/_pyodide/_base.py", line 310, in run coroutine = eval(self.code, globals, locals) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "<exec>", line 5, in <module> ModuleNotFoundError: No module named 'pyscript.ffi' (edited)
Andrea Giammarchi 10/12/2024 08:25
we don't have those primitives so I believe your PyScript version was not the PyScript we all run too
Avatar
Avatar
Quint
I cant recreate the error now for some reason. Digging through the log this was the error. [pyexec] Python exception: Traceback (most recent call last): File "/home/pyodide/pyscript/_internal.py", line 104, in run_pyscript result = eval_code(code, globals=__main__.__dict__) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/lib/python311.zip/_pyodide/_base.py", line 468, in eval_code .run(globals, locals) ^^^^^^^^^^^^^^^^^^^^ File "/lib/python311.zip/_pyodide/_base.py", line 310, in run coroutine = eval(self.code, globals, locals) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "<exec>", line 5, in <module> ModuleNotFoundError: No module named 'pyscript.ffi' (edited)
What Andrea says... please update to the latest version of PyScript (check the <head> of your HTML). See: https://pyscript.net/releases/2024.11.1/
21:01
not a thing anymore?
Avatar
What is this? Uncaught SyntaxError: Unexpected token 'export' (at core.js:1:1)
Avatar
Avatar
Quint
What is this? Uncaught SyntaxError: Unexpected token 'export' (at core.js:1:1)
ios / safari ?
Avatar
Renan Tescarolo Dias 11/12/2024 00:25
Hello. My solution was using the link "https://pyscript.net/latest/pyscript.js", but that page is no longer available. I changed it to "https://pyscript.net/releases/2024.11.1/core.js". However, Iโ€™m now getting the following error: "DeprecationWarning: Pyarrow will become a required dependency of pandas in the next major release of pandas (pandas 3.0), to allow more performant data types, such as the Arrow string type, and better interoperability with other libraries) but was not found to be installed on your system." Pyarrow isnโ€™t compatible with pyscript, right? Any ideas on how to resolve this issue?
Avatar
Renan Tescarolo Dias 11/12/2024 00:40
I don't know what the origin of the error is, but this function that checks if pyscript is ready for use never finds pyscript.interpreter.globals. function verificarPyScriptAsync() { return new Promise(function (resolve) { function verificar() { try { if (!pyscript.interpreter.globals) { setTimeout(verificar, 500); } else { //console.log("Python Liberado:" + new Date()); hideLoadingImage(); resolve(); // Resolvendo a Promise quando a condiรงรฃo รฉ atendida } } catch (ex) { setTimeout(verificar, 500); } } verificar(); // Inicia a verificaรงรฃo }); }`
Avatar
NaturalAliens 11/12/2024 09:16
Hello @ntoll I naively browsed to https://pyscript.net/tech-preview/micropython/repl.html this is the first hit for "pyscript micropython" on google, and try to apply the documentation here https://pyscript.recipes/latest/js-integration/import-js/#__tabbed_1_1 which says "The PyScript (Pyodide) and PyScript(Micropython) version of this recipe are identical" but all I get is below. What am I misunderstanding?
import pyscript Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: no module named 'pyscript' from pyscript import window Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: no module named 'pyscript' >>>
Avatar
Avatar
Renan Tescarolo Dias
I don't know what the origin of the error is, but this function that checks if pyscript is ready for use never finds pyscript.interpreter.globals. function verificarPyScriptAsync() { return new Promise(function (resolve) { function verificar() { try { if (!pyscript.interpreter.globals) { setTimeout(verificar, 500); } else { //console.log("Python Liberado:" + new Date()); hideLoadingImage(); resolve(); // Resolvendo a Promise quando a condiรงรฃo รฉ atendida } } catch (ex) { setTimeout(verificar, 500); } } verificar(); // Inicia a verificaรงรฃo }); }`
Andrea Giammarchi 11/12/2024 09:25
we don't leak pyscript globally, it's been over a year now ... we have py:ready event triggered, if needed to know when PyScript is ready. Use Promise.withResolvers() around to resolve whatever you need so you never need ever again a setTimeout dance. There are also hooks ... in few words, PyScript has been different for a long time, that latest end point only caused troubles for the present and future of the project.
Avatar
Avatar
NaturalAliens
Hello @ntoll I naively browsed to https://pyscript.net/tech-preview/micropython/repl.html this is the first hit for "pyscript micropython" on google, and try to apply the documentation here https://pyscript.recipes/latest/js-integration/import-js/#__tabbed_1_1 which says "The PyScript (Pyodide) and PyScript(Micropython) version of this recipe are identical" but all I get is below. What am I misunderstanding?
import pyscript Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: no module named 'pyscript' from pyscript import window Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: no module named 'pyscript' >>>
latest is actually very old and deprecated. Suggest you try here: https://docs.pyscript.net/2024.11.1/ Don't forget you can make a pyscript.com page and clone examples from there... to experiment
Avatar
Avatar
NaturalAliens
Hello @ntoll I naively browsed to https://pyscript.net/tech-preview/micropython/repl.html this is the first hit for "pyscript micropython" on google, and try to apply the documentation here https://pyscript.recipes/latest/js-integration/import-js/#__tabbed_1_1 which says "The PyScript (Pyodide) and PyScript(Micropython) version of this recipe are identical" but all I get is below. What am I misunderstanding?
import pyscript Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: no module named 'pyscript' from pyscript import window Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: no module named 'pyscript' >>>
Andrea Giammarchi 11/12/2024 09:27
it's a tech preview ... to have a MicroPython terminal in PyScript you need <script type="mpy" terminal worker>import code; code.interact()</script> these days, it's in the docs https://docs.pyscript.net/2024.11.1/user-guide/terminal/ (edited)
Avatar
no - see prev msg
Avatar
Avatar
pmp-p
ios / safari ?
Andrea Giammarchi 11/12/2024 09:29
PyScript works in all mobile devices AFAIK, including iOS and Safari (both Mobile and Desktop)
Avatar
Avatar
Neon22
latest is actually very old and deprecated. Suggest you try here: https://docs.pyscript.net/2024.11.1/ Don't forget you can make a pyscript.com page and clone examples from there... to experiment
NaturalAliens 11/12/2024 09:30
ok then I don't understand: all the examples on https://docs.pyscript.net/2024.11.1/examples/ appear to be pyiodide based.
Avatar
yes - they are being updated to include mpy and also teh webworkers stuff. You can see latest version at the github. but we wait ๐Ÿ™‚
๐Ÿ‘ 1
09:33
E.g. mpy based ltk for added GUI - works fast and well in 2024.11.1 - here - https://github.com/pyscript/ltk
LTK is a little toolkit for writing UIs in PyScript - pyscript/ltk
Avatar
Avatar
NaturalAliens
ok then I don't understand: all the examples on https://docs.pyscript.net/2024.11.1/examples/ appear to be pyiodide based.
Andrea Giammarchi 11/12/2024 09:36
most examples use 3rd party packages and MicroPython packages are different (no matplotlib and so on) so some demo will still inevitably be on Pyodide, some other can easily be on MicroPython
Avatar
Avatar
Andrea Giammarchi
we don't leak pyscript globally, it's been over a year now ... we have py:ready event triggered, if needed to know when PyScript is ready. Use Promise.withResolvers() around to resolve whatever you need so you never need ever again a setTimeout dance. There are also hooks ... in few words, PyScript has been different for a long time, that latest end point only caused troubles for the present and future of the project.
Renan Tescarolo Dias 11/12/2024 15:52
Hi Andrea. Thank you so much for your help. Iโ€™m having trouble adapting my code to the new structures of pyscript. Previously, I used a command similar to pyscript.interpreter.globals.get('gerarElementos')(dicionarioConfigElementos, listaElementos) to call my functions in the Python file. My functions arenโ€™t triggered by a button, but rather by events happening in the back-end (arrival of new data, visiting a specific location in the view, loading multiple charts simultaneously, etc.). Without interpreter.globals, how should I call a specific function from my Python file?
Avatar
I previously used to use an element's "text" to change the text displayed on my page. Since the update that removed that option, I switched to "innerText", but now I'm wondering if that's the wrong approach. AI says that I should be using "content", but that doesn't seem to work (at least not as a direct replacement)? Wondering what a human would say. Thanks in advance
Avatar
@ableman I'm assuming you're using pyscript.web for working with the DOM. We just proxy the attributes from the underlying JS objects, so innerText, innerHtml, etc... should just work as the docs on W3 schools or MDN say. Anything AI says is likely to be wrong. ๐Ÿ˜‰ (But you know that already).
16:43
Also, https://docs.pyscript.net is your friend here... (and it's early days, so feedback about docs is most welcome).
Avatar
yeah, thanks! I got a bit confused because innerText was apparently an IE thing first.
Avatar
Hehehe... Oh man. It's the web... so it's like digging into digital archaeology... the APIs could date from the 1990s. ๐Ÿ˜‰ (But it's also fun... and the pyscript.web namespace and other upcoming APIs are designed to give you access to web-by stuff but in a Pythonic manner).
Avatar
Avatar
ableman
yeah, thanks! I got a bit confused because innerText was apparently an IE thing first.
Andrea Giammarchi 11/12/2024 17:38
it was born in IE but it's different from textContent ... innerText, once read, returns only visible content, skipping tons of stuff that's not displayed to users ... for some it's a great magic thing, for others it just screams for shenanigans ... but that being said, it does something different, it's not a replacement for textContent, or vice-versa, these are two completely different accessors ... unfortunately, the old Web documentation ignored that difference, so there's still a lot of content out there using innerText for no reasons, but be sure these are just two different things, not meant to be removed from the Web, rather useful on occasions
Avatar
Avatar
ableman
I previously used to use an element's "text" to change the text displayed on my page. Since the update that removed that option, I switched to "innerText", but now I'm wondering if that's the wrong approach. AI says that I should be using "content", but that doesn't seem to work (at least not as a direct replacement)? Wondering what a human would say. Thanks in advance
Chris Laffra 11/12/2024 18:35
If you use LTK, you use text() or html(), which is borrowed from jQuery not the lower level browser APIs. https://github.com/pyscript/ltk/blob/42d51745ed1ae2b06ed86f418ab62d23e2c1d8e1/ltk/widgets.py#L384
LTK is a little toolkit for writing UIs in PyScript - pyscript/ltk
Avatar
Avatar
Andrea Giammarchi
you can send data over HTTPS, heck the whole thing needs HTTPS to work but I don't know internals of your library and if it doesn't use normal HTTPS and it uses something else server-side-only, I am afraid you need some reverse-proxy or whatever other mean to send such data to your server + such server must accept browsers' requests from other domains.
blackbooks 13/12/2024 09:36
Thanks again for you patience, i have invested some time into resolving my issue and would like your comment on my current position: Creating a HTTPS Connection from within the webassember is not possible, a secure connection can only be established from the browser via JavaScript. My course of action would be to keep my data collection within the Python environment, pass the data back into the browser and make the HTTPS Post request via the JavaScript interface. I contacted the admin of my Data Collection server and he was open to set the CORS headers to my requirements. Is this the assumption correct? Does my plan make sense? Is there another more suitable approach that i have missed? Could i use the js.fetch call within the PyScript environment instead of doing pure JavaScript in the browser? Thx in advance (edited)
Avatar
Avatar
blackbooks
Thanks again for you patience, i have invested some time into resolving my issue and would like your comment on my current position: Creating a HTTPS Connection from within the webassember is not possible, a secure connection can only be established from the browser via JavaScript. My course of action would be to keep my data collection within the Python environment, pass the data back into the browser and make the HTTPS Post request via the JavaScript interface. I contacted the admin of my Data Collection server and he was open to set the CORS headers to my requirements. Is this the assumption correct? Does my plan make sense? Is there another more suitable approach that i have missed? Could i use the js.fetch call within the PyScript environment instead of doing pure JavaScript in the browser? Thx in advance (edited)
Andrea Giammarchi 13/12/2024 11:23
you can use every JS API you like from PyScript directly, including fetch https://docs.pyscript.net/2024.11.1/api/#pyscriptfetch or anything really on window
Avatar
Avatar
Andrea Giammarchi
you can use every JS API you like from PyScript directly, including fetch https://docs.pyscript.net/2024.11.1/api/#pyscriptfetch or anything really on window
blackbooks 13/12/2024 11:40
Nice, so i can keep all of my code python-based, i simply need to rewrite my request calls using the fetch command.
Avatar
Chris Laffra 13/12/2024 13:35
Fetch works, xmlhttprequest as well. In LTK, there is a nice abstraction, ltk.get() and ltk.post().
13:38
Contribute to laffra/todo development by creating an account on GitHub.
13:38
@blackbooks see above
Avatar
Hello guys, when I modify the DOM in a loop the DOM is only update once at the end and during this time the DOM is frozed, how can I solve this with pyscript ?
15:12
I tried with setTimeout and requestAnimationFrame but nothing works
Avatar
Avatar
Loic
I tried with setTimeout and requestAnimationFrame but nothing works
Chris Laffra 13/12/2024 15:57
That should work
Avatar
If I want to make an element disappear, what's the best way to do so? Currently I'm doing start_button.style["display"] = "none" But could I just remove it from the parent element somehow?
16:09
like..., main.remove(start_button) would be good, but I don't see any option like that
Avatar
Avatar
ableman
If I want to make an element disappear, what's the best way to do so? Currently I'm doing start_button.style["display"] = "none" But could I just remove it from the parent element somehow?
Chris Laffra 13/12/2024 21:41
The Element.remove() method removes the element from the DOM.
Avatar
Thanks, I was looking at pyscript on GitHub and didn't see that there.
Avatar
Chris Laffra 14/12/2024 11:54
With LTK, managing the DOM is a lot easier. See this example of adding and removing elements from the DOM: https://pyscript.github.io/ltk/?tab=1 (edited)
Avatar
anotherbazeinthewall 14/12/2024 19:24
Apologies if this is covered elsewhere but I couldn't find it.... Is there a simple workaround for input() in the terminal? <script type="py" terminal worker> while True: name = input("What's your name? ") print(f"Hello {name}!") </script>
Avatar
Avatar
anotherbazeinthewall
Apologies if this is covered elsewhere but I couldn't find it.... Is there a simple workaround for input() in the terminal? <script type="py" terminal worker> while True: name = input("What's your name? ") print(f"Hello {name}!") </script>
I am not certain but I think if its a worker then you should not be trying to use input. Imagine you have three workers and they all ask for inputs. For only a tiny amount of extra code consider the ltk and present it from the main thread. ltk link in msg directly above this one.
๐Ÿ™ 1
Avatar
I love it when I have days to goof off and write code for fun, almost always it means questions here though! So you all can blame the ice. I have no idea what I'm doing but I'm having fun tinkering. I have a litestar endpoint that returns an SVG image as text in the variable myimage I would eventually like to insert that SVG into a canvas, but at the moment I will just settle for getting it to display on the page. I have a div with an id of "foo" I use mydiv = page.find("#foo") to get the div, and then I try mydiv.append(myimage) However, that results in this error: File "/lib/python312.zip/_pyodide/_base.py", line 597, in eval_code_async await CodeRunner( File "/lib/python312.zip/_pyodide/_base.py", line 413, in run_async await coroutine File "<exec>", line 11, in <module> TypeError: 'list' object is not callable I'm unsure what I might be doing wrong.
Avatar
Avatar
rhacer
I love it when I have days to goof off and write code for fun, almost always it means questions here though! So you all can blame the ice. I have no idea what I'm doing but I'm having fun tinkering. I have a litestar endpoint that returns an SVG image as text in the variable myimage I would eventually like to insert that SVG into a canvas, but at the moment I will just settle for getting it to display on the page. I have a div with an id of "foo" I use mydiv = page.find("#foo") to get the div, and then I try mydiv.append(myimage) However, that results in this error: File "/lib/python312.zip/_pyodide/_base.py", line 597, in eval_code_async await CodeRunner( File "/lib/python312.zip/_pyodide/_base.py", line 413, in run_async await coroutine File "<exec>", line 11, in <module> TypeError: 'list' object is not callable I'm unsure what I might be doing wrong.
Andrea Giammarchi 15/12/2024 15:50
why you want to insert SVG into a canvas? in any case, you can create window.Image.new() and then set its .src to a data URL that represents such SVG then you have an image ... that represents an SVG ... not sure canvas in general understands out of the box SVG elements as image otherwise
Avatar
Avatar
Andrea Giammarchi
why you want to insert SVG into a canvas? in any case, you can create window.Image.new() and then set its .src to a data URL that represents such SVG then you have an image ... that represents an SVG ... not sure canvas in general understands out of the box SVG elements as image otherwise
Andrea Giammarchi 15/12/2024 15:50
We've covered "inline SVG" - which is dropping the SVG syntax right into HTML. You can use that same inline SVG in other places as well, like in an or
Avatar
Avatar
Andrea Giammarchi
why you want to insert SVG into a canvas? in any case, you can create window.Image.new() and then set its .src to a data URL that represents such SVG then you have an image ... that represents an SVG ... not sure canvas in general understands out of the box SVG elements as image otherwise
Because I would like it to be a sprite at some point.
Avatar
Avatar
Andrea Giammarchi
Thank you so much
Avatar
Avatar
rhacer
Because I would like it to be a sprite at some point.
Andrea Giammarchi 15/12/2024 15:52
yeah, I understand, I just don't think canvas can handle SVGs out of the box ... images though, overly-sure it can
Avatar
Oh thank you. So if I want an image on a canvas it needs to be PNG or JPG?
Avatar
Avatar
rhacer
Oh thank you. So if I want an image on a canvas it needs to be PNG or JPG?
Andrea Giammarchi 15/12/2024 16:34
no, it needs to be an Image element, the rest is handled behind the scene
16:35
so ... you set a .src with a data-uri that represent that SVG, once .onload happens, you pass that image to the canvas and it'll do the right thing ... passing right away the SVG, AFAIK, and IIRC, would not work.
Avatar
Thank you
Avatar
Avatar
rhacer
Thank you
Andrea Giammarchi 15/12/2024 16:38
example: const svg = ` <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" > <polygon points="1 6 1 22 8 18 16 22 23 18 23 2 16 6 8 2 1 6" /> <line x1="8" y1="2" x2="8" y2="18" /> <line x1="16" y1="6" x2="16" y2="22" /> </svg> `.trim(); const image = new Image; image.src = `data:image/svg+xml;utf8,${svg}`; image.onload = () => { document.body.append(image); }; you can do that via PyScript directly, the result would be the same.
16:39
you would eventually pass that image to the canvas instead of appending it randomly on the body
Avatar
That is a huge help. Much appreciated
Avatar
Avatar
rhacer
That is a huge help. Much appreciated
Andrea Giammarchi 15/12/2024 16:41
in PyScript code ... from pyscript import document, window svg = ''' <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" > <polygon points="1 6 1 22 8 18 16 22 23 18 23 2 16 6 8 2 1 6" /> <line x1="8" y1="2" x2="8" y2="18" /> <line x1="16" y1="6" x2="16" y2="22" /> </svg> '''.strip() image = window.Image.new() image.src = f'data:image/svg+xml;utf8,{svg}' image.onload = lambda e: document.body.append(image) (edited)
Avatar
Dแด€Cส€แด‡แด…แด€ 16/12/2024 15:51
@everyone it is possible to import a python module in a html site with pyscript?
15:52
and if yes, how ?
Avatar
Avatar
Dแด€Cส€แด‡แด…แด€
@everyone it is possible to import a python module in a html site with pyscript?
Andrea Giammarchi 16/12/2024 16:34
yes, use [files] in your config toml to put the module in your virtual FS on bootstrap and it'll be there ready to use.
Avatar
Avatar
Andrea Giammarchi
yes, use [files] in your config toml to put the module in your virtual FS on bootstrap and it'll be there ready to use.
Andrea Giammarchi 16/12/2024 16:35
Avatar
Dแด€Cส€แด‡แด…แด€ 16/12/2024 17:34
thanks
Avatar
What was the version that used to be hosted at https://pyscript.net/latest ? (edited)
๐Ÿ‘€ 1
Avatar
kind of curious, when I look here https://github.com/pyscript/pyscript/blob/main/core/package.json the version is 0.6.22 but when I do npm i @pyscript/core the version is 0.6.23. Where is the mysterious version 0.6.23 located?
PyScript is an open source platform for Python in the browser. Try PyScript: https://pyscript.com Examples: https://tinyurl.com/pyscript-examples Community: https://discord.gg/HxvBtukrg2 - pyscri...
17:14
was there like a rollback but npm didn't get the message?
Avatar
Avatar
Kassy
What was the version that used to be hosted at https://pyscript.net/latest ? (edited)
I ran into this too, as someone had written some code using /latest and I was making a bunch of derivative projects using it. Sometime on Sunday people were alerting me that all my projects were broken, and it seems to be this /latest is not working (for me)
17:34
so I need to learn what's going on. It seems like a simple fix is not too far away
Avatar
Avatar
ableman
kind of curious, when I look here https://github.com/pyscript/pyscript/blob/main/core/package.json the version is 0.6.22 but when I do npm i @pyscript/core the version is 0.6.23. Where is the mysterious version 0.6.23 located?
Andrea Giammarchi 17/12/2024 17:35
on npm ... we have canary releases in there, you should use our official stable version instead
๐Ÿ‘ 1
Avatar
Avatar
Andrea Giammarchi
on npm ... we have canary releases in there, you should use our official stable version instead
Andrea Giammarchi 17/12/2024 17:35
Avatar
Avatar
ableman
kind of curious, when I look here https://github.com/pyscript/pyscript/blob/main/core/package.json the version is 0.6.22 but when I do npm i @pyscript/core the version is 0.6.23. Where is the mysterious version 0.6.23 located?
Andrea Giammarchi 17/12/2024 17:37
if curious about that specific version, it was published in here to have feedback from the community https://github.com/pyscript/pyscript/pull/2265
Description This MR would like to land an experimental implementation of the py-game special type with the following features: a &lt;script type=&quot;py-game&quot;&gt; with either...
17:38
the npm version is for easy development and for easy reachability, it's not meant to be used for any production purpose, you need to use our official releases for that, currently we are here: https://pyscript.net/releases/2024.11.1/core.js
๐Ÿ‘ 1
17:40
npm versions might break, won't likely be documented (part of the release process) and so on ... it's in our docs but of course one must know where to look at in there ... not sure how else we could mention it but maybe having a first paragraph on the npm README might help.
Avatar
The canary version on npm does not look like what was hosted as https://pyscript.net/latest/pyscript.js For instance, the latter was a JS script, not a module, and it exposed the "pyscript" global object (that I used to instrument pyodide from JS directly)
๐Ÿ‘ 1
Avatar
Avatar
Kassy
The canary version on npm does not look like what was hosted as https://pyscript.net/latest/pyscript.js For instance, the latter was a JS script, not a module, and it exposed the "pyscript" global object (that I used to instrument pyodide from JS directly)
Andrea Giammarchi 17/12/2024 17:46
latest pyscript does not exist anymore and it won't exist anymore in the near future (or ever)
Avatar
I understand that, but is there a way I can at least rebuild it offline while waiting for upgrading my scripts? (edited)
Avatar
Andrea Giammarchi 17/12/2024 17:47
that never pointed at npm, it was stuck into 2023 early version of PyScript ... it was a mistake and it's not going to be repeated
Avatar
Avatar
Kassy
I understand that, but is there a way I can at least rebuild it offline while waiting for upgrading my scripts? (edited)
Andrea Giammarchi 17/12/2024 17:49
the way has always been to pin-point your PyScript version instead of trusting latest without the ability to quickly catch up on changes ... it's a curse of any latest out there, for every library or software, it was a bad decision to provide that and we're all paying the consequenceses these days, sorry you are having issues now but I am sure it was never meant, or advertised, to be used for production purposes.
17:51
basically ... it's like you decided to be stuck in Python 2 while the project moved to Python 3 ... not the same but very similar issue ... latest 2 is gone, you should take a chance to move into modern PyScript, the migration shouldn't be too painful as we kept most handy things around
17:51
and yet, we're trying to re-shape the API trying to not break what's working already, still we're reaching a "final shape" after years of exploration around needs and possibilities behind this project.
17:53
last from me, I know CalVer convention doesn't help because nobody can pin-point major breaking changes out of incremental dates ... but any 2023.X.X release up to before August shoud still work the way it used to, I hope this helps a little.
17:55
/cc to @ntoll ... maybe we can find the latest PyScript "classic" version we supported and mentioned it anywhere so that people stuck with it can have a way to migrate consciously?
Avatar
No problem, I understand the best solution is to upgrade my scripts to a more recent version of PyScript Thanks for your answer ๐Ÿ‘
Avatar
Avatar
Andrea Giammarchi
npm versions might break, won't likely be documented (part of the release process) and so on ... it's in our docs but of course one must know where to look at in there ... not sure how else we could mention it but maybe having a first paragraph on the npm README might help.
https://docs.pyscript.net/2024.11.1/user-guide/offline/ It's listed as the recommended way of running offline, which is how I ran into this (edited)
Avatar
Avatar
ableman
https://docs.pyscript.net/2024.11.1/user-guide/offline/ It's listed as the recommended way of running offline, which is how I ran into this (edited)
Andrea Giammarchi 18/12/2024 08:30
I see ... well, that's just conveniente because it provides what we bundle for the release ... I don't remember if we provide that zip in the release end point too but if we do, we should update that documentation page. Thanks
Avatar
I made a PR to the docs to do what I ended up doing https://github.com/pyscript/docs/pull/159 but wasn't sure it would be helpful or if it follows your standards, so closed it
Include instructions for getting a specific version off npm when runnโ€ฆ pyscript offline
Avatar
Dแด€Cส€แด‡แด…แด€ 18/12/2024 18:06
someone can help me ? i have an error and i don't understand why
Avatar
Avatar
Dแด€Cส€แด‡แด…แด€
someone can help me ? i have an error and i don't understand why
Avatar
Dแด€Cส€แด‡แด…แด€ 18/12/2024 18:17
thanks a lot
18:17
i'm just a moron ๐Ÿ˜‚ (edited)
Avatar
someone can help me ? when i start my Html code the web site resort "muto_muto.pyscriptapps.com does not allow the connection."
Avatar
Avatar
Muto
someone can help me ? when i start my Html code the web site resort "muto_muto.pyscriptapps.com does not allow the connection."
just guessing but maybe you need mini-coi. You could try your script on pyscript.com and see if it works there. or use it to debug why your local site is not working. - https://docs.pyscript.net/2024.11.1/user-guide/workers/
Avatar
Avatar
Neon22
just guessing but maybe you need mini-coi. You could try your script on pyscript.com and see if it works there. or use it to debug why your local site is not working. - https://docs.pyscript.net/2024.11.1/user-guide/workers/
When il start my local it's work but not work on pyscryipt.com
Avatar
someone can help me ? When i run my html code don't work, it show me a blank page. https://pyscript.com/@muto_muto/dark-salad/
10:49
@here it's urgent
Avatar
You need to give more context in order for help to happen.
11:00
What html doesn't work..? What were you expecting? What happens instead?
11:01
@Muto for a start... there's no index.html (the default page to load). Could that be it?
Avatar
Avatar
Muto
@here it's urgent
Introduction.html is probably the file to rename to index.html (it is the only one with a python script tag). the main.json file looks like a config.toml file (although unreferenced in the potential index.html). The code.html looks like a 1980's basic program (no disrespect intended) converted to python but I don't understand its purpose. Sorry we can't work out what you're trying to do.
Avatar
Avatar
Neon22
Introduction.html is probably the file to rename to index.html (it is the only one with a python script tag). the main.json file looks like a config.toml file (although unreferenced in the potential index.html). The code.html looks like a 1980's basic program (no disrespect intended) converted to python but I don't understand its purpose. Sorry we can't work out what you're trying to do.
the problem is simply that the site is not displayed
Avatar
Avatar
Muto
@here it's urgent
Andrea Giammarchi 19/12/2024 13:26
the only error I see is CSS related ... otherwise I see the site without issues whatsoever ... there was a recent change in there though, and IIRC the trick was to reset cookies for that site, eventually log-in back again, and see if that improved ... we had a similar issue days (or weeks ago, I can't remember) so this might be the trick
Avatar
Avatar
Andrea Giammarchi
the only error I see is CSS related ... otherwise I see the site without issues whatsoever ... there was a recent change in there though, and IIRC the trick was to reset cookies for that site, eventually log-in back again, and see if that improved ... we had a similar issue days (or weeks ago, I can't remember) so this might be the trick
Andrea Giammarchi 19/12/2024 13:28
for context, this is what I see when landing in there:
Avatar
I have solves the problems thank at all
๐Ÿฅณ 1
Avatar
What do you guys get when you run this website? https://nathenjacobe.github.io/uncertainty-calculator/ Firefox doesn't seem to load whereas Chromium browsers do? Any ideas why? Source code is available on the repo.
07:59
would be nice if you could post screenshots of your consoles, with XHR and request statuses, if it doesn't load. I found a temporary fix; closing and restarting firefox seems to work sometimes...
Avatar
Avatar
nlj
would be nice if you could post screenshots of your consoles, with XHR and request statuses, if it doesn't load. I found a temporary fix; closing and restarting firefox seems to work sometimes...
try 2024.11.1 - you're using 2024.4.1 which is very much out of date
๐Ÿ‘ 1
Avatar
it seems to have resolved on its own now? it just takes time to load across all browsers, as opposed to aborting beforehand. Thanks for your help
Avatar
Avatar
Andrea Giammarchi
you can use every JS API you like from PyScript directly, including fetch https://docs.pyscript.net/2024.11.1/api/#pyscriptfetch or anything really on window
blackbooks 20/12/2024 16:20
Hi i just wanted to share some findings regarding my issue. I was still running PyScript 2024.5.2 when the insecure request warning appeared when sending data via https post request using the requests library, however with the current version this issue has dissappeared. Testing showed that the issue was resolved with the PyScript 2024.6.1 release. Turns out the requests library is supported by Pyodide since version 0.25.0 from January 2024 https://blog.pyodide.org/posts/0.25-release/ and the insecure requests bug was fixed in the underlying urllib3 in version 2.2.1 library in february https://github.com/urllib3/urllib3/releases/tag/2.2.1 PyScript 2024.5.2 was still being shipped with urllib3 version 2.1.0, The fixed came through in the first june release. Now i am not sure to be frustrated about wasting my last 2 weeks chasing this bug, or being reliefed that i dont have to write the adapters myself. Thx to you and everyone who engaged!
Happy New Year! Pyodide 0.25 is a major release with some exciting and experimental features. This major release includes requests library support, experimental support for JS Promise integration, and build system improvements. requests Support in Pyodide For a long time, the Pyodide community has been requesting support for the requests, Python...
๐Ÿš€ urllib3 is fundraising for HTTP/2 support urllib3 is raising ~$40,000 USD to release HTTP/2 support and ensure long-term sustainable maintenance of the project after a sharp decline in financial ...
Avatar
Avatar
blackbooks
Hi i just wanted to share some findings regarding my issue. I was still running PyScript 2024.5.2 when the insecure request warning appeared when sending data via https post request using the requests library, however with the current version this issue has dissappeared. Testing showed that the issue was resolved with the PyScript 2024.6.1 release. Turns out the requests library is supported by Pyodide since version 0.25.0 from January 2024 https://blog.pyodide.org/posts/0.25-release/ and the insecure requests bug was fixed in the underlying urllib3 in version 2.2.1 library in february https://github.com/urllib3/urllib3/releases/tag/2.2.1 PyScript 2024.5.2 was still being shipped with urllib3 version 2.1.0, The fixed came through in the first june release. Now i am not sure to be frustrated about wasting my last 2 weeks chasing this bug, or being reliefed that i dont have to write the adapters myself. Thx to you and everyone who engaged!
Andrea Giammarchi 20/12/2024 16:35
thanks for this follow up ... PyScript versions matter, which is the reason we ask to pinpoint out of our official releases but it's great to know the upstream bug has been fixed. We try to communicate in our rleases-notes which latest version of either Pyodide or MicroPython are shipping, or have been updated, but maybe we should make it a rule that every single release note, updated or not behind the interpreter scene, should mention the current Pyodide and MicroPython version so that it would be slightly easier to track down what would break and what wouldn't out of that detail? I am CCing @ntoll as he might agree or disagree or have a better idea than mine ... still, glad you sorted that out! (edited)
Avatar
Hii this is a very nooby question and most likely duplicated, but I don't get how one can call a pyscript function and pass arguments to it.. so if one has a basic setup like: index.html: """ <!DOCTYPE html> <html lang="en"> <head> <!-- PyScript CSS --> <link rel="stylesheet" href="https://pyscript.net/releases/2024.10.2/core.css"> <!-- This script tag bootstraps PyScript --> <script type="module" src="https://pyscript.net/releases/2024.10.2/core.js"></script> </head> <body> <!-- SCRIPT CONTAINING JS DATA --> <script> let jsData = [1, 2, 3] </script> </body> </html> """ and now I want to call a python function with the jsData from within a script in the body: <script type="text/javascript"> function callPyScriptFoo(data) { // how to call a pyscript function here with the casted jsData? // so I want to do: let result = callPythonFooFromJS(data); } </script> main.py """ def callPythonFooFromJS(event, jsData): # can one set multiple parameters? or whats the correct way to handle this? # do something with the jsData.. result = [3, 2, 1] return result """ How can one achieve this? Help would be much appreciated...!
Avatar
Andrea Giammarchi 21/12/2024 13:11
from pyscript import window def callPythonFooFromJS(event, jsData): # do something with the jsData return [3, 2, 1] window.callPythonFooFromJS = callPythonFooFromJS then you have a py:all-done event on the main meaning all your scripts have been executed and there you'll have your function
๐Ÿš€ 1
Avatar
Avatar
Andrea Giammarchi
thanks for this follow up ... PyScript versions matter, which is the reason we ask to pinpoint out of our official releases but it's great to know the upstream bug has been fixed. We try to communicate in our rleases-notes which latest version of either Pyodide or MicroPython are shipping, or have been updated, but maybe we should make it a rule that every single release note, updated or not behind the interpreter scene, should mention the current Pyodide and MicroPython version so that it would be slightly easier to track down what would break and what wouldn't out of that detail? I am CCing @ntoll as he might agree or disagree or have a better idea than mine ... still, glad you sorted that out! (edited)
The page for each release includes information about the versions of interpreters shipped. This is done via PyScript itself... e.g. take a look at: https://pyscript.net/releases/2024.11.1/
Avatar
Avatar
ntoll
The page for each release includes information about the versions of interpreters shipped. This is done via PyScript itself... e.g. take a look at: https://pyscript.net/releases/2024.11.1/
Andrea Giammarchi 21/12/2024 16:41
well ... that's not the Pyodide version though, it's the C-Python behind ... example: 3.12.1 (main, Nov 15 2024, 14:17:00) [Clang 19.0.0git doesn't tell which Pyodide we're using. Pyodide might be based on the same C-Python version and yet have fixed internal Pyodide only bugs while releasing and the same goes for MicroPython. (edited)
๐Ÿ‘ 1
16:44
we have that detail down the rabbit-hole (Polyscript interpreter versions) and we could have a way to expose those versions via our config ... we have type field, py or mpy, we don't have pyodide or micropython fields, which could be instrumented to expose the currently used version. That would also work only out of our build steps, but maybe somewhere pyodide, or micropython, expose that detail ... which would make it easier to show that on that very same page too ... I'll check it once back from vacations. (edited)
Avatar
Torstein Strรธmme 24/12/2024 15:08
Hi, new to pyscript here. I'm trying to dynamically pass Python code into the terminal by following example in https://docs.pyscript.net/2024.11.1/user-guide/terminal/ , but I don't understand what I'm doing wrong. There is no message, error or otherwise, in the console. Changing mpy to just py does something, but not what I expect. Am I doing something wrong? <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Terminal</title> <link rel="stylesheet" href="https://pyscript.net/releases/2024.11.1/core.css"> <script type="module" src="https://pyscript.net/releases/2024.11.1/core.js"></script> </head> <body> <div> <button id="btn-run">Run</button> </div> <div> <script id="my_terminal" type="mpy" terminal worker>print("First is OK")</script> </div> <script> const runButton = document.getElementById('btn-run'); runButton.onclick = async () => { const myterm = document.querySelector("#my_terminal"); await myterm.process('print("This doesnt show")'); }; </script> </body> </html> (edited)
Avatar
so, I noticed that I can't reuse the same option in multiple selects. No big deal, I can always rebuild the list of options, just wondering if there's a way to do it that I'm misisng.
Avatar
the key part that's getting me is that I want to decide at a later point to hide some of the options, and I want to do it on every select that has it
Avatar
Hello! I'm new to the newer version of pyscript! Anybody tried using sklearn?
Avatar
Okay, it worked. scikit-learn instead of sklearn!
Avatar
imsomebodys 31/12/2024 04:35
is it possible to run micropython from pyscript on a game like screeps that uses node 10
Avatar
How are you guys typically debugging? Not possible from browser or IDE? (edited)
Avatar
Hey; just started playing around with pyscript. I'm curious where to find a list of versions of pyscript. In the older examples that I was following initially the scripts were sourced from https://pyscript.net/latest/pyscript.js but modern examples show something like https://pyscript.net/releases/2024.10.2/core.css -- I can see that the releases usually align with the tags on pyscript/pyscript but older tags aren't viable. So just wondering if there's a list of versions that the releases exist for, or something like that.
Avatar
Avatar
Skenvy
Hey; just started playing around with pyscript. I'm curious where to find a list of versions of pyscript. In the older examples that I was following initially the scripts were sourced from https://pyscript.net/latest/pyscript.js but modern examples show something like https://pyscript.net/releases/2024.10.2/core.css -- I can see that the releases usually align with the tags on pyscript/pyscript but older tags aren't viable. So just wondering if there's a list of versions that the releases exist for, or something like that.
latest seemed like a good idea at the time but the continual improvements to a new project like pyscript meant old code would subtly fail. So now deprecated and removed to avoid confusion. Check docs for latest (its in the dropdown at the top). Personally recommend always looking for the newest one and using that. So far every step has been continual improvement ๐Ÿ™‚ https://docs.pyscript.net/2024.11.1/
Avatar
Avatar
Quint
How are you guys typically debugging? Not possible from browser or IDE? (edited)
if you use pyscript.com to learn, can invoke terminal and print() if using pyodide. Can import https://github.com/gruns/icecream if you want an improvement over print. Can also use something like this if not using terminal (in pyodide and in mpy) import ltk def print(*args): ltk.find("body").append(" ".join(str(a) for a in args), "<br>") and then you can print to the dom instead of a dedicated terminal (which takes up room). Or use the logger - ltk has a nic logger.... (edited)
๐Ÿฆ Never use print() to debug again. Contribute to gruns/icecream development by creating an account on GitHub.
Avatar
Avatar
Neon22
if you use pyscript.com to learn, can invoke terminal and print() if using pyodide. Can import https://github.com/gruns/icecream if you want an improvement over print. Can also use something like this if not using terminal (in pyodide and in mpy) import ltk def print(*args): ltk.find("body").append(" ".join(str(a) for a in args), "<br>") and then you can print to the dom instead of a dedicated terminal (which takes up room). Or use the logger - ltk has a nic logger.... (edited)
Was looking more for stepping/watch debuging (edited)
Avatar
Avatar
Quint
Was looking more for stepping/watch debuging (edited)
I thought so - maybe the devs can answer with what the current plan is...
Avatar
I have 2 questions:
  • is there a way to interact with pyscript.toml through the CLI, akin to how poetry will udpate your pyproject.toml ?
  • is there a way to add my entire package/module without listing every single file explicitly in [files] section?
Avatar
Avatar
Sprezz
I have 2 questions:
  • is there a way to interact with pyscript.toml through the CLI, akin to how poetry will udpate your pyproject.toml ?
  • is there a way to add my entire package/module without listing every single file explicitly in [files] section?
not sure if it's best practice as quite new to pyscript, but for point #2 I read in the docs that you can't include complete dirs as files, so to get around the pain of listing everything explicitly I package my module into a wheel and then include it in the pyscript.toml packages = []
Avatar
Avatar
Djo
not sure if it's best practice as quite new to pyscript, but for point #2 I read in the docs that you can't include complete dirs as files, so to get around the pain of listing everything explicitly I package my module into a wheel and then include it in the pyscript.toml packages = []
makes it quite annoying, when modifying the developing the module while testing with pyscript
Avatar
i'm running from a Dockerfile and include the wheel build in there, so each time I make changes I just run docker compose up --build to relaunch with the changes
19:57
it's not quite hot reloading but not finding it too bad, though maybe there is a better a way!
Avatar
Quick question, I'm trying to load two pyscript configurations in the same page, one that I can dynamically include in the page when I want to run tests, that is the same as the main configuration but plugs in the files property to include upytest.py, e.g. I have <script type="py" src="../../scripts/example/example.py" config="../../scripts/pyscript.toml"></script> <script type="py" src="../../tests/main.py" config="../../tests/pyscript.toml" terminal></script> And I've noticed that even if the contents of the files is the same, neither of them is even requested. Are configs only loaded if only one config is used on a single page?
Avatar
Ok different upytest question on top of that one; say I have the following html <input type="text" id="input_number" placeholder="Type number here..." /> <button id="do_a_thing_button" py-click="do_a_thing">Get next value</button> <div id="output_number"></div> <script type="py" src="../../scripts/example/example.py" config="../../scripts/pyscript.toml"></script> <script type="py" src="../../tests/main.py" config="../../scripts/pyscript.toml" terminal></script> And the py-click="do_a_thing" that is brough in from the src="../../scripts/example/example.py" is def do_a_thing(event): input_text = document.querySelector("#input_number") input_number = input_text.value output_div = document.querySelector("#output_number") output_div.innerText = str(1+int(input_number)) And the src="../../tests/main.py" is doing upytest ~ import json from base import upytest from pyscript import web results = await upytest.run("./tests") output = web.div(json.dumps(results), id="results") web.page.append(output)
07:32
Which is discovering and running the test import asyncio from pyscript import document, web, when async def test_example(): input_container = document.querySelector("#input_number") input_container.value = "101" just_a_button = web.page.find("#do_a_thing_button")[0] call_flag = asyncio.Event() @when("click", just_a_button) def on_click(event): call_flag.set() # Now let's simulate a click on the button (using the low level JS API) # so we don't risk dom getting in the way just_a_button._dom_element.click() await call_flag.wait() output_container = document.querySelector("#output_number") assert output_container.innerHTML == "102", output_container.innerHTML (trying to learn how to put this together so it looks very similar to how upytest is used in pyscript core) -- My question is -- is there any way that the upytest executing in the second <script> that's discovered the test_example to await the execution of the do_a_thing run from the first <script>? I can see the result is appearing as expected on the page, but there's obviously a race, because the test is failing (but obviously passes if I add the on-click function into the test)
Avatar
Hello folks. I am new to this. We had one question. The py /notebook files....is there a way to save it in remote storage (e.g. s3)? We plan to integrate into one of web app pages this one. @Andrea Giammarchi (edited)
Avatar
imsomebodys 05/01/2025 06:49
can you now use pygame
06:49
?
Avatar
Avatar
imsomebodys
can you now use pygame
Gabe the Bear 05/01/2025 09:50
No. I'm still getting "Cannot find a pure Python wheel" error. Pygame is an SDL2 (C) wrapper, so it needs to be compiled to Pyodide package. Also, SDL support in Pyodide is still experimental: https://pyodide.org/en/stable/usage/sdl.html
Avatar
Avatar
imsomebodys
can you now use pygame
You can include โ€œpygame-ceโ€ in packages list to get the wasm wheel installed for it.
10:25
I donโ€™t think running an actual game loop is very compatible with pyscript/pyodide though (I am just using it for image and rect functions with a different rendering library). Better I think to use the โ€œpygbagโ€ build tool for web packaging full pygame games (edited)
๐Ÿ‘ 1
Avatar
Avatar
Djo
I donโ€™t think running an actual game loop is very compatible with pyscript/pyodide though (I am just using it for image and rect functions with a different rendering library). Better I think to use the โ€œpygbagโ€ build tool for web packaging full pygame games (edited)
Gabe the Bear 05/01/2025 10:50
Pygbag is great for this. Although you don't get as much freedom to style the page and install dependencies (networkx is stuck at 2.8.4, for example), it does a lot of heavy lifting for you, like turning the screen into a canvas and scaling with the browser window. @Djo What "different rendering library" were you using?
11:01
Description This MR would like to land an experimental implementation of the py-game special type with the following features: a &lt;script type=&quot;py-game&quot;&gt; with either...
๐Ÿ˜ƒ 1
๐Ÿ‘€ 1
Avatar
Avatar
Gabe the Bear
Pygbag is great for this. Although you don't get as much freedom to style the page and install dependencies (networkx is stuck at 2.8.4, for example), it does a lot of heavy lifting for you, like turning the screen into a canvas and scaling with the browser window. @Djo What "different rendering library" were you using?
I'm using "zengl", it's a fantistic lib. Some learning curve though if you are not familiar with OpenGL. There's some examples from the author here: https://pyscript.com/@szabolcsdombi/collections/15a772b9-5c23-4eee-8018-aa465b31c83c
๐Ÿ˜ƒ 1
๐Ÿ‘€ 1
Avatar
imsomebodys 05/01/2025 16:51
is there an example on how to run pygame because i generated pygame code to test and it did not run
Avatar
Morning @here! Happy new year to you all and I hope 2025 is flourishing and fulfilling for you. PyScript core team will be returning from the holiday season throughout the course of this week. If you've had questions over the holiday season and still not managed to work things out (hint: try looking at https://docs.pyscript.net ๐Ÿ˜‰ ) feel free to ask again / send an update and we'll see if we can help. ๐Ÿš€
Avatar
Avatar
Djo
not sure if it's best practice as quite new to pyscript, but for point #2 I read in the docs that you can't include complete dirs as files, so to get around the pain of listing everything explicitly I package my module into a wheel and then include it in the pyscript.toml packages = []
CyberneticDaisy 06/01/2025 10:11
that's what i've been doing, but if I'm not careful to build the wheel before every page reload, it notices the file hasn't changed and caches it and won't load any changes until I put a ? query in the url to it. which is rather annoying.
Avatar
@Djo hot reloading would be helpful. FWIW I have a script in the invent framework that watches my code directories as I develop, and automagically rebuilds and gzips the assets, so I just have to re-load the browser to run the test suite, test the examples etc... https://github.com/invent-framework/invent/blob/main/utils/serve.py (although I just noticed a speeling mistaik... I should know how to spell "handler"). ๐Ÿ˜‰
Express yourself with code. Contribute to invent-framework/invent development by creating an account on GitHub.
10:18
So, we're almost there.
Avatar
Avatar
Djo
I'm using "zengl", it's a fantistic lib. Some learning curve though if you are not familiar with OpenGL. There's some examples from the author here: https://pyscript.com/@szabolcsdombi/collections/15a772b9-5c23-4eee-8018-aa465b31c83c
This is VERY COOL if you're into WebGL
Avatar
Avatar
ntoll
This is VERY COOL if you're into WebGL
specifically if you're into GLSL shaders
Avatar
Avatar
ntoll
@Djo hot reloading would be helpful. FWIW I have a script in the invent framework that watches my code directories as I develop, and automagically rebuilds and gzips the assets, so I just have to re-load the browser to run the test suite, test the examples etc... https://github.com/invent-framework/invent/blob/main/utils/serve.py (although I just noticed a speeling mistaik... I should know how to spell "handler"). ๐Ÿ˜‰
Sounds great, will check it out thanks! Restarting my docker build is pretty quick with the docker cache so isn't so bad overall but it's not the best workflow, combined with the loadup time in the browser having to re-get the micropip packages, would be awesome if this helps smooth things up more.
21:34
And yeah WebGL/GLSL is awesome stuff ๐Ÿ˜„ Working atm on a little 3D "game engine"/framework which is going great, I saw your post about the superdough sound implementation, this sounds perfect for what I need will definitely have a go with it
Avatar
Still curious to hear any ideas on my upytest question above. I am assuming that the intent of upytest is to run a test suite on a page by interacting with the elements and whatnot -- similar browser automation tests e.g. selenium -- but my problem is that I can't just "add" a <script type="py" src="main.py" config="pyscript.toml" terminal></script> sort of script, to test an already present script, because there's a race condition, i.e. the script that I already have on the page AND the upytest invoking script. Having both, the page behaves as expected but obviously the upytest case finishes ahead of when the page actually updates so reports it failed. The only reliable way I can get upytest to assert the result of another script is by including the required scripts in my [files], importing the actual script into the test script and executing the function from within the test case upytest runs -- which feels like a lot of manual decoration as well as being a pretty big gap between "having the thing be the way it is and test it as is" and "have a test pretend to be the actual thing and just test itself not the actual thing" -- so I can't tell if I'm just wildly misunderstanding the intent or how to use upytest.
Avatar
Avatar
Skenvy
Still curious to hear any ideas on my upytest question above. I am assuming that the intent of upytest is to run a test suite on a page by interacting with the elements and whatnot -- similar browser automation tests e.g. selenium -- but my problem is that I can't just "add" a <script type="py" src="main.py" config="pyscript.toml" terminal></script> sort of script, to test an already present script, because there's a race condition, i.e. the script that I already have on the page AND the upytest invoking script. Having both, the page behaves as expected but obviously the upytest case finishes ahead of when the page actually updates so reports it failed. The only reliable way I can get upytest to assert the result of another script is by including the required scripts in my [files], importing the actual script into the test script and executing the function from within the test case upytest runs -- which feels like a lot of manual decoration as well as being a pretty big gap between "having the thing be the way it is and test it as is" and "have a test pretend to be the actual thing and just test itself not the actual thing" -- so I can't tell if I'm just wildly misunderstanding the intent or how to use upytest.
Andrea Giammarchi 07/01/2025 09:11
there are events triggered when a script is ready to be executed or actually when it's fully executed and you receive the script as target reference in the event. It should be fairly simple to add a listener right before creating such <script type="py"> (with or without terminal attribute) and then avoid race conditions until such script py:ready event is triggered (or other events, all listed in our docs). Maybe this way you can do what you are trying to do with ease?
Avatar
Avatar
Skenvy
Still curious to hear any ideas on my upytest question above. I am assuming that the intent of upytest is to run a test suite on a page by interacting with the elements and whatnot -- similar browser automation tests e.g. selenium -- but my problem is that I can't just "add" a <script type="py" src="main.py" config="pyscript.toml" terminal></script> sort of script, to test an already present script, because there's a race condition, i.e. the script that I already have on the page AND the upytest invoking script. Having both, the page behaves as expected but obviously the upytest case finishes ahead of when the page actually updates so reports it failed. The only reliable way I can get upytest to assert the result of another script is by including the required scripts in my [files], importing the actual script into the test script and executing the function from within the test case upytest runs -- which feels like a lot of manual decoration as well as being a pretty big gap between "having the thing be the way it is and test it as is" and "have a test pretend to be the actual thing and just test itself not the actual thing" -- so I can't tell if I'm just wildly misunderstanding the intent or how to use upytest.
Hi @Skenvy (I wrote upytest to scratch an itch - just catching up post-holidays). Yes, the intent is for upytest to be a PyTest-ish framework for unit testing in the browser with PyScript (working with both MicroPython and Pyodide). I think I should probably update the README in the github repository to explain how to "idiomatically" use it. But the tl;dr is that there should be a test page specifically for your test suite... in which you copy over your project, its test suite and reference upytest too. Then you do something like: import upytest await upytest.run("./tests") You can see this pattern in the test suite for the Invent framework here: https://github.com/invent-framework/invent
Express yourself with code. Contribute to invent-framework/invent development by creating an account on GitHub.
10:16
FWIW, with Invent I zip up the invent module and the associated tests directory into a foo.tgz file that I simply unzip onto the filesystem. I also copy over the upytest module too. This setup is found here: https://github.com/invent-framework/invent/blob/main/static/settings.json If you look at index.html in the root of that repository, you'll see where it all comes together.
Express yourself with code. Contribute to invent-framework/invent development by creating an account on GitHub.
Avatar
Gabe the Bear 07/01/2025 11:29
Hi everyone. I'm a Svelte (a web framework) developer and would like to integrate PyScript with my Svelte application. Is there any guide for such integration? Some background: Svelte is a compiler based on Vite. I saw in docs that Pyodide has a plugin for Vite integration, so I'm wondering if there's a similar PyScript plugin.
Avatar
Avatar
Vk
Hello folks. I am new to this. We had one question. The py /notebook files....is there a way to save it in remote storage (e.g. s3)? We plan to integrate into one of web app pages this one. @Andrea Giammarchi (edited)
Folks: Request again for help / advise on this! ๐Ÿ™๐Ÿป
Avatar
Avatar
Vk
Hello folks. I am new to this. We had one question. The py /notebook files....is there a way to save it in remote storage (e.g. s3)? We plan to integrate into one of web app pages this one. @Andrea Giammarchi (edited)
Andrea Giammarchi 07/01/2025 15:47
PyScript runs on the browser (client side) ... I am sure there is an API that allows you to put/post files directly to S3 but that API would likely expose your tokens/API to do so, unless you use PyScript .com and create a secret, so that the reverse-proxy can hide that token/authentication thing from the rest of the world ... any JS module, or even python one, would work there, yet you need to reverse-proxy the dance, which is not super portable. It's been long time I want to solve this problem but the TL;DR is that no service is currently secure around this topic so I wouldn't know what to suggest. AFAIK exposing also S3 links might lead to increased bills if that link leaks and many tries to reach it as they charge even 404 (AFAIK).
๐Ÿ™๐Ÿป 1
Avatar
imsomebodys 08/01/2025 00:42
so I was able to run pyscript in a browser game but im unable to set config is there a way programmically to install packages
Avatar
imsomebodys 08/01/2025 02:56
can you load config file from url
Avatar
imsomebodys 08/01/2025 03:37
in deepest world i can run pyscript just config is ignored
Avatar
imsomebodys 08/01/2025 07:38
apperently when you load a py file with document.createElement the config attribute is ignored
Avatar
imsomebodys 08/01/2025 08:36
i should mention all code is running in iframe
Avatar
Has there been any thought about including the micropython-ulab module in the PyScript version of micropython? It would unlock a bunch of interesting use cases where there is a desire for numerical processing while still being lightweight.
Avatar
cรฉsar 08/01/2025 19:16
Hi. I'm using pyscript.com to test pyscript and LTK, but can't use storage module. Using the console, it seems the storage module isn't installed. help> modules pyscript Here is a list of modules whose name or summary contains 'pyscript'. If there are any, enter a module name to get more help. pyscript pyscript.display pyscript.event_handling pyscript.magic_js pyscript.util
Avatar
Avatar
cรฉsar
Hi. I'm using pyscript.com to test pyscript and LTK, but can't use storage module. Using the console, it seems the storage module isn't installed. help> modules pyscript Here is a list of modules whose name or summary contains 'pyscript'. If there are any, enter a module name to get more help. pyscript pyscript.display pyscript.event_handling pyscript.magic_js pyscript.util
FYI there is a pyscript.com and an ltk channel; on this discord. You will probably get more focused response if you repost there...
Avatar
How can PyWorker use packages in pyscript.toml? (edited)
19:50
I can't use micropip because that's not installed for PyWorker either.
Avatar
I figured it out, but I needed to read the source code of Polyscript for that.
Avatar
Avatar
ntoll
Hi @Skenvy (I wrote upytest to scratch an itch - just catching up post-holidays). Yes, the intent is for upytest to be a PyTest-ish framework for unit testing in the browser with PyScript (working with both MicroPython and Pyodide). I think I should probably update the README in the github repository to explain how to "idiomatically" use it. But the tl;dr is that there should be a test page specifically for your test suite... in which you copy over your project, its test suite and reference upytest too. Then you do something like: import upytest await upytest.run("./tests") You can see this pattern in the test suite for the Invent framework here: https://github.com/invent-framework/invent
I'm not sure how this would work with what I'm trying to do.. running all the tests from one page? My goal is to be able to have it run on each page that already contains another <script type="py" to test the functionality of the other script and whether or not the other script is successfully updating the correct elements on the page or not. So I'd have two <script type="py" on each of several pages, one that is "the regular script to test" and one that is "the upytest invoker" but my problem is that the upytest invoker is unaware of or unable to wait on the timing of the other script completing its action.
Avatar
OK. Why do you need to organise the Python code into two script tags?
Avatar
Primarily because it seemed like the easiest way to be able to dynamically build a static site with or without the test script included i.e. build it with test=true or something and be able to check each page and see tests pass, or build it with test=false and have the test script not built in. I realise that's an elaborate explanation that might have a different simpler answer, it's just the journey of trying to get different things to work that took me to that being the thing I was trying to do when I hit the obvious roadblock of realising that there would be a race condition between two scripts executing on the same page without awareness of each other. The simplest way to state the problem is "I was playing around making a site that uses pyscript, and I want some way to test each of the pages" -- of course browser automation would be an easy answer, but my goal was to try and learn what I could about the pyscript ecosystem and its tools and stumbled across upytest and found examples of the core pyscript repo using it to test the state of components on a page, and figured from that that it could be used the way I envisioned, and that's how I ended up wanting "a regular script" plus "a script that tests the other script already on the page."
Avatar
Hmm... OK. Thanks for the context. As far as I can tell you could just use one script that also includes your tests... but you could put the code for kicking off the tests behind a conditional checking if you've (for example) appended ?test=true to the URL. E.g. perhaps use something like this..? (This is raw untested code written without trying it out): from pyscript import window if window.location.endswith("?test=true"): # start the tests This code won't work, but you get the idea.
Avatar
Hmm good idea! The way I had thought about it so far I had one test page for each regular page and replaced the regular script with a test script that would import the python file the regular script pointed to and include the function that was intended for a certain action in the test method, i.e. a regular page would have a script add a function to a button, and a test page would have the invoker read a test file that imported that function and ran it on the simulated click. But that seemed like a lot of steps and way too wide a gap for locality of concern, so thats when I realised I'd probably misunderstood and joined the server to ask lol.
14:34
I still like the idea of attaching the div with the terminal output if it's a test but wouldn't want to if it was the "not test" version of the page, so that'd be the next thing for me to figure out if I use that "check the query string" method.
Avatar
Is there a way to make an IntersectionObserver or equivalent with pyscript?
Avatar
Specifically, I want to move an element but only after it has rendered, to trigger its transition effects. Like main.append(square) square.style["bottom"] = "300px" Doesn't trigger the style transition unless I put a sleep in-between
Avatar
Avatar
Skenvy
I still like the idea of attaching the div with the terminal output if it's a test but wouldn't want to if it was the "not test" version of the page, so that'd be the next thing for me to figure out if I use that "check the query string" method.
Possibly you don't need a terminal if the test is successful. So you could use a textarea instead (made visble in the same way ntoll describes). If the test failed then you'll get the report but probably need the inspector or to make a terminal change to the config for better debugging...???
Avatar
Avatar
ntoll
Currently it's internal Anaconda, 'cos we need to figure out how and for what we pay Damien for his time. TL; DR: bureaucracy. Once that's done, it'll be in the open on PyScript's / MP's GitHub.
Hi @ntoll. A chunk of time has passed since this comment. Any update you can share?
Avatar
Hello pytribe, have a question just to clear iโ€™m not doing something stuped. I am using html elements as property โ€˜domโ€™ in classes and mpy interpreter and instead of @when(โ€˜clickโ€™..) just do self.dom.onclick = self.method. It works but not sure is it memory and cpu efficient. In py @when(โ€˜clickโ€™, <proxy>) works but as now tried in mpy didnt so got it that way, (i know the pyscript documentation shows only to use selector in @when not the very proxy yet tried bec not using ids in html elements). PP also will add second question on something which works too but not sure how will be garbaged well on prolonged use - self.dom.self = self which works (with purpose to give back the class instance containing the html element from the very html element) https://rdsm.pyscriptapps.com/self-dom-onclick-self-test/latest/ (edited)
Avatar
alternatively, what's the best way to make a css keyframe to be used in animations?
Avatar
Avatar
ableman
alternatively, what's the best way to make a css keyframe to be used in animations?
I'm not entirely sure but I suspect the solution is to model it in css and get it to work. Then use pyscript to insert the css in the dom and attach it to the element. Can't find an example of his quickly. Although the ltk does have a way to add css to an element directly. but get the css working first IMHO.
Avatar
Avatar
Neon22
I'm not entirely sure but I suspect the solution is to model it in css and get it to work. Then use pyscript to insert the css in the dom and attach it to the element. Can't find an example of his quickly. Although the ltk does have a way to add css to an element directly. but get the css working first IMHO.
Thanks. I ended up making a class that constructs keyframes based on a dictionary you pass in. I am probably being a bit silly, but I'm trying to not write any html/css/javascript files and just doing everything in pyscript. The issue I had is that whereas style is just a dictionary that you can attach, you can't put a keyframe in that dictionary (at least not in any way I figured out) so I made a style element, but defining that style element requires a big block of text, which is basically just writing css as a string, which I wanted to avoid.
Avatar
Avatar
ableman
Thanks. I ended up making a class that constructs keyframes based on a dictionary you pass in. I am probably being a bit silly, but I'm trying to not write any html/css/javascript files and just doing everything in pyscript. The issue I had is that whereas style is just a dictionary that you can attach, you can't put a keyframe in that dictionary (at least not in any way I figured out) so I made a style element, but defining that style element requires a big block of text, which is basically just writing css as a string, which I wanted to avoid.
sorry, sounds like I didn't quite work out what you were trying to do. If you share an example on pyscript.com then other people may be able to help out too. Cheers.
21:21
Possibly you can try to use pyscript to get the JS side of something with an animate on it and see what you get...?
21:26
There's a to_py method in the pyodide ffi module. Not sure if its the right thing.. - https://pyodide.org/en/stable/usage/api/python-api/ffi.html
Avatar
Avatar
ableman
Thanks. I ended up making a class that constructs keyframes based on a dictionary you pass in. I am probably being a bit silly, but I'm trying to not write any html/css/javascript files and just doing everything in pyscript. The issue I had is that whereas style is just a dictionary that you can attach, you can't put a keyframe in that dictionary (at least not in any way I figured out) so I made a style element, but defining that style element requires a big block of text, which is basically just writing css as a string, which I wanted to avoid.
21:32
Also the ltk has a demo where it does this also: https://pyscript.github.io/ltk/?tab=5
Avatar
Avatar
Neon22
Also the ltk has a demo where it does this also: https://pyscript.github.io/ltk/?tab=5
Thank you very much, I think that ltk is exactly what I was looking for.
Avatar
Is it passible to use yattag along side pyscript?
Avatar
Avatar
Quint
Is it passible to use yattag along side pyscript?
yattag seems like a python only lib so you should be able to use packages = [""] in the toml config file and import it in your main.py file. Can't speak to how its design would intehrate to being run from inside the browser - as its designed for diff use. YMMV
Avatar
Avatar
Neon22
yattag seems like a python only lib so you should be able to use packages = [""] in the toml config file and import it in your main.py file. Can't speak to how its design would intehrate to being run from inside the browser - as its designed for diff use. YMMV
Ya. I'm running into this error. Uncaught (in promise) PythonError: Traceback (most recent call last): File "/lib/python3.12/site-packages/micropip/_commands/install.py", line 146, in install raise ValueError( ValueError: Can't find a pure Python 3 wheel for: 'yattag' See: https://pyodide.org/en/stable/usage/faq.html#why-can-t-micropip-find-a-pure-python-wheel-for-a-package at new_error (pyodide.asm.js:10:9958) at pyodide.asm.wasm:0x16dd24 at pyodide.asm.wasm:0x1777ca at _PyEM_TrampolineCall_JS (pyodide.asm.js:10:123144) at pyodide.asm.wasm:0x1c2f97 at pyodide.asm.wasm:0x2c7d12 at pyodide.asm.wasm:0x20a96c at pyodide.asm.wasm:0x1c3684 at pyodide.asm.wasm:0x1c3993 at pyodide.asm.wasm:0x1c3a11 at pyodide.asm.wasm:0x29ea60 at pyodide.asm.wasm:0x2a5057 at pyodide.asm.wasm:0x1c3b51 at pyodide.asm.wasm:0x1c37ba at pyodide.asm.wasm:0x176e2a at callPyObjectKwargs (pyodide.asm.js:10:62076) at Module.callPyObjectMaybePromising (pyodide.asm.js:10:63254) at wrapper (pyodide.asm.js:10:27079) at onGlobalMessage (pyodide.asm.js:10:99160) (edited)
02:09
trying to figure out how to make a.whl for it now
Avatar
Avatar
Quint
Ya. I'm running into this error. Uncaught (in promise) PythonError: Traceback (most recent call last): File "/lib/python3.12/site-packages/micropip/_commands/install.py", line 146, in install raise ValueError( ValueError: Can't find a pure Python 3 wheel for: 'yattag' See: https://pyodide.org/en/stable/usage/faq.html#why-can-t-micropip-find-a-pure-python-wheel-for-a-package at new_error (pyodide.asm.js:10:9958) at pyodide.asm.wasm:0x16dd24 at pyodide.asm.wasm:0x1777ca at _PyEM_TrampolineCall_JS (pyodide.asm.js:10:123144) at pyodide.asm.wasm:0x1c2f97 at pyodide.asm.wasm:0x2c7d12 at pyodide.asm.wasm:0x20a96c at pyodide.asm.wasm:0x1c3684 at pyodide.asm.wasm:0x1c3993 at pyodide.asm.wasm:0x1c3a11 at pyodide.asm.wasm:0x29ea60 at pyodide.asm.wasm:0x2a5057 at pyodide.asm.wasm:0x1c3b51 at pyodide.asm.wasm:0x1c37ba at pyodide.asm.wasm:0x176e2a at callPyObjectKwargs (pyodide.asm.js:10:62076) at Module.callPyObjectMaybePromising (pyodide.asm.js:10:63254) at wrapper (pyodide.asm.js:10:27079) at onGlobalMessage (pyodide.asm.js:10:99160) (edited)
Just built my own whl for it and it seems fine now.
Avatar
if I have two .py file for example a_file.py and b_file.py, b_file.py has import a_file statement. am I able to load both of them? or am I going to need to package them first?
Avatar
Avatar
Quint
if I have two .py file for example a_file.py and b_file.py, b_file.py has import a_file statement. am I able to load both of them? or am I going to need to package them first?
The files thing in the toml will do that. You only need packages if they are published on pypi. https://docs.pyscript.net/2024.11.1/user-guide/configuration/ (edited)
๐Ÿ‘ 1
Avatar
Avatar
Andrea Giammarchi
Hi Andrea, I am trying to load more than one image using your ref code. but only one coming in. I don't undderstand why. I hate to ask you to look at this version of your code. No problem if you don't have time. https://pyscript.com/@neon22/svg-image-mpy/latest?files=main.py
Avatar
Avatar
Neon22
Hi Andrea, I am trying to load more than one image using your ref code. but only one coming in. I don't undderstand why. I hate to ask you to look at this version of your code. No problem if you don't have time. https://pyscript.com/@neon22/svg-image-mpy/latest?files=main.py
Andrea Giammarchi 13/01/2025 13:04
image.onload = lambda e: append_image(e.target)
Avatar
Avatar
Andrea Giammarchi
image.onload = lambda e: append_image(e.target)
Andrea Giammarchi 13/01/2025 13:05
AFAIK Python has no (automatic) block scope (like JS) so if you loop and you have async events the latest image reference will point at the latest image you assigned in that loop ... with my change all should be fine, the image is referenced by the event.target (or event.currentTarget) (edited)
Avatar
Avatar
Andrea Giammarchi
AFAIK Python has no (automatic) block scope (like JS) so if you loop and you have async events the latest image reference will point at the latest image you assigned in that loop ... with my change all should be fine, the image is referenced by the event.target (or event.currentTarget) (edited)
Andrea Giammarchi 13/01/2025 13:05
Avatar
Andrea Giammarchi 13/01/2025 13:07
also ... append does not understand HTML, it's either plain text (appended as such, hence sanytized) or a DOM element ... if you want to put that <br> in the mix, you need br = lambda: document.createElement('br') and pass br() instead of a string
13:08
I might update the clone to reflect that too (edit: done) (edited)
Avatar
Can JavaScript get access to a Python class and instantiate it? I'm trying to accomplish something like this: PYTHON from pyscript import window class my_class(): def greet(): return "hello" window.my_class = my_class JAVASCRIPT myClass = window.my_class const instance = new myClass() const greet = instance.greet() console.log(greet) // 'hello' (edited)
Avatar
Avatar
Mauriel
Can JavaScript get access to a Python class and instantiate it? I'm trying to accomplish something like this: PYTHON from pyscript import window class my_class(): def greet(): return "hello" window.my_class = my_class JAVASCRIPT myClass = window.my_class const instance = new myClass() const greet = instance.greet() console.log(greet) // 'hello' (edited)
Andrea Giammarchi 13/01/2025 22:58
any PyScript .com link where you tried and failed with some error message more useful? or maybe it was just a curiosity? we can certainly handle JS to Python references, but I think the same should happen vice-versa, hence my question for a better page/code to check live, thanks! (edited)
Avatar
Avatar
Andrea Giammarchi
any PyScript .com link where you tried and failed with some error message more useful? or maybe it was just a curiosity? we can certainly handle JS to Python references, but I think the same should happen vice-versa, hence my question for a better page/code to check live, thanks! (edited)
Andrea Giammarchi 13/01/2025 23:00
on the other side, if that class comes from a worker, you'd likely need to await any of its methods while invoking those, as the sync ability only maps on the same thread back and forward, otherwise it's Python driving JS, not vice-versa.
Avatar
Avatar
Andrea Giammarchi
on the other side, if that class comes from a worker, you'd likely need to await any of its methods while invoking those, as the sync ability only maps on the same thread back and forward, otherwise it's Python driving JS, not vice-versa.
Andrea Giammarchi 13/01/2025 23:01
so that maybe the answer is maybe ... but also not necessarily the way you'd expect that.
Avatar
!๐’ช ษนษupoษฏโ‡‚ใ„ฅ8๐Ÿ ๐’ช! 14/01/2025 07:01
this is quite the error message... import requests from bs4 import BeautifulSoup web = requests.get('https://www.geoguessr.com/world-cup') does requests just not work on pyscript?
Avatar
Avatar
!๐’ช ษนษupoษฏโ‡‚ใ„ฅ8๐Ÿ ๐’ช!
this is quite the error message... import requests from bs4 import BeautifulSoup web = requests.get('https://www.geoguessr.com/world-cup') does requests just not work on pyscript?
Andrea Giammarchi 14/01/2025 09:05
Avatar
Andrea Giammarchi 14/01/2025 09:06
it's pyodide, eventually, but I wonder if you try with a worker attribute (and the right headers)
Avatar
Thanks heaps Andrea. Works perfectly and I understand it. Cheers...
๐Ÿ‘ 1
Avatar
Avatar
Andrea Giammarchi
any PyScript .com link where you tried and failed with some error message more useful? or maybe it was just a curiosity? we can certainly handle JS to Python references, but I think the same should happen vice-versa, hence my question for a better page/code to check live, thanks! (edited)
Thanks for your response. I have been unable to get anything but an empty object when trying to instantiate a class defined in Python from JavaScript. (edited)
Avatar
Avatar
Andrea Giammarchi
it's pyodide, eventually, but I wonder if you try with a worker attribute (and the right headers)
!๐’ช ษนษupoษฏโ‡‚ใ„ฅ8๐Ÿ ๐’ช! 14/01/2025 20:25
what is a worker attribute? sorry iโ€™m new to coding
Avatar
Avatar
!๐’ช ษนษupoษฏโ‡‚ใ„ฅ8๐Ÿ ๐’ช!
what is a worker attribute? sorry iโ€™m new to coding
Andrea Giammarchi 15/01/2025 08:50
<script type="py" worker>
๐Ÿ‘ 1
Avatar
Avatar
Mauriel
Thanks for your response. I have been unable to get anything but an empty object when trying to instantiate a class defined in Python from JavaScript. (edited)
Andrea Giammarchi 15/01/2025 08:55
1. methods in Python requires a self argument 2. Python does not use, or understand, new check this out: https://pyscript.com/@agiammarchi/dry-water-copy/latest?files=main.py,index.html
๐Ÿ‘ 1
08:56
also, from main, you need to await callbacks because you cannot block ... it's all in our docs though
Avatar
Avatar
Andrea Giammarchi
1. methods in Python requires a self argument 2. Python does not use, or understand, new check this out: https://pyscript.com/@agiammarchi/dry-water-copy/latest?files=main.py,index.html
Thanks, that works for the main thread. However, could the same be achieved from a worker? Or are workers constrained to returning serializable data? From the docs: Arguments to and the results from such calls, when used in a worker, must be serializable, otherwise they won't work. https://pyscript.com/@mauriel/dry-water/latest?files=index.html (edited)
Avatar
Avatar
Andrea Giammarchi
it's pyodide, eventually, but I wonder if you try with a worker attribute (and the right headers)
!๐’ช ษนษupoษฏโ‡‚ใ„ฅ8๐Ÿ ๐’ช! 16/01/2025 05:40
still the same error, what are the headers you had in mind? also, what is a polyfill? ive tried googling it but none of the guides ive read make sense
Avatar
Avatar
!๐’ช ษนษupoษฏโ‡‚ใ„ฅ8๐Ÿ ๐’ช!
still the same error, what are the headers you had in mind? also, what is a polyfill? ive tried googling it but none of the guides ive read make sense
Andrea Giammarchi 16/01/2025 08:46
Avatar
Avatar
Mauriel
Thanks, that works for the main thread. However, could the same be achieved from a worker? Or are workers constrained to returning serializable data? From the docs: Arguments to and the results from such calls, when used in a worker, must be serializable, otherwise they won't work. https://pyscript.com/@mauriel/dry-water/latest?files=index.html (edited)
Andrea Giammarchi 16/01/2025 08:51
the goal of the project is to drive JS from Python, not vice-versa, so you are right, that would not work out of the box. The reason is double (triple with workers) GC orchestration which is way too convoluted and probably deadly slow ... when I said workers, I meant that you can import js instead of from pyscript import window and pass js.my_class = my_class then notify js.dispatchEvent(js.Event.new('my_class')) and from the JS side of affairs, the one executed through the worker via import or any other way, you can addEventListener('my_class', callback) and in there use my_class same way you'd do on the main thread. As summary: JS references are preserved in Python side but not vice-versa when it comes to cross-thread interaction but pyodide preserves both references within the same runtime, being that on main or in a worker (two different worlds/realms).
๐Ÿ‘ 1
Avatar
Avatar
Andrea Giammarchi
the goal of the project is to drive JS from Python, not vice-versa, so you are right, that would not work out of the box. The reason is double (triple with workers) GC orchestration which is way too convoluted and probably deadly slow ... when I said workers, I meant that you can import js instead of from pyscript import window and pass js.my_class = my_class then notify js.dispatchEvent(js.Event.new('my_class')) and from the JS side of affairs, the one executed through the worker via import or any other way, you can addEventListener('my_class', callback) and in there use my_class same way you'd do on the main thread. As summary: JS references are preserved in Python side but not vice-versa when it comes to cross-thread interaction but pyodide preserves both references within the same runtime, being that on main or in a worker (two different worlds/realms).
Andrea Giammarchi 16/01/2025 08:54
on that note, I think even the async ref.greetings() is not needed if on the same thread, it was my mistake suggesting it, apologies. (edited)
Avatar
Can a worker start a PyWorker?
14:07
Ahh... sorry. Read it as "Can PyScript start a PyWorker".
14:07
It's main thread only.
Avatar
Thanks!
14:31
If I have a <script type="py" ... worker terminal>, is there any way to declare a sync function to be called on the main thread?
14:32
my use case is that creating a ton of JavaScript objects seems to be very slow on pyodide in a worker (it's fast on the main thread). so it would be great if I could just declare a sync function to call so that this part would happen on the main thread.
Avatar
Avatar
ambv
If I have a <script type="py" ... worker terminal>, is there any way to declare a sync function to be called on the main thread?
Andrea Giammarchi 16/01/2025 15:08
every <script type="py/mpy" worker> exposes a xworker property with a sync proxy where you can define any callback you like, synchronous or asynchronous, and that callback will be available from pyscript via from pyscript import xworker and then xworker.sync.fromMain(...args) will work synchronously. To provide some context, the time.sleep() works like that, it's a sync.sleep function exposed through the xworker.sync proxy and used in PyScript when blocking for X milliseconds is desired. When you have thousand references from the main it's a good thing to keep those in there, or better, crytical UI related path (or rendering path) should happen in the UI so that from the worker you just provide raw data to deal with, not handle all objects from there. Hpefully some of this helps, happy to help further otherwise.
๐Ÿ‘๐Ÿป 1
15:09
P.S. the sync works both ways ... you can define from a worker callbacks that can be called from main, but that will be asynchronos (inevitably, we can't block the main thread) ... the other way around can be both sync or async.
15:10
P.S.2 ... if from pyscript import xworker does not work, try from polyscript import xworker as I am sure that's a polyscript thing, not sure we expose it in PyScript too but I think we do, for what I remember
Avatar
thanks, I'll try it in a while
15:18
15:18
this is happening to me every once in a while with worker terminal
Avatar
joeyjoeyjojo 19/01/2025 22:56
Hello All, probably a stupid question but how to I use the pydom module in the latest version of pyscript?
๐Ÿ‘ 1
Avatar
Avatar
joeyjoeyjojo
Hello All, probably a stupid question but how to I use the pydom module in the latest version of pyscript?
Josh (EduBlocks) 19/01/2025 23:36
It's now known as pyscript.web, check out https://docs.pyscript.net/2024.11.1/user-guide/dom/#pyscriptweb ๐Ÿ™‚ (edited)
Avatar
joeyjoeyjojo 20/01/2025 11:39
Thanks Josh ๐Ÿ‘ that should have been obvious Separately Iโ€™m having a problem trying to use some old pyscript code from an example with the latest version. Itโ€™s raising an error: โ€œCannot read properties of undefined (reading โ€˜callโ€™)โ€ I am importing from js import navigator and awaiting navigator.serial.requestPort(). Based on this article: jeff.glass/post/pyscript-webserial/ Iโ€™m assuming itโ€™s some change in how I might access browser apis? (edited)
Avatar
Avatar
joeyjoeyjojo
Thanks Josh ๐Ÿ‘ that should have been obvious Separately Iโ€™m having a problem trying to use some old pyscript code from an example with the latest version. Itโ€™s raising an error: โ€œCannot read properties of undefined (reading โ€˜callโ€™)โ€ I am importing from js import navigator and awaiting navigator.serial.requestPort(). Based on this article: jeff.glass/post/pyscript-webserial/ Iโ€™m assuming itโ€™s some change in how I might access browser apis? (edited)
That approach is quite old and is now deprecated. The link above to the latest docs is well worth the effort to walk through. There are also examples. Strongly suggest you get a pyscript.com account and play away... ๐Ÿ™‚
Avatar
Avatar
Neon22
That approach is quite old and is now deprecated. The link above to the latest docs is well worth the effort to walk through. There are also examples. Strongly suggest you get a pyscript.com account and play away... ๐Ÿ™‚
joeyjoeyjojo 21/01/2025 10:27
Thanks Neon slowly getting to grips with the docs (and JavaScript concepts) and pyscript.com is awesome. The part I am struggling with is finding information on what has been deprecated, when that was and what the new approach is. From reading and looking on YouTube Iโ€™m getting some context now, I assume there isnโ€™t a page somewhere that lists these deprecations?
Avatar
There have been so many deprecations and changes. Some have come and gone. Its the nature of a fairly new project I think. It is quite consistent at the moment though and stabilising well. I would suggest using the docs as your primary source. If your mental model is based on a snapshot of experience some time ago then I think looking it up in the doc link above will be ideal. I can also recommend the ltk for any UI you need to make. You can use this instead of pyweb. It exists standalone. The docs describe the current approach quite well. I know people are working on bolstering them. Also the examples wil be rereleased soon. We nned a few more as not demos of many of the newer features and approaches. You can find working very new examples on pyscript.com if you search this discord for issues/examples. Good luck
Avatar
Avatar
Neon22
There have been so many deprecations and changes. Some have come and gone. Its the nature of a fairly new project I think. It is quite consistent at the moment though and stabilising well. I would suggest using the docs as your primary source. If your mental model is based on a snapshot of experience some time ago then I think looking it up in the doc link above will be ideal. I can also recommend the ltk for any UI you need to make. You can use this instead of pyweb. It exists standalone. The docs describe the current approach quite well. I know people are working on bolstering them. Also the examples wil be rereleased soon. We nned a few more as not demos of many of the newer features and approaches. You can find working very new examples on pyscript.com if you search this discord for issues/examples. Good luck
joeyjoeyjojo 21/01/2025 10:55
Thanks Neon, thatโ€™s understandable. I caused my own problems because the examples that most interested me were older ๐Ÿ˜ต
Avatar
ThisIsMyUsername 23/01/2025 17:45
I am working on a website that has a simple IDE. I am using code mirror as the editor. I would like to set a timer so that if the code in code mirror has an inifinite loop, pyscript will terminate the code from code mirror without causing issues for the website as a whole. I have seen stack overflow questions about doing this in python but the solutions have not worked in pyscript. I have written a javascript function that times out javascript code.
17:46
Is there something built into pyscript to handle this?
Avatar
ThisIsMyUsername 23/01/2025 18:27
I was reading the documentation and found this.
18:27
window.setTimeout(lambda x: importlib.reload(temp), 10, "OK")
18:27
I need to play with it more, but I think that this is doing what I want.
Avatar
hello, i have some quiestion if some1 can please help me: 1. how to make a py script path to python code .py ? 2. how to make a button to start a python script function ? 3. is it posible to dellet the black bord behind the output in the web? thenks if any1 can help me!!
Avatar
ThisIsMyUsername 24/01/2025 14:40
The setTimeout is not doing what I want. I think that this maybe on the write track. Does anyone have any insight into this problem?
Avatar
Avatar
ThisIsMyUsername
The setTimeout is not doing what I want. I think that this maybe on the write track. Does anyone have any insight into this problem?
The ltk uses CodeMirror to allow onscreen editing. ltk.window.CodeMirror() You can see the demo running here: https://pyscript.github.io/ltk/?tab=8 If you want to roll your own then the ltk source is in github
Avatar
Avatar
ThisIsMyUsername
The setTimeout is not doing what I want. I think that this maybe on the write track. Does anyone have any insight into this problem?
Perhaps wrap the function you're using in create_proxy..? See: https://docs.pyscript.net/2024.11.1/user-guide/ffi/#create_proxy
Avatar
How to I use custom library that is installed on my device on pyscript?
Avatar
ThisIsMyUsername 28/01/2025 15:14
@ntoll Thanks. This is psuedocode what I have so far. It is running the timer before the code starts.
15:14
def unloadCode(): if not codeCompleted: raise ZeroDivisionError("Division by zero!") return "var OK" def executeCode(code, inputs, output): window.setTimeout(create_proxy(unloadCode()), int(5 * 1000), "OK") runCode window.clearTimeout("OK")
15:15
The documentation on the pyscript website is not clear to me. I am not familar with proxies. I based this algorithm off of this page. https://www.w3schools.com/jsref/met_win_settimeout.asp (edited)
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
Avatar
@ThisIsMyUsername don't call the function inside setTimeout I.e. it's unloadCode not unloadCode().
Avatar
I need to include libraries that arenโ€™t in default python and my python part needs to be used with venv. How do I do that?
Avatar
ThisIsMyUsername 28/01/2025 17:07
Thanks!
Avatar
Avatar
igilq
I need to include libraries that arenโ€™t in default python and my python part needs to be used with venv. How do I do that?
how do you run venv in the browser. Pyscript is for the Browser environment
Avatar
Avatar
igilq
I need to include libraries that arenโ€™t in default python and my python part needs to be used with venv. How do I do that?
did you test that all the extra parts already work on python-wasm (pygbag) / pyodide (pyscript) ? pygbag packager has a bit of venv building based upon a main.py + https://packaging.python.org/en/latest/specifications/inline-script-metadata/#inline-script-metadata to help you test that but it is far from trivial and there's no pyscript template yet.
Avatar
Those extra libraries work on local device that they are supposed to work on, the server when tested with normal python script gives me information about temperature and humidity in current place. When I ported that script to html with some extra changes and launch that html code in browser, it has problems with those extra libraries that need to be installed
05:41
It doesnโ€™t even detect them
05:45
<!DOCTYPE html> <html lang="en"> <head> <!--tagi meta--> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!--pyscript css--> <link rel="stylesheet" href="https://pyscript.net/releases/2024.1.1/core.css"> <!--css dla przykladu, nie jest potrzebny--> <link rel="stylesheet" href="./assets/css/examples.css" /> <!--tylko uruchamiacz pyscript--> <script type="module" src="https://pyscript.net/releases/2024.1.1/core.js"></script> <!--ekran wczytywania--> <style> #loading { outline: none; border: none; background: transparent } </style> <script type="module"> const loading = document.getElementById('loading'); addEventListener('py:ready', () => loading.close()); loading.showModal(); </script> <title>Document</title> </head> <body> <dialog id="loading"> <h1>Loading...</h1> </dialog> <section class="pyscript"> Temperatura i wilgotnoล›ฤ‡: <script type="py"> import time from adafruit-blinka import board from adafruit-circuitpython-dht import adafruit_dht sensor = adafruit_dht.DHT11(board.D2) while True: try: temperature_c = sensor.temperature temperature_f = temperature_c * (9 / 5) + 32 humidity = sensor.humidity print("Temp={0:0.1f}^C, Temp={1:0.1f}^F, Humidity={2:0}") except RuntimeError as error: print(error.args[0]) time.sleep(2.0) continue except Exception as error: sensor.exit() raise error time.sleep(3.0) </script> </section> </body> </html>
05:45
This is html code that I am working on
Avatar
I think you're missing the config. E.g. a pyscript.toml with a packages tag inside which indicates the packages you need to load. Its a two step process. Check out https://docs.pyscript.net/2024.11.1/user-guide/configuration/ I'd suggest you create it first on pyscript.com then migrate it to your server. I don't think you need them here but you may need mini-coi or other aspects for a local served setup. Also the packages wil have to be pure python. I have not looked up the two adafruit packages you are using. Good luck
Avatar
Avatar
igilq
<!DOCTYPE html> <html lang="en"> <head> <!--tagi meta--> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!--pyscript css--> <link rel="stylesheet" href="https://pyscript.net/releases/2024.1.1/core.css"> <!--css dla przykladu, nie jest potrzebny--> <link rel="stylesheet" href="./assets/css/examples.css" /> <!--tylko uruchamiacz pyscript--> <script type="module" src="https://pyscript.net/releases/2024.1.1/core.js"></script> <!--ekran wczytywania--> <style> #loading { outline: none; border: none; background: transparent } </style> <script type="module"> const loading = document.getElementById('loading'); addEventListener('py:ready', () => loading.close()); loading.showModal(); </script> <title>Document</title> </head> <body> <dialog id="loading"> <h1>Loading...</h1> </dialog> <section class="pyscript"> Temperatura i wilgotnoล›ฤ‡: <script type="py"> import time from adafruit-blinka import board from adafruit-circuitpython-dht import adafruit_dht sensor = adafruit_dht.DHT11(board.D2) while True: try: temperature_c = sensor.temperature temperature_f = temperature_c * (9 / 5) + 32 humidity = sensor.humidity print("Temp={0:0.1f}^C, Temp={1:0.1f}^F, Humidity={2:0}") except RuntimeError as error: print(error.args[0]) time.sleep(2.0) continue except Exception as error: sensor.exit() raise error time.sleep(3.0) </script> </section> </body> </html>
E.g. <py-config> { "packages": ["adafruit-blinka", "adafruit_dht", "adafruit-circuitpython-dht"] } </py-config>
Avatar
Avatar
Neon22
E.g. <py-config> { "packages": ["adafruit-blinka", "adafruit_dht", "adafruit-circuitpython-dht"] } </py-config>
So I add this to pyscript.toml and this should fix it? (edited)
Avatar
Avatar
igilq
So I add this to pyscript.toml and this should fix it? (edited)
FabioRosado 29/01/2025 13:49
It should install the packages yes, but the above config is set to json. You would need to paste this in a pyscript.toml file packages = ["adafruit-blinka", "adafruit_dht", "adafruit-circuitpython-dht"] If you create a toml file make sure o update your <script type='py'> with <scrip type='py' config="./pyscript.toml" hope it helps
Avatar
Avatar
FabioRosado
It should install the packages yes, but the above config is set to json. You would need to paste this in a pyscript.toml file packages = ["adafruit-blinka", "adafruit_dht", "adafruit-circuitpython-dht"] If you create a toml file make sure o update your <script type='py'> with <scrip type='py' config="./pyscript.toml" hope it helps
I updated and it doesnโ€™t give any errors but it is stuck on loading screen
Avatar
FabioRosado 29/01/2025 14:15
have a look at the dev tools in the console and check if there are any issues there, I havent tested these packages so not sure if they would work out of the box
Avatar
there are errors
14:28
Uncaught (in promise) PythonError: Traceback (most recent call last): File "/lib/python3.11/site-packages/micropip/_commands/install.py", line 146, in install raise ValueError( ValueError: Can't find a pure Python 3 wheel for: 'adafuit-dht' See: https://pyodide.org/en/stable/usage/faq.html#why-can-t-micropip-find-a-pure-python-wheel-for-a-package k https://cdn.jsdelivr.net/pyodide/v0.24.1/full/pyodide.asm.js:9 new_error https://cdn.jsdelivr.net/pyodide/v0.24.1/full/pyodide.asm.js:9 _PyCFunctionWithKeywords_TrampolineCall https://cdn.jsdelivr.net/pyodide/v0.24.1/full/pyodide.asm.js:9 callPyObjectKwargs https://cdn.jsdelivr.net/pyodide/v0.24.1/full/pyodide.asm.js:9 callPyObject https://cdn.jsdelivr.net/pyodide/v0.24.1/full/pyodide.asm.js:9 wrapper https://cdn.jsdelivr.net/pyodide/v0.24.1/full/pyodide.asm.js:9 pyodide.asm.js:9:104344 (edited)
Avatar
So, I tried running python only from one library, got new error
Avatar
New error says that it wasnโ€™t able to detect board and/or microcontroller
Avatar
ThisIsMyUsername 29/01/2025 16:14
I am trying to run code with a PyWorker. The PyWorker fucntion is not able to find my python file. Ideally, I would like to call PyWorker with a tempory python file stored in the browser. I have tried this and calling PyWorker with a file stored in the project on the harddrive. Either way, PyWorker is not able to find the file, but I can open the file and print the file to the contents. Can anyone tell me why the open fucntion is able to find the file but not the PyWorker function?
16:16
Here is simplified code for what I am doing.
16:17
def storeCode(code, inputs): open("temp.py", "w").write("print('Code from editor') def executeCode(code, inputs, output): print(open("temp.py", "r").read()) PyWorker("temp.py", type="pyodide")
Avatar
@ThisIsMyUsername you need the donkey ๐Ÿซ (seriously... docs here: https://docs.pyscript.net/2024.11.1/api/#pyscriptcoredonkey) Any questions... just ask. ๐Ÿ˜‰
16:22
The donkey is ready and willing to any work you need... the donkey starts in a worker, and then waits patiently until needed. Then all you need do is pass in the code to evaluate and it'll return the result. ๐Ÿ‘ It's a donkey and does what donkeys do, without complaint. ๐Ÿ˜‰
16:23
(It's not an actual donkey... ๐Ÿ˜› but the metaphor is cute.)
Avatar
ThisIsMyUsername 29/01/2025 16:35
@ntoll Thanks.
Avatar
NP - keep asking questions..!
Avatar
Avatar
igilq
New error says that it wasnโ€™t able to detect board and/or microcontroller
Normal tools won't work if they don't go through socket or web usb / serial, interfacing with micro controller from client webpage is quite hard work, see https://viper-ide.org/ for micropython and for cpython i'd suggest https://github.com/MrYsLab/telemetrix ( with help of a websocket + asyncio it can work : i could drive a cheap robot arm with that via an esp8266 )
Avatar
Thatโ€™s a lot of stuff, what I wanted to do is to display information about humidity and temperature in my room on good looking interface, I decided to make it in html since it is easy to make it look good but I am running to problems. Should I still continue to make it in html with pyscript or just abandon it?
Avatar
@igilq that depends. Can you link to your code. Many folks have PyScript interacting with microcontrollers quite successfully.
Avatar
Avatar
ntoll
@igilq that depends. Can you link to your code. Many folks have PyScript interacting with microcontrollers quite successfully.
Do you want me to send you link to my code? I donโ€™t think that I read your question correctly
Avatar
Yes please, so folks can see what you're trying to do. ๐Ÿ‘
Avatar
Avatar
ntoll
@ThisIsMyUsername you need the donkey ๐Ÿซ (seriously... docs here: https://docs.pyscript.net/2024.11.1/api/#pyscriptcoredonkey) Any questions... just ask. ๐Ÿ˜‰
Gosh I wish we had a usage example of the donkey in action...
Avatar
Avatar
ntoll
Yes please, so folks can see what you're trying to do. ๐Ÿ‘
Avatar
couple of things. Use latest pyscript 2024.11.1 rather than older 2024.1.1 You use print so you need a terminal or to print to the dom. You could change your script invocation to be: <script type="py" config="./pyscript.toml" terminal> or you could overwrite print in your script so it appended to the page, or use display() from pyscript instead. If the terminal is a bit big - you can add __terminal__.resize(60, 20) below your imports. I don't think you need "setuptools" in your toml packages. I'm getting this error: Adafruit-PlatformDetect version 3.77.0 was unable to identify the board and/or microcontroller running the Emscripten platform. Please be sure you have the latest packages by running: 'pip3 install --upgrade adafruit-blinka adafruit-platformdetect' If you are running the latest package, your board may not yet be supported. Please open a New Issue on GitHub at https://github.com/adafruit/Adafruit_Blinka/issues and select New Board Request. I would have expected to see some kind of "connect with the board" code so I don;t know how the AdaFruit code is supposed to find your external device. Is it by wifi ? The errors looks like the code expects the Adadfruit board to be your browser but I expect you have a DHT11 unit plugged into an ADAfruit board of some kind....?
Avatar
Avatar
Neon22
couple of things. Use latest pyscript 2024.11.1 rather than older 2024.1.1 You use print so you need a terminal or to print to the dom. You could change your script invocation to be: <script type="py" config="./pyscript.toml" terminal> or you could overwrite print in your script so it appended to the page, or use display() from pyscript instead. If the terminal is a bit big - you can add __terminal__.resize(60, 20) below your imports. I don't think you need "setuptools" in your toml packages. I'm getting this error: Adafruit-PlatformDetect version 3.77.0 was unable to identify the board and/or microcontroller running the Emscripten platform. Please be sure you have the latest packages by running: 'pip3 install --upgrade adafruit-blinka adafruit-platformdetect' If you are running the latest package, your board may not yet be supported. Please open a New Issue on GitHub at https://github.com/adafruit/Adafruit_Blinka/issues and select New Board Request. I would have expected to see some kind of "connect with the board" code so I don;t know how the AdaFruit code is supposed to find your external device. Is it by wifi ? The errors looks like the code expects the Adadfruit board to be your browser but I expect you have a DHT11 unit plugged into an ADAfruit board of some kind....?
The board is the server which I want to host code on. It would gather information from dht11 and then display it in browser with html
Avatar
So you need the code in the browser to communicate with the AdaFruit board. Pyscript runs in the Browser and circuitpython or micropython run on the board. I am just a little confused. Can you tell me which AdaFruit board it is you are using, or do you have a DHT11 somehow plugged into a windows machine ?
11:36
When you say import board where is board defined ?
11:40
OK so I looked a little deeper and it appears the dht module you reference runs on a raspberry pi
Python library to read the DHT series of humidity and temperature sensors on a Raspberry Pi or Beaglebone Black. - adafruit/Adafruit_Python_DHT
Avatar
Board is raspberry pi 5, python code works normally on it as standalone, dht is connected directly to raspberry pi 5
Avatar
The raspberryPi wil lbe running python3.
11:42
ok. so you want someone on a machine on your network to navigate to an html file and open it in the browser. Then you want that program (The one you're trying to write to display info in the browser) to talk to the raspberrypi and find out what the values are. This sounds like setting up a server on the pi and then you talk to that server from your html page. Does that sound like what you're trying to do
11:45
That simpletest example above gathers the sensor data. Then you need to look for how to setup a simple python server on your raspberrypi so it can serve data to any connecting system. Its a common thing to do.
11:46
Then write the pyscript code to do a fetch from the server you started on teh pi. So you'll need to enter a local host address and use the port you started the server on. I think that's the shape of the problem you're trying to solve. Do you agree ? Have I missed something ?
11:49
This looks like a fairly straightforward python server for the raspberryPI - https://www.electromaker.io/tutorial/blog/how-to-make-a-raspberry-pi-python-web-server
Because of its small footprint, energy efficiency, and reasonable computing power, the Raspberry Pi makes an excellent server. You can spin up a media server using the likes of Plex, Emby, and OpenMediaVault, or a web server running WordPress, Drupal, Apache, or NGINX. Similarly, you can make a Python web server with a Raspberry Pi and Flask. Le...
11:50
The file that you woudl serve from this server could be made in pyscript and it could then do live fecthes for realtime update and look pretty nice if you used the ltk to do most of the html/css heavy lifting
11:50
Its well after midnight here and I have to go to sleep. Good luck
Avatar
Thatโ€™s a lot of stuff
Avatar
run the simpletest.py locally on the PI first of all. If it works then see if you can make a server using the second link
11:57
modify simpletest to write the current 'numbers' to a txt file. Then serve those numbers in the webserver. Once that's working then you coudl make a pyscript thing to show the numbers nicer
11:58
Good luck
11:59
Maybe you don;t need teh server if you only look at it on the raspberry but you never did say if my guess about what youre trying to do is correct'
Avatar
python code works, example code on webpage (edited)
12:04
what i want to do is to make some sort of basic interface that would show temperature and humidity on screen that is connected directly to pi with flex cable
12:07
i dont need it to be shown on diffrent devices, just only on pi
Avatar
Hi @igilq - thank you for your patience. It feels to me like you're having quite a adventure (which is both a good thing, and infuriating... we've all been there, you're among friends). So.... as I understand it, you have a DHT attached to a Pi serving a website in some way..? PyScript works in the browser, and ONLY in the browser. Therefore, it can only connect to devices that the user's browser can connect to. If your browser is running on a laptop, you would connect a MicroPython based board to the laptop and use the browser to connect to it. You could even use browser based BLE to connect..! However, if the sensor is connected to the Pi, then the Pi isn't the thing running the browser. It's just the thing serving the website. Does this clear things up? Happy to answer more, if the above makes sense. ๐Ÿ‘ (edited)
Avatar
Avatar
ntoll
Hi @igilq - thank you for your patience. It feels to me like you're having quite a adventure (which is both a good thing, and infuriating... we've all been there, you're among friends). So.... as I understand it, you have a DHT attached to a Pi serving a website in some way..? PyScript works in the browser, and ONLY in the browser. Therefore, it can only connect to devices that the user's browser can connect to. If your browser is running on a laptop, you would connect a MicroPython based board to the laptop and use the browser to connect to it. You could even use browser based BLE to connect..! However, if the sensor is connected to the Pi, then the Pi isn't the thing running the browser. It's just the thing serving the website. Does this clear things up? Happy to answer more, if the above makes sense. ๐Ÿ‘ (edited)
dht is attached to pi and i wanted to use website to make some sort of interface for viewing information from sensor, i only want to make some sort of app/code that would every 1 minute fetch information from sensor and then show it on some sort of basic interface. i even tried running website locally with pyscript to see if it would work this way but it doesn't. what you said clears things up but i don't know now if i should be still going with pyscript for interface or i should change it to flask
Avatar
@igilq sounds like Flask is more what you need. Basically, I imagine it working like this:
  • Every time a request comes in, Flask could check to see if it has a reading.
  • If it doesn't it should take a reading, and store it, along with a timestamp.
  • If it does, it should check the associated timestamp. If the timestamp is over a minute ago, take another reading and update the timestamp to now.
  • Otherwise, just return the stored reading, since the timestamp tells us it's current.
  • Then, once the reading is obtained through whatever method applies, it should just respond with some HTML that says "The temperature is whatever..." ๐Ÿ™‚
Done! ๐Ÿ™‚
(edited)
12:56
I don't think you need PyScript for this. Just run Flask on the Pi.
Avatar
yeah... i will switch to flask but i dont know how to configure it to use python code that is already working
Avatar
So, in the function that is used to respond to the request, just use whatever Python code you're used to using on the Pi to read the value. That's it.
Avatar
Alright, thanks for explaining I will try doing some stuff with this flask and see what will be result of it
Avatar
idk if thats a silly question but i never worked with either pyscript or extensions in general, is there only the option to open a entire new tab using extensions with pyscript or can u still do things inside small popup windows (edited)
Avatar
JTS.Dawnstar[] 01/02/2025 05:40
I'm making a website that includes the python editor widget supplied by PyScript, but I'm unsure of the security concerns. I know that generally letting the user run arbitrary code is a very bad idea, but I've heard some stuff about the editor widget being "sandboxed". Would anyone with more technical know-how be able to confirm that a user of my website wouldn't be able to, say, interact with my sql database via the editor widget?
Avatar
Avatar
JTS.Dawnstar[]
I'm making a website that includes the python editor widget supplied by PyScript, but I'm unsure of the security concerns. I know that generally letting the user run arbitrary code is a very bad idea, but I've heard some stuff about the editor widget being "sandboxed". Would anyone with more technical know-how be able to confirm that a user of my website wouldn't be able to, say, interact with my sql database via the editor widget?
Josh (EduBlocks) 01/02/2025 11:44
Because the interpreters in PyScript are powered by WebAssembly, and everything is ran within the context of the end users browser (which is sandboxed and one of the safest/most battle tested environments), you donโ€™t need to worry about malicious arbitrary code
Avatar
JTS.Dawnstar[] 01/02/2025 18:24
Awesome, thanks for the confirmation
๐Ÿ‘ 1
Avatar
Avatar
Josh (EduBlocks)
Because the interpreters in PyScript are powered by WebAssembly, and everything is ran within the context of the end users browser (which is sandboxed and one of the safest/most battle tested environments), you donโ€™t need to worry about malicious arbitrary code
JTS.Dawnstar[] 01/02/2025 20:04
Actually, I just realised that you're the main dev behind edublocks! Great work on that btw. I was wondering if there's a way that I'd be able to embed the edublocks block-based editor into my website, so that users would be able to switch between the PyScript text-based editor and that?
Avatar
Avatar
JTS.Dawnstar[]
Actually, I just realised that you're the main dev behind edublocks! Great work on that btw. I was wondering if there's a way that I'd be able to embed the edublocks block-based editor into my website, so that users would be able to switch between the PyScript text-based editor and that?
Josh (EduBlocks) 01/02/2025 20:12
That's me! Thank you! ๐Ÿ˜„ You should be able to embed a link like this within an iframe to include and EduBlocks editor on your own website: https://app.edublocks.org/project/new?mode=python&type=blocks&embed=true If you wanted access to the text-based editor within edublocks, just switch the type parameter to text ๐Ÿ™‚
Avatar
JTS.Dawnstar[] 01/02/2025 20:32
Amazing, thanks for the help!
Avatar
Does anyone have any tips on troubleshooting a very slow to load package? Adding the imagehash library to packages in my pyscript.toml causes the Hello World example to take many seconds to load even without importing / using the library: https://pyscript.com/@dyeaw/hello-world/latest
15:06
It does look like imagehash is also pulling in scipy, numpy, pywavelets, and pillow. Maybe the startup time is due to download size of all of these?
Avatar
Yeah, it's probably network latency. If you open the dev sidebar in the browser and select the "network" section, you'll see this in action.
Avatar
I think it is CPU bound which is weird. Scipy and matplotlib do take about a second of network download, but that is in the middle of 5.5 seconds of near 100% CPU usage and the profiler shows that it is in a Jank process delay that whole time, hmmm.
Avatar
Hmm... interesting.
Avatar
Avatar
Josh (EduBlocks)
That's me! Thank you! ๐Ÿ˜„ You should be able to embed a link like this within an iframe to include and EduBlocks editor on your own website: https://app.edublocks.org/project/new?mode=python&type=blocks&embed=true If you wanted access to the text-based editor within edublocks, just switch the type parameter to text ๐Ÿ™‚
JTS.Dawnstar[] 04/02/2025 00:18
When I embed that in an iframe, I get an error in the console saying that it violates a Content Security Policy. Any ideas?
Avatar
Avatar
danyeaw
Does anyone have any tips on troubleshooting a very slow to load package? Adding the imagehash library to packages in my pyscript.toml causes the Hello World example to take many seconds to load even without importing / using the library: https://pyscript.com/@dyeaw/hello-world/latest
JTS.Dawnstar[] 04/02/2025 00:22
I've had some similar issues with using the Bokeh package. The workaround I had some success with was storing serialized versions of objects from the package, and then unserializing those at runtime (Instead of importing the package). Not sure if that would work for imagehash, but it might be a thing to try
Avatar
Avatar
JTS.Dawnstar[]
When I embed that in an iframe, I get an error in the console saying that it violates a Content Security Policy. Any ideas?
Josh (EduBlocks) 04/02/2025 10:54
Ah, yes. Because of us using PyScript workers and the headers needed for that, it will show that error. I think adding credentialless should fix it https://developer.mozilla.org/en-US/docs/Web/Security/IFrame_credentialless#the_solution_%E2%80%94_iframe_credentialless
IFrame credentialless provides a mechanism for developers to load third-party resources in s using a new, ephemeral context. It doesn't have access to its regular origin's network, cookies, and storage data. It uses a new context local to the top-level document lifetime. In return, the Cross-Origin-Embedder-Policy (COEP) embedding rules can be l...
Avatar
Avatar
JTS.Dawnstar[]
I've had some similar issues with using the Bokeh package. The workaround I had some success with was storing serialized versions of objects from the package, and then unserializing those at runtime (Instead of importing the package). Not sure if that would work for imagehash, but it might be a thing to try
I fixed my specific problem by writing my own small image hash algorithm using numpy and opencv (which are packages I was already using for my app). But I would like to try to investigate more why this is happening. I'm interested in how you serialized objects from the package, do you have an example somewhere?
Avatar
Josh (EduBlocks) 04/02/2025 13:35
Here's an example of how to use the donkey functionality within PyScript. This allows you to create a worker that is ready and waiting to recieve, and then execute, Python code ๐Ÿ™‚ https://pyscript.com/@joshualowe1002/using-pyscript-donkey/latest?files=index.html,index.js
๐ŸŽ‰ 1
Avatar
Bravo @Josh (EduBlocks)
Avatar
Anyone can help me.Why alpha and releases is not same (edited)
02:46
Now l can not run the program
Avatar
Avatar
oklkl665
Anyone can help me.Why alpha and releases is not same (edited)
As we announced over a year ago, alpha was being dropped, and you should use a pinned version of a release. Alpha hasn't been updated for over a year, and we finally got around to removing it just before Christmas.
Avatar
Avatar
ntoll
As we announced over a year ago, alpha was being dropped, and you should use a pinned version of a release. Alpha hasn't been updated for over a year, and we finally got around to removing it just before Christmas.
But l need to use.because alpha and releases Function is not same.now l program can't run.also l program is important for something.l don't want to use more snd and more time to Change.do u have other things can use alpha again
Avatar
i used alpha 6 mouths....and l am beginner (edited)
Avatar
Where is your source code? How is it hosted? If you can share I'll take a look. For future reference, when anything is marked as alpha, it is unstable and shouldn't be relied upon.
Avatar
Do u know what releases version number and a test version with the same functionality.
Avatar
You could try release: 2023.05.1 but it's old, buggy and we no longer support that code base. My advice is to migrate to the latest version as soon as possible.
Avatar
l tried.it not work.unm.my key question is this.in alpha.if print("abc").abc will show my computer.but releases is show not and nothing.the system will put my information in debug console
07:12
This is a example
Avatar
In the recent versions of PyScript, if you want to see the output of print, you need to ensure terminal is included as an attribute of the script tag that starts PyScript. E.g. <script type="py" src="main.py" terminal></script>
08:49
If you're using PyScript.com please share the link to your project and I'd be happy to take a look. ๐Ÿ‘
08:51
Also, there's lots of help at https://docs.pyscript.net/ ๐Ÿ‘ (edited)
Avatar
Avatar
ntoll
In the recent versions of PyScript, if you want to see the output of print, you need to ensure terminal is included as an attribute of the script tag that starts PyScript. E.g. <script type="py" src="main.py" terminal></script>
@ntoll thx.l can saw my code.๐Ÿ‘
๐ŸŽ‰ 1
Avatar
So im a noob when it comes to Pyscript, but i have python experience. How do i pip install modules like scikit-learn? (edited)
Avatar
Avatar
TRT1000
So im a noob when it comes to Pyscript, but i have python experience. How do i pip install modules like scikit-learn? (edited)
Best advice is: open a pyscript.com account. Clone the examples, experiment. Also docs are here: https://docs.pyscript.net/2025.2.2/ Make sure to use Version 2025.2.2 as it is the most uptodate atm. Package loading is described here: https://docs.pyscript.net/2025.2.2/user-guide/configuration/ but suggest you look at examples...
Avatar
thanks
Avatar
Chris Laffra 09/02/2025 17:03
I visited FOSDEM'25 last week in Brussels. There was a dedicated room to Python talks. My presentation about PyScript (with demos of MicroPython, PyOdide, LTK, PySheets, and DuckDB) is now available on YouTube. Enjoy: https://www.youtube.com/watch?v=FJJkUKwgycA
๐Ÿ™ 1
Avatar
Avatar
Chris Laffra
I visited FOSDEM'25 last week in Brussels. There was a dedicated room to Python talks. My presentation about PyScript (with demos of MicroPython, PyOdide, LTK, PySheets, and DuckDB) is now available on YouTube. Enjoy: https://www.youtube.com/watch?v=FJJkUKwgycA
Andrea Giammarchi 10/02/2025 12:46
great talk and great questions ... just one minor note, with PyScript you can use any ECMAScript Module out of the box, you don't need to have libraries available on the window object, also because global context pollution is a thing "from the past" and discouraged a lot these days, due name clashing and other issues ... modules are "sandboxed" in scope and functionality, including private variables out of a publicly exported namespace. One can chose, js_modules in config is there to help chosing ESM these days, even for projects such as THREE js (edited)
โค๏ธ 1
Avatar
Avatar
Andrea Giammarchi
great talk and great questions ... just one minor note, with PyScript you can use any ECMAScript Module out of the box, you don't need to have libraries available on the window object, also because global context pollution is a thing "from the past" and discouraged a lot these days, due name clashing and other issues ... modules are "sandboxed" in scope and functionality, including private variables out of a publicly exported namespace. One can chose, js_modules in config is there to help chosing ESM these days, even for projects such as THREE js (edited)
Andrea Giammarchi 10/02/2025 12:47
12:49
It has global namespace pollution ๐Ÿค“
12:51
BTW. This was my 12th conference presentation on PyScript since first trying the awesome project 1.5 years ago.
12:53
Next PyScript demo will be at DuckDB Amsterdam Meetup #2 in 10 days from now: https://duckdb.org/events/2025/02/20/duckdb-amsterdam-meetup-2/
DuckDB is an in-process SQL database management system focused on analytical query processing. It is designed to be easy to install and easy to use. DuckDB has no external dependencies. DuckDB has bindings for C/C++, Python, R, Java, Node.js, Go and other languages.
๐ŸŽ‰ 1
Avatar
Andrea Giammarchi 10/02/2025 14:37
agreed ... our tests should always reflect our latest offer but the thing is, our tests also reflect PyScript ability to be pinned ... that test is one of those things where pinned version of both PyScript and THREE.js made the difference, but I agree with you it's also one of the most compelling tests ... maybe we should spend time improving it and bring in our latest offer in there ... ping to @ntoll about this, feels reasonable to me, even if I know almost nothing about THREE.js ๐Ÿ˜…
Avatar
Indeed this has been fixed in github but not yet released (I think)
๐Ÿ‘ 1
Avatar
OK... lemme follow up on the borked example.
15:04
OK... I've followed up with colleagues in Anaconda who originally created the examples. I'll let you know how I get on, or ask for my account to be added to the admin for these examples and change them myself. ๐Ÿ‘
Avatar
ThisIsMyUsername 11/02/2025 15:55
@ntoll, my time to work on the project that you helped me with previously has been spotty the past couple of weeks. I am still working on using donkey to get me code to run in another "thread" (may not be the right term).
15:56
When I rollup my js file, I get this message. @pyscript/core/dist/core.js (guessing "core_js") (edited)
15:57
Here is the relavent code.
15:57
import { donkey } from '@pyscript/core/dist/core.js'; const { process, // process(code) code (visible in the terminal) execute, // execute(statement) in Python exec way evaluate, // evaluate(expression) in Python eval way clear, // clear() the terminal reset, // reset() the terminal (including colors) kill, // kill() the worker forever } = donkey({ type: 'py' || 'mpy', // the Python interpreter to run persistent: false, // use true to track globals and locals terminal: '', // optionally set a target terminal container config: {}, // the worker config (packages, files, etc.) });
15:58
I have tried adding "@pyscript/core/dist/core.js": "^1.0.0" to my .json file, but that is causing errors also. What do I need to do to be able tim import donkey?
Avatar
@ThisIsMyUsername how are you referencing PyScript..? In the <head> and from where..?
16:32
If you're using local PyScript files you should read this: https://docs.pyscript.net/2025.2.2/user-guide/offline/ Otherwise it "should just work" โ„ข๏ธ (error output welcome!)
Avatar
ThisIsMyUsername 11/02/2025 16:40
I have a link to pyscript in the head of my html file. All of my pyscript code is in <py-script> tags in my html. Where should I import donkey? When I import it in <script> tags in my html file, I get an Uncaught SyntaxError: Cannot use import statement outside a module error.
Avatar
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width,initial-scale=1" /> <title> Monty Hall problem </title> <link rel="stylesheet" href="https://pyscript.net/releases/2025.2.2/core.css"> <script type="module" src="https://pyscript.net/releases/2025.2.2/core.js"></script> </head> <body> <h1>Monty Hall problem</h1> <div id="output"></div> <script type="py" terminal></script> <script type="py" src="./main.py" config="./pyscript.json"></script> </body> </html> Hello I'm trying to get a terminal to open and give inputs to the terminal. For some reason the terminal is not opening, though it did with the same code before but I couldn't give any inputs (the input and send in the image was removed, I only want to give inputs inside the terminal) (edited)
Avatar
so the terminal is for reporting. - https://docs.pyscript.net/2025.2.2/user-guide/terminal/ I think you want a repl. Try the python editor https://docs.pyscript.net/2025.2.2/user-guide/editor/ Also you have two script tags in your html. Change this: <script type="py" terminal></script> <script type="py" src="./main.py" config="./pyscript.json"></script> to: <script type="py" src="./main.py" config="./pyscript.json" terminal></script> and put all your code in main.py
Avatar
Avatar
Neon22
so the terminal is for reporting. - https://docs.pyscript.net/2025.2.2/user-guide/terminal/ I think you want a repl. Try the python editor https://docs.pyscript.net/2025.2.2/user-guide/editor/ Also you have two script tags in your html. Change this: <script type="py" terminal></script> <script type="py" src="./main.py" config="./pyscript.json"></script> to: <script type="py" src="./main.py" config="./pyscript.json" terminal></script> and put all your code in main.py
Thank you, I appreciate it
Avatar
I managed to get the terminal to work using terminal worker and "code.interact()" The issue right now is that the function inside my main.py is not defined in the terminal so I have to import the libraries and define the function manually.
Avatar
Avatar
Taima
I managed to get the terminal to work using terminal worker and "code.interact()" The issue right now is that the function inside my main.py is not defined in the terminal so I have to import the libraries and define the function manually.
I don't know if I can help you but if you're using pyscript.com - you could post a link and maybe someone who knows what they're doing could help you.
Avatar
Avatar
ThisIsMyUsername
import { donkey } from '@pyscript/core/dist/core.js'; const { process, // process(code) code (visible in the terminal) execute, // execute(statement) in Python exec way evaluate, // evaluate(expression) in Python eval way clear, // clear() the terminal reset, // reset() the terminal (including colors) kill, // kill() the worker forever } = donkey({ type: 'py' || 'mpy', // the Python interpreter to run persistent: false, // use true to track globals and locals terminal: '', // optionally set a target terminal container config: {}, // the worker config (packages, files, etc.) });
Andrea Giammarchi 16/02/2025 08:56
re-bundling is discouraged but it's true we could export also those files explicitly, as those are already bundled and everything should just work ... arguably the current export might be problematic if the rest of the setup is not done the same way we do so I think we should provide a @pyscript/core/js same way we provide @pyscript/core/css and point already to the dist/* folder ... will do soon!
Avatar
Avatar
Andrea Giammarchi
re-bundling is discouraged but it's true we could export also those files explicitly, as those are already bundled and everything should just work ... arguably the current export might be problematic if the rest of the setup is not done the same way we do so I think we should provide a @pyscript/core/js same way we provide @pyscript/core/css and point already to the dist/* folder ... will do soon!
Andrea Giammarchi 17/02/2025 09:13
it's done on npm ... if anyone could confirm @pyscript/core/js as import works in there, that'd be awesome, thank yuo!
Avatar
ThisIsMyUsername 17/02/2025 14:55
@Neon22 @Andrea Giammarchi @ntoll I was trying to add the code below to my js file. I have moved it to a script tag in my html and I am getting different errors. I think that this is a step in the right direction.
14:55
<script type="module"> import { donkey } from "https://pyscript.net/releases/2024.11.1/core.js"; const { process, // processes code (visible in the terminal) execute, // executes statements (similar to Python's exec) evaluate, // evaluates expressions (like Python's eval) clear, // clears the terminal reset, // resets the terminal (including colors) kill, // kills the worker process permanently } = donkey({ type: 'py', // or 'mpy' if you need MicroPython persistent: false, // change to true if you want globals/locals to persist terminal: 'terminal', // ID of the terminal container config: {}, // additional configuration (packages, files, etc.) }); // Example usage: execute(); // execute('print("Hello from donkey and PyScript!")'); </script>
14:56
But now, I am getting these errors. (index):374 Uncaught TypeError: execute is not a function at (index):374:5
14:56
This is the second error. PythonError: Traceback (most recent call last): File "/lib/python312.zip/_pyodide/_base.py", line 523, in eval_code .run(globals, locals) ^^^^^^^^^^^^^^^^^^^^ File "/lib/python312.zip/_pyodide/_base.py", line 357, in run coroutine = eval(self.code, globals, locals) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "<exec>", line 1, in <module> AttributeError: 'NoneType' object has no attribute 'terminal'
14:59
Ok, well I changed the script tag to <script type="py" terminal> and it resolved the errors above.
15:05
Now, I am getting this error. Uncaught SyntaxError: Cannot use import statement outside a module which is the reason that I tried putting the above code in the js file in the first place.
Avatar
Avatar
ThisIsMyUsername
Now, I am getting this error. Uncaught SyntaxError: Cannot use import statement outside a module which is the reason that I tried putting the above code in the js file in the first place.
Andrea Giammarchi 17/02/2025 15:25
honestly ... lot of things don't look right ... first of all, to provide the best help you can hope for we really need a PyScript .com page to work with ... just giving us code we test and know it works does not really help us in helping you around the issue.
Avatar
Avatar
Andrea Giammarchi
honestly ... lot of things don't look right ... first of all, to provide the best help you can hope for we really need a PyScript .com page to work with ... just giving us code we test and know it works does not really help us in helping you around the issue.
Andrea Giammarchi 17/02/2025 15:27
secondly ... execute(code) is by contract/API that is going to execute some code ... you are invoking it with no code to execute and it's unclear what you are doing ... as summary, the moment you provide a minimal example of your failing attempts is the moment we can help. If you paste code out of an import we use already daily it doesn't help us in helping you so all these threads could be 1. a thread and 2. have a failing example we can work on, thank you!
Avatar
Avatar
Andrea Giammarchi
secondly ... execute(code) is by contract/API that is going to execute some code ... you are invoking it with no code to execute and it's unclear what you are doing ... as summary, the moment you provide a minimal example of your failing attempts is the moment we can help. If you paste code out of an import we use already daily it doesn't help us in helping you so all these threads could be 1. a thread and 2. have a failing example we can work on, thank you!
ThisIsMyUsername 17/02/2025 15:54
I had commented out the execute function call to see if I could get it to work. I had previously been calling it with code. Here is a link to a project https://pyscript.com/@jic12/calm-queen/latest?files=main.py,index.html
Avatar
Avatar
ThisIsMyUsername
I had commented out the execute function call to see if I could get it to work. I had previously been calling it with code. Here is a link to a project https://pyscript.com/@jic12/calm-queen/latest?files=main.py,index.html
Andrea Giammarchi 17/02/2025 15:57
Avatar
Avatar
Andrea Giammarchi
Andrea Giammarchi 17/02/2025 15:59
that's on us thought, the examples indeed don't await a donkey ... /cc @ntoll as that's a bug in our documentation!
16:02
that just needs an await donkey right..?
16:02
(fixing now)
Avatar
Avatar
ntoll
that just needs an await donkey right..?
Andrea Giammarchi 17/02/2025 16:02
yes, but the whole API is async ... not sure that's reflected in our docs!
Avatar
On it
Avatar
Avatar
ThisIsMyUsername
I had commented out the execute function call to see if I could get it to work. I had previously been calling it with code. Here is a link to a project https://pyscript.com/@jic12/calm-queen/latest?files=main.py,index.html
Andrea Giammarchi 17/02/2025 16:05
we are sorry about this, it was our fault but basically the donkey and all its related methods around are async, because workers are async by nature from the main and so on. This was on our side and I am sorry to hear you wasted more time than you should have around this matter, so that as late help attempt just stick an await in front of ay of your donkey related calls and you should be fine.
Avatar
@ThisIsMyUsername what @Andrea Giammarchi just said. Really sorry about that. It's my fault and I'm fixing it now.
16:08
Once approved, you can feel happy that your feedback has improved PyScript! Hurrah!
Avatar
Avatar
Andrea Giammarchi
secondly ... execute(code) is by contract/API that is going to execute some code ... you are invoking it with no code to execute and it's unclear what you are doing ... as summary, the moment you provide a minimal example of your failing attempts is the moment we can help. If you paste code out of an import we use already daily it doesn't help us in helping you so all these threads could be 1. a thread and 2. have a failing example we can work on, thank you!
ThisIsMyUsername 17/02/2025 16:08
Guys, I apprecitate your help.
โค๏ธ 2
Avatar
OK. All fixed and deployed. The docs are now updated.
Avatar
Andrea Giammarchi 17/02/2025 16:10
to whom it might concern, JS modules are top-level await by design/specs and we provided a top-level await ability out of the box in your Python related code ... sometimes we forget about this symmetry as it's something recently introduced, and documentation might fall behind ... at least we're fixing this as we speak so ... our apologieses for misleading you out there, we're trying our best to keep docs and expectations in sync.
Avatar
ThisIsMyUsername 17/02/2025 16:18
@Andrea Giammarchi The project that you posted with await is still getting the error AttributeError: 'NoneType' object has no attribute 'terminal' and the project that you posted still has not output. Maybe it is running the code and just not being pushed anywhere?
16:24
The project that you sent with await works on chrome but not safari.
Avatar
Avatar
ThisIsMyUsername
The project that you sent with await works on chrome but not safari.
Andrea Giammarchi 17/02/2025 17:28
safari has other issues though ... I can't see any error on Chrome in my link
Avatar
Avatar
Andrea Giammarchi
safari has other issues though ... I can't see any error on Chrome in my link
Andrea Giammarchi 17/02/2025 17:31
let's try to work this through ... what target platform are you after?
Avatar
Avatar
Andrea Giammarchi
let's try to work this through ... what target platform are you after?
Andrea Giammarchi 17/02/2025 17:32
that includes Chrome/ium and Safari/WebKit to be aligned!
Avatar
ThisIsMyUsername 17/02/2025 17:50
@Andrea Giammarchi I would prefer it to work on all browsers but that is not a priority. I found this project from neon22 (https://pyscript.com/@neon22/using-pyscript-donkey/latest?files=index.html), and it is a solid base for me to build from. But both the simple project that you posted with await and this project from neon22 work on the pyscript.com website, but not when I run them through my IDE. Here is a link to the project (https://pyscript.com/@jic12/muddy-union/latest?files=index.html) that I am working on now on pyscript.com. I should be able to just copy and paste this into my index.html, right?
Avatar
ThisIsMyUsername 17/02/2025 17:59
In chrome I am getting this error with code copied and pasted from the project a29fbdbf-9352-49f0-a683-c5938601ee6d:1 PythonError: Traceback (most recent call last): File "/lib/python312.zip/_pyodide/_base.py", line 523, in eval_code .run(globals, locals) ^^^^^^^^^^^^^^^^^^^^ File "/lib/python312.zip/_pyodide/_base.py", line 357, in run coroutine = eval(self.code, globals, locals) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "<exec>", line 1, in <module> AttributeError: 'NoneType' object has no attribute 'terminal'.
Avatar
Avatar
ThisIsMyUsername
@Andrea Giammarchi I would prefer it to work on all browsers but that is not a priority. I found this project from neon22 (https://pyscript.com/@neon22/using-pyscript-donkey/latest?files=index.html), and it is a solid base for me to build from. But both the simple project that you posted with await and this project from neon22 work on the pyscript.com website, but not when I run them through my IDE. Here is a link to the project (https://pyscript.com/@jic12/muddy-union/latest?files=index.html) that I am working on now on pyscript.com. I should be able to just copy and paste this into my index.html, right?
Andrea Giammarchi 17/02/2025 19:34
you need a web server that allows SharedArrayBuffer, plently explained in our docs around workers
19:36
the donkey is a worker by default and design, if you can't provide those headers, there are workarounds ... the "work in all platforms" is a browsers limitation we don't get to control, so we hear you, and already know that's not fortunate, but there's nothing we can do about it ... yet all things work in PyScript .com so if it works there, be sure you follow the same guidance around how your files should be serverd, which is proven to work on GitHub pages too, so that in a way or another, you would be covered, but there's nothing we need to fix around your issues.
Avatar
ThisIsMyUsername 17/02/2025 19:37
Thanks. Iโ€™ll look into it.
Avatar
Avatar
ThisIsMyUsername
@Andrea Giammarchi I would prefer it to work on all browsers but that is not a priority. I found this project from neon22 (https://pyscript.com/@neon22/using-pyscript-donkey/latest?files=index.html), and it is a solid base for me to build from. But both the simple project that you posted with await and this project from neon22 work on the pyscript.com website, but not when I run them through my IDE. Here is a link to the project (https://pyscript.com/@jic12/muddy-union/latest?files=index.html) that I am working on now on pyscript.com. I should be able to just copy and paste this into my index.html, right?
I grabbed the example from @Josh (EduBlocks) who posted it somewhere in this forum. I believe these will make it into the published examples soon.
Avatar
Avatar
ThisIsMyUsername
Thanks. Iโ€™ll look into it.
Its worth noting tha there is the mini-coi as outlined in the docs (https://docs.pyscript.net/2025.2.2/user-guide/workers/#option-1-mini-coi). But there is also the implementation used by the ltk (kitchensink.js) which embeds the minicoi in the index.html file. This may work for you ? here: https://pyscript.github.io/ltk/?tab=3&runtime=mpy check the "page source"
Avatar
Avatar
Neon22
Its worth noting tha there is the mini-coi as outlined in the docs (https://docs.pyscript.net/2025.2.2/user-guide/workers/#option-1-mini-coi). But there is also the implementation used by the ltk (kitchensink.js) which embeds the minicoi in the index.html file. This may work for you ? here: https://pyscript.github.io/ltk/?tab=3&runtime=mpy check the "page source"
Andrea Giammarchi 18/02/2025 10:29
watch out ... one does not simply embed mini-coi or the other script ... it has to be a script a part ... I don't think @Chris Laffra is using mini-coi ... it sits there but it's actually registering nothing as Service Worker and that should actually error?
10:29
in short, follow the docs for mini-coi, don't copy and paste it in your index.html (edited)
Avatar
Chris Laffra 18/02/2025 10:37
@Neon22 @Andrea Giammarchi The mini-coi inclusion in PySheets appears to be very old. I removed it today. The PySheets server sets the required headers when handling requests to load the PySheet main index.html. See https://github.com/PySheets/pysheets/blob/main/src/pysheets.py#L39
Source for PySheets. Contribute to PySheets/pysheets development by creating an account on GitHub.
๐Ÿ‘ 1
Avatar
Avatar
Chris Laffra
@Neon22 @Andrea Giammarchi The mini-coi inclusion in PySheets appears to be very old. I removed it today. The PySheets server sets the required headers when handling requests to load the PySheet main index.html. See https://github.com/PySheets/pysheets/blob/main/src/pysheets.py#L39
Andrea Giammarchi 18/02/2025 10:56
yup, that's the right approach if you can control your server headers, glad you removed that script and I have no idea how that worked, if ever so it's good it's not there anymore ๐Ÿ‘
Avatar
Avatar
Andrea Giammarchi
yup, that's the right approach if you can control your server headers, glad you removed that script and I have no idea how that worked, if ever so it's good it's not there anymore ๐Ÿ‘
Chris Laffra 18/02/2025 10:56
I think it did nothing ๐Ÿค“
๐Ÿ˜ 2
Avatar
Code that doesn't exist, can't have bugs, nor does it need testing. ๐Ÿ˜›
Avatar
ThisIsMyUsername 18/02/2025 23:23
@Andrea Giammarchi I got it working with your mini-coi.js code(much simpler than I was making it!). I am make a website in flask. The documentation says, "The simplest way to use mini-coi is to place the mini-coi.js file in the root of your website (i.e. /), and reference it as the first child tag in the <head> of your HTML documents: <script src="/mini-coi.js" scope="./"></script>"
23:24
But with a flask site, js files are supposed to go in /root/static/js directory. using a statement like <script src="{{ url_for('static', filename='js/cm62.bundle.js') }}"></script>
23:26
Does mini-coi.js have to be in the root directorty?
23:32
@Neon22, In the project (https://pyscript.com/@neon22/using-pyscript-donkey/latest?files=index.html) that I reference the other day, what is the farmer function?
Avatar
Avatar
ThisIsMyUsername
@Neon22, In the project (https://pyscript.com/@neon22/using-pyscript-donkey/latest?files=index.html) that I reference the other day, what is the farmer function?
good question. Looks like a bit of leftover code that was not deleted when making this into an example. @Josh (EduBlocks) might be able to answer.
Avatar
ThisIsMyUsername 18/02/2025 23:53
@neon22 Thanks. I was mostly just curious.
Avatar
ThisIsMyUsername 19/02/2025 00:52
@Andrea Giammarchi I figured out a solution changing my flask code, but I think that being able to move mini-coi.js to the static directory would be the better approach if you can help. If not it works as is!
Avatar
Avatar
ThisIsMyUsername
@Andrea Giammarchi I figured out a solution changing my flask code, but I think that being able to move mini-coi.js to the static directory would be the better approach if you can help. If not it works as is!
Andrea Giammarchi 19/02/2025 08:22
you can put mini-coi.js and serve it as static JS file as long as it's at the root of every other JS file or the Service Worker won't be able to control/intercept those calls
Avatar
Avatar
ThisIsMyUsername
Does mini-coi.js have to be in the root directorty?
Andrea Giammarchi 19/02/2025 08:23
root of the JS files ... if it's in /js/ that should work
Avatar
ThisIsMyUsername 19/02/2025 13:41
That worked.
๐Ÿฅณ 2
Avatar
Woohoo
Avatar
Avatar
ThisIsMyUsername
That worked.
If you wanted to write that up - could be turned into a section in the docs...? maybe...
Avatar
ThisIsMyUsername 20/02/2025 17:42
@Neon22 Do you mean getting mini-coi to work with flask?
Avatar
ThisIsMyUsername 20/02/2025 18:22
const instance = await donkey({ terminal: 'output-div', type: 'py' }); const { execute, reset } = instance; async function onRunClicked(event) { reset(); await execute(inputFunction + code); console.log( document. getElementById("outputdiv").innerText); }
Avatar
ThisIsMyUsername 20/02/2025 18:29
I posted a question earlier and deleted it becasue I think that I understand the problem better. I have been working on this for more than an hour now. I have the above code. I want to do unittesting on the output from the code being executed. Simple if the output matches the expected output should be fine. When I try to log the output from the code, I think that console.log is running before the output has been pushed to the output div. Is there a way the I can get the output from the execute function call directly? I tried using setTimeout but it did not do what I wanted it to do. Or is there unittesting built into pyscript to test the output of code being run? I am currnetly redirecting the input to get input from a list of values. I could consider something similar to redirect the print function also. I can create a simple project in pyscript.com but it will be monday before I can do that. Any thoughts would be appreciated in the mean time.
Avatar
Avatar
ThisIsMyUsername
@Neon22 Do you mean getting mini-coi to work with flask?
yes please ๐Ÿ™‚
Avatar
Avatar
ThisIsMyUsername
I posted a question earlier and deleted it becasue I think that I understand the problem better. I have been working on this for more than an hour now. I have the above code. I want to do unittesting on the output from the code being executed. Simple if the output matches the expected output should be fine. When I try to log the output from the code, I think that console.log is running before the output has been pushed to the output div. Is there a way the I can get the output from the execute function call directly? I tried using setTimeout but it did not do what I wanted it to do. Or is there unittesting built into pyscript to test the output of code being run? I am currnetly redirecting the input to get input from a list of values. I could consider something similar to redirect the print function also. I can create a simple project in pyscript.com but it will be monday before I can do that. Any thoughts would be appreciated in the mean time.
Josh (EduBlocks) 20/02/2025 21:38
The donkey was created for me on EduBlocks and it just so happens that I have something very similar to what you're describing which uses the pyscript donkey behind the scenes (https://docs.edublocks.org/docs/classrooms/for-teachers#create-tests) I just overwrite builtins.print and that achieves what I need for the basic testing functionality that I have. (edited)
Follow this guide to create distinct EduBlocks classrooms and assignments and provide feedback to students
Avatar
Avatar
Neon22
yes please ๐Ÿ™‚
ThisIsMyUsername 20/02/2025 21:42
@Neon I will next week.
Avatar
Avatar
Josh (EduBlocks)
The donkey was created for me on EduBlocks and it just so happens that I have something very similar to what you're describing which uses the pyscript donkey behind the scenes (https://docs.edublocks.org/docs/classrooms/for-teachers#create-tests) I just overwrite builtins.print and that achieves what I need for the basic testing functionality that I have. (edited)
ThisIsMyUsername 20/02/2025 21:43
Thanks. Iโ€™ll look into it.
Avatar
Josh (EduBlocks) 20/02/2025 21:43
I'd give you the code i'm using but it has many different layers that would add more complexity to you understanding how it all works. So more than happy to answer any questions
Avatar
I am trying to be a rocket scientist for a moment ๐Ÿ™‚ and I have a difficulty with package loading. I hope someone can help me: There is a package called skyfield which has all pure python packages except for sgp4. There is a recently made pure python version of sgp4 called sgp4_pure_python. This package has a provides tag that says it provides sgp4. I am loading that pure python package before skyfield in my toml file but micropip cannot see that the requirement has been met. And it fails in the console with no pure python package for sgp4 can be found. The problem is documented here (on the skyfield github) - https://github.com/skyfielders/python-skyfield/issues/1040#issuecomment-2675499472 Question:
  • is it possible that the packages are not being loaded in the order they are defined in the toml file ? (maybe its all async, maybe it should be resolved but micropip is not smart enough?)
Working sgp4 demo is here: https://pyscript.com/@neon22/sgp4-starter-pkg/latest?files=main.py,pyscript_py.toml Nonworking skyfield demo is here: https://pyscript.com/@neon22/skyfield-starter-pkg/latest?files=main.py,pyscript_py.toml
I was trying to build a pyscript example that would show local decl,azi for ISS, etc in the browser. Almost everything comes in but it can't find a python only whl on pypi for the sgp4 dependen...
Avatar
I have noticed that micropip has a --no-deps option. Invoked like this: micropip.install("pkg", deps=False) Is there a way to signal nodeps in the toml file ? That might allow me to load the skyfield package. (edited)
Avatar
Is it possible to make this work in pyscript to automate package installation https://youtu.be/XBlTunKsXJA?si=8t_78VSvib5UhimY
Avatar
cspotcode (Andrew B) 23/02/2025 14:31
When a python package, published to pypi, requires a JS shim to expose platform functionality in the browser:
  • how does that python package ship that .js file via pypi?
  • And how should its docs tell consumers to configure their pyscript toml to load that .js file? I understand js_modules.main and js_modules.worker accept URLs. But what is the URL to a .js file bundled in a pypi package?
I understand that pyscript supports plugins which must be loaded before python executes. But what if my pypi package wants to inject the .js script when it is imported? To be completely transparent to the consumer?
Avatar
Avatar
cspotcode (Andrew B)
When a python package, published to pypi, requires a JS shim to expose platform functionality in the browser:
  • how does that python package ship that .js file via pypi?
  • And how should its docs tell consumers to configure their pyscript toml to load that .js file? I understand js_modules.main and js_modules.worker accept URLs. But what is the URL to a .js file bundled in a pypi package?
I understand that pyscript supports plugins which must be loaded before python executes. But what if my pypi package wants to inject the .js script when it is imported? To be completely transparent to the consumer?
Andrea Giammarchi 24/02/2025 08:27
add that .js file (it should be a module) in the TOML as js_modules.main (or worker, or both) and import it before importing the package relying on its presence and that should be it?
Avatar
Hi, I'm trying to use an external API that requires authentication. The pyscript.com's API proxies work like a charm for POST requests, however, when I try to have a GET request with query parameters, the parameters seem to be lost. How can I ensure that query parameters from the Python GET requests reach the target external API when using the pyscript.com API proxies? (If I include the parameters in the API proxy's URL, the query works, but this only allows constant parameters to be used.)
Avatar
Avatar
Andrea Giammarchi
add that .js file (it should be a module) in the TOML as js_modules.main (or worker, or both) and import it before importing the package relying on its presence and that should be it?
cspotcode (Andrew B) 24/02/2025 14:02
How do I add the .js file in the TOML when it was bundled into the wheel from Pypi? What is the URL of a .js file inside of a wheel?
Avatar
why not make it a resource named pyscript.js and automatically scan for it / load it
Avatar
Avatar
cspotcode (Andrew B)
How do I add the .js file in the TOML when it was bundled into the wheel from Pypi? What is the URL of a .js file inside of a wheel?
Andrea Giammarchi 25/02/2025 15:48
right ... if the wheel from PyPy requires a JS file I am sure they have documentation around how to put that on your HTML page ... it's the same with pyscript and you'll reach that (I bet it's a global leak namespace) via window.thatGloballyLeakedNamespace and you can use it from there without needing anything in the TOML file, but it's an awkward indirection, our config doesn't provide a way to trash non-modules on the HTML so far so I am not sure how to do that but, like I've said the module runs when you import it, so you can import that .js file via your TOML, if defined, before importing your PyPy wheel module. That file can do everything you need ... that being said, if there was a minimalistic example of the issue you are trying to solve in ... let's say PyScript .com, I could surely help more with an example.
Avatar
Avatar
Andrea Giammarchi
right ... if the wheel from PyPy requires a JS file I am sure they have documentation around how to put that on your HTML page ... it's the same with pyscript and you'll reach that (I bet it's a global leak namespace) via window.thatGloballyLeakedNamespace and you can use it from there without needing anything in the TOML file, but it's an awkward indirection, our config doesn't provide a way to trash non-modules on the HTML so far so I am not sure how to do that but, like I've said the module runs when you import it, so you can import that .js file via your TOML, if defined, before importing your PyPy wheel module. That file can do everything you need ... that being said, if there was a minimalistic example of the issue you are trying to solve in ... let's say PyScript .com, I could surely help more with an example.
cspotcode (Andrew B) 25/02/2025 16:48
My perspective is as the author & publisher of this wheel, providing documentation for consumers of the wheel. I need to:
  • publish my wheel
    • with versioning. v2 .js is not compatible with v1 .py
  • provide documentation to end-users
    • a copy-paste config they can use
    What does that documentation look like?
How do other wheel authors handle this?
16:52
For example, perhaps the README for my wheel should say: To use libfoo in pyscript, you will need to declare the following in your pyscript.toml: packages = ["libfoo@2.0.1"] [js.modules] "https://cdn.pyscript.com/serve-files-from-wheels/pypi/libfoo@2.0.1/subpath/shim.js" = "libfooshim" What should that URL look like? Important characteristics:
  • the .js version number matches the wheel's version number
  • the .js is bundled within the wheel, no need to publish in two places
16:56
  • I realize it may be impossible to load a .js from within the wheel, but wouldn't it be great if that was possible? Because, in general, that's our goal with libraries:
The library's code, all internal details, are self-contained within the deliverable, and the library can load itself into the runtime environment correctly when imported
Avatar
@cspotcode (Andrew B) there are probably interesting "hacks" you could do. E.g. taking the .js code from the virtual filesystem and loading it into the DOM by manually creating a script tag with the content of your .js. That feels very dirty to me though.
Avatar
cspotcode (Andrew B) 25/02/2025 19:33
I mean, yeah, pyscript should have support for this use-case built-in. Like pmp-p suggested here https://discord.com/channels/972017612454232116/972020206538997822/1343620411241267241
19:34
Many of the solutions for injecting only feel dirty subjectively, they are not actually dirty. Just that our heuristics give us a gut feeling that we shouldn't, even though it's not actually bad.
19:35
You can imagine what pyscript's runtime is doing to load the js, for example
Avatar
Hi, have anyone know any browser for android which supports the file system api?
Avatar
Avatar
cspotcode (Andrew B)
You can imagine what pyscript's runtime is doing to load the js, for example
Andrea Giammarchi 26/02/2025 13:00
nothing dirty, just clean ESM
Avatar
Avatar
cspotcode (Andrew B)
For example, perhaps the README for my wheel should say: To use libfoo in pyscript, you will need to declare the following in your pyscript.toml: packages = ["libfoo@2.0.1"] [js.modules] "https://cdn.pyscript.com/serve-files-from-wheels/pypi/libfoo@2.0.1/subpath/shim.js" = "libfooshim" What should that URL look like? Important characteristics:
  • the .js version number matches the wheel's version number
  • the .js is bundled within the wheel, no need to publish in two places
Andrea Giammarchi 26/02/2025 13:05
that's how we basically use 3rd party and there's nothing wrong in there ... .js stuff gotta be published on .js places, .py and wheels stuff gotta be published on .py places ... so you publish your wheel and you publish on npm your wheel module ... you sync versioning between the two, you use one out of dozen CDN providers for your JS that ends up in the config. https://esm.run/ would be my suggestion as it's provided by JSDelivr which is likely the most used CDN out there on the web ... it automatically packages your stuff as ESM too so your config would look like: packages = ["libfoo@2.0.1"] [js_modules.main] "https://esm.run/libfoo@2.0.1" = "libfooshim" and ... that's it? ๐Ÿค” (edited)
Load modern JavaScript packages built for you on-demand. Works in modern web browsers, node.js, and deno.
Avatar
cspotcode (Andrew B) 26/02/2025 13:56
Ok thanks, that clears it up, the expectation is to publish .js components to a different location. Thanks!
Avatar
Avatar
Ridensium
Hi, have anyone know any browser for android which supports the file system api?
It's only likely to be very recent Chromium based browsers (if at all).
16:16
@Andrea Giammarchi @cspotcode (Andrew B) aha... so I like how this conversation went... we should include this advice in our docs. Will make sure we include it in the next release.
๐Ÿ‘ 1
Avatar
Andrea Giammarchi 26/02/2025 16:22
worth mentioning latest Pyodide allows registration of JS modules as Python modules, bypassing our requirement to import through pyscript.js_modules.libfooshim so that one could just import libfooshim and call it a day. I welcome that DX and I think we could provide the same to simplify even further Python users' expectations.
๐Ÿ‘ 1
Avatar
Avatar
Andrea Giammarchi
worth mentioning latest Pyodide allows registration of JS modules as Python modules, bypassing our requirement to import through pyscript.js_modules.libfooshim so that one could just import libfooshim and call it a day. I welcome that DX and I think we could provide the same to simplify even further Python users' expectations.
Andrea Giammarchi 26/02/2025 16:25
my gut tells me we could already do that by simply automating js_module dance as Pyodide dance but it conflicts with the main / worker disambiguation needed to know if a JS module should come from main or from the worker gobal context/scope itself ... yet there's room for improvements in our config, maybe worth discussing this next community call?
Avatar
Definitely
Avatar
Andrea Giammarchi 26/02/2025 16:26
I'll try to prepare a PoC then by next Tuesday
๐Ÿ‘ 1
16:30
@cspotcode (Andrew B) to summarize my thoughts ... this is the current state: packages = ["libfoo@2.0.1"] [js_modules.main] "https://esm.run/libfoo@2.0.1" = "libfooshim" from pyscript.js_modules import libfooshim this works well with PyScript projects but it won't scale by any mean elsewhere ... what I have in mind is (instead): packages = ["libfoo@2.0.1"] [py_modules.main] "https://esm.run/libfoo@2.0.1" = "libfooshim" import libfooshim so that your code doesn't have an indirect dependency of an outer scope/context/namespace it doesn't know, and that code could be more portable around ... /cc @ntoll
Avatar
cspotcode (Andrew B) 26/02/2025 16:31
It'd be great if my module, say funaudiolibrary, could do something like if is_in_browser: browser_shim = browser.load_js(location="worker", path=__file__ + "../jsAudioShim.js") So if a consumer of my module does import funaudiolibrary, and my library does that call to load the JS, the consumer doesn't need to think about the details
Avatar
Andrea Giammarchi 26/02/2025 16:32
that's possible already ... we have both py_imports and js_imports exposed as API to do runtime imports, you just need an await upfront
Avatar
OK... so I see what's happening here. Your library is for both browser and other platform. You want to be able to do both and have a route for browser configuration/setup depending on if the module is running in PyScript? ๐Ÿ‘
Avatar
Andrea Giammarchi 26/02/2025 16:33
apologies, that's js_import and py_import
Avatar
cspotcode (Andrew B) 26/02/2025 16:35
Ideally, yeah. For example, if someone wants to use pyglet (a rendering library, among other things) on Mac, Linux, Windows, there are platform-specific things happening behind the scenes, but the consumer (largely) doesn't need to know. pyglet.Window().run() will launch a GUI window on the platform, even though the underlying OS APIs might be totally different. pyglet abstracts that. It would be great if we could support the browser environment in the same way
Avatar
Andrea Giammarchi 26/02/2025 16:36
your library can then do: try: from pyscript import js_import libfooshim, = await js_import("https://esm.run/libfoo@2.0.1") except: libfooshim = None
Avatar
cspotcode (Andrew B) 26/02/2025 16:36
Sounds like js_import does fit the bill, with the await caveat, and the inability to bundle the .js in the wheel.
Avatar
Avatar
cspotcode (Andrew B)
Sounds like js_import does fit the bill, with the await caveat, and the inability to bundle the .js in the wheel.
Andrea Giammarchi 26/02/2025 16:37
I can provide guidance to make that possible via URL.createObjectURL and a base64 encoded version of your dependency that could land with the wheel itslef ... it's slightly convoluted but possible
16:38
in that case you'll use window.import(blob) instead but the await, I am afraid, it's a Web/JS modules limitations when these are not known/static upfront
Avatar
cspotcode (Andrew B) 26/02/2025 16:38
Thanks, I've done work with those blob APIs in the past, so I can do that.
Avatar
Andrea Giammarchi 26/02/2025 16:39
I can land SQLite as WASM blob with the same tehcnique, as example, so anyone could do the same with just JS modules
16:40
here you see why I suggested a config hook, as that's resolved before your code gets a chance to run, not while it's running
Avatar
cspotcode (Andrew B) 26/02/2025 16:40
pyscript supports synchronously importing python modules. It does this by ensuring they are pre-loaded into memory before your .py entrypoint executes, right? It'd be cool if a wheel could somehow declare its own .js component to be pre-loaded in a similar way. Is this a step too far, too complex? Maybe, but just thinking out loud
16:42
I can't find the config hook suggestion, but I think we're talking along the same lines (edited)
Avatar
Andrea Giammarchi 26/02/2025 16:42
a .js file in the Python VFS has no meaning for the Web ... you want to load that file to the browser, not just in your virtual Python WASM environment ... so that's a "yes, you can have JS files in the VFS" and a "meh, not much after you can do"
16:43
to clarify, dynamic import which is what you are asking is always async, there's no way around it
Avatar
cspotcode (Andrew B) 26/02/2025 16:43
Well, I can give it meaning to the web by asking the worker or main thread to execute that code The idea is just to avoid imposing a non-pythonic pattern upon consumers of my library
16:44
If pyscript is doing some async setup before my code runs, and it can do some async setup to load my wheel-bundled .js shim into the worker or main thread And if the consumers of my library can import funaudiolibrary and it works, that's cool!
Avatar
Andrea Giammarchi 26/02/2025 16:45
your code runs async in PyScript but the moment you land a dynamic JS import you either await it or you have zero guarantee your next Python code/line will have that landed ... so ... it's problematic, at least from the Web side point of view, but I strongly doubt even latest WASM efforts could solve that
Avatar
cspotcode (Andrew B) 26/02/2025 16:45
Suppose my wheel contains some static metadata that says "hey python runtimes in the browser, please load this .js into the worker ahead of time"
Avatar
Avatar
cspotcode (Andrew B)
Suppose my wheel contains some static metadata that says "hey python runtimes in the browser, please load this .js into the worker ahead of time"
Andrea Giammarchi 26/02/2025 16:46
that's like my config suggestion, it works, it requires orchestration if it comes from random modules though
Avatar
cspotcode (Andrew B) 26/02/2025 16:46
Nice, we are exactly on the same page: it would be nice if this was a supported standard
๐Ÿ‘ 1
Avatar
Andrea Giammarchi 26/02/2025 16:47
can you list how many projects out there give you a config able to land JS modules avaiable for Python to work out there? maybe we can get some inspiring solution ... but my gut says: none
Avatar
cspotcode (Andrew B) 26/02/2025 16:48
I can think of a couple interpretations of what you're asking, but not sure which one was your intention, can you rephrase?
Avatar
Andrea Giammarchi 26/02/2025 16:48
don't get me wrong, I think with Python usable in browsers your requirement for a wheel and dependencies related that breaks Python boundaries make perfect sense and I am trying to help finding the best solution out there ... (edited)
Avatar
cspotcode (Andrew B) 26/02/2025 16:49
I also acknowledge that, perhaps, we will conclude this idea is hypothetically possible but the level of effort is not warranted. But I still think it's valuable to think through the design
Avatar
Andrea Giammarchi 26/02/2025 16:49
I don't have in mind that best solution, in a universal way, I have in mind what could work out of our config features
Avatar
Avatar
cspotcode (Andrew B)
I also acknowledge that, perhaps, we will conclude this idea is hypothetically possible but the level of effort is not warranted. But I still think it's valuable to think through the design
Andrea Giammarchi 26/02/2025 16:50
nope, I think any effort to make this possible is a way forward for Python on the Web, being that PyScript or anything else ... but it requires coordination with all pieces of the puzzles, including perhaps micropip (edited)
16:51
or pypy repo
16:52
or pip ... well ... you got my thinking, I am sure about it!
Avatar
cspotcode (Andrew B) 26/02/2025 16:55
Well, and I wonder: if these tools offer the runtime a way to iterate the list of dependencies and read their contents, maybe they don't require code changes to allow pyscript to prototype a solution. Pyscript bootstrapping might look something like this: 1. Ask micropip/pypy/pip to fetch a list of dependencies 2. Load them all into the VFS 3. For each dep in VFS:
  • if exists(dep.path + "/browser_env.json"): await load_vfs_js_into_environment(parse_json(dep.path + "/browser_env.json").shimPath)
4. execute user's entrypoint .py EDIT clarity
(edited)
16:56
So the wheel author needs to include a browser_env.json manifest in their wheel, but micropip/pypy/etc don't need to understand it
Avatar
Andrea Giammarchi 26/02/2025 16:59
that's why we're discussing this ... and I like it ... please tell me more... that manifest has explicit JS dependencies, are those modules? global leaks? (unfortunately still a common use case) ... what's the field in there and how does it look like? I think this would be a great conversation out of our repo discussions space though, I don't feel like bothering everyone else around this, as it won't be solved in a couple of back and forward, it will take time to settle in a shippable way. (edited)
17:00
in short, I think we're creating a standard and Discord is not the best place to do so (from experience ๐Ÿ˜…)
Avatar
cspotcode (Andrew B) 26/02/2025 17:00
haha fair point, are you suggesting we move conversation to a github issue, or to a discord sub-thread to avoid spamming everyone?
Avatar
Andrea Giammarchi 26/02/2025 17:03
After a long discussion on our Discord channel I feel like we need to provide broader context and an easier way to discuss this forward, so here the discussion around that topic.
Avatar
Andrea Giammarchi 26/02/2025 17:11
@cspotcode (Andrew B) I've summarized curent state of discussion adding my further thoughts around something that will scale more in case this needs to be filed as standard proposal or to the pip community: https://github.com/pyscript/pyscript/discussions/2301#discussioncomment-12328770
After a long discussion on our Discord channel I feel like we need to provide broader context and an easier way to discuss this forward, so here the discussion around that topic.
Avatar
cspotcode (Andrew B) 26/02/2025 17:14
Thanks! I wrote up a strawman of the browser_env.json manifest and how it might work for funaudiolibrary
Avatar
Avatar
ntoll
It's only likely to be very recent Chromium based browsers (if at all).
For mac/windows/linux indeed; yet for android tested some without success (the folder shows in the tree in python but without contents); for ios/ipados since 2024 apple allows browsers with thirdparty engines but its still to early to expect one. Checked in caniuse and none of the common android browsers support it, so was curious if any not so popular is out there and can do the job.
๐Ÿ‘ 1
Avatar
Eruvanos [UTC+1] 27/02/2025 15:54
I am testing out puepy, but wonder how to fetch data from an api. I tried import js # in some component js.setTimer(self._load_data, 0) Not sure if it is the right way at all, but I have no other idea.
Eruvanos [UTC+1] started a thread. 27/02/2025 20:57
Avatar
How to make POST request without Proxy. It is working only when I use proxy. Else, I am getting CORS error. Any help?
Avatar
Avatar
magulan30
How to make POST request without Proxy. It is working only when I use proxy. Else, I am getting CORS error. Any help?
Not sure I know what you're getting at but maybe you need mini-coi.js to get around cors problem?? https://docs.pyscript.net/2025.2.4/user-guide/workers/#option-1-mini-coi
Avatar
Chris Laffra 28/02/2025 10:01
I am running into this PyOdide issue and have not been able to find out yet where I am missing the create_proxy call. How do I set pyodide.setDebug(true)? pyodide.asm.js:10 Uncaught Error: This borrowed proxy was automatically destroyed at the end of a function call. Try using create_proxy or create_once_callable. For more information about the cause of this error, use pyodide.setDebug(true)
Avatar
Avatar
Chris Laffra
I am running into this PyOdide issue and have not been able to find out yet where I am missing the create_proxy call. How do I set pyodide.setDebug(true)? pyodide.asm.js:10 Uncaught Error: This borrowed proxy was automatically destroyed at the end of a function call. Try using create_proxy or create_once_callable. For more information about the cause of this error, use pyodide.setDebug(true)
Andrea Giammarchi 28/02/2025 10:25
could you try to add experimental_create_proxy = "auto" in your config and tell me if that solved? not an answer to your question but curious to know if "we" can make that error disappear
Avatar
Avatar
Andrea Giammarchi
could you try to add experimental_create_proxy = "auto" in your config and tell me if that solved? not an answer to your question but curious to know if "we" can make that error disappear
Andrea Giammarchi 28/02/2025 10:28
about setDebug(true) we don't have a config value for that (we probably should, right @ntoll ?) but you could try to: from _pyscript import interpreter interpreter.setDegug(True) and see if that works
Avatar
Avatar
Andrea Giammarchi
could you try to add experimental_create_proxy = "auto" in your config and tell me if that solved? not an answer to your question but curious to know if "we" can make that error disappear
Chris Laffra 28/02/2025 10:28
I tried that before. Just tried it again (on my main and on my worker) and the same error occurs. So this flag does not do much?
Avatar
Avatar
Chris Laffra
I tried that before. Just tried it again (on my main and on my worker) and the same error occurs. So this flag does not do much?
Andrea Giammarchi 28/02/2025 10:29
it does a lot but not everything ... let me know if the other workaround helps you, thanks
Avatar
+1 on setDebug if MicroPython has a story for it. I mean, what does setting that flag even do?
Avatar
Avatar
ntoll
+1 on setDebug if MicroPython has a story for it. I mean, what does setting that flag even do?
Andrea Giammarchi 28/02/2025 10:30
in MicroPython I think it does nothing? never tried though
10:31
yeah, it doesn't exist in MicroPython
Avatar
Chris Laffra 28/02/2025 10:31
The setDebug works! It now actually tells me what function it happened to. "<function check_version.<locals>.report_version at 0x1090b40>
๐Ÿฅณ 1
10:33
And the elusive error that has annoyed me for months is now gone ๐Ÿค“
๐Ÿ‘ 1
Avatar
Bravo @Chris Laffra - take those small wins! ๐Ÿ˜‰
Avatar
Andrea Giammarchi 28/02/2025 10:36
I am not super confident in exposing the interepter as disasters might happen but it's great to hear advanced users might take the risk to reach it out ... although I think a debug = true in the config should be allowed for Pyodide use cases and maybe be ignored in MicroPython as it doesn't exist
10:37
I am also nearly sure we used to have it in PyScript Classic ... or so I remember
Avatar
Chris Laffra 28/02/2025 10:46
I have another issue, this one is on MicroPython. This happens when you accidentally try to append a Python (LTK) object with jquery, instead of its DOM element. Somehow this works on PyOdide, but on MicroPython you get this cryptic error. Makes me wonder: What is a "symbol"? Any ideas how to get a Python stack to show the Python function that causes this?
Avatar
Error messages (due to the "micro" nature of MicroPython) are very small. I don't think you get the stack trace. My advice for MP things is to head over to the MP site or discord and ask?
10:49
Here's the thing. You and I are using MicroPython in an atypical (at the moment) situation for MicroPython. It was created for microcontrollers and hardware devs who work in a different way to perhaps we do.
Avatar
Chris Laffra 28/02/2025 10:49
I tried joining the MicroPython Discord ๐Ÿค“
10:50
I just sent you an invite @Chris Laffra to the webassembly channel on the MP server.
๐Ÿ‘ 1
Avatar
Avatar
Chris Laffra
I have another issue, this one is on MicroPython. This happens when you accidentally try to append a Python (LTK) object with jquery, instead of its DOM element. Somehow this works on PyOdide, but on MicroPython you get this cryptic error. Makes me wonder: What is a "symbol"? Any ideas how to get a Python stack to show the Python function that causes this?
Andrea Giammarchi 28/02/2025 11:22
a symbol is a JavaScript primitive that I think cannot be converted in MicroPython, hence the error ... could you create a minimal example of that failure? maybe I could help out there
11:24
in the whole Pyodide documentation (around translation) it's only mentioned as Symbol.iterator which is a special symbol reached out to iterate ... maybe your string conversion is iterating chars instead? https://pyodide.org/en/stable/usage/type-conversions.html (edited)
Avatar
Avatar
Andrea Giammarchi
a symbol is a JavaScript primitive that I think cannot be converted in MicroPython, hence the error ... could you create a minimal example of that failure? maybe I could help out there
Chris Laffra 28/02/2025 11:59
12:00
Note that I am doing all kinds of non-standard stuff here, even more than @ntoll may think ๐Ÿค“
12:02
Write browser apps entirely in Python. Contribute to pyscript/ltk development by creating an account on GitHub.
Avatar
Andrea Giammarchi 28/02/2025 13:46
I have a 404 there?
Avatar
Avatar
Chris Laffra
Andrea Giammarchi 28/02/2025 13:48
maybe adding a __getitem__ too would solve?
Avatar
Avatar
Andrea Giammarchi
I have a 404 there?
Chris Laffra 28/02/2025 14:06
Avatar
trying to use pyscript on a google site so I can implement a python text game and it wont allow input. really want to see if I can get this to work?
Avatar
Avatar
connie
trying to use pyscript on a google site so I can implement a python text game and it wont allow input. really want to see if I can get this to work?
Andrea Giammarchi 28/02/2025 16:18
I think not yet ... you either need a worker with correct headers set or you need to wait for me to investigate the latest Pyodide feature that allows non blocking things behind the scene
Avatar
Andrea Giammarchi 28/02/2025 16:27
I think there's some magic Pyodide does when it receives a Python object through a JS DOM API ... that doesn't work if you use worker attribute, simply because we're also not transforming Python objects to DOM elements ... I am not sure this is a bug, it's either a unique feature Pyodide has but it requires internal work for MicroPython to be compatible with that feature (and I have no idea what Pyodide does to convert Python objects to DOM elements) and we would need to understand that intent from a worker where references just travel around as these are without us magic-guessing what to do in there to make those Python reference automagically become DOM elements. In pyscript.web we created a layer that deals with that out of the box but in your example you are really invoking native JS functions and there is no specification about what these should do when a foreign/non element object is appended, throwing feels like actually the expected behavior, the Pyodide result is surprising to me.
Avatar
Avatar
Andrea Giammarchi
I think there's some magic Pyodide does when it receives a Python object through a JS DOM API ... that doesn't work if you use worker attribute, simply because we're also not transforming Python objects to DOM elements ... I am not sure this is a bug, it's either a unique feature Pyodide has but it requires internal work for MicroPython to be compatible with that feature (and I have no idea what Pyodide does to convert Python objects to DOM elements) and we would need to understand that intent from a worker where references just travel around as these are without us magic-guessing what to do in there to make those Python reference automagically become DOM elements. In pyscript.web we created a layer that deals with that out of the box but in your example you are really invoking native JS functions and there is no specification about what these should do when a foreign/non element object is appended, throwing feels like actually the expected behavior, the Pyodide result is surprising to me.
Andrea Giammarchi 28/02/2025 16:29
what is it that let you define a Python class / reference a suitable DOM element behind the scene? If you use some magic getter that is implicitly invoked maybe we can try to see if that's the case for us too but keep in mind whatever you are using is likely good to just append string content, not to append random nodes in the wild (but I haven't checked that yet).
Avatar
Avatar
Andrea Giammarchi
what is it that let you define a Python class / reference a suitable DOM element behind the scene? If you use some magic getter that is implicitly invoked maybe we can try to see if that's the case for us too but keep in mind whatever you are using is likely good to just append string content, not to append random nodes in the wild (but I haven't checked that yet).
Andrea Giammarchi 28/02/2025 16:31
for instance, I think if you replace Text with any other outer component/container you'll have a similar issue all over the place, example try to pass a <div> that contains two <hr> in it and see what happens ... so this issue is actually not really an issue to me, just expectations a bit far off in terms of interoperability, and only Text might work because maybe some symbol is retrieved as last resort but symbols cannot travel between worker and main so it's a no-go no matter how you look at that, while passing nodes explicitly will always work. (edited)
Avatar
Avatar
Andrea Giammarchi
for instance, I think if you replace Text with any other outer component/container you'll have a similar issue all over the place, example try to pass a <div> that contains two <hr> in it and see what happens ... so this issue is actually not really an issue to me, just expectations a bit far off in terms of interoperability, and only Text might work because maybe some symbol is retrieved as last resort but symbols cannot travel between worker and main so it's a no-go no matter how you look at that, while passing nodes explicitly will always work. (edited)
Andrea Giammarchi 28/02/2025 16:32
I hope this answer makes sense ... but I can see pyodide dealing with known symbols that could produce some text to body.append but that cannot possibly work out of workers and it's all Pyodide internals at play there, imho.
Avatar
Hi, just found this project yesterday and I have to ask: Is there any way to compile (just to something like to pyc, I'm not expecting something like nuitka) python code used in webpage? I was hopping to include this as some kind of minification (the obfuscation isn't necessary, but possibly useful in the long run)
15:51
Could pyscript already run a pyc file in place of python code?
Avatar
py (without docstrings) often compresses better than pyc and certainly have better compat for future releases, mypyc or nuitka will be bigger since they unwrap a lot of things
Avatar
Avatar
Whacka!
Could pyscript already run a pyc file in place of python code?
its running in wasm so maybe not. chris @Chris Laffra did some work on minification for obfuscation earlier. try searching the discord
Avatar
Hey can anyone help me with this: Openscad has a wasm version: https://github.com/openscad/openscad-wasm/releases Three files - [openscad.worker.wasm, openscad.js, openscad.worker.js] How do I persuade the toml/index files to let me import these ?
Webassembly port of OpenSCAD. Contribute to openscad/openscad-wasm development by creating an account on GitHub.
Avatar
Soupersoop 08/03/2025 00:15
I've been breaking my back to try and get a game of mine that I was previously just making in Python to run in Pyscript, but getting Pygame imported has been a huge pain. I've tried <script type="py-game"></script>, but it would only load the file specified in the src attribute. So far, I've found the most progress through this in the Python file where Pygame would've been imported originally, however I've still been running into issues. Surely there has to be a way to run Pygame and import from multiple files, right?
Avatar
Avatar
Soupersoop
I've been breaking my back to try and get a game of mine that I was previously just making in Python to run in Pyscript, but getting Pygame imported has been a huge pain. I've tried <script type="py-game"></script>, but it would only load the file specified in the src attribute. So far, I've found the most progress through this in the Python file where Pygame would've been imported originally, however I've still been running into issues. Surely there has to be a way to run Pygame and import from multiple files, right?
do you have a linkable page - maybe on pyscript.com ?
Avatar
ok. so the pyscript devs made a special version which is designed for pygame. You're trying to do that process on your own directly. I am sure its possible but you might want to start by using the specially designed mechanism. Docs for that are here: - https://docs.pyscript.net/2025.2.4/user-guide/pygame-ce/ so the index.html calls a special version.
09:09
Josh made a sample which you might want to take a look at too for direct comparison. (its missing a few things but basically there: https://discord.com/channels/972017612454232116/972017612454232119/1336839097637015584
Avatar
OK. You got me there. Something is not working and I don't understand it. the toml file is not enabling the [files] tag. (or I just cannot see what is plainly wrong.)
09:54
My version of your pygame code is here. but it just won't import the (renamed) objectsAA.py file as defined in the toml. Complains about async and importing packages. - https://pyscript.com/@neon22/tsa-video-game-copy/latest?files=main.py,pyscript.toml I even tried going back to [fetch] in the toml and no luck... Any ideas anyone ??
Avatar
Hmm. The path seems to be set to '/home/pyodide'. Not sure how to resolve the filepath to the top level directory.
10:09
Its not as simple as "../../objects.py' = '' No idea how to reach it. @Andrea Giammarchi ??
Avatar
On testing device (slow cpu android) mpy loads 20 times faster compared to pyodide and has 20 times slower performance. I know the approach to use mpy for gui and py in a worker but syncing data takes lots of time as its up to few mb. So thinking for approach first to load mpy then py and latter not in worker but with same code and to overtake and delete the mpy one. Has anyone tried another ways to improve loading as well performace? (edited)
Neon22 started a thread. 08/03/2025 20:19
Avatar
it works for me https://pmp-p.ddns.net/pygbag/tests/undervurld/build/web/?-i but i'm quite sure the pybag's tricks won't work out of the box on pyscript's pyodide. because your use case is non standard : async input in terminal then switching to game canvas. If you make it in pyscript maybe use a pygame_gui widget for input or some html code around the canvas
08:11
also be aware that wasd won't be mapped correctly on non english keyboards
Avatar
Avatar
Neon22
Its not as simple as "../../objects.py' = '' No idea how to reach it. @Andrea Giammarchi ??
Andrea Giammarchi 09/03/2025 16:45
currently, py-game supports only packages, not files entries in the config ... I did see that coming, will work on a fix tomorrow and publish to fix this issue. I'll file an issue in the meantime.
Avatar
Avatar
Andrea Giammarchi
currently, py-game supports only packages, not files entries in the config ... I did see that coming, will work on a fix tomorrow and publish to fix this issue. I'll file an issue in the meantime.
Andrea Giammarchi 09/03/2025 16:49
issue here ... will try to apply changes ASAP but this is why we wanted people to play around, so we can fine-tune what's missing ... on the other hand, py-game is currently an entirely different "beast" from just py in a similar way mpy is different from py and py-editor is different from py too: our bad not empathasing that enough ... issue fled, at least files should work or assets and user-land modules can't work in there, apologies: https://github.com/pyscript/pyscript/issues/2309
What happened? The current config resolution for py-game is not fully aligned with what PyScript or py scripts can do and [files] is one one of those things that are not resolved. We should provide...
Avatar
Avatar
Andrea Giammarchi
issue here ... will try to apply changes ASAP but this is why we wanted people to play around, so we can fine-tune what's missing ... on the other hand, py-game is currently an entirely different "beast" from just py in a similar way mpy is different from py and py-editor is different from py too: our bad not empathasing that enough ... issue fled, at least files should work or assets and user-land modules can't work in there, apologies: https://github.com/pyscript/pyscript/issues/2309
Andrea Giammarchi 09/03/2025 16:59
why is it different? ... thanks for asking:
  • you might want both py and mpy plus py-game on the same page ... because py-game right now is a main-thread only affair, we wanted to decouple the contract about having only one py or mpy on the main thread, when worker attribute is not there (and py-game cannot run in workers right now)
  • similar to py-editor, the py-game script type has a different goal, scope, constraints, and implicit features ... it's OK to want py features in there, we just need to figure out if all of them are possible or not. [files] is one of those that just weren't fully considered but I don't see why these shouldn't be in there
  • because it's a different "parsing goal", other features such as interpreter or experimental features might just not be there at this time, yet again [files] should be there and I'll work on it to make it happen
If you have any other question, I'd be more than happy to answer, so far I feel sorry for not thinking about that obvious part of the equation to make py-game easier to use and develop for.
Avatar
Hi, I am using PYSCRIPT with <script type="py-editor"> tags to place fields on my site where visitors can enter and execute Python code. The default behavior is that if the user clicks the RUN button multiple times, the return replaces the previous return. How can I make sure that each time the user clicks RUN, the return is added below the previous ones? Also, how can I add a small "Clear this return" button for each of them? Thanks for your help!
Avatar
Avatar
Andrea Giammarchi
why is it different? ... thanks for asking:
  • you might want both py and mpy plus py-game on the same page ... because py-game right now is a main-thread only affair, we wanted to decouple the contract about having only one py or mpy on the main thread, when worker attribute is not there (and py-game cannot run in workers right now)
  • similar to py-editor, the py-game script type has a different goal, scope, constraints, and implicit features ... it's OK to want py features in there, we just need to figure out if all of them are possible or not. [files] is one of those that just weren't fully considered but I don't see why these shouldn't be in there
  • because it's a different "parsing goal", other features such as interpreter or experimental features might just not be there at this time, yet again [files] should be there and I'll work on it to make it happen
If you have any other question, I'd be more than happy to answer, so far I feel sorry for not thinking about that obvious part of the equation to make py-game easier to use and develop for.
Andrea Giammarchi 09/03/2025 17:08
Some made-up F.A.Q:
  • could py-game just be a config flag? Yes, it could be, but it has to be a dedicated config flag or no other py scripts can run on the same main thread at the moment
  • why is not py-game just like py with same capabilities? Because we need to disambiguate between an env meant to stay on the page (that's py) and an env meant to be created at runtime on demand and with multiple possible visual targets (that's py-game) ... on top of that, we're trying to figure out if py-game should be coupled entirely with pygame-ce package, and the current logic is trying to suggest: it shouldn't!
  • will things be better in the near future? Yes, that's currently our promise and initial intent, among tons of discussions, about how py-game should land and be used ... anyone with an issue is helping us shaping that future and because it's a dedicated target, we can also play the non-breaking card while developing a better all-in-one solution
Thanks for your patience, still here to answer possible extra questions ๐Ÿ‘‹
Avatar
Avatar
TR
Hi, I am using PYSCRIPT with <script type="py-editor"> tags to place fields on my site where visitors can enter and execute Python code. The default behavior is that if the user clicks the RUN button multiple times, the return replaces the previous return. How can I make sure that each time the user clicks RUN, the return is added below the previous ones? Also, how can I add a small "Clear this return" button for each of them? Thanks for your help!
Andrea Giammarchi 09/03/2025 17:12
py-editor appends output the same way your console would do when you run a program ... it just keeps adding output. The idea of confined outputs that could be replaced or pre-pended on next run is interesting though, but I am afraid unless you have some slightly more advanced JS/DOM skills and the will to interfere with the current behavior, that's not backed in yet ... it's still a valid use case, imho, we never had a request for that though so we never planned a way to give you a way to either drop a RUN outcome once live or prepend every other run.
Avatar
Avatar
Andrea Giammarchi
py-editor appends output the same way your console would do when you run a program ... it just keeps adding output. The idea of confined outputs that could be replaced or pre-pended on next run is interesting though, but I am afraid unless you have some slightly more advanced JS/DOM skills and the will to interfere with the current behavior, that's not backed in yet ... it's still a valid use case, imho, we never had a request for that though so we never planned a way to give you a way to either drop a RUN outcome once live or prepend every other run.
Thank you for your reply! But I'm surprised: your answer seems to say that in the default behavior, outputs are added to previous ones (which is exactly what I'd like), whereas on the contrary, what I'm seeing is that every time a python cell is executed, it replaces the previous output!
17:19
(sorry, maybe I misunderstood something in your answer, since English is not my native language)
Avatar
I also have another problem: I can't use matplotlib. When I try this : <script type="py-editor" env="seance1" config='{"packages":["numpy", "matplotlib"]}'> import numpy as np import matplotlib.pyplot as plt xpoints = np.array([1, 8]) ypoints = np.array([3, 10]) plt.plot(xpoints, ypoints) plt.show() </script> I get this error in output:
19:40
`Matplotlib is building the font cache; this may take a moment. Error: Traceback (most recent call last): File "/lib/python312.zip/_pyodide/_base.py", line 597, in eval_code_async await CodeRunner( File "/lib/python312.zip/_pyodide/_base.py", line 411, in run_async coroutine = eval(self.code, globals, locals) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "", line 7, in File "/lib/python3.12/site-packages/matplotlib/pyplot.py", line 3590, in plot return gca().plot( ^^^^^ File "/lib/python3.12/site-packages/matplotlib/pyplot.py", line 2540, in gca return gcf().gca() ^^^^^ File "/lib/python3.12/site-packages/matplotlib/pyplot.py", line 1000, in gcf return figure() ^^^^^^^^ File "/lib/python3.12/site-packages/matplotlib/pyplot.py", line 934, in figure manager = new_figure_manager( ^^^^^^^^^^^^^^^^^^^ File "/lib/python3.12/site-packages/matplotlib/pyplot.py", line 464, in new_figure_manager _warn_if_gui_out_of_main_thread() File "/lib/python3.12/site-packages/matplotlib/pyplot.py", line 441, in _warn_if_gui_out_of_main_thread canvas_class = cast(type[FigureCanvasBase], _get_backend_mod().FigureCanvas) ^^^^^^^^^^^^^^^^^^
19:40
File "/lib/python3.12/site-packages/matplotlib/pyplot.py", line 280, in getbackend_mod switch_backend(rcParams._get("backend")) # type: ignore[attr-defined] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/lib/python3.12/site-packages/matplotlib/pyplot.py", line 342, in switch_backend module = importlib.import_module(cbook._backend_module_name(newbackend)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/lib/python312.zip/importlib/__init.py", line 90, in import_module return _bootstrap._gcd_import(name[level:], package, level) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "", line 1387, in _gcd_import File "", line 1360, in _find_and_load File "", line 1331, in _find_and_load_unlocked File "", line 935, in _load_unlocked File "", line 995, in exec_module File "", line 488, in _call_with_frames_removed File "/lib/python3.12/site-packages/matplotlib_pyodide/wasm_backend.py", line 18, in from js import ImageData, document ImportError: cannot import name 'document' from 'js' (unknown location)`
Avatar
I also tried this: import numpy as np import matplotlib.pyplot as plt xpoints = np.array([1, 8]) ypoints = np.array([3, 10]) fig, ax = plt.subplots() plt.plot(xpoints, ypoints) display(fig) but I still have the long error message in output.
Avatar
Avatar
TR
Thank you for your reply! But I'm surprised: your answer seems to say that in the default behavior, outputs are added to previous ones (which is exactly what I'd like), whereas on the contrary, what I'm seeing is that every time a python cell is executed, it replaces the previous output!
Andrea Giammarchi 09/03/2025 19:55
I meant if you print stuff gets appended ... next run do you want the previous prints to be cleared or not? this is probably what I am not understanding.
Avatar
Avatar
Andrea Giammarchi
I meant if you print stuff gets appended ... next run do you want the previous prints to be cleared or not? this is probably what I am not understanding.
At the next run I'd like the output not to replace the one that was returned previously, but rather for the new output to be added to the previous one. (And I'd also like to be able to add a little button to delete each output individually... but that's a next step I suppose! ๐Ÿ˜… )
20:04
(Thanks a lot in advance for your help Andrea!!!!! ๐Ÿซถ )
Avatar
Avatar
TR
At the next run I'd like the output not to replace the one that was returned previously, but rather for the new output to be added to the previous one. (And I'd also like to be able to add a little button to delete each output individually... but that's a next step I suppose! ๐Ÿ˜… )
Andrea Giammarchi 09/03/2025 20:06
yeah, we never had a request like that and it's not clear what's the advantage or the feature in keeping potentially very long answers on the DOM as those will be likely different from the next run ... maybe we should think something backward, as in: "keep this result in the dom and run next thing before or after that" ???
Avatar
Avatar
TR
I also tried this: import numpy as np import matplotlib.pyplot as plt xpoints = np.array([1, 8]) ypoints = np.array([3, 10]) fig, ax = plt.subplots() plt.plot(xpoints, ypoints) display(fig) but I still have the long error message in output.
Andrea Giammarchi 09/03/2025 20:07
I've tried this and it works without issues: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="https://pyscript.net/releases/2025.2.4/core.css"> <script type="module" src="https://pyscript.net/releases/2025.2.4/core.js"></script> </head> <body> <script type="py-editor" env="seance1" config='{"packages":["numpy", "matplotlib"]}'> import numpy as np import matplotlib.pyplot as plt xpoints = np.array([1, 8]) ypoints = np.array([3, 10]) fig, ax = plt.subplots() plt.plot(xpoints, ypoints) from pyscript import display display(fig, target="outcome") </script> <div id="outcome"></div> </body> </html> of course you need special headers to have workers fully working out of py-editor or not much could happen in there, if you require document access ... that's all covered by our docs: https://docs.pyscript.net/2025.2.4/user-guide/workers/
Avatar
( In fact, to tell you the whole story: I was already behaving this way on my site, up until a year ago. And this year, I was informed that my site was no longer working. I think I was using an old version of PYSCRIPT. So I tried to update my site by loading the latest version of PYSCRIPT. I also had to change the way I displayed python cells, because before I had this : <py-repl output=โ€œresultat3โ€> CODE HERE </py-repl> <div style=โ€œtext-align:centerโ€ id=โ€œresultat3โ€></div> And now I've had to replace it with : <script type=โ€œpy-editorโ€ env=โ€œseance1โ€ config='{โ€œpackagesโ€:[โ€œnumpyโ€, โ€œmatplotlibโ€]}'> CODE HERE </script> Except that now the ouputs are replaced each time when RUN is clicked again, whereas previously they were added one after the other, which was a behavior that suited me fine.)
Avatar
Avatar
TR
( In fact, to tell you the whole story: I was already behaving this way on my site, up until a year ago. And this year, I was informed that my site was no longer working. I think I was using an old version of PYSCRIPT. So I tried to update my site by loading the latest version of PYSCRIPT. I also had to change the way I displayed python cells, because before I had this : <py-repl output=โ€œresultat3โ€> CODE HERE </py-repl> <div style=โ€œtext-align:centerโ€ id=โ€œresultat3โ€></div> And now I've had to replace it with : <script type=โ€œpy-editorโ€ env=โ€œseance1โ€ config='{โ€œpackagesโ€:[โ€œnumpyโ€, โ€œmatplotlibโ€]}'> CODE HERE </script> Except that now the ouputs are replaced each time when RUN is clicked again, whereas previously they were added one after the other, which was a behavior that suited me fine.)
Andrea Giammarchi 09/03/2025 20:13
the scroll-back to run again or scroll-forward to see results feels like a very bad UX to me but sorry we broke that in the meanwhile, it was for (imho) good.
20:15
or better, nobody complained so far while using our latest about that ... so it's a hard-sell to me it was better before, we have real-world use cases that would beg otherwise at this point, but happy to think and talk about alternatives to bring that behavior back, if more than a user needs that (and more convoluted logic is needed on our side around it) (edited)
20:17
to me is like using clear in a Linux terminal ... when I run same program twice, I'm likely never interested in reading the previous results, even less interested to scrol every time further up to re-read the program I am running, but that might be "just me" although so far it was a shared feeling among others using py-editor too. (edited)
Avatar
Avatar
Andrea Giammarchi
I've tried this and it works without issues: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="https://pyscript.net/releases/2025.2.4/core.css"> <script type="module" src="https://pyscript.net/releases/2025.2.4/core.js"></script> </head> <body> <script type="py-editor" env="seance1" config='{"packages":["numpy", "matplotlib"]}'> import numpy as np import matplotlib.pyplot as plt xpoints = np.array([1, 8]) ypoints = np.array([3, 10]) fig, ax = plt.subplots() plt.plot(xpoints, ypoints) from pyscript import display display(fig, target="outcome") </script> <div id="outcome"></div> </body> </html> of course you need special headers to have workers fully working out of py-editor or not much could happen in there, if you require document access ... that's all covered by our docs: https://docs.pyscript.net/2025.2.4/user-guide/workers/
I've just tried your code on a new blank HTML page. At first it didn't work (still the long red error message), but after putting <script src=โ€œhttps://physique-pcsi.prepa-balzac.fr/statique/mini-coi.jsโ€></script> between the <head> tags it works perfectly, thanks! The best part is that it has exactly the behavior I was hoping for: each time RUN is clicked the new graphic is added after the old one instead of replacing it, so it's perfect!!!! ๐Ÿ‘ BUT UNFORTUNATELY, when I try to put this on my main site, matplotlib doesn't work anymore ๐Ÿ˜ฆ : I get the long red error message again. I did put <script src=โ€œhttps://physique-pcsi.prepa-balzac.fr/statique/mini-coi.jsโ€></script> in the html header. Here's the actual destination where it doesn't work: https://physique-pcsi.prepa-balzac.fr/numerique/equadiffs.php (you can type anything into the javascript prompt that appears when the page starts). Here's the โ€œnakedโ€ page where it works fine: https://physique-pcsi.prepa-balzac.fr/statique/bacasable.html (edited)
Avatar
Avatar
TR
I've just tried your code on a new blank HTML page. At first it didn't work (still the long red error message), but after putting <script src=โ€œhttps://physique-pcsi.prepa-balzac.fr/statique/mini-coi.jsโ€></script> between the <head> tags it works perfectly, thanks! The best part is that it has exactly the behavior I was hoping for: each time RUN is clicked the new graphic is added after the old one instead of replacing it, so it's perfect!!!! ๐Ÿ‘ BUT UNFORTUNATELY, when I try to put this on my main site, matplotlib doesn't work anymore ๐Ÿ˜ฆ : I get the long red error message again. I did put <script src=โ€œhttps://physique-pcsi.prepa-balzac.fr/statique/mini-coi.jsโ€></script> in the html header. Here's the actual destination where it doesn't work: https://physique-pcsi.prepa-balzac.fr/numerique/equadiffs.php (you can type anything into the javascript prompt that appears when the page starts). Here's the โ€œnakedโ€ page where it works fine: https://physique-pcsi.prepa-balzac.fr/statique/bacasable.html (edited)
Andrea Giammarchi 10/03/2025 07:38
you need to move that mini-coi.js file as static asset at the root of your site, you cannot have remote service workers by specs.
Avatar
Avatar
Andrea Giammarchi
you need to move that mini-coi.js file as static asset at the root of your site, you cannot have remote service workers by specs.
Andrea Giammarchi 10/03/2025 07:39
Avatar
Avatar
Andrea Giammarchi
Andrea Giammarchi 10/03/2025 07:41
that means that both numerique and statique folders must contain a mini-coi.js or you can put mini-coi.js at the root of your site and use it when needed in pages via <script src="/mini-coi.js"></script> and all pages would equally work.
07:42
in short: don't use absolute URLs for mini-coi because Service Worker require the service worker file to be at the same folder or any parent folder to work correctly, it cannot be in another subfolder that is parallel to your one ... it's about folder tree and ownership that must be confined.
Avatar
Avatar
Andrea Giammarchi
issue here ... will try to apply changes ASAP but this is why we wanted people to play around, so we can fine-tune what's missing ... on the other hand, py-game is currently an entirely different "beast" from just py in a similar way mpy is different from py and py-editor is different from py too: our bad not empathasing that enough ... issue fled, at least files should work or assets and user-land modules can't work in there, apologies: https://github.com/pyscript/pyscript/issues/2309
Andrea Giammarchi 10/03/2025 12:18
it's fixed in https://cdn.jsdelivr.net/npm/@pyscript/core@0.6.37/dist/core.js which you can try already until we release ... all config things should just work out of the box now, and yes ... there were issues even with just packages before, now it should all be good, please let me know if that's not the case, thank you! (edited)
Avatar
Avatar
Andrea Giammarchi
it's fixed in https://cdn.jsdelivr.net/npm/@pyscript/core@0.6.37/dist/core.js which you can try already until we release ... all config things should just work out of the box now, and yes ... there were issues even with just packages before, now it should all be good, please let me know if that's not the case, thank you! (edited)
Andrea Giammarchi 10/03/2025 15:31
amend ... it's 0.6.39 now (edited)
Neon22 started a thread. 10/03/2025 20:27
Avatar
Hey Soupersoop - its working - see the thread and clone it.
Avatar
Avatar
Soupersoop
I've been breaking my back to try and get a game of mine that I was previously just making in Python to run in Pyscript, but getting Pygame imported has been a huge pain. I've tried <script type="py-game"></script>, but it would only load the file specified in the src attribute. So far, I've found the most progress through this in the Python file where Pygame would've been imported originally, however I've still been running into issues. Surely there has to be a way to run Pygame and import from multiple files, right?
Andrea Giammarchi 11/03/2025 10:58
Avatar
Andrea Giammarchi 11/03/2025 12:14
FYI PyScript 2025.3.1 has just been released so the demo should just work out of official release now.
Avatar
Drew Echerd 11/03/2025 12:44
Hey, is there a way to get code completion in VS Code or Cursor for pyscript when I am writing my python files. I understand that the recommended way to do this is to just use pyscript.com and copy it over but is there a way to get completion to work? I've search the server and am not seeing anything other than others asking the same question without any clear answer. Following along with the example I expected to pip install pyscript and write from pyscript import document and get autocompletion in the IDE. I understand it is designed to run in the browser but we write in an IDE.
Avatar
Trying to use Origin private file system aka opfs with pyscript.fs. Mounting and reading with micropython works great on all major browsers including mobile ones, writing need to be in a worker due to nature of it. So please if anyone tried it, to share some tips how to refresh the mounted in the main.py after the saving file in the worker (syncfs() seems to work in one direction only, from fs to the mounted one yet in opfs file is made in worker so need to sync from mounted back to the fs). Current solution is to save it both in main and in worker, in the latter for persistance in next page load. Example link - https://rdsm.pyscriptapps.com/persipy/latest/ (edited)
Avatar
How could i use sci-kit learn with pyscript?
Avatar
Avatar
TRT1000
How could i use sci-kit learn with pyscript?
Chris Laffra 17/03/2025 23:53
scikit-learn comes with PyOdide (https://pyodide.org/en/stable/usage/packages-in-pyodide.html), so you can use it like any other packages in PyScript. Add the package to your PyScript config and import the modules as you would normally do.
00:40
thanks
Avatar
Having another go at raising this issue here. Can anyone help me with this: Openscad has a wasm version: https://github.com/openscad/openscad-wasm/releases Three files - [openscad.worker.wasm, openscad.js, openscad.worker.js] How do I persuade the toml/index files to let me import these ? Hate to ask @Andrea Giammarchi but you're most likely to know ...?
Webassembly port of OpenSCAD. Contribute to openscad/openscad-wasm development by creating an account on GitHub.
Avatar
Yeah @Andrea Giammarchi is most likely to know. But he and I both have a VERY meeting heavy day today, so may not be immediately responsive. We'll get there! ๐Ÿ‘ (And thank you for your patience ๐Ÿค— ).
Avatar
Avatar
Neon22
Having another go at raising this issue here. Can anyone help me with this: Openscad has a wasm version: https://github.com/openscad/openscad-wasm/releases Three files - [openscad.worker.wasm, openscad.js, openscad.worker.js] How do I persuade the toml/index files to let me import these ? Hate to ask @Andrea Giammarchi but you're most likely to know ...?
Andrea Giammarchi 18/03/2025 11:41
I am afraid I am not following what's the issue in here ... if those are JS modules you can import them out of absolute or relative urls? any demo I can check to see what's wrong? I just see a list of files in your link.
Avatar
Avatar
Andrea Giammarchi
I am afraid I am not following what's the issue in here ... if those are JS modules you can import them out of absolute or relative urls? any demo I can check to see what's wrong? I just see a list of files in your link.
Thanks. I have made a first attempt. I seem to have the openscad module loading but can't work out how to translate the JS to pyscript. Explanation is here: https://pyscript.com/@neon22/openscad-wasm-01/latest?files=main.py,pyscript.toml,index.html
02:10
This was made easier, I think, because they wrapped their own wasm file in a controlling js and I think its operating from their own worker too.
Avatar
Avatar
Neon22
Thanks. I have made a first attempt. I seem to have the openscad module loading but can't work out how to translate the JS to pyscript. Explanation is here: https://pyscript.com/@neon22/openscad-wasm-01/latest?files=main.py,pyscript.toml,index.html
Andrea Giammarchi 19/03/2025 08:45
Avatar
Thanks heaps Andrea. I have it doing a small build here. I wil look into adding a webgl viewer.
๐Ÿ‘ 1
Avatar
Andrea Giammarchi 19/03/2025 12:24
glad it worked ... also please bear in mind I have no idea what you are doing there ๐Ÿ˜… ... so that I can't really help further or validate results
Avatar
Avatar
Andrea Giammarchi
glad it worked ... also please bear in mind I have no idea what you are doing there ๐Ÿ˜… ... so that I can't really help further or validate results
Yep - its a niche area. Language within a language, specific built to construct 3D geometry using solids and boolean ops. Very useful for 3D printers - Thingiverse is chock full of openSCAD files and uses the "Customizer" to allow parametric adjustments to complex 3D models. Now running in your browser in python thanks to the openSCAD WASM team and, of course, your kind self. Cheers... E.g. a knitting machine I am hopefully working on: https://i.postimg.cc/3x3HSY4J/knitting-machine-01.png
โค๏ธ 1
Avatar
Avatar
Neon22
Yep - its a niche area. Language within a language, specific built to construct 3D geometry using solids and boolean ops. Very useful for 3D printers - Thingiverse is chock full of openSCAD files and uses the "Customizer" to allow parametric adjustments to complex 3D models. Now running in your browser in python thanks to the openSCAD WASM team and, of course, your kind self. Cheers... E.g. a knitting machine I am hopefully working on: https://i.postimg.cc/3x3HSY4J/knitting-machine-01.png
Andrea Giammarchi 20/03/2025 08:31
feels like the only missing thing is a ThreeJS visualizer of the results? ๐Ÿ˜‡
Avatar
Avatar
Andrea Giammarchi
feels like the only missing thing is a ThreeJS visualizer of the results? ๐Ÿ˜‡
on my list !!
Avatar
Hi all, I'm new here... I discover pyscript and it seems a very interesting project. I'm trying to add a web/svg driver to pygraph an interactive geometry educational software, current implementation is in Tk. I'm able to build an interactive SVG draw: https://pyscript.com/@zarch/svg-interactive-geometries but when I try to abstract a bit the code I'm not able to execute, here an example: https://pyscript.com/@zarch/svg-plane in main.py I'm importing some not existing object: Wrong and bellow I'm calling some not existing method: plane.wrong() but in the console I do not see any error message. Perhaps, the issue is due to current project structure (see figure). Is it possible to structure the code using folders and subfolders? I did not see any other example that might guide me and I was not able to find documentation on this probably due to my lack of knowledge in the field.
Avatar
Avatar
zarch
Hi all, I'm new here... I discover pyscript and it seems a very interesting project. I'm trying to add a web/svg driver to pygraph an interactive geometry educational software, current implementation is in Tk. I'm able to build an interactive SVG draw: https://pyscript.com/@zarch/svg-interactive-geometries but when I try to abstract a bit the code I'm not able to execute, here an example: https://pyscript.com/@zarch/svg-plane in main.py I'm importing some not existing object: Wrong and bellow I'm calling some not existing method: plane.wrong() but in the console I do not see any error message. Perhaps, the issue is due to current project structure (see figure). Is it possible to structure the code using folders and subfolders? I did not see any other example that might guide me and I was not able to find documentation on this probably due to my lack of knowledge in the field.
looks pretty close. "./cart"="" is not needed only the files. The py files inside the folder seem correctly defined. To access then you need to import abstract in your main. Have you looked in here: https://docs.pyscript.net/2025.3.1/user-guide/configuration/#files Also - there is quite a good svg lib I use which is linked on this page. See if it works for you. I use outerHTML rather than as_str to append to dom elements.
10:52
nice draggable example
Avatar
In your svg-plane example the error message in the Console(using Chrome) indicates you have not imported color. That is why this code is failing.
Avatar
@Neon22 Thank you for the tips <3, I was stack, Now I've fixed the import issue, it is not fully working but I'm getting closer ๐Ÿ˜‰
Avatar
I turned on the terminal to help with debugging. check this out: https://pyscript.com/@neon22/svg-plane-copy/latest?files=main.py,cart/svg_driver.py
11:18
can't find svg.Group I also made a mini main2.py - you can check imports etc in there if it useful
11:18
I modded your toml
11:19
I have to go to bed now. good luck @zarch
11:22
one more thing. the svg you're using seems like svg1. it does not seem to use style. This started in svg2. The code is a lot smaller and simpler to traverse (IMHO). I do recommend that svg.py I pointed you too in my link above. Best of luck
Avatar
Avatar
Neon22
one more thing. the svg you're using seems like svg1. it does not seem to use style. This started in svg2. The code is a lot smaller and simpler to traverse (IMHO). I do recommend that svg.py I pointed you too in my link above. Best of luck
I'm not using style because I need to understand how to make the svg and tk interface consistent and at this stage is enough to define the style for each svg element. I have a working example now (not yet interactive), I would like to integrate an editor on it where the user can define its own geometries and play a bit with the code. From the documentation it is not clear how I can do it. Ideally I would like to have some sample code already written in the editor, and the result already rendered in the svg-container, then the user can play with the code and see the result. I tried following the example https://github.com/pyscript/pyscript/blob/2025.3.1/core/tests/manual/submit.py but raise an AttributeError for both code and process. Probably I'm missing something stupid.
PyScript is an open source platform for Python in the browser. Try PyScript: https://pyscript.com Examples: https://tinyurl.com/pyscript-examples Community: https://discord.gg/HxvBtukrg2 - pyscri...
Avatar
Avatar
zarch
I'm not using style because I need to understand how to make the svg and tk interface consistent and at this stage is enough to define the style for each svg element. I have a working example now (not yet interactive), I would like to integrate an editor on it where the user can define its own geometries and play a bit with the code. From the documentation it is not clear how I can do it. Ideally I would like to have some sample code already written in the editor, and the result already rendered in the svg-container, then the user can play with the code and see the result. I tried following the example https://github.com/pyscript/pyscript/blob/2025.3.1/core/tests/manual/submit.py but raise an AttributeError for both code and process. Probably I'm missing something stupid.
Ohers use that aspect more than me. Perhaps they can respond. However I believe there are two approaches. the ltk does it with coderunner: https://pyscript.github.io/ltk/?tab=8&runtime=mpy and edublocks uses the Donkey to evaluate code in a Worker. - https://docs.pyscript.net/2025.3.1/api/#pyscriptcoredonkey Search this discord for code for "editor". The ltk has the benefit of already being a bit like Tk. I am a fan.
โค๏ธ 1
Avatar
Chris Laffra 20/03/2025 20:49
@zarch take a look at PyAlgoViz, it uses LTK with MIcroPython for the UI, a worker in PyOdide, and renders animations using an HTML canvas. https://github.com/laffra/pyalgoviz-pyscript
Contribute to laffra/pyalgoviz-pyscript development by creating an account on GitHub.
Avatar
@Neon22 and @Chris Laffra thank you for your interesting link I'm going to study them!
Avatar
Hi everyone. I am a lecturer at a university of applied sciences, and for our programming course, we would like to add a sort of Python interpreter to the exam environment. We are considering PyScript for this, as it can easily be embedded into the HTML page with the exam questions. Everything has been set up successfully, and everything works (almost) as expected. However, in theory, students could use the 'requests' and/or 'js' (fetch) modules (which is quite simple, already achievable with 3 or 4 lines of code that you can learn by heart) to connect to the outside world. So, someone could potentially set up a small server before the exam starts, that everyone could use to send and/or retrieve answers. We have already been busy trying to disable certain modules, but unfortunately without success. It is also not possible to intercept the requests via service workers. My question: Is it possible, through PyScript (configuration), or by another method, to somehow block access to the outside world? Thanks in advance!
Avatar
pyscript may use different runtimes, so an easy way to control network access from students would be to rebuild pyodide runtime with proxy support , regarding JS running without ssl on a LAN should be enough to block most fetch() but there may be a better solution to do that with service worker+ssl. (edited)
Avatar
Chris Laffra 24/03/2025 17:08
@Bart By default, network access should be blocked already (CORS)? (edited)
Avatar
@pmp-p, thanks for your response, it made me look at service workers again, and I'll give it another try. If that doesn't work, I'll look into rebuilding Pyodide-with-proxy, so thanks for this suggestion too!
19:54
@Chris Laffra, that depends on the server you want to access. So if someone (a student) sets up a server with CORS enabled, you can just send a request to it (in PyScript with the requests module). There is normally nothing in the browser that prevents that, as far as I know.
Avatar
perhaps you can use the install option which urns it into a pwa. then remove the router connection manually.
Avatar
Avatar
Bart
Hi everyone. I am a lecturer at a university of applied sciences, and for our programming course, we would like to add a sort of Python interpreter to the exam environment. We are considering PyScript for this, as it can easily be embedded into the HTML page with the exam questions. Everything has been set up successfully, and everything works (almost) as expected. However, in theory, students could use the 'requests' and/or 'js' (fetch) modules (which is quite simple, already achievable with 3 or 4 lines of code that you can learn by heart) to connect to the outside world. So, someone could potentially set up a small server before the exam starts, that everyone could use to send and/or retrieve answers. We have already been busy trying to disable certain modules, but unfortunately without success. It is also not possible to intercept the requests via service workers. My question: Is it possible, through PyScript (configuration), or by another method, to somehow block access to the outside world? Thanks in advance!
What if they open new browser tab to that server with answers and do it manually or with some ai helpers โ€ฆ guess exam network should be local for good.
Avatar
@Ridensium, the test environment is quite well secured, so opening new tabs or applications is not possible. However, as soon as an HTML page (with PyScript) is included in a question, this page is allowed to open connections to other servers.
Avatar
Avatar
Neon22
perhaps you can use the install option which urns it into a pwa. then remove the router connection manually.
What exactly do you mean by 'remove router connection'? Because once PyScript is loaded, the server is no longer needed. But via Python code you can access other servers. Or maybe I don't understand what you mean?
Avatar
ThisIsMyUsername 24/03/2025 22:29
@Bart I am working on something similar. I will send you a video of what I am working on in a day or two. I plan on posting it to GitHub once it gets to a stable point.
Avatar
Avatar
ThisIsMyUsername
@Bart I am working on something similar. I will send you a video of what I am working on in a day or two. I plan on posting it to GitHub once it gets to a stable point.
That would be great, of course! Looking forward to it!
Avatar
Avatar
Bart
What exactly do you mean by 'remove router connection'? Because once PyScript is loaded, the server is no longer needed. But via Python code you can access other servers. Or maybe I don't understand what you mean?
So you can experiment with the "install" link on the lower RHS of any pyscript.com link. E.g. one of these - https://docs.pyscript.net/2025.3.1/examples/. It will install everything referenced onto that machine (I assume a machine people are using to take the test). Then you can physically remove the machine from all networks, or from all networks except some specific ones - via settings on your class/floor/? physical router. And so - to load the page does not require reaching the internet(all resources are on that machine). Therefore opening another tab, or handcrafting a fetch call in an editor exposed on that page, will not be able to reach anywhere because it is not accessable.
Avatar
Avatar
Neon22
So you can experiment with the "install" link on the lower RHS of any pyscript.com link. E.g. one of these - https://docs.pyscript.net/2025.3.1/examples/. It will install everything referenced onto that machine (I assume a machine people are using to take the test). Then you can physically remove the machine from all networks, or from all networks except some specific ones - via settings on your class/floor/? physical router. And so - to load the page does not require reaching the internet(all resources are on that machine). Therefore opening another tab, or handcrafting a fetch call in an editor exposed on that page, will not be able to reach anywhere because it is not accessable.
Ah, now I understand what you mean! Thanks for the suggestion, but I'm afraid that this is not an option in our environment. A constant internet connection to external servers is required for the test software in any case. Allowing/disallowing certain networks could be an option, but in our organization (> 35000 students) the network (configuration) is tightly sealed. In addition, about 8 classes take the test simultaneously, three times a day, so it should not actually require any manual actions from the proctor (to avoid mistakes).
Avatar
Avatar
Bart
@Ridensium, the test environment is quite well secured, so opening new tabs or applications is not possible. However, as soon as an HTML page (with PyScript) is included in a question, this page is allowed to open connections to other servers.
Imagine you can do something with pyscript, will the student be able to open inspector on the exam page or electron/capacitor app and use javascript to do the shenanigans there? I guess its better to do the prevention from the js part as pyscript is on top of it. (edited)
Avatar
Avatar
Ridensium
Imagine you can do something with pyscript, will the student be able to open inspector on the exam page or electron/capacitor app and use javascript to do the shenanigans there? I guess its better to do the prevention from the js part as pyscript is on top of it. (edited)
No, that is not possible. The inspector / dev-toolbar is blocked. That is why I initially wanted to use service workers. But they can only catch Fetch-request events, not XMLHttpRequest events. Unfortunately, the PyScript requests module uses this last type, and I am unable to listen for those requests.
Avatar
Avatar
Bart
No, that is not possible. The inspector / dev-toolbar is blocked. That is why I initially wanted to use service workers. But they can only catch Fetch-request events, not XMLHttpRequest events. Unfortunately, the PyScript requests module uses this last type, and I am unable to listen for those requests.
Gotcha, no way to execute some js, but able to pyscript. The latter uses an api which js cannot intercept. So the logical solution is to make pyscript to use the js.fetch api meaning provide your own customised version of it with tweaking it. Or after loading to delete or change the pyscript.fetch. Frankly i always supposed pyscript uses js fetch instead the other one, thanks thats a good to know.
Avatar
Avatar
Ridensium
Gotcha, no way to execute some js, but able to pyscript. The latter uses an api which js cannot intercept. So the logical solution is to make pyscript to use the js.fetch api meaning provide your own customised version of it with tweaking it. Or after loading to delete or change the pyscript.fetch. Frankly i always supposed pyscript uses js fetch instead the other one, thanks thats a good to know.
That's right. A modified version will probably cost me some time and effort, so I was looking for something simpler (like iframe-sandboxing or so). But if a modified version is the only way, I'll go with that. By the way, PyScript uses both Fetch and XHR, as far as I can see. The requests module uses XHR and the JS module uses Fetch.
Avatar
Avatar
Chris Laffra
Thanks, I'll look into it!
๐Ÿ‘ 1
Avatar
Avatar
Bart
Hi everyone. I am a lecturer at a university of applied sciences, and for our programming course, we would like to add a sort of Python interpreter to the exam environment. We are considering PyScript for this, as it can easily be embedded into the HTML page with the exam questions. Everything has been set up successfully, and everything works (almost) as expected. However, in theory, students could use the 'requests' and/or 'js' (fetch) modules (which is quite simple, already achievable with 3 or 4 lines of code that you can learn by heart) to connect to the outside world. So, someone could potentially set up a small server before the exam starts, that everyone could use to send and/or retrieve answers. We have already been busy trying to disable certain modules, but unfortunately without success. It is also not possible to intercept the requests via service workers. My question: Is it possible, through PyScript (configuration), or by another method, to somehow block access to the outside world? Thanks in advance!
Thanks to all the tips and suggestions, I started looking at the problem from scratch again. In the end, it seems that I have found a solution to my problem: the HTML page (with PyScript) that I load in an iframe in the exam, is on its own server. I have modified the server in such a way that it also returns a Content-Security-Policy header for the HTML page in question. This allows you to determine exactly which domains are allowed/not allowed. If I add the domains to load/run PyScript, PyScript works properly, but as soon as you want to access other domains, they are blocked by the browser (both XHR and Fetch). This seems to have solved the problem. So @pmp-p, @Ridensium, @Neon22, @Chris Laffra and @ThisIsMyUsername: thanks for thinking along!
๐Ÿ‘ 2
Avatar
egor.kraev 26/03/2025 08:42
Hi all! wanted to ask, is there a contractor community here as well? Would like to maybe hire someone to knock out a quick POC in PyScript, to see if it's a match for what we need - and obviously that would need to be someone who actually knows PyScript ๐Ÿ™‚
08:48
Also, can PyScript save stuff to local disk that is available next time the user opens the same page in the same browser on the same machine?
Avatar
Avatar
egor.kraev
Also, can PyScript save stuff to local disk that is available next time the user opens the same page in the same browser on the same machine?
you can save to the OPFS (https://developer.mozilla.org/en-US/docs/Web/API/File_System_API/Origin_private_file_system) is available through JS but also the new implementation of the File system API allows for easy storage upload/download of files on eth user's local file system - if using Chrome... https://developer.mozilla.org/en-US/docs/Web/API/File_System_API In pyscript support is here: - https://docs.pyscript.net/2025.3.1/user-guide/filesystem/
The origin private file system (OPFS) is a storage endpoint provided as part of the File System API, which is private to the origin of the page and not visible to the user like the regular file system. It provides access to a special kind of file that is highly optimized for performance and offers in-place write access to its content.
The File System API โ€” with extensions provided via the File System Access API to access files on the device file system โ€” allows read, write and file management capabilities.
Avatar
Florian Stormacq 27/03/2025 12:13
Subject: Help Needed: "ModuleNotFoundError" When Importing a Local Python File in PyScript Hi everyone, I'm having trouble importing a local Python file while using PyScript. I keep getting the same error: ModuleNotFoundError: No module named 'restricted_checks' However, the file does exist and is properly referenced in my project structure and pyscript.json file.

Project structure:

project/ โ”œโ”€โ”€ src/ โ”‚ โ”œโ”€โ”€ app.html โ”‚ โ”œโ”€โ”€ routes/ โ”‚ โ”‚ โ”œโ”€โ”€ +layout.js โ”‚ โ”‚ โ”œโ”€โ”€ +layout.svelte โ”‚ โ”‚ โ”œโ”€โ”€ +page.svelte โ”‚ โ”‚ โ””โ”€โ”€ components/ โ”‚ โ”‚ โ”œโ”€โ”€ diagram.svelte <-- (PyScript is included here) โ”‚ โ”‚ โ””โ”€โ”€ editor.svelte โ”œโ”€โ”€ static/ โ”‚ โ”œโ”€โ”€ python/ โ”‚ โ”‚ โ”œโ”€โ”€ config/ โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ pyscript.json โ”‚ โ”‚ โ”œโ”€โ”€ src/ โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ main.py โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ restricted_checks.py โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ worker.py <-- (where the import issue occurs)

How PyScript is included in diagram.svelte:

<script type="py" src="python/src/main.py" config="python/config/pyscript.json"></script>

Contents of pyscript.json:

{ "package": ["ast", "pyscript", "pyodide", "ast", "time", "copy", "js"], "interpreter": "https://cdn.jsdelivr.net/pyodide/v0.27.0/full/pyodide.mjs", "files": { "../src/restricted_checks.py": "" } }

Problematic import statement in worker.py:

import restricted_checks I expected this to work since restricted_checks.py is listed in pyscript.json. However, it is still unable to find the module.
  • Am I referencing the file correctly in pyscript.json?
  • Do I need to modify how I import the module inside worker.py?
Any help would be greatly appreciated! Thanks in advance. ๐Ÿ˜Š
Avatar
Chris Laffra 28/03/2025 08:58
@Florian Stormacq You want to replace the empty string in your JSON with an actual target location. (edited)
Avatar
Florian Stormacq 28/03/2025 09:33
Hi @Chris Laffra , I updated my pyscript.json file as suggested: { "package": ["ast", "pyscript", "pyodide", "time", "copy", "js", "asyncio"], "interpreter": "https://cdn.jsdelivr.net/pyodide/v0.27.0/full/pyodide.mjs", "files" : { "../src/restricted_checks.py": "./restricted_checks.py" } } However, the issue persists. According to PyScriptโ€™s documentation:
If you make the target an empty string, the final "filename" part of the source URL becomes the destination filename, in the root of the filesystem, to which the content is copied.
Even with an explicit path, PyScript still fails to locate the module. Any ideas? Thanks! ๐Ÿ˜Š
Avatar
Avatar
Florian Stormacq
Hi @Chris Laffra , I updated my pyscript.json file as suggested: { "package": ["ast", "pyscript", "pyodide", "time", "copy", "js", "asyncio"], "interpreter": "https://cdn.jsdelivr.net/pyodide/v0.27.0/full/pyodide.mjs", "files" : { "../src/restricted_checks.py": "./restricted_checks.py" } } However, the issue persists. According to PyScriptโ€™s documentation:
If you make the target an empty string, the final "filename" part of the source URL becomes the destination filename, in the root of the filesystem, to which the content is copied.
Even with an explicit path, PyScript still fails to locate the module. Any ideas? Thanks! ๐Ÿ˜Š
Chris Laffra 28/03/2025 14:08
Contribute to laffra/pyalgoviz-pyscript development by creating an account on GitHub.
14:09
I think the double-dot is your problem
Avatar
donut_panda505 12/04/2025 21:59
quick question and this may just be a python problem do you now why i can't interact with my code like input something on the website side of things. (edited)
22:03
Avatar
I have been trying to learn how to do that, and I think it has something to do with PyDom. I don't really know though.
Avatar
donut_panda505 12/04/2025 22:53
ah
Avatar
Avatar
donut_panda505
Click to see attachment ๐Ÿ–ผ๏ธ
Its probable you're trying to use the terminal as though its not on a webpage. Its useful to print stuff out with but maybe what you need is the pyeditor instead. Maybe you're trying to run python code and have interactive user input as though you were running on your local machine?? Consider the py-editor - https://docs.pyscript.net/2025.3.1/user-guide/editor/. IMHO you can take look at this problem in a webpage kind of way instead. I use the Ltk to create user input sections in html and css and then use those as values of variables in my code. So its not clear from your statement what you're trying to do. Anyway - check out that help page and the rest of the help (which has a search feature), and see if its useful.
05:17
Ltk has a demo page - the kitchensink here: https://pyscript.github.io/ltk/?tab=8&runtime=mpy
Avatar
Avatar
donut_panda505
Click to see attachment ๐Ÿ–ผ๏ธ
You need to do something like this, for input to work... the import thing being BOTH the terminal and worker attributes: <script type="py" src="mycode.py" terminal worker></script>
Avatar
Avatar
donut_panda505
quick question and this may just be a python problem do you now why i can't interact with my code like input something on the website side of things. (edited)
Andrea Giammarchi 14/04/2025 16:12
if you use a worker you can input("question") and retrieve the answer just like you'd do in regular Python ... it's all in our docs https://docs.pyscript.net/2025.3.1/user-guide/workers/
Avatar
hi all, I am interested in the simplest/smallest way to use python in the web browser, can anyone recommend a python approach to a gui in a browsers with pyscript using micropython?
Avatar
Avatar
agile_prg
hi all, I am interested in the simplest/smallest way to use python in the web browser, can anyone recommend a python approach to a gui in a browsers with pyscript using micropython?
The smallest way is to manipulate the dom yourself (vs the easiest which might be to use the Ltk - depending on exactly what you need). The dom commands can be found around here: - https://docs.pyscript.net/2025.3.1/user-guide/dom/#pyscriptweb
Avatar
Hello everyone! ๐Ÿ‘‹ I'm having trouble using the requests library in my PyScript project on pyscript.com. I keep getting a ModuleNotFoundError: No module named 'requests' error. I've tried a few things based on standard Pyodide/PyScript documentation, but none seem to be working on the pyscript.com platform: 1. Using <py-env> in my HTML: <py-env> - requests - beautifulsoup4 </py-env> 2. Using await micropip.install(['requests', 'beautifulsoup4']) within my <py-script> code. Here's the traceback I consistently receive: Traceback (most recent call last): File "/lib/python312.zip/_pyodide/_base.py", line 597, in eval_code_async await CodeRunner( File "/lib/python312.zip/_pyodide/_base.py", line 411, in run_async coroutine = eval(self.code, globals, locals) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "<exec>", line 2, in <module> ModuleNotFoundError: No module named 'requests' The module 'requests' is included in the Pyodide distribution, but it is not installed. You can install it by calling: await micropip.install("requests") in Python, or await pyodide.loadPackage("requests") in JavaScript See https://pyodide.org/en/stable/usage/loading-packages.html for more details. I'm trying to fetch data from my website (trafficcameras.info, where I believe I've correctly configured CORS to allow requests from pyscript.com). Could anyone on the pyscript.com platform advise on the correct way to install and use external libraries like requests? Is there a specific method or are there any known issues I should be aware of? Any help would be greatly appreciated! Thank you.
Avatar
Avatar
agile_prg
hi all, I am interested in the simplest/smallest way to use python in the web browser, can anyone recommend a python approach to a gui in a browsers with pyscript using micropython?
Chris Laffra 15/04/2025 13:42
For a very simple, but still useful app that uses MicroPython, look at https://github.com/laffra/pyalgoviz-pyscript/tree/main. It also uses a worker in PyOdide. The README has a link to a live preview. Furthermore, the LTK documentation links to a really minimal LTK app that uses MicroPython. See https://github.com/pyscript/ltk?tab=readme-ov-file#getting-started
Write browser apps entirely in Python. Contribute to pyscript/ltk development by creating an account on GitHub.
Avatar
Avatar
jawad
Hello everyone! ๐Ÿ‘‹ I'm having trouble using the requests library in my PyScript project on pyscript.com. I keep getting a ModuleNotFoundError: No module named 'requests' error. I've tried a few things based on standard Pyodide/PyScript documentation, but none seem to be working on the pyscript.com platform: 1. Using <py-env> in my HTML: <py-env> - requests - beautifulsoup4 </py-env> 2. Using await micropip.install(['requests', 'beautifulsoup4']) within my <py-script> code. Here's the traceback I consistently receive: Traceback (most recent call last): File "/lib/python312.zip/_pyodide/_base.py", line 597, in eval_code_async await CodeRunner( File "/lib/python312.zip/_pyodide/_base.py", line 411, in run_async coroutine = eval(self.code, globals, locals) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "<exec>", line 2, in <module> ModuleNotFoundError: No module named 'requests' The module 'requests' is included in the Pyodide distribution, but it is not installed. You can install it by calling: await micropip.install("requests") in Python, or await pyodide.loadPackage("requests") in JavaScript See https://pyodide.org/en/stable/usage/loading-packages.html for more details. I'm trying to fetch data from my website (trafficcameras.info, where I believe I've correctly configured CORS to allow requests from pyscript.com). Could anyone on the pyscript.com platform advise on the correct way to install and use external libraries like requests? Is there a specific method or are there any known issues I should be aware of? Any help would be greatly appreciated! Thank you.
Rather than using requests - we have an equivalent fetch API built into PyScript. Docs here: https://docs.pyscript.net/2025.3.1/api/#pyscriptfetch Happy to answer questions.
16:30
The problem is we're limited by the browser in what we can do in terms of network access. The requests module uses underlying operating system IO which simply isn't available in the browser... hence the fetch API we have that sits on top of the browser's way to make HTTP requests. It is possible to use requests with Pyodide... but this is a but more involved.
๐Ÿ™Œ 1
Avatar
Hi, tried to use pandas with 2025.2.1 and gave me a deprecation warning about pyarrow and wouldn't let the page work. changed to use 2024.1.1 and it works fine. what do I do to make it work with more recent releases? (doesn't like it when i add it to packages in the toml) (edited)
Avatar
Avatar
captix
Hi, tried to use pandas with 2025.2.1 and gave me a deprecation warning about pyarrow and wouldn't let the page work. changed to use 2024.1.1 and it works fine. what do I do to make it work with more recent releases? (doesn't like it when i add it to packages in the toml) (edited)
I think the problem lies in pandas at the moment. They mandated pyArrow since pandas3. Alas pyArrow does not have a pure python wheel - so cannot load natively in the browser. https://pypi.org/project/pyarrow/#files Probably there are two solutions - load the latest pandas version prior to pyarrow being included, or let pyodide poeple know so they can create a wasm? version of the entire package for pyodide to use. (I suspect this is probably already on their list but every voice helps.) IMHO - Your best bet is to use the latest pandas precompiled by pyodide. This is version 2.2.3 - https://pyodide.org/en/stable/usage/packages-in-pyodide.html
๐Ÿ‘ 1
21:12
packages = [ "pandas==2.2.3"]
Avatar
Avatar
ntoll
The problem is we're limited by the browser in what we can do in terms of network access. The requests module uses underlying operating system IO which simply isn't available in the browser... hence the fetch API we have that sits on top of the browser's way to make HTTP requests. It is possible to use requests with Pyodide... but this is a but more involved.
Thanks. I'm trying to fetch data from a specific URL (https://trafficcameras.info/product/m25-100-9a-junction-16-15/) using pyscript.fetch but I consistently get a TypeError: Failed to fetch error in the PyScript console. I've tried a basic fetch(url) and also included a User-Agent header, but the error persists. Are there any specific configurations or considerations I might be missing when trying to fetch from external websites? Any help or insights would be greatly appreciated!
M25 100 9a junction 16 15 M20 Kent | South East England. Fully optimised for Mobiles and Tablets,free live access up to 3000 traffic cameras.
Avatar
Avatar
jawad
Thanks. I'm trying to fetch data from a specific URL (https://trafficcameras.info/product/m25-100-9a-junction-16-15/) using pyscript.fetch but I consistently get a TypeError: Failed to fetch error in the PyScript console. I've tried a basic fetch(url) and also included a User-Agent header, but the error persists. Are there any specific configurations or considerations I might be missing when trying to fetch from external websites? Any help or insights would be greatly appreciated!
OK... So I've just tried to recreate your use-case with the given URL. It's as I thought... the website to which you are trying to connect to doesn't allow requests from websites hosted on other domains (the famous CORS issues). Here's what I see in my console that confirms it: Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://trafficcameras.info/product/m25-100-9a-junction-16-15/. (Reason: CORS header โ€˜Access-Control-Allow-Originโ€™ missing). Status code: 200.
Avatar
The solution (if you're using PyScript.com) is to make use of an API proxy. The docs for which are here: https://pyscript.com/docs/api-proxies-and-secrets BTW, all of this ^^^^ is beyond PyScript's control. It's imposed on us by the way browsers work, and PyScript is, of course, working in the browser sandbox so has to play by the browser's rules.
12:52
The PyScript.com API proxy feature is specifically created for just this problem. 1. You have a URL (like yours) that doesn't have CORS support. 2. Create an API proxy inside Pyscript.com that makes requests to your URL (i.e. the pyscript.com server will do it on your behalf). 3. Call the API proxy from your pyscript.com hosted app... 4. The proxied response is returned to you. That's it.
Avatar
Great thank you will take a look ๐Ÿ‘
๐Ÿ‘ 1
Avatar
is it possible to dynamically add pyscript abilities to a web page after it has fully loaded?
Avatar
Hey, I'm trying to make animations for physics learning applications using pyscript. Using Matplotlib animations would be ideal, like the one provided here: https://pyscript.com/@agiammarchi/attempt-at-matplotlib-animations-copy/latest?files=main.py,index.html I tried copy and pasting that same code and running it locally so that I could try inspecting it, and upon opening on Firefox, it came up with the following error: (PY0001): Polyscript: Access to local files (using [[fetch]] configurations in &lt;py-config&gt;) is not available when directly opening a HTML file; you must use a webserver to serve the additional files. See <a style="text-decoration: underline;" href="https://github.com/pyscript/pyscript/issues/257#issuecomment-1119595062">this reference</a> on starting a simple webserver with Python. If someone could help or assist in resolving this issue, it would be greatly appreciated. (edited)
Avatar
angel I am interested in doing something similar, maybe we could exchange notes/collaborate
โค๏ธ 1
Avatar
Avatar
agile_prg
angel I am interested in doing something similar, maybe we could exchange notes/collaborate
I found the solution from a blog post by Jeff Glass. https://jeff.glass/post/pyscript-need-a-server/ The other workaround to doing this is including all of your code within the HTML file instead.
Avatar
Avatar
angel
I found the solution from a blog post by Jeff Glass. https://jeff.glass/post/pyscript-need-a-server/ The other workaround to doing this is including all of your code within the HTML file instead.
Jeff's post is still correct but many of the details are no longer accurate as of 2025.3.1. So please use the latest version (e.g. no [fetch] in toml anymore - use[files] instead.) The main thing appears to be you are trying to use it as an html file which someone just tries to open on a local machine. The entire design of the www is basically working against you there. So you have two easy solutions: 1. Put your code on pyscript.com - point people to that. All cors issues taken care of - and it all still only runs on your local machine. 2. Once you have that - use the "Install" option on Lower RHS and it will become a PWA on your own system and no longer need internet access at all.
โค๏ธ 1
22:56
if you only ever need it to run on your local machine - then just use python and maybe Customtkinter for UI. no need to use pyscript at all.
Avatar
I would like to have the option to do both,
  • run locally if I have the file
  • run from a website
That would allow for these simulations to be deployed to students most easily without redundant alternative solutions, at least for now for the purpose of developing our project. Thank you for the suggestion of uploading the code to pyscript.com and making PWA's. That should work when we have everything finalized and have it ready to deploy. For now, since we are students relatively new to all of this software and so our simulations and we are still exploring in our development, we would like to have a locally hosted while we develop and test things. Also, I apologize; I didn't clarify that I am using 2025.3.1. and .json for the configuration file.
Avatar
I am in micropython. I want to load a version of the configparser (not available for mpy). I have downloaded the whl from pypi, renamed and put this in my toml "./configparser7.whl" = "./*" under [files] What is the right way to import this ? If I try from configparser7 import * I get a Promise Error: custom.js:189 Uncaught (in promise) ErrnoError
13:50
Actually the Promise error disappears if I comment out the line in the toml. So I'm not doing that right... ??
Avatar
Also mip.install("https://files.pythonhosted.org/packages/09/fe/f61e7129e9e689d9e40bbf8a36fb90f04eceb477f4617c02c6a18463e81f/configparser-7.2.0-py3-none-any.whl") did not work
Avatar
angel do you think it is possible to use a no webserver approach?
โค๏ธ 1
15:49
that is what I am doing for something I am working on
Avatar
Avatar
agile_prg
angel do you think it is possible to use a no webserver approach?
I'm not sure that I know what that entails or how to do it lol
Avatar
Is there a way to mess with the Terminal through CSS? ๐Ÿ‘€
Avatar
Hey, does pyscript work with the Vernier Python libraries like GDX?
02:03
A Python module for reading from Vernier Go Directยฎ Sensors using USB or BLE. - VernierST/godirect-py
Avatar
Chris Laffra 22/04/2025 09:33
This is unlikely to work as PyScript runs inside the browser. Access to local sensors, such as USB and BLE is possible using the JavaScript BLE APIs, but it is not likely that this Python module is aware of this.
Avatar
Hey, is there an issue with terminal worker and py-editor working in Linux or Firefox or something? I can't get them to work.
Avatar
I work in Linux and Firefox and they "work for me" โ„ข๏ธ Do you have an example we can look at?
14:48
@angel ^^^^
Avatar
Avatar
ntoll
@angel ^^^^
hi, so this is what happens to me when I try to set up the py-editor. Notice that I pressed the "run function", and then nothing happened. If I press the stop button, the popout asking me if I wish to stop running the code will appear, but nothing will change despite the options that I pick.
23:56
The code is just the following: <script type="py-editor" target="test" config='{"packages":["numpy", "matplotlib"]}'> import sys from pyscript import display import numpy as np f = np.array([5, 5]) print(f) display(f) </script> (edited)
23:59
When I try to use terminal worker, the terminal appears, but I can't type anything in it. None of the code that I typed to appear in it appears either. And the only keys that will do something are the F keys, typing some characters.
Avatar
Andrea Giammarchi 02/05/2025 09:28
to whom it might concern, if you also need/want an easy way to upload files to your virtual PyScript FileSystem here an answer that demoes that + it explains how that works: https://github.com/pyscript/pyscript/pull/463#issuecomment-2846652825
โค๏ธ 1
Avatar
Do we need to hold onto the file handle or can we do a directory listing of memfs ?
Avatar
Avatar
angel
hi, so this is what happens to me when I try to set up the py-editor. Notice that I pressed the "run function", and then nothing happened. If I press the stop button, the popout asking me if I wish to stop running the code will appear, but nothing will change despite the options that I pick.
looks like there is no terminal in the latest version both prints and display goto DOM.
Avatar
JTS.Dawnstar[] 08/05/2025 05:31
When using API Proxies to avoid the CORS error when making a get request to a url with pyscript.fetch, is there any way to get the proxy to accept a query string at the end of the url? For me, it always gives me a 302 when I do that, despite the webpage that I'm proxying loading perfectly fine when I open it through the browser. Has anyone had success with passing query strings to get requests made through api proxies?
Avatar
Avatar
darvasd
Hi, I'm trying to use an external API that requires authentication. The pyscript.com's API proxies work like a charm for POST requests, however, when I try to have a GET request with query parameters, the parameters seem to be lost. How can I ensure that query parameters from the Python GET requests reach the target external API when using the pyscript.com API proxies? (If I include the parameters in the API proxy's URL, the query works, but this only allows constant parameters to be used.)
JTS.Dawnstar[] 08/05/2025 06:13
This is perhaps a better statement of my problem, although it doesn't seem like anyone responded last time it was asked
Avatar
Both PyScript.com developers are on holiday at the moment. @FabioRosado or @Martin will see my ping (my reply to you) on their return. Hang in there! We'll investigate.
07:43
@JTS.Dawnstar[] ^^^^
Avatar
JTS.Dawnstar[] 08/05/2025 08:04
Awesome sauce, thanks for the update
08:05
In the meantime, I think I was able to find a 3rd party proxy service that does the trick, but it'd still definitely be nice to be able to do it all through pyscript
Avatar
FabioRosado 08/05/2025 08:17
Hello @JTS.Dawnstar[] do you mean putting a secret in a query parameter? Or just adding a query parameter? If you mean secrets good news! We have implemented that and itโ€™s currently in dev so we can test more but when we come back from holidays we will release it ๐Ÿ˜
08:18
If you men the latter it should work already by setting the url with a query parameter when you create the proxy.
Avatar
JTS.Dawnstar[] 08/05/2025 08:20
For my program, I need to be able to pass query parameters to the proxy url, which will then be passed to the target url (So that I can, say, dynamically pass query selectors), but just appending ?key=value to the proxy url doesn't seem to have any effect on the response the target url gives (edited)
08:21
I've been able to get constant query selectors to work, but I'd like to be able to dynamically pass them
Avatar
Yeah, so as I understand it, this is a need for dynamic query parameters passed via the PyScript.com proxy..? Also, thanks for the heads up WRT corsproxy.io - I'm going to check it out. ๐Ÿ‘
Avatar
FabioRosado 08/05/2025 13:22
When we launch the feature to prod you will also be able to pass query parameters dynamically ๐Ÿ™‚
๐ŸŽ‰ 1
Avatar
JTS.Dawnstar[] 08/05/2025 15:56
Oh awesome! That'll be just perfect
Avatar
vlad.flore 13/05/2025 16:42
Hello! I am a new PyScript user, and I am working on a small app, that I'd like it to be able to generate pdfs. Has anyone had some experience with generating pdfs in PyScript? Could I get some pointers where to look? Thank you!
Avatar
Avatar
vlad.flore
Hello! I am a new PyScript user, and I am working on a small app, that I'd like it to be able to generate pdfs. Has anyone had some experience with generating pdfs in PyScript? Could I get some pointers where to look? Thank you!
I have had significant success using fpdf2. Here is my toml: packages = ["fpdf2==2.8.1"] beware of version problems. My main.py starts like this: from fpdf import FPDF # for report writing from fpdf.fonts import FontFace from fpdf.enums import TableSpan, Align from fpdf.svg import SVGObject This is how I subclass it )as per their docs) class PDF_base(FPDF): def __init__(self, title, grouped=False): self.title = title self.grouped = grouped self.table_heights = {'A4': 26, "Letter": 24} # how many lines can I get on the page based on format super().__init__() def footer(self): # Position cursor at 1.5 cm from bottom: self.set_y(-15) # Setting font: helvetica italic 8 self.set_font("helvetica", "I", 8) # Printing page number: self.cell(0, 10, f"Page {self.page_no()}/{{nb}}", align="C") If you really need to dig around look inside the quite large: https://pyscript.com/@neon22/working-sectional/latest?files=main.py,pyscript_pyo.toml
๐Ÿ‘ 1
Avatar
Hello ! I have a problem with PyScript on my website, whereas previously everything worked without a hitch. Could someone help me? Thank you very much for your help! ๐Ÿ™๐Ÿป
Avatar
Avatar
TR
Hello ! I have a problem with PyScript on my website, whereas previously everything worked without a hitch. Could someone help me? Thank you very much for your help! ๐Ÿ™๐Ÿป
It works fine with the 2023.03.1 version https://_12.pyscriptapps.com/pycon-xterm/latest/
Avatar
I have a question guys. Let me start with some background. My most recent project was on the stack FastAPI + Postgres + React + Tailwind. Backend running on one port and frontend running on a different port. Both code bases in one repo, but in separate folders and separate servers. Clearly, my dream is to code Python on the web. Not only does this allow more flexibility and conciseness in managing my GUI in conjunction with underlying flexible data structures (which is more tedious to do in JavaScript), but I am thinking of reusing code files in both backend and frontend.
Avatar
Again, at the moment I am used to running two servers on two ports. But with my first PyScript project I am trying to think what the equivalent would be. Since I assume some Python code would run on the server, then I assume I can still have my FastAPI backend. Now regarding the frontend, these are some options I see but am not clear whether they are viable or correct. Should I: 1) simply have a public folder? and serve that with nginx? I suppose the public folder is inside the fastapi static folder? Not sure about this. Some static files are not public? Or no static folder but only a public folder? 2) Should I run a server with python3 and http.server? 3) Should I run two fastapi servers, one for backend and one for frontend? Before reading the user guide I had created a folder structure that contained this in the root: backend, frontend, static, and venv. Now... on localhost since I am not using nginx I had decided to create two fastapi servers, but I suppose on production I can simply serve with nginx? Also my frontend server simply contained by frontend routes and some frontend configuration. I don't even know if this folder will be necessary in production? What do you think? I am trying to really start this inaugural pyscript project. I do like frontend/backend decoupling. I also like scalability. Also... the interesting caveat is that I have a few Python classes I want to use in my PyScript. These reusable python classes I have no idea where to put them (static, public, backend folder, frontend folder, or elsewhere) Finally... suppose this is a proprietary applciation. A lot of the core functionality will have to do with PyScript. But yet these files will all be served to client. So I may as well make my proprietary software open source right? Any thoughts?
Avatar
The front end will be pyscript - the backend - something else. The front end is essentially entirely static. Could even be installed as a PWA (you can see this option on pyscript.com in Lower right corner.) https://pyscript.com/docs/pwa
โค๏ธ 1
23:28
There is an obfuscator for pyscript somewhere - search the discord...
Avatar
ppowell777 18/05/2025 22:33
I am doing a very simple fetch(url) to a URL at localhost/cma_mysql/getstuff.php?username=ppowell777 from localhost:8000/login.html using Python scripts as PyScript. The PHP portion that connects to my local MySQL platform works just fine on its own, retrieving data if found and returning a JSON array string, however, localhost:8000/login.html, using pyscript.fetch(url), canโ€™t reach it itself due to CORS error. I have tried everything I can think of, and I visited several tutorials on the matter, and everything I tried was blocked by the browser, and since this is my own CMA on my browser on my laptop and nowhere else on earth, I would only need to fix this ultimately for myself at the moment. Can anyone please help? --Thanks (edited)
Avatar
Avatar
ppowell777
I am doing a very simple fetch(url) to a URL at localhost/cma_mysql/getstuff.php?username=ppowell777 from localhost:8000/login.html using Python scripts as PyScript. The PHP portion that connects to my local MySQL platform works just fine on its own, retrieving data if found and returning a JSON array string, however, localhost:8000/login.html, using pyscript.fetch(url), canโ€™t reach it itself due to CORS error. I have tried everything I can think of, and I visited several tutorials on the matter, and everything I tried was blocked by the browser, and since this is my own CMA on my browser on my laptop and nowhere else on earth, I would only need to fix this ultimately for myself at the moment. Can anyone please help? --Thanks (edited)
JTS.Dawnstar[] 19/05/2025 04:30
Take this with a grain of salt since I'm not the most experienced with web stuff, but I was able to use the pyscript proxy to resolve this issue for one of my apps https://pyscript.com/docs/api-proxies-and-secrets
๐Ÿ‘ 1
04:31
(although in my case it was a generic CORS error, it may be more complicated for your case)
Avatar
Avatar
JTS.Dawnstar[]
(although in my case it was a generic CORS error, it may be more complicated for your case)
ppowell777 19/05/2025 11:17
Ok I created my own API Proxy on PyScript.com, but, now what? My code is on my laptop, running a simple Python http.server pointing to a URL, producing CORS errors (for any URL in the outside world, include my own IIS http://localhost on my same laptop), so how do I use the API Proxy here to work alongside my existing code to help the existing code navigate the "outside world"?
Avatar
Avatar
ppowell777
Ok I created my own API Proxy on PyScript.com, but, now what? My code is on my laptop, running a simple Python http.server pointing to a URL, producing CORS errors (for any URL in the outside world, include my own IIS http://localhost on my same laptop), so how do I use the API Proxy here to work alongside my existing code to help the existing code navigate the "outside world"?
There's a reference to a similar? problem involving fetch which seems similar to yours in discord above: https://discord.com/channels/972017612454232116/972020206538997822/1362021650102616216 Hope this helps? (put that script in right place on your local server)
Avatar
Avatar
Neon22
There's a reference to a similar? problem involving fetch which seems similar to yours in discord above: https://discord.com/channels/972017612454232116/972020206538997822/1362021650102616216 Hope this helps? (put that script in right place on your local server)
ppowell777 19/05/2025 12:47
I'm sorry, but everything you said was in Greek mixed with Ancient Egyptian. I understood none of it, including the original post. The URL trying to reach out to the outside world, literally, is "http://localhost:8000". Nothing more. It's on my laptop offline. It's running on a Python http.server that will do an await fetch('http://localhost/cma/get_stuff.php?username=ppowell777', method="GET"). I can't process how an API Proxy on PyScript.com will be able to communicate with a URL that simply resides on my laptop and nowhere else on the planet to another URL that resides nowhere else on the planet. And the only local server I have is IIS on my laptop, and that has no concept of Python and cannot have a concept of Python apparently ever.
Avatar
ppowell777 19/05/2025 15:39
This is the code I use to fetch a url: async def getUrlBody(url): logger = logging.getLogger(name) if url is None or len(url.strip()) == 0: error = 'url not provided for getUrlBody()' logger.error(error) raise EOFError(error) global json json = '' try: #json = await fetch(url, method="GET").json() json = await fetch(url, { 'method': 'GET', 'headers': { 'Content-Type': 'text/html', 'Access-Control-Allow-Origin': '*' } }).json() except NameError as error: logger.error(error) raise except TypeError as error2: logger.error(error2) raise
Avatar
Avatar
ppowell777
I'm sorry, but everything you said was in Greek mixed with Ancient Egyptian. I understood none of it, including the original post. The URL trying to reach out to the outside world, literally, is "http://localhost:8000". Nothing more. It's on my laptop offline. It's running on a Python http.server that will do an await fetch('http://localhost/cma/get_stuff.php?username=ppowell777', method="GET"). I can't process how an API Proxy on PyScript.com will be able to communicate with a URL that simply resides on my laptop and nowhere else on the planet to another URL that resides nowhere else on the planet. And the only local server I have is IIS on my laptop, and that has no concept of Python and cannot have a concept of Python apparently ever.
So sorry. I admit I can't work out what you're trying to do. I really hope someone else can help you here. Best wishes
Avatar
Original message was deleted or could not be loaded.
There's no pyscript in here and it can't see pyscript websites... Spam ??
Avatar
It's you pyscript an online website because it just a scraping analyzing tool
Avatar
Avatar
Arinze
It's you pyscript an online website because it just a scraping analyzing tool
Andrea Giammarchi 20/05/2025 13:16
not clear what that was but it felt like spam and it's not related to this project or this channel in particular. Please let's keep this place as clean as possible, 3rd party sites or tools are welcome if contextual, here that didn't look contextual at all.
13:17
if anyone needs to know if PyScript needs JS to run ... yes it does, end of analysis
๐Ÿ‘ 1
Avatar
ppowell777 20/05/2025 23:19
I finally figured out how to retrieve url content using pyodide.http.open_url(url) instead, and it works absolutely fine albeit synchronously. I am now stuck. I have another problem with fetch() sending data from http://localhost:8000 to http://localhost:80 where my MySQL database lies: I have a Python function that should send over encoded/encrypted data from a multipart/form-data encoded <form> on http://localhost:8000 via Python server to my http://localhost:80 via IIS to place data into MySQL database table
๐Ÿ‘ 1
Avatar
Deleted User 22/05/2025 09:09
When I was younger, so much younger than today, I never needed anybody's help in any way. But now... I need help with pyscript using pygame-ce and running on pyscript.com. My script tst.py: import pygame as pg pg.init() error_sound = pg.mixer.Sound("error.wav") is not able to read wav files from the project directory. It generates the following error in browser console: Uncaught (in promise) PythonError: Traceback (most recent call last): File "/lib/python312.zip/_pyodide/_base.py", line 597, in eval_code_async await CodeRunner( File "/lib/python312.zip/_pyodide/_base.py", line 411, in run_async coroutine = eval(self.code, globals, locals) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "<exec>", line 3, in <module> FileNotFoundError: No file 'error.wav' found in working directory '/home/pyodide'. All my files are together in the project directory on pyscript.com. The index.html looks like this: <!DOCTYPE html> <html lang="en"> <head> <title>Master Mind</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="https://pyscript.net/releases/2025.2.1/core.css"> <script type="module" src="https://pyscript.net/releases/2025.2.1/core.js"></script> </head> <body> <canvas id="canvas"></canvas> <script type="py-game" src="tst.py" config="tst.toml"></script> </body> </html> and the tst.toml is: name = "Master Mind" packages = ["pygame-ce"] [files] "./error.wav" = "" Won't you please, please help me?
Avatar
Avatar
Deleted User
When I was younger, so much younger than today, I never needed anybody's help in any way. But now... I need help with pyscript using pygame-ce and running on pyscript.com. My script tst.py: import pygame as pg pg.init() error_sound = pg.mixer.Sound("error.wav") is not able to read wav files from the project directory. It generates the following error in browser console: Uncaught (in promise) PythonError: Traceback (most recent call last): File "/lib/python312.zip/_pyodide/_base.py", line 597, in eval_code_async await CodeRunner( File "/lib/python312.zip/_pyodide/_base.py", line 411, in run_async coroutine = eval(self.code, globals, locals) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "<exec>", line 3, in <module> FileNotFoundError: No file 'error.wav' found in working directory '/home/pyodide'. All my files are together in the project directory on pyscript.com. The index.html looks like this: <!DOCTYPE html> <html lang="en"> <head> <title>Master Mind</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="https://pyscript.net/releases/2025.2.1/core.css"> <script type="module" src="https://pyscript.net/releases/2025.2.1/core.js"></script> </head> <body> <canvas id="canvas"></canvas> <script type="py-game" src="tst.py" config="tst.toml"></script> </body> </html> and the tst.toml is: name = "Master Mind" packages = ["pygame-ce"] [files] "./error.wav" = "" Won't you please, please help me?
could be easier if you share a pyscript.com link to the project. Btw .wav is not a very good idea on web , it is a huge raw format. Better use .ogg 22khz mono
๐Ÿ‘ 2
Avatar
Andrea Giammarchi 22/05/2025 10:10
[files] "./error.wav" = "./"
10:10
with a space before [files]
Avatar
Avatar
Deleted User
When I was younger, so much younger than today, I never needed anybody's help in any way. But now... I need help with pyscript using pygame-ce and running on pyscript.com. My script tst.py: import pygame as pg pg.init() error_sound = pg.mixer.Sound("error.wav") is not able to read wav files from the project directory. It generates the following error in browser console: Uncaught (in promise) PythonError: Traceback (most recent call last): File "/lib/python312.zip/_pyodide/_base.py", line 597, in eval_code_async await CodeRunner( File "/lib/python312.zip/_pyodide/_base.py", line 411, in run_async coroutine = eval(self.code, globals, locals) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "<exec>", line 3, in <module> FileNotFoundError: No file 'error.wav' found in working directory '/home/pyodide'. All my files are together in the project directory on pyscript.com. The index.html looks like this: <!DOCTYPE html> <html lang="en"> <head> <title>Master Mind</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="https://pyscript.net/releases/2025.2.1/core.css"> <script type="module" src="https://pyscript.net/releases/2025.2.1/core.js"></script> </head> <body> <canvas id="canvas"></canvas> <script type="py-game" src="tst.py" config="tst.toml"></script> </body> </html> and the tst.toml is: name = "Master Mind" packages = ["pygame-ce"] [files] "./error.wav" = "" Won't you please, please help me?
I appreciate the Beatles reference there... ๐Ÿ˜‰ (edited)
๐Ÿ‘ 1
Avatar
Avatar
pmp-p
could be easier if you share a pyscript.com link to the project. Btw .wav is not a very good idea on web , it is a huge raw format. Better use .ogg 22khz mono
Deleted User 22/05/2025 14:34
Thanks for hint.
Avatar
Avatar
Andrea Giammarchi
[files] "./error.wav" = "./"
Deleted User 22/05/2025 14:35
Thank you, but it didn't help.
Avatar
Andrea Giammarchi 22/05/2025 16:26
if only we could have that online link to check and help
Avatar
Avatar
ppowell777
I finally figured out how to retrieve url content using pyodide.http.open_url(url) instead, and it works absolutely fine albeit synchronously. I am now stuck. I have another problem with fetch() sending data from http://localhost:8000 to http://localhost:80 where my MySQL database lies: I have a Python function that should send over encoded/encrypted data from a multipart/form-data encoded <form> on http://localhost:8000 via Python server to my http://localhost:80 via IIS to place data into MySQL database table
ppowell777 22/05/2025 18:09
Fixed it. I did away with pyscript.fetch and pyodide.http.pyfetch and stuck with pyodide.http.open_url(), which consistently works, and I will be retrieving all data via query strings back and forth, highly encrypted and encoded of course. I just can't grasp how to use fetch within my offline project, and posting form data remotely seems almost impossible for me or for PyScript, but, either way, I found a workaround that works for me, so thanks
Avatar
Andrea Giammarchi 22/05/2025 19:13
offline and "posting data remotely" usually don't work due ... well, offline
Avatar
Hi I am starting learning pyscript. I read the website and it says micro Python is faster because of the file size is small. I want to use it and do some basic web UI work. However the pyscript page only have doc for pyscript not micro python. Why? Are the code and API exact the same as pyscript?
Avatar
Avatar
VM
Hi I am starting learning pyscript. I read the website and it says micro Python is faster because of the file size is small. I want to use it and do some basic web UI work. However the pyscript page only have doc for pyscript not micro python. Why? Are the code and API exact the same as pyscript?
pyscript is the "same" for py (pyodide) or mpy (micrpython). There are some differences because micropython is a subset of cpython and optimised for low ram microcontrollers. It does start faster because it has less to load. But its not necessarily faster once loaded. One main difference is in how packages are loaded. One way (IMHO) to have good flexibility is to use a template which allows you to use both/either until mpy can't do what you want and you have to switch to py. You can probably keep using mpy by putting the pyodide part in a Named worker and getting that job done asynchronously. This hybrid approach is actually the preferred one - Alas you will not find any examples (yet) that demonstrate how to do this. If you want a starter template to have a go with (which also uses the ltk to do Reactive UI components, and allows mpy/py switching) then you could check out this demo here: - https://pyscript.com/@neon22/starter-03-reactiveui/latest?files=README.md One day something like this might make it into "examples". Cheers...
Avatar
RickSanchez76 23/05/2025 18:16
i'm having trouble importing a python file into main.py, is there a way to do so? the code runs fine when it's all in one file, but i'm expecting that the final product would be so large it would be difficult to manage as a single file. the python file i'm trying to import is in the same directory. (edited)
Avatar
Avatar
RickSanchez76
i'm having trouble importing a python file into main.py, is there a way to do so? the code runs fine when it's all in one file, but i'm expecting that the final product would be so large it would be difficult to manage as a single file. the python file i'm trying to import is in the same directory. (edited)
Deleted User 23/05/2025 21:02
Hi, if you have in your main.py for example: import xxx and xxx.py is in the same directory (project directory) as main.py then you should have the following lines in the configuration .toml file: [files] "xxx.py" = ""
Avatar
Avatar
Deleted User
Hi, if you have in your main.py for example: import xxx and xxx.py is in the same directory (project directory) as main.py then you should have the following lines in the configuration .toml file: [files] "xxx.py" = ""
RickSanchez76 23/05/2025 21:30
that works. thanks!
Avatar
more correctly it'd be "./xxx.py" = "" this allows you to put that file in a subdirectory E.g. make an assets directory put the file in there and use: "./assets/xxx.py" = ""
๐Ÿ‘ 1
Avatar
if I'm told I need to load tzdata via pyodide, where should that be done? The .toml file?
18:30
..yes.
Avatar
Is there a way to get type hinting for Pyscript in the development environment. Not necessary, but super helpful in preventing silly mistakes
Avatar
I think somone posted a file you could maybe use in vscode. Try searching discord for it (I may be mistaken). I don't think there is any hinting in the pyscript.com dev interface.
Avatar
RickSanchez76 25/05/2025 16:04
is it not possible to connect to api's through pyscript? i get an error when trying to add the yfinance package.
Avatar
time to see how to access custom attributes, I guess.
Avatar
Avatar
RickSanchez76
is it not possible to connect to api's through pyscript? i get an error when trying to add the yfinance package.
error indicates it can't find a pure python wheel for that package. It needs to be pure python so it can run in the browser...
20:42
There does appear to be a pure python wheel available when I lok on pypi. try specifying the version
Avatar
Avatar
Neon22
There does appear to be a pure python wheel available when I lok on pypi. try specifying the version
RickSanchez76 25/05/2025 21:05
what would that look like in the toml file? i am apparently not the only person who has had this issue, and the conclusion on the internet is: the browser can't open a socket connection therefore no api requests.
Avatar
I went here: https://pypi.org/project/pyfinance/#files and I see its 1.3.0 So the toml will have to for pyodide not mpy and would have a line like: `packages = [ "pyfinance==1.3.0"]
21:46
Looking closer the actual error is for a (probably dependent) package of curl-cffi > 0.7.. looking
21:47
There is no pure python wheel for that
Avatar
Its also quite possible that you cannot initiate a curl op from within the page - but pyodide's openurl works and pyscript's fetch also works for gathering data from web urls. Maybe someone else with more knowledge can speak to that. What aspects of pyfinance are you using. Do you have a demo page on pyscript.com which illuminates what you're trying to do ?
Avatar
If the problem lies inside reading datasets. Its probable that the csv links mentioned here: https://github.com/bsolomon1124/pyfinance/blob/master/pyfinance/datasets.py could/should be directly added to your files in the toml so they can be prefected and made available. This might mean editing the above file so teh links are to these local files. but then it might work
Python package designed for general financial and security returns analysis. - bsolomon1124/pyfinance
Avatar
I tired the example on the official website. It works. However when I change the library from 2024.1.1 to 2025.5.1 , it stopped working. It says pyweb does have warning function.
Avatar
Avatar
Neon22
If the problem lies inside reading datasets. Its probable that the csv links mentioned here: https://github.com/bsolomon1124/pyfinance/blob/master/pyfinance/datasets.py could/should be directly added to your files in the toml so they can be prefected and made available. This might mean editing the above file so teh links are to these local files. but then it might work
RickSanchez76 26/05/2025 02:28
that's not the same package, here is the repo:https://github.com/ranaroussi/yfinance
Download market data from Yahoo! Finance's API. Contribute to ranaroussi/yfinance development by creating an account on GitHub.
Avatar
Avatar
Neon22
Its also quite possible that you cannot initiate a curl op from within the page - but pyodide's openurl works and pyscript's fetch also works for gathering data from web urls. Maybe someone else with more knowledge can speak to that. What aspects of pyfinance are you using. Do you have a demo page on pyscript.com which illuminates what you're trying to do ?
RickSanchez76 26/05/2025 02:31
i do https://ricksanchez76.pyscriptapps.com/lightweight-chart/latest/ though i have 'solved' the issue on the backend i would prefer to do this on the front end if possible to reduce strain on the server and the possible rate limiting of the api.
Avatar
Avatar
RickSanchez76
that's not the same package, here is the repo:https://github.com/ranaroussi/yfinance
ok that uses threading (in utils). Alas not possible in the Browser. Instead teh approach would be to use async/await and/or Named Workers to do the tasks asynchronously. I did not go too deep into it but there are some architectural changes needed. If the finance devs could use asyncio instead of threading. That would be cool. (might be simple to do) Also:
  • consider using 2025.5.1 instead of 2024.1.1. Just so much better.
  • importing lightweight charts in Js might be easier for you to use if you put it in the toml as js-module. You can then more easily use it in python and lose the data in index.html approach - https://docs.pyscript.net/2025.3.1/user-guide/configuration/#javascript-modules
  • for UI. consider using eth ltk. has done all the work - uses wrapped jquery for ui elements. very good to work with - small code, reactive UI as well (for controls). Consider using this as a base: https://pyscript.com/@neon22/starter-03-reactiveui/latest
  • In base.py (in yfinance) is where the curl is referenced. Suggest ask devs how to overload with your functoin intead. Your function (which you can experiment with) would use fetch in a Named worker to get the data as it was asked for. You can probably coerce your current main into testing this and stuffing a chart into an ltk.Div() Ltk kitchensink demo is here: https://pyscript.github.io/ltk/?tab=4&runtime=mpy These architectural changes would possibly enable that package to work.
Avatar
Hmmm. Also I see websockets. This IS built on top of asyncio but its designed to also serve. which you can't do from within a browser. I'm starting to think the road to using yfinance is going to be very difficult - it is designed for the desktop. Perhaps, if you're only using a subset of its functionality, you could fork the parts you need into yfinance_light and move the things you need as you go.
Avatar
Avatar
Neon22
ok that uses threading (in utils). Alas not possible in the Browser. Instead teh approach would be to use async/await and/or Named Workers to do the tasks asynchronously. I did not go too deep into it but there are some architectural changes needed. If the finance devs could use asyncio instead of threading. That would be cool. (might be simple to do) Also:
  • consider using 2025.5.1 instead of 2024.1.1. Just so much better.
  • importing lightweight charts in Js might be easier for you to use if you put it in the toml as js-module. You can then more easily use it in python and lose the data in index.html approach - https://docs.pyscript.net/2025.3.1/user-guide/configuration/#javascript-modules
  • for UI. consider using eth ltk. has done all the work - uses wrapped jquery for ui elements. very good to work with - small code, reactive UI as well (for controls). Consider using this as a base: https://pyscript.com/@neon22/starter-03-reactiveui/latest
  • In base.py (in yfinance) is where the curl is referenced. Suggest ask devs how to overload with your functoin intead. Your function (which you can experiment with) would use fetch in a Named worker to get the data as it was asked for. You can probably coerce your current main into testing this and stuffing a chart into an ltk.Div() Ltk kitchensink demo is here: https://pyscript.github.io/ltk/?tab=4&runtime=mpy These architectural changes would possibly enable that package to work.
RickSanchez76 26/05/2025 04:29
i appreciate the thorough analysis. there is a python implementation of lightweight charts though it does not seem to be intended for use in the browser which is why i was utilizing the js version. https://github.com/louisnw01/lightweight-charts-python as for yfinance i am really only using yfinance.download() or yfinance.Ticker.history() which appears to use tickers.py and multi.py for the request handling. i do have yfinance working in the js version of lightweight charts through the backend of my flask app through some weird code which is sure to make things tedious to implement across the app. i don't have much experience with any of these technologies since i work mostly on cli tools so i appreciate any insight.
Python framework for TradingView's Lightweight Charts JavaScript library. - louisnw01/lightweight-charts-python
Avatar
WIsh I had the time but here's an approach. Using the ltk for UI elements. mpyfinance and matplotli to do teh Candlestick graphs. doc on using matplotlib here but not my thing. https://www.geeksforgeeks.org/how-to-create-a-candlestick-chart-in-matplotlib/
Your All-in-One Learning Portal: GeeksforGeeks is a comprehensive educational platform that empowers learners across domains-spanning computer science and programming, school education, upskilling, commerce, software tools, competitive exams, and more.
Avatar
Avatar
Neon22
WIsh I had the time but here's an approach. Using the ltk for UI elements. mpyfinance and matplotli to do teh Candlestick graphs. doc on using matplotlib here but not my thing. https://www.geeksforgeeks.org/how-to-create-a-candlestick-chart-in-matplotlib/
RickSanchez76 26/05/2025 14:45
yeah, i started with the matplotlib example pyscript. the lightweight charts library is much more usable. tradingview really does have the best charts.
14:51
here's an example of what i was able to implement on the server.
14:56
for reference this is how many features tradingview has. i don't have 1000 people to work on a charting library like they do, but there are some limitations to the platform that i am trying to resolve by building my own.
Avatar
.. always fun when pyscript.com decides to be wrong. stares perplexedly at the website insisting something isn't there when it clearly is
21:17
.. oh. Neeevermind. It's an error on my end, and PyCharm didn't catch it so I couldn't figure out what it was
๐Ÿ‘ 1
21:18
do kinda wish it'd stop silently failing a lot
Avatar
Mild but relevant question
03:06
How do I tell pyodide to ignore something (such as {% load static %}) so django can work? >_>
Avatar
Avatar
Kylindra
How do I tell pyodide to ignore something (such as {% load static %}) so django can work? >_>
maybe import jinga2 (is that what its called) which allows templating. But honestly not enough info to answer your question.
Avatar
Avatar
RickSanchez76
here's an example of what i was able to implement on the server.
you should be able to fetch that using namedworker and fetch - https://docs.pyscript.net/2025.3.1/api/#pyscriptfetch
Avatar
Avatar
Neon22
you should be able to fetch that using namedworker and fetch - https://docs.pyscript.net/2025.3.1/api/#pyscriptfetch
RickSanchez76 27/05/2025 06:23
great. i'll take a look in the morning. just got done refactoring thousands of lines of js spaghetti code. someone wrote dozens of single use functions all over the place and it didn't make a lot of sense. i now have some decent boilerplate for the chart features though, with hotkeys.
Avatar
RickSanchez76 27/05/2025 06:30
i'll have to merge these charts before i implement all the new features, and connect it to real data but it should look nice when it's done.
๐Ÿ‘ 1
Avatar
Avatar
Neon22
maybe import jinga2 (is that what its called) which allows templating. But honestly not enough info to answer your question.
the problem is it needs to run in the .py file so django can replace it. I worked around it by hardcoding the static path bug
20:52
*but that's not ar eal fix
Avatar
Weird. "alert(window.location.hostname)" cause it to throw an error but that's given in the pyscript docs as an example
06:08
ah-ha. It's not pyscript, it's django
Avatar
.. or is it pyscript..?
08:15
the problem is:
08:15
08:15
i've flushed the cache, restarted the server, deleted all staticfiles, I don't get this.
Avatar
(the solution apparently is to do both)
Avatar
_encopresis_ 18/06/2025 22:33
I am new to pyscript, I am having trouble with DOM manipulation. from pyscript import document Is not importing document (this is how you're supposed to do it in the documentation).
Avatar
I'm playing around with getting a simple drag-and-drop example going (allowing dragging of an element from one part of the DOM tree to another). I've basically been porting this HTML & vanilla JS example to use PyScript. I eventually got it to work, but there was one particular rough edge that I thought I'd ask about. Here are the relevant python functions that handle the various drag and drop events: @when('dragstart', '.draggable') def on_dragstarted(event): print('drag started') event.dataTransfer.clearData() event.dataTransfer.setData('text/plain', event.target.id) event.currentTarget.style.backgroundColor = 'yellow' @when('dragover', '.dropzone') def on_dragover(event): event.preventDefault() @when('drop', '.dropzone') def on_drop(event): print('item dropped on dropzone') drag_id = event.dataTransfer.getData('text') drag_element = page[f'#{drag_id}'][0] dropzone = event.target dropzone.append(drag_element._dom_element) It's those last few lines that caught me out. 1. Getting the dragged element via the page['#{drag_id}'][0] code is pretty verbose. Is there really no Python equivalent there to the JS .getElementById() call? 2. The page['#{drag_id}'][0] expression returns a pyscript.web.Element object, but event and event.target are JavaScript objects. This means if I try to just do dropzone.append(drag_element) I don't get the desired behavior. With Pyodide, a new element simply gets created with text like '<pyscript.web.div object at 0xda5490>'. In Micropython, I get some errors: Aborted(Assertion failed: stringToUTF8Array expects a string (got symbol)) micropython.mjs:1:10797 Uncaught RuntimeError: Aborted(Assertion failed: stringToUTF8Array expects a string (got symbol)) Is there a better way to get the event target so I don't have to use ._dom_element? (edited)
In this tutorial, we will build a drag-and-drop example using the HTML Drag and Drop API with vanilla JavaScript to use the event handlers.
Avatar
Avatar
_encopresis_
I am new to pyscript, I am having trouble with DOM manipulation. from pyscript import document Is not importing document (this is how you're supposed to do it in the documentation).
if you'd like to have a go using the ltk, which also gives you reactive ui elements then there is a good (if I say so myself) example of DnD here in his tutorial pyscript.com link. https://pyscript.com/@neon22/starter-06-dnd/latest?files=README.md
Avatar
_encopresis_ 19/06/2025 07:20
I'll have to look more into that, thanks
Avatar
Is there a more direct equivalent to JavaScript's .getElementById() call than using the page object's .find() call? Using something like page['#my-element-id'] produces a sequence, so you still need to index into it to grab the [0] element. Additionally, if you have the element's ID in a variable, you then need to do something like page[f'#{element_id}'][0], which is very verbose.
Avatar
Good question. I've always been a bit wary of that decision... to return a collection. Suggestions welcome!
Avatar
Ok, so I'm not missing something then, good to know. I was going to suggest that perhaps when the query is an ID (starts with '#') we could return the object directly instead of a collection, but that just takes care of the need for the [0] and leaves the need to construct the ID w/ the f-string or prepending '#' some other way. Seems like having a specific query method for searching by ID would be helpful. Seems to be the same decision the JavaScript designers came to as well.
01:16
Then my other question above basically boils down to: Is there a way to get the target object of an event as a Python/MicroPython object instead of a JavaScript object? event.target always returns a JS object.
Avatar
Consider the ltk, it uses jquery as underlying model. I was initially skeptical about this but jQuery has already standarised lots of these kinds of interactions. e.g. ltk.find(selector) works for "#idname", ".aclassname", and any kind of gt, lt etc selector shennanigans that jQuery supports - which is all of them. Also the ltk can force a list (python list) return if you use ltk.find_list() instead of find. or leave it in jQuery land for chaining ops in the same way jQuery does.
Avatar
Is there documentation anywhere for what the input arguments are for the various HTML elements in pyscript.web.*? The docs just say that 'Usage of these is explained in the user guide,' but the user guide doesn't give those details and refers back to that first page.
20:05
It gives some example usage, but they are all examples with simple positional arguments (and even there, it appears that you get different results depending on what you actually pass in for those arguments, such as plain text vs other elements).
Avatar
I'm testing out the new 2025.7.1. I'm running into an issue that didn't exist in the 5.1 version. Notably, I'm getting this error: "Unhandled Promise Rejection: Error: Object has already been destroyed...". This seems to occur for any python function called from the JavaScript side. Currently, on the python side these are setup as follows at the end of the script: js.startBootstrapWrapper = startBootstrapWrapper js.buildTableWrapper = buildTableWrapper js.calcMeansSDMW = calcMeansSDMW js.passFileData = passFileData js.getListData = getListData js.checkCommSystem = checkCommSystem (the beginning of the script loads from "pyscript import window as js".) Note that these are async functions and running in a worker. Is this no long the proper way to do this? (edited)
Avatar
I wonder if you have to .new() one of those
Avatar
Avatar
Ben Smith
I'm testing out the new 2025.7.1. I'm running into an issue that didn't exist in the 5.1 version. Notably, I'm getting this error: "Unhandled Promise Rejection: Error: Object has already been destroyed...". This seems to occur for any python function called from the JavaScript side. Currently, on the python side these are setup as follows at the end of the script: js.startBootstrapWrapper = startBootstrapWrapper js.buildTableWrapper = buildTableWrapper js.calcMeansSDMW = calcMeansSDMW js.passFileData = passFileData js.getListData = getListData js.checkCommSystem = checkCommSystem (the beginning of the script loads from "pyscript import window as js".) Note that these are async functions and running in a worker. Is this no long the proper way to do this? (edited)
This still persists in version 2025.7.2
Avatar
Avatar
Ben Smith
This still persists in version 2025.7.2
I've also tried to explicitly using create_proxy and turning on experimental_create_proxy. I'm not sure what else to try.
Ben Smith started a thread. 06/07/2025 03:17
Avatar
I'm trying to re-implement a little Python script that I have as a terminal app in PyScript. As of now I'm just trying to test if I can get an interactive terminal in PyScript to work. I'm using the example in the documentation: <!doctype html> <html lang="en"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width,initial-scale=1" /> <title>PyScript!</title> <link rel="stylesheet" href="https://pyscript.net/releases/2025.7.3/core.css" /> <script type="module" src="https://pyscript.net/releases/2025.7.3/core.js" ></script> </head> <body> <script type="py" terminal worker> name = input("What is your name? ") print(f"Hello, {name}") </script> </body> </html> But I get an error: <PyodideFuture pending cb=[WebLoop._decrement_in_progress()]>. What am I doing wrong here?
Avatar
Avatar
aknaton
I'm trying to re-implement a little Python script that I have as a terminal app in PyScript. As of now I'm just trying to test if I can get an interactive terminal in PyScript to work. I'm using the example in the documentation: <!doctype html> <html lang="en"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width,initial-scale=1" /> <title>PyScript!</title> <link rel="stylesheet" href="https://pyscript.net/releases/2025.7.3/core.css" /> <script type="module" src="https://pyscript.net/releases/2025.7.3/core.js" ></script> </head> <body> <script type="py" terminal worker> name = input("What is your name? ") print(f"Hello, {name}") </script> </body> </html> But I get an error: <PyodideFuture pending cb=[WebLoop._decrement_in_progress()]>. What am I doing wrong here?
Statbot BOT 14/07/2025 07:37
(edited)
Avatar
Avatar
aknaton
I'm trying to re-implement a little Python script that I have as a terminal app in PyScript. As of now I'm just trying to test if I can get an interactive terminal in PyScript to work. I'm using the example in the documentation: <!doctype html> <html lang="en"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width,initial-scale=1" /> <title>PyScript!</title> <link rel="stylesheet" href="https://pyscript.net/releases/2025.7.3/core.css" /> <script type="module" src="https://pyscript.net/releases/2025.7.3/core.js" ></script> </head> <body> <script type="py" terminal worker> name = input("What is your name? ") print(f"Hello, {name}") </script> </body> </html> But I get an error: <PyodideFuture pending cb=[WebLoop._decrement_in_progress()]>. What am I doing wrong here?
Do you have a URL so we can take a look?
Avatar
Avatar
ntoll
Do you have a URL so we can take a look?
Avatar
Aha. So your webserver is configured with a very restrictive referrer policy of strict-origin-when-cross-origin which is stopping Pyodide from working. Contrast this with pyscript.com whose referrer policy is no-referrer.
08:50
For Pyodide to use a worker it needs a browser feature called shared array buffer (SAB). SAB is only available when you have a less restrictive referrer policy. Your policy is saying not to trust content from other websites to which you may link (such as pulling in PyScript in your head).
08:51
I have no idea what server you're using, but I guess a quick Google of "apache referrer policy config" (for example) might be helpful..?
08:52
Essentially you need to get your server to say that content from third party sources (like pyscript.net) is OK to use because you trust it.
08:52
Hope this helps... keep asking questions! ๐Ÿ‘
Avatar
Avatar
ntoll
Aha. So your webserver is configured with a very restrictive referrer policy of strict-origin-when-cross-origin which is stopping Pyodide from working. Contrast this with pyscript.com whose referrer policy is no-referrer.
I uploaded the file to my site (Azure) just now. I got the same error when i ran it locally with just python3 -m http.server. But thanks for the feedback. I will look into it.
08:53
so if you ran it locally with python did you visit 127.0.0.1 or localhost..?
08:53
(believe it or not, this is important)
โ˜๏ธ 1
Avatar
localhost
08:54
I have just done exactly the same with equivalent code, with latest PyScript, and it just works for me. In fact, I'm about to use this in a PyScript tutorial this afternoon.
08:54
Not sure what's going on.
08:54
What browser are you using?
Avatar
Chrome on Linux (I have also tested it on my Windows box with the same result)
Avatar
So I'm on Chrome with Linux.
09:00
OK... I can see the error now.
09:02
Again, it's because Python has the same referrer policy. ๐Ÿ˜•
09:06
Once this is updated, the error goes away. The best thing to do is use mini-coi which ensures the local server is configured and serves with the correct headers. Try this command (assuming you have npx installed): npx mini-coi . (to serve your local directory). I've just tried it and it works - proving that it's as I expected... an issue with the server configuration.
๐Ÿ‘ 1
09:10
Hope this helps @aknaton ^^^ - I'm trying to work out why the default referrer policy for python -m http.server is set the way it is. I've just updated to a new laptop and a new (Fedora) version of Linux.
09:11
It used to work on my older laptop (Ubuntu).
09:11
The thing is, http.server is a very naive and simple implementation for local testing purposes only. I'm not sure how easily configurable it is.
Avatar
Running it with npx mini-coi works like a charm. I will have to look into if I can configure my azure site. Many thanks for the help!
Avatar
NP - thank you for bringing this to our attention. Web servers and browsers (and how they interact) is a complicated story. We want it to be as simple and easy as possible. I'm gonna dig into http.server and see if we can document it somewhere since many Python folks will default to this without realising mini-coi is available. ๐Ÿ‘
09:18
ChatGPT just suggested:
The built-in Python http.server module doesn't provide a command-line option to change HTTP headers like Referrer-Policy directly. However, you can achieve this by subclassing the request handler and adding the desired header.
from http.server import HTTPServer, SimpleHTTPRequestHandler class CustomHandler(SimpleHTTPRequestHandler): def end_headers(self): # Add Referrer-Policy header self.send_header('Referrer-Policy', 'no-referrer') super().end_headers() if __name__ == '__main__': port = 8000 server_address = ('', port) httpd = HTTPServer(server_address, CustomHandler) print(f"Serving on port {port} with Referrer-Policy: no-referrer") httpd.serve_forever()
๐Ÿ‘ 1
09:23
Hmm... but that still doesn't work. I suspect more headers need to be set (as happens in mini-coi). (edited)
Avatar
Andrea Giammarchi 14/07/2025 09:26
you need mor than that for SAB, 3 headers which are the one mini-coi enables
09:26
A minimalistic version of coi-serviceworker. Contribute to WebReflection/mini-coi development by creating an account on GitHub.
09:27
h.set('Cross-Origin-Opener-Policy', 'same-origin'); h.set('Cross-Origin-Embedder-Policy', 'require-corp'); h.set('Cross-Origin-Resource-Policy', 'cross-origin');
๐Ÿ‘ 1
Avatar
Thanks @Andrea Giammarchi and @ntoll . I haven't been able to make my static Azure Web Site work. But I have gotten a Netlify site to work (where it was much easier to configure the headers).
Avatar
Absolutely no problem at all... glad you've got it working. While we rely on headers, and can tell which ones you should use, we can't actually fix your server setup for you. ๐Ÿ˜‰
Avatar
Still got problems. I get this error: Command>Traceback (most recent call last): File "/lib/python312.zip/pyodide/_base.py", line 597, in eval_code_async await CodeRunner( File "/lib/python312.zip/_pyodide/_base.py", line 411, in run_async coroutine = eval(self.code, globals, locals) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "<exec>", line 16, in <module> File "https://pyscript.net/releases/2025.7.3/core-D0lb6Ll.js", line 1, in MessagePort.<anonymous> File "https://pyscript.net/releases/2025.7.3/core-D0lb6Ll_.js", line 1, in ??? File "https://pyscript.net/releases/2025.7.3/xterm-readline-CYfUpnbV.js", line 1, in C.Readline.read File "https://pyscript.net/releases/2025.7.3/xterm-readline-CYfUpnbV.js", line 1, in ??? File "https://pyscript.net/releases/2025.7.3/xterm-readline-CYfUpnbV.js", line 1, in new h.State File "https://pyscript.net/releases/2025.7.3/xterm-readline-CYfUpnbV.js", line 1, in f.Tty.calculatePosition pyodide.ffi.JsException: TypeError: u is not iterable I intepret this that the code (on the js side) has problems with the input in the terminal. My site is available at: https://exquisite-basbousa-8aad52.netlify.app/ The headers are like: โฏ curl -I https://exquisite-basbousa-8aad52.netlify.app/ HTTP/2 200 accept-ranges: bytes age: 0 cache-control: public,max-age=0,must-revalidate cache-status: "Netlify Edge"; fwd=miss content-type: text/html; charset=UTF-8 cross-origin-embedder-policy: require-corp cross-origin-opener-policy: same-origin cross-origin-resource-policy: cross-origin date: Wed, 16 Jul 2025 12:27:27 GMT etag: "a1ad4bb13a6dddbf7033cd8c03d60795-ssl" referrer-policy: no-referrer server: Netlify strict-transport-security: max-age=31536000; includeSubDomains; preload x-nf-request-id: 01K09K8FB58T4S4G762W864MAG content-length: 727 Oneone has any thoughts about this?
Avatar
@aknaton it looks like the imports are all ok. you don't need / (can't use) sys though. The problem seems to be using input from the terminal. Its getting jammed up in an xterm readline function.
05:45
Consider using the ltk and ltk.Input() to gather the typed in value.
๐Ÿ‘ 1
Avatar
@Neon22 Thanks for the feedback. I'll look into the ltk stuff.
Avatar
@aknaton Here's a starter using the ltk with reactive UI. I only imported oracle as an example. you'll have to do the rest. I had to change to pyodide (micropython would be faster to start) but you used the enum module which is not available in mpy. Cheers https://pyscript.com/@neon22/oracle-starter/latest?files=main.py
Avatar
Tank you for the example ๐Ÿ˜… . It was very kind.
Avatar
pliroforiki 19/07/2025 19:33
hi all, I would like to have a REPL that when student presses RUN to run an assert to check if answer is correct. I pinpoint that to the dev team in GitHub and said it was fixed. see: https://github.com/pyscript/pyscript/issues/2320 But it does not work.
Checklist I added a descriptive title I searched for other issues and couldn&#39;t find a solution or duplication I already searched in Google and didn&#39;t find any good information or help What ...
Avatar
Hi, I have a button element in my HTML code: <button py-click="test">test</button>, however when I deploy this the button is not clickable. What could be the problem?
Avatar
@koko ๐Ÿ‘‹ the py-click attribute is deprecated. Use the @when decorator instead. See: https://docs.pyscript.net/2025.7.3/api/#pyscriptwhen Happy to answer questions. <button id="my-button">Click Me</button> from pyscript import when @when("click", "#my-button") def handle_click(event): # Do stuff
๐Ÿ‘ 1
Avatar
I just got caught out by micropython's lack of a step when slicing strings. E.g. a= "Foo" and a[::-1] fails... is this on the roadmap for mpy ? Seems it got closed a couple of years ago in mpy issues... So using: "".join(reversed(a))
Avatar
Avatar
Neon22
I just got caught out by micropython's lack of a step when slicing strings. E.g. a= "Foo" and a[::-1] fails... is this on the roadmap for mpy ? Seems it got closed a couple of years ago in mpy issues... So using: "".join(reversed(a))
I might be ignorant of something obvious, but I can't immediately see why it shouldn't be possible to add it in the mpy codebase ๐Ÿค” If so with a somewhat discordant helper function. https://github.com/micropython/micropython/blob/master/py/objstrunicode.c#L195 (Do you have a link to the issue that was closed? Would be interesting to see)
Avatar
@edw often the MicroPython philosophy is to try to leave as much out as possible (for space reasons - remember MP is mostly run on microcontrollers that may only have just 16k of memory!), especially if the same effect can be achieved through existing capabilities.
๐Ÿ‘Œ 1
Avatar
Avatar
ntoll
@edw often the MicroPython philosophy is to try to leave as much out as possible (for space reasons - remember MP is mostly run on microcontrollers that may only have just 16k of memory!), especially if the same effect can be achieved through existing capabilities.
yeah, so I understand :-) it's apparent from the pretty wild number of defines one can use to limit what's included in a build
Avatar
Yes the micropython build is complex. I think we are helped by this because its acknowledged that the wasm port can be quite bit larger (e.g. stack sizes have been increased). However to make full use of this "free pass" requires quite a lot of work. Alas I cannot allocate time to work on the wasm port PRs. So I have a local solution and on we go ๐Ÿ™‚
๐Ÿ‘Œ 1
Avatar
What is the received wisdom for the best way to load a remote csv file (quite large) and then query it in a speedy fashion. Is it:
  • use csv package and hope it all fits into ram,
  • use pandas and to_sql to read it and get it into some kind of sql form stored in local browser fs?
  • use the new thing (sorry forgotten who's writing it) which allows remote access to very large files in some way ?
  • something else...?
Avatar
I'd try them in the order in which you listed the suggestion. ๐Ÿ˜‰
Avatar
blackbooks 08/08/2025 12:43
Hi, i have a flask project with a vital pyscript component which needs to pass serious data security checks. I will probably need to establish a robust vulnerability tracking within my ci/cd pipeline. From what i gathered, i will need to generate a software bill of materials (SBOM) for each of my releases and pass this to a tracking software like dependency-track which continuously scans for newfound vulnerabilites in various databases. My issues is, that PyScript and its dependencies as well as the libraries configured for the virtual environment in the browser are not necessarily part of the requirements of my project, since they are loaded locally during use. Now the most straightforward workaround would be to simply manually add pyscript and the libraries for the VE in my project and force them like this into the requirements and the sbom. Or is there are more reliable way to automate this process?
Avatar
@blackbooks I suspect your suggestion is the path of least resistance at this moment in time.
Avatar
is there a way to get nice typehinting/autocomplete for import pyscript or import js features in IDE/VSCode?
Avatar
@Djo some folks in teh community are already working on this.
Avatar
Avatar
ntoll
@Djo some folks in teh community are already working on this.
Thanks, thatโ€™s awesome will keep an eye out ๐Ÿ™‚
๐Ÿ‘ 1
Avatar
Paul Everitt 11/08/2025 17:35
Hi, quick question about MicroPython builds. It appears PyScript does an early import of pathlib. https://github.com/pyscript/pyscript/blob/67fa31e4eab3b98f3f8fc364990736e528539d2f/core/src/stdlib.js#L36 But MicroPython doesn't have pathlib. Do you ship a patched MicroPython that uses the 3rd party implementation?
PyScript is an open source platform for Python in the browser. Try PyScript: https://pyscript.com Examples: https://tinyurl.com/pyscript-examples Community: https://discord.gg/HxvBtukrg2 - pyscri...
Avatar
@Paul Everitt hey hey... MicroPython has a large number of build flags to enable/disable various things. I believe pathlib is one of those. The build you may have could be a simple/vanilla build. The build Damien creates for us has everything but the kitchen sink (well, in MicroPython terms... it's still not all that much stuff) turned on.
Avatar
Paul Everitt 11/08/2025 18:25
Thanks for the fast reply! I'm using a build from Koudai that has t-string support. I'll pass along to him.
Avatar
is pyscript down?
Avatar
metalmephisto 12/08/2025 21:44
Hey everyone. I'm very new to using py-script. Is it possible for me to import the library PyMuPDF? Or?
Avatar
Avatar
metalmephisto
Hey everyone. I'm very new to using py-script. Is it possible for me to import the library PyMuPDF? Or?
on pypi there are several variants of pymupdf. - https://pypi.org/search/?q=PyMuPDF The first one in that list, and most recent, does not have a pure python version available. You can see that by looking in the "Download files" section https://pypi.org/project/PyMuPDF/#files So as there is no pure python version and no wasm version - you're not going to be able to use it in the browser - you could ask them to make a wasm build but YMMV. I use fpdf2 when I need to write pdf files. Not sure of your usecase though. If you were to use fpdf2 you'd enable it by adding this to your toml file packages = [ "fpdf2==2.8.1"] and then import fpdf2 as PDF in your main (edited)
Avatar
Avatar
M4D4Y
is pyscript down?
if it was - does not seem to be anymore. BUT if you are using some very old script from random web location - its most likely that your version of pyscript is gone, or the syntax has changed. Docs are: https://docs.pyscript.net/2025.8.1/beginning-pyscript/#indexhtml (edited)
Avatar
Avatar
Neon22
on pypi there are several variants of pymupdf. - https://pypi.org/search/?q=PyMuPDF The first one in that list, and most recent, does not have a pure python version available. You can see that by looking in the "Download files" section https://pypi.org/project/PyMuPDF/#files So as there is no pure python version and no wasm version - you're not going to be able to use it in the browser - you could ask them to make a wasm build but YMMV. I use fpdf2 when I need to write pdf files. Not sure of your usecase though. If you were to use fpdf2 you'd enable it by adding this to your toml file packages = [ "fpdf2==2.8.1"] and then import fpdf2 as PDF in your main (edited)
metalmephisto 13/08/2025 00:52
Is it easy to compile it to wasm myself?
Avatar
well Its beyond me but I'd hate suggest it is very difficult if you're into compilation...
๐Ÿ‘ 1
00:53
When you've written a new code module in a language like C/C++, you can compile it into WebAssembly using a tool like Emscripten. Let's look at how it works.
Avatar
Avatar
M4D4Y
is pyscript down?
It's currently working for me
Avatar
๐•ฃ๐•ฉ๐••๐•š๐•’๐•ฅ๐•š๐• ๐•Ÿ๐•ฉ 14/08/2025 14:23
How do I clear .py file cache in pyscript
โž• 1
14:23
The browser keeps using the old ones even If I update them
Avatar
Avatar
๐•ฃ๐•ฉ๐••๐•š๐•’๐•ฅ๐•š๐• ๐•Ÿ๐•ฉ
How do I clear .py file cache in pyscript
Can you add a little more info. Personally I am not having this problem. Is this on pyscript.com, or localhost, or your own webserver ?
Avatar
๐•ฃ๐•ฉ๐••๐•š๐•’๐•ฅ๐•š๐• ๐•Ÿ๐•ฉ 15/08/2025 03:56
Like even if I update my python files, the browser keeps using the old ones
03:56
This is on localhost
Avatar
Avatar
๐•ฃ๐•ฉ๐••๐•š๐•’๐•ฅ๐•š๐• ๐•Ÿ๐•ฉ
Like even if I update my python files, the browser keeps using the old ones
for chrome, open dev tools, go to Network tab, and tick [] Disable cache
Avatar
Avatar
Djo
for chrome, open dev tools, go to Network tab, and tick [] Disable cache
๐•ฃ๐•ฉ๐••๐•š๐•’๐•ฅ๐•š๐• ๐•Ÿ๐•ฉ 16/08/2025 09:31
okay thank you
Avatar
Got caught out again I think. Trying to "Download" a file created in the browser to the use's machine. Works in Pyodide but fails in mpy because no assign in js for micropython. Is there a way to do this in mpy, I don't want to have to add a worker just to do this task... encoded_data = data.encode('utf-8') my_stream = io.BytesIO(encoded_data) js_array = Uint8Array.new(len(encoded_data)) js_array.assign(my_stream.getbuffer()) #! FAILS in mpy file = File.new([js_array], "unused_file_name.txt", {type: "text/plain"}) url = URL.createObjectURL(file) hidden_link = document.createElement("a") hidden_link.setAttribute("download", "my_other_file_name.txt") hidden_link.setAttribute("href", url) hidden_link.click()
Avatar
Uint8Array.new(encoded_data) does not work ?
Avatar
Avatar
pmp-p
Uint8Array.new(encoded_data) does not work ?
yes - perfect - so simple thanks
22:39
ahhh but its empty ๐Ÿ™ I wil lkeep looking thanks for the tip
Avatar
Avatar
Neon22
ahhh but its empty ๐Ÿ™ I wil lkeep looking thanks for the tip
Andrea Giammarchi 04/09/2025 13:14
what is js_array.assign ??? that looks like a JS pollution only Pyodide provide ... we do, however, convert Python things to JS things on occasion and that's via something like: def as_jsview(buffer): pyview = memoryview(buffer) jsview = js.Uint8Array.new(len(pyview)) for i in range(len(pyview)): jsview[i] = pyview[i] return jsview
Avatar
I have taken advantage of the Uint8Array.new(encoded_data) which allows initialization of the data rather than use assign which pyodide put in for adding the data later. This works. My ref for assign was from the recipe here (which is now out of date) https://pyscript.recipes/2024.5.2/basic/file-download/ I can confirm all works well in mpy and py. I will submit a PR to docs of an improved download routine which uses IO streams as sometimes the file handle is not always available. e.g. when calling something like the RawConfigParser. My new one uses binary or text streams and converts to bin to 'save' so all mimetypes are handled by one routine.
22:25
once again - that link is now out of date
๐Ÿ™Œ 1
Avatar
Here is some feedback as a beginner user: 1. I am trying to use custom packages in PyScript. For example I have folders and subfolders inside my static folder. Trying to import them has led to errors and nothing about my code seems off. I tried doing "files": { "./mypackage/init.py": "./mypackage/init.py", "./mypackage/myfile.py": "./mypackage/myfile.py", } This leads to an error stemming in main.py that reads like this: PythonError: Traceback (most recent call last): File "<stdin>", line 3, in <module> File "mypackage/myfile.py", line 8, in init SyntaxError: invalid syntax (at micropython.mjs:1:101857) at proxy_convert_mp_to_js_obj_jsside (micropython.mjs:1:101857) at proxy_convert_mp_to_js_obj_jsside_with_free (micropython.mjs:1:103869) at Object.runPythonAsync (micropython.mjs:1:93348) at Object.$t [as runAsync] (_python.js:66:34) at I.e.<computed> (utils.js:79:34) at async Object.runAsync (hooks.js:54:23) 2. None of the Examples in pyscript.com use custom packages/modules. They are mainly one-file python applications. It would be nice to have at least one example that actually imports custom modules. 3. In the documentation, the files section of config shows you how to import files in different ways. However, custom modules might be an important use case here that is currently not present in the docs (as far as I can see). 4. The error that I got when importing a module is a bit cryptic. And in fact, it makes it seem like the error is in myfile.py line 8, or in "<stdin>" line 3. When in reality, the error comes from main.py line 3! So really I would expect an error like File "main.py", line 3... because this is the origin of the error. Instead, the error pointed me to myfile.py which was not related to the issue. So in other words, this error might be useful to an advanced user but for me it was confusing. (edited)
01:01
5. A syntax error in pyscript.json failed silently. For example, I had this: { "packages": [], } The trailing comma caused none of my GUI components to load. But no error was shown. It took me a while to spot that trailing comma. Thank you!
Avatar
UPDATE: Regarding Point #4, the reason it is pointing to myfile.py as an error source is because the browser was using an OUTDATED version of the file. And indeed, this line also contained a module import which was causing the error. Despite hard refresh, the browser was using an outdated version of the file, and I had to open a new browser window in order to show the latest version. I have still not figured out how to import custom modules but I am working on it. UPDATE2: The package/module imports were working fine. The confusion was caused due to updated files. I removed all contents from my module and the error was pointing to the import, so I thought the error was the package import. In reality, when opening an incognito window I was able to notice that the error was in the module itself BEFORE I deleted the contents. Incognito window was able to obtain the latest version of the file. As to why the regular browser was not refreshing the module, I don't know. But the error was inside the old version of the module. UPDATE3: Never mind my comments about the error being confusing. The only thing confusing was that it was pointing to a line in an empty file. But this was caused due to the micropython file being outdated despite hard refresh. That's what made it confusing. But after using incognito window I could see that the error was indeed a syntax error from the original file. (edited)
Avatar
also try this pattern when setting file location in the toml file (or json, I am using toml so my patter is in toml) [files] "./yourdirname/foo.py" = "" Which has the foo.py file under the directory yourdirname. Cannot stress enough that the first one needs a "./" and after the '=""' has no dot Then import foo will work as expected
Avatar
Yes but if you have multiple packages like /dir1/module1 .py /dir2/module2.py, then the init files will have name conflict. By the way, I tried running without adding init.py to the files list and it still worked. Does that mean we don't need to add init.py to be able to import modules?
Avatar
I have a question, how do I set data attributes? Like in a pyscript web div. I had trouble finding it in the docs. I prefer to set them in initialization like div(classes=[...], data_myattr='myattr') but I am curious as to all the other ways too Edit: I suppose it uses setattr from _dom_element. this means I must look inside Pyodide source code I suppose. Namely pyodide.ffi.JsProxy Edit2: I personally think there should be a little more info about getting and setting attributes in the pyscript tutorial. There seem to be more than one way but unless one is very versed in JS, it can be a little difficult. Edit3: I suppose I have to use setAttribute and it is impossible to do in constructor. Please mention this somewhere in tutorial or docs? It was quite difficult to find this out. (edited)
Avatar
@nitau this is great feedback. Thank you. Both Andrea and I have tasks to improve docs and examples this quarter.
โค๏ธ 1
08:06
Your feedback will help.
Avatar
Hello, I am encountering this issue: When I use @when, the event listeners will be attached to whatever elements are present in the DOM that match that query. However, when I dynamically add new elements to the DOM that also match, the events will no longer trigger. If I want to reattach the event listeners using when() then I will get an error such as: ..File "/lib/python313.zip/inspect.py", line 3344, in signature return Signature.fromcallable(obj, followwrapped=follow_wrapped, ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ globals=globals, locals=locals, eval_str=eval_str) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/lib/python313.zip/inspect.py", line 3070, in from_callable return _signature_from_callable(obj, sigcls=cls, follow_wrapper_chains=follow_wrapped, globals=globals, locals=locals, eval_str=eval_str) File "/lib/python313.zip/inspect.py", line 2514, in _signature_from_callable obj = unwrap(obj, stop=(lambda f: hasattr(f, "__signature") or isinstance(f, types.MethodType))) File "/lib/python313.zip/inspect.py", line 791, in unwrap raise ValueError('wrapper loop when unwrapping {!r}'.format(f)) ValueError: wrapper loop when unwrapping <function handle_click at 0x10369f8> This is related to the behavior of the @when only working with elements that are on the page when "when" is called. If you call "when" when the events are not yet on the page, it will not work. And similarly, when you add elements after "when" has been called, it will not work either. Is there a solution for this? Dynamically adding/removing elements from the page is important for me, and allows a dynamic/fluid experience without having to refresh the page. I will try to find a workaround in the meantime! Thank you for reading
Avatar
Ok here is my workaround: @when("click", "body") def handle_delegated_click(event): if event.target.matches(".my-class") or event.target.closest(".my-class"): handle_click(event) This allows me to add/remove events dynamically and have them keep their listeners. If there is a better way, or a PyScript way to do this, please let me know. For now, this "appears" to work, so I will keep this for now until further notice! Edit: I did have some issues with delegating the "blur" event. "body" never truly loses focus. So I used "focusout" event instead and it worked. (edited)
Avatar
You can use the decorator but you can also manually set it. I found the decorator did not help with dynamic adjustments. See https://docs.pyscript.net/2025.8.1/api/#pyscriptwhen
Avatar
Thank you. For me, both options resulted in that error when I tried to set them twice. For example, even when calling "when" dynamically it would result in an error if I had already used "when" with the same parameters previously.
Avatar
I have to admit I moved the the ltk to simplify my life considerably but pyweb is getting there
โค๏ธ 1
Avatar
How do I access the clipboard in Pyscript? apparently in javascript it is navigator, but in pyscript there is no navigator class when I do from pyscript import navigator or from pyscript.web import navigator. Also, for similar questions, what is the recommended way to find things out if they are not present in the tutorial? Just the source code? Let me know. Edit: apparently it is window.navigator.clipboard. But the question stands: to find full documentation about all the methods/properties present in pyscript, must I go to a) JavaScript documentation b) PyScript documentation somewhere that I am not aware of? c) PyScript source code. Let me know so that if I do come up with a question of "where can I find X" I will definitely go this first. This one was just a "guess" that worked. For reference this is how I accessed the clipboard (make sure to place inside an async function): text = await window.navigator.clipboard.readText() await window.navigator.clipboard.writeText('hello') (edited)
Avatar
The entire JS web API is not reflected in the pyscript docs. Its not up to me but I suspect that's because its so big and already documented as JS. Pyscript bridges between these so you have access to everything that's in JS - if you can find the right handle. A google search wil take you to (say) -https://developer.mozilla.org/en-US/docs/Web/API/Clipboard_API where all the JS methods are available. Using a python mechanism you could try dir(js) or dir(js.window) For some things you have to look in the JS console instead of teh python terminal - especilly if trying to check for bug messages.
The Clipboard API provides the ability to respond to clipboard commands (cut, copy, and paste), as well as to asynchronously read from and write to the system clipboard.
โค๏ธ 1
Avatar
Awesome. Makes sense.
Avatar
Here is another question: @when('contextmenu', 'body') def handle_contextmenu(event): event.preventDefault() @when('contextmenu', 'body) async def handle_contextmenu(event): event.preventDefault() The async version, preventDefault will fail (will show the right click menu). This is problematic because if I have any async operation inside this handler, I cannot preventDefault. In fact, it doesn't even matter if I await within the function body. Just having the async keyword there will cause that to fail. I don't know if this is a bug of a javascript thing. I will try to find a work around. But yeah I am awaiting all sorts of stuff, and I need preventDefault to work. For an example of my use case. I must await all clipboard operations. this means that I perform clipboard operations inside an event handler (or anything else that requires an await) then I might not be able to preventDefault. Meanwhile, I will try to hack around it. Or maybe I am doing something wrong, I don't know. (edited)
Avatar
Here is another one: if I do 'mydiv = div("line1\nline2")' the div does not display the new lines. They display as spaces. This might be a javascript issue and not a pyscript issue (due to HTML new lines in divs)... I suppose there is a tradeoff in mirroring javascript... you get completeness, expected behaviors, and less transition time from javascript to pyscript, yet you do end up inheritting the javascript quirks. I suppose you could modify the div constructor to properly insert HTML new lines into the div? But I suppose that wouldn't mirror Javascript behavior. Personally, I would prefer "\n" to behave in the expected fashion. (edited)
Avatar
Swap newline for a br inside lt,gt tags
Avatar
Yes, correct. I had to do "div(mystr.replace('\n', '<br>')" and also when loading the div text I had to use innerHTML rather than textContent (textContent removed the new lines). (edited)
Avatar
๐ŸฅทJayKayAce๐Ÿฅท 18/09/2025 14:21
Hello I was trying out the pygame-ce example in the latest version and I got some micropip issues where it could not finde a wheel for pygame-ce. Am I doing something wrong in regards to selecting pyodide vs micropython that I am not aware of?
14:22
I was copying it into pyscript.com and could not get it to work.
14:23
Here is the shareable link for my app https://jaykayace.pyscriptapps.com/dawn-dream/latest/
Avatar
Avatar
๐ŸฅทJayKayAce๐Ÿฅท
Here is the shareable link for my app https://jaykayace.pyscriptapps.com/dawn-dream/latest/
Interesting. Looks like its all setup right. The Console says ValueError: Can't find a pure Python 3 wheel for: 'pygame-ce'
Avatar
ok. simple mistake. The src link in your index points to a file called quickstart.py which does not exist. But you do have a main.py. Either rename the file main.py or edit the src= link in index.html
07:02
Ahhh... but there is a more subtle error. Apparently for 2025.8.1 there is no pygame-ce build. In order to get this to work you need to change to 2025.7.3 (two lines in your index.html file)
๐Ÿ‘ 1
โค๏ธ 1
Avatar
Avatar
Neon22
Interesting. Looks like its all setup right. The Console says ValueError: Can't find a pure Python 3 wheel for: 'pygame-ce'
๐ŸฅทJayKayAce๐Ÿฅท 19/09/2025 15:35
Thanks
Avatar
Hello, I have a console embedded in a page this way: <script id="console" type="py" terminal worker> import code code.interact() </script> I'd like to send there code from a code snippet line by line, to execute it there. That's my current code: return async event => { const buttonEl = event.currentTarget const codeEl = buttonEl.nextElementSibling const myterm = document.querySelector("#console"); let code = getCopyableText(codeEl); for (const line of code.split('\n')) { try { await myterm.process(line); await new Promise(r => setTimeout(r, 1)); } catch (e) { console.error(e.message) } } } The issue is that sometimes it confuses lines (first screenshot), unless I set a high enough timeout (just fine, second screenshot). Is there known tuned value for the timeout this to be reliable or is there a way to enforce synchronous execution? Thank you.
Avatar
Separate issue/question: having a following console <script id="console" type="py" terminal worker> import code code.interact() </script> I'd like to push multiple lines to the console say to define a function (visibly in the terminal). await document.querySelector("#console").process('def hello():') await document.querySelector("#console").process(' print("hi")') await document.querySelector("#console").process(' ') await document.querySelector("#console").process(' ') I'm not able to exit the ... three dots mode (like still defining function), until I hit a return (enter) on my keyboard in the terminal. process('\n') ends with Cannot read properties of undefined (reading 'inputType'). Any advice appreciated! (edited)
02:48
I was also trying with donkey, but without success so far.
Avatar
@maciek this is how we achieve the same effect on the homepage https://pyscript.net/ : https://github.com/pyscript/pyscript.net/blob/main/main.py
PyScript is a platform for Python in the browser.
Website for pyscript.net - this is a static html/js site - pyscript/pyscript.net
๐Ÿ‘ 2
๐Ÿ™Œ 1
Avatar
Eruvanos [UTC+1] 10/10/2025 13:56
How could I set a timeout for the fetch function to abort before 60 seconds? For JS it would look like this: fetch(url, { signal: AbortSignal.timeout(5000) }) Source: https://stackoverflow.com/a/50101022
Avatar
I'm trying to build this using pyscript. https://threejs.org/examples/?q=ammo#physics_ammo_volume It needs the ammo.wasm.wasm module which I can't seem to correctly get on esm.run The older ammo.js version is not ES6. Can anyone help me to get the ammo module loaded in pyscript ? [js_modules.main] "https://esm.run/three" = "THREE" "https://esm.run/three/addons" = "addons" "https://esm.run/ammo" = "ammo" #"https://esm.run/ammo.wasm" = "ammo"
๐Ÿ‘ 1
Avatar
@Neon22 just catching up on Discord things (this week is a planning week at Anaconda, so full on meetings)... have you been able to make progress on this..?
Avatar
Not yet. At least they make an es6 version but can't work out to load it
๐Ÿ‘ 1
Avatar
Im trying to use sqllite asm from micropyton script but cant figure out how to initialize the module. I've added it to js_modules, and can import it in micropyton, but them im lost on how to replicate the .js howto into Python https://sqlite.org/wasm/doc/trunk/demo-123.md
Avatar
Avatar
Jos
Im trying to use sqllite asm from micropyton script but cant figure out how to initialize the module. I've added it to js_modules, and can import it in micropyton, but them im lost on how to replicate the .js howto into Python https://sqlite.org/wasm/doc/trunk/demo-123.md
I experimented a bit with a sqlite3 vs pandas approach. I have some working sqlite3 code in here - maybe it will help ??
Avatar
Avatar
Neon22
I experimented a bit with a sqlite3 vs pandas approach. I have some working sqlite3 code in here - maybe it will help ??
Thanks, however the difference is that there is no (installable) model sqllite for micropyton, so my hope was to load the sqllite-wasm module on the html page, and then use it via the js_import proxy
Avatar
Avatar
Jos
Thanks, however the difference is that there is no (installable) model sqllite for micropyton, so my hope was to load the sqllite-wasm module on the html page, and then use it via the js_import proxy
Hmm. I can't remember how to do it. I'm hoping someone will come and help. Here is my summary: This zip archive contains a subdirectory jswasm which has sqlite3.wasm in it. because its mpy I am unsure how to correctly load it. E.g. this toml files with a faetch error [files] "./sqlite-wasm-3500400.zip" = "./*" [js_modules.main] "./sqlite-wasm-3500400/jswasm/sqlite3.wasm" = "sqlite3" (edited)
Avatar
This is the error i am getting: Failed to load module script: Expected a JavaScript-or-Wasm module script but the server responded with a MIME type of "application/wasm". Strict MIME type checking is enforced for module scripts per HTML spec.
12:08
which suggests doing something like loading it in html using js first but I thought it would work more easily: async function loadWasm() { const response = await fetch('sqlite3.wasm'); const wasmBytes = await response.arrayBuffer(); const module = await WebAssembly.instantiate(wasmBytes, importObject); // importObject for any imports // Use module.instance.exports } loadWasm(); I have uploaded the sqlite3.wasm file to the pyscript.com page to get around the zip container. - https://pyscript.com/@neon22/csv-to-pandas-sqlite3/latest?files=pyscript_mpy.toml,index.html,main.py
Avatar
Avatar
Neon22
which suggests doing something like loading it in html using js first but I thought it would work more easily: async function loadWasm() { const response = await fetch('sqlite3.wasm'); const wasmBytes = await response.arrayBuffer(); const module = await WebAssembly.instantiate(wasmBytes, importObject); // importObject for any imports // Use module.instance.exports } loadWasm(); I have uploaded the sqlite3.wasm file to the pyscript.com page to get around the zip container. - https://pyscript.com/@neon22/csv-to-pandas-sqlite3/latest?files=pyscript_mpy.toml,index.html,main.py
I was able to figure this out. This is my first time diving into the (Micro)Python - JS module interop , so let me share my learnings:
  • there are several implementation of sqlite-wasm floating around the CDN's . Finding one good implementation and sticking to that while debugging helped.
  • Understanding the difference between ECM and UMD , and figuring out that this is an UMD old school module was important.
then on the UMD path :
  • Use the latest version of the library - ๐Ÿซข
  • understand that the <script ....js> part needs to go in HTML
  • then use pyscript.window to sql_obj = await window.initSqlJs({"locateFile": locate_file_js})
  • and that the locate_file_js needs to handle ffi.to_js interop.
ended up wrapping everything in a class for reuse
16:03
py class SQLite: # ..... async def _perform_initialization(self): """Internal method to perform the actual initialization""" # https://sql.js.org/documentation/global.html#initSqlJs if not hasattr(window, "initSqlJs"): raise RuntimeError("initSqlJs not found on window. Make sure sql-wasm.js script tag is in the HTML page.") # Create locateFile function for WASM loading def locate_file(file, *args): if self._cdn == "cdnjs": return f"https://cdnjs.cloudflare.com/ajax/libs/sql.js/{self._version}/{file}" elif self._cdn == "jsdelivr": return f"https://cdn.jsdelivr.net/npm/sql.js@{self._version}/dist/{file}" else: return f"https://unpkg.com/sql.js@{self._version}/dist/{file}" # Convert to JS function locate_file_js = ffi.to_js(locate_file) # Initialize SQLite-wasm sql_obj = await window.initSqlJs({"locateFile": locate_file_js}) if not sql_obj: raise RuntimeError("Failed to initialize SQLite-wasm") self._sql = sql_obj self._initialized = True See it in action : https://jos_verlinde.pyscriptapps.com/sqlite-wasm-demo/v1/ (edited)
Avatar
Avatar
Jos
Im trying to use sqllite asm from micropyton script but cant figure out how to initialize the module. I've added it to js_modules, and can import it in micropyton, but them im lost on how to replicate the .js howto into Python https://sqlite.org/wasm/doc/trunk/demo-123.md
@Andrea Giammarchi is looking into sqlite support and has lots of interesting ideas. Bear with - it's the weekend but he'll be about next week. All feedback would be very useful for us - and Andrea has been reaching into the sqlite WASM community for ideas too. ๐Ÿ‘
๐Ÿ‘ 1
Avatar
Avatar
ntoll
@Andrea Giammarchi is looking into sqlite support and has lots of interesting ideas. Bear with - it's the weekend but he'll be about next week. All feedback would be very useful for us - and Andrea has been reaching into the sqlite WASM community for ideas too. ๐Ÿ‘
thanks - after the initial bumps and roadblocks the basics appear to be working quite well- There are a few more places where I've found to need to use ffi.to_js ( binding queries for one) py stmt = db.prepare("SELECT * FROM test WHERE name LIKE ?") # Bindings to queries need to be converted to JS array stmt.bind(ffi.to_js([r"%row%",])) while stmt.step(): row = stmt.getAsObject() print(f"Found: {row['name']}") # Should print results ill be using this in a larger webapp - ill share feedback
Avatar
For me by far the most annoying is the clumsiness of inspection of any returned <JSProxy> object. it makes even simple object mostly opaque from Python. ( Im rather a fan of typing as that prevents me from making soo many errors) so to try to understand the format of the row object in the above : row = stmt.getAsObject() print("What is a row?") print(f"{row=}") print(f"{type(row)=}") print(f"{repr(row)=}") print(f"{row.__class__=}") print(f"{dir(row)=}") # probably a dictionary-like object print(f"Found: {row['name']}") # Print results - so its a dict, or a tuple ? # or a named tuple maybe? print(f"Found: {row.name}") # Also prints the results, so a TypedDict or named tuple ? outputs : What is a row? row=<JsProxy 185> type(row)=<class 'JsProxy'> repr(row)=<JsProxy 185> row.__class__=<class 'JsProxy'> dir(row)=['__class__', 'id', '__del__', 'name', 'new'] Found: Second row data Found: Second row data the JS API indicates it should be an Array IIUC : https://sql.js.org/documentation/Database.html#.QueryExecResult
Avatar
Avatar
Jos
py class SQLite: # ..... async def _perform_initialization(self): """Internal method to perform the actual initialization""" # https://sql.js.org/documentation/global.html#initSqlJs if not hasattr(window, "initSqlJs"): raise RuntimeError("initSqlJs not found on window. Make sure sql-wasm.js script tag is in the HTML page.") # Create locateFile function for WASM loading def locate_file(file, *args): if self._cdn == "cdnjs": return f"https://cdnjs.cloudflare.com/ajax/libs/sql.js/{self._version}/{file}" elif self._cdn == "jsdelivr": return f"https://cdn.jsdelivr.net/npm/sql.js@{self._version}/dist/{file}" else: return f"https://unpkg.com/sql.js@{self._version}/dist/{file}" # Convert to JS function locate_file_js = ffi.to_js(locate_file) # Initialize SQLite-wasm sql_obj = await window.initSqlJs({"locateFile": locate_file_js}) if not sql_obj: raise RuntimeError("Failed to initialize SQLite-wasm") self._sql = sql_obj self._initialized = True See it in action : https://jos_verlinde.pyscriptapps.com/sqlite-wasm-demo/v1/ (edited)
trying to make a ref version for me but your wasm files are under a protected link. Can you point me to original wasm file location ?
21:08
Ahh. ok. got confused. working it out now
Avatar
Avatar
Neon22
trying to make a ref version for me but your wasm files are under a protected link. Can you point me to original wasm file location ?
Avatar
@Andrea Giammarchi wrt tot the opening of database files I think it should be possible to save time by avoiding roundtrips . I have the database file linked in the filesection - so it is on the vfs already. and cached . Then that roundtrip through file.read() ; js.Uint8Array.new(file_data) ; before handing it off to sql["Database"].new takes a considerable amount of time that I assume would be faster by not crossing boundaries for every byte. Timing for a 6.762.496 board_comparison.db below. Opening database 'board_comparison.db'... micropython.mjs:1 20:20:25 reading file_data from 'board_comparison.db'... micropython.mjs:1 20:20:38 creating db_array from file_data... micropython.mjs:1 20:20:43 creating Database instance from db_array... micropython.mjs:1 20:20:43 Done creating Database. I see similar delays if I fetch the DB ( only worse by fetching) (edited)
Avatar
Avatar
Jos
For me by far the most annoying is the clumsiness of inspection of any returned <JSProxy> object. it makes even simple object mostly opaque from Python. ( Im rather a fan of typing as that prevents me from making soo many errors) so to try to understand the format of the row object in the above : row = stmt.getAsObject() print("What is a row?") print(f"{row=}") print(f"{type(row)=}") print(f"{repr(row)=}") print(f"{row.__class__=}") print(f"{dir(row)=}") # probably a dictionary-like object print(f"Found: {row['name']}") # Print results - so its a dict, or a tuple ? # or a named tuple maybe? print(f"Found: {row.name}") # Also prints the results, so a TypedDict or named tuple ? outputs : What is a row? row=<JsProxy 185> type(row)=<class 'JsProxy'> repr(row)=<JsProxy 185> row.__class__=<class 'JsProxy'> dir(row)=['__class__', 'id', '__del__', 'name', 'new'] Found: Second row data Found: Second row data the JS API indicates it should be an Array IIUC : https://sql.js.org/documentation/Database.html#.QueryExecResult
Andrea Giammarchi 20/10/2025 08:55
JSProxy are JS ... Proxy ... there is no class, type, repr in JS so you'd be better off with js.console.log(js_proxy) in there to see what you are dealing with
08:56
if ref.__class__.__name__ == 'JSProxy': from pyscript import window window.console.log(ref)
08:56
as example
Avatar
Avatar
Jos
@Andrea Giammarchi wrt tot the opening of database files I think it should be possible to save time by avoiding roundtrips . I have the database file linked in the filesection - so it is on the vfs already. and cached . Then that roundtrip through file.read() ; js.Uint8Array.new(file_data) ; before handing it off to sql["Database"].new takes a considerable amount of time that I assume would be faster by not crossing boundaries for every byte. Timing for a 6.762.496 board_comparison.db below. Opening database 'board_comparison.db'... micropython.mjs:1 20:20:25 reading file_data from 'board_comparison.db'... micropython.mjs:1 20:20:38 creating db_array from file_data... micropython.mjs:1 20:20:43 creating Database instance from db_array... micropython.mjs:1 20:20:43 Done creating Database. I see similar delays if I fetch the DB ( only worse by fetching) (edited)
Andrea Giammarchi 20/10/2025 08:57
I'd like to see an example but the conversion JS to Python byte array is not for free, unfortunately, we can try to see if Pyodide has a faster path but I think MicroPython surely doesn't
Avatar
Avatar
Andrea Giammarchi
if ref.__class__.__name__ == 'JSProxy': from pyscript import window window.console.log(ref)
JSProxy are JS ... Proxy
thanks window.console.log(ffi.to_js(ref)) works quite well
I'd like to see an example
Working on an app that would benefit from faster opening , ill be able to share somewhere this week. I should be able to distill a small repro form that as well. ( although that is essentially the sample I share above with a larger database. ) https://jos_verlinde.pyscriptapps.com/sqlite-wasm-demo/v1/
(edited)
Avatar
Avatar
Jos
JSProxy are JS ... Proxy
thanks window.console.log(ffi.to_js(ref)) works quite well
I'd like to see an example
Working on an app that would benefit from faster opening , ill be able to share somewhere this week. I should be able to distill a small repro form that as well. ( although that is essentially the sample I share above with a larger database. ) https://jos_verlinde.pyscriptapps.com/sqlite-wasm-demo/v1/
(edited)
Andrea Giammarchi 20/10/2025 16:37
it's window.console.log(ref) only if ref is a JSProxy, ottherwise you'll log the JS version of that ref but you wouldn't know what ref was in the Python world ... you can mix and match though, both approaches are useful but print works fine for Python things while console.log works fine for JS things, I hope this makes sense.
16:38
and to clarify, a Python / Pyodide proxy via window.console.log(py_ref) would also just show PyProxy ... there is symmetry, I agree both cases are not the best/easiest experience to deal with though.
Avatar
Hi, could someone tell me what's wrong with this simple code? If I remove config="pyscript.json this works fine (well, not really because I need the packages declaration found in the config file for my script, but I'm able to do a hello world). I just get a white/blank page with no error in the JS console ๐Ÿ™ Both script.py and pyscript.json doesn't appear to be requested by the browser, no trace in the web server logs. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>PyScript Offline</title> <script type="module" src="pyscript/core.js"></script> <link rel="stylesheet" href="pyscript/core.css"> </head> <body> <py-config> interpreter = "pyodide/pyodide.mjs" </py-config> <script type="py" config="pyscript.json" src="script.py" terminal></script> </body> </html> Thanks!
Avatar
Avatar
f4242
Hi, could someone tell me what's wrong with this simple code? If I remove config="pyscript.json this works fine (well, not really because I need the packages declaration found in the config file for my script, but I'm able to do a hello world). I just get a white/blank page with no error in the JS console ๐Ÿ™ Both script.py and pyscript.json doesn't appear to be requested by the browser, no trace in the web server logs. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>PyScript Offline</title> <script type="module" src="pyscript/core.js"></script> <link rel="stylesheet" href="pyscript/core.css"> </head> <body> <py-config> interpreter = "pyodide/pyodide.mjs" </py-config> <script type="py" config="pyscript.json" src="script.py" terminal></script> </body> </html> Thanks!
Your scripts aren't quite right. My suggestion is you goto pyscript.com and clone examples and experiment there. <!-- Import PyScript --> <link rel="stylesheet" href="https://pyscript.net/releases/2025.8.1/core.css"> <script type="module" src="https://pyscript.net/releases/2025.8.1/core.js"></script> (edited)
Avatar
Avatar
Neon22
Your scripts aren't quite right. My suggestion is you goto pyscript.com and clone examples and experiment there. <!-- Import PyScript --> <link rel="stylesheet" href="https://pyscript.net/releases/2025.8.1/core.css"> <script type="module" src="https://pyscript.net/releases/2025.8.1/core.js"></script> (edited)
Hum, on pyscript.com, this works without this: <py-config> interpreter = "pyodide/pyodide.mjs" </py-config> But on my PC, if I run without, this doesn't work, I get this exception: pyodide.asm.js:8 Uncaught (in promise) PythonError: Traceback (most recent call last): File "/lib/python313.zip/_pyodide/_base.py", line 666, in pyimport_impl res = __import__(stem, fromlist=fromlist) ModuleNotFoundError: No module named 'micropip' The module 'micropip' is included in the Pyodide distribution, but it is not installed. You can install it by calling: await micropip.install("micropip") in Python, or await pyodide.loadPackage("micropip") in JavaScript See https://pyodide.org/en/stable/usage/loading-packages.html for more details. at new_error (pyodide.asm.js:8:578922) at pyodide.asm.wasm:0x10f02c at pyodide.asm.wasm:0x10f0f4 at callPyObjectKwargs (pyodide.asm.js:8:632734) at Module.callPyObject (pyodide.asm.js:8:633880) at Function.apply (pyodide.asm.js:8:650121) at Object.apply (pyodide.asm.js:8:646549) at Object.pyimport (pyodide.asm.js:8:682760) But, seem like I can make it work if I add the micropip in the packages list in my pyscript.json!
Avatar
If you're not using a versioned core etc, you're running a very old out of date code which will cause you problems. It's essential you use a versioned core...
Avatar
Avatar
Andrea Giammarchi
I'd like to see an example but the conversion JS to Python byte array is not for free, unfortunately, we can try to see if Pyodide has a faster path but I think MicroPython surely doesn't
Just FYI: I was able to get an average 1000x perf improvement by adding .js functions to download the file from a url and cache it in IndexDB. ( no worker involved yet) its a blob of JS that I'd rather not have to add, and maintain - but not having to wait 40 seconds on every reload soon got me past that. is it possible to inject a JS script tag into the hosting page from MicroPython - รงause that would allow to hide those implementation 'details' from anyone using a MicroPython package or is there anything in the new packaging discussion that could help here ? (edited)
Avatar
@Jos this is something we're looking into this quarter WRT the packaging discussions we're about to kick off. ๐Ÿ‘
๐Ÿ‘ 1
Avatar
Hi! I'm trying to mount local storage (with await fs.mount("/data")). Chrome asked permission the first time and it was granted. Worked fine. Now, even if Chrome show the permissions is granted, I get this exception.... I wonder if someone already had this issue? Traceback (most recent call last): File "/lib/python313.zip/_pyodide/_base.py", line 597, in eval_code_async await CodeRunner( ...<9 lines>... .run_async(globals, locals) File "/lib/python313.zip/_pyodide/_base.py", line 413, in run_async await coroutine File "<exec>", line 33, in <module> File "<exec>", line 26, in initPyscriptEnv File "/home/pyodide/pyscript/fs.py", line 16, in mount mounted[E]=await I.mountNativeFS(E,B) ^^^^^^^^^^^^^^^^^^^^^^^^^^ pyodide.ffi.JsException: NotAllowedError: The request is not allowed by the user agent or the platform in the current context.
Avatar
@f4242 hmmm.... that's not good. Can you please raise an issue over on GitHub for this? https://github.com/pyscript/pyscript/issues (it'll mean you'll be able to track how this is being addressed, and others will be able to find your report in case they encounter it too)
PyScript is an open source platform for Python in the browser. Try PyScript: https://pyscript.com Examples: https://tinyurl.com/pyscript-examples Community: https://discord.gg/HxvBtukrg2 - pyscri...
๐Ÿ‘ 1
Avatar
are you able to reproduce? https://ffff11.pyscriptapps.com/nameless-water/latest/ Open link, accept the prompt, close chrome, open again (edited)
Avatar
Aha... @f4242 I can reproduce the error. I see: Traceback (most recent call last): File "/lib/python313.zip/_pyodide/_base.py", line 597, in eval_code_async await CodeRunner( ...<9 lines>... .run_async(globals, locals) File "/lib/python313.zip/_pyodide/_base.py", line 413, in run_async await coroutine File "<exec>", line 30, in <module> File "<exec>", line 23, in initPyscriptEnv File "/home/pyodide/pyscript/fs.py", line 16, in mount mounted[E]=await I.mountNativeFS(E,B) ^^^^^^^^^^^^^^^^^^^^^^^^^^ pyodide.ffi.JsException: NotAllowedError: The request is not allowed by the user agent or the platform in the current context.
10:22
@Andrea Giammarchi ^^^^ any idea what's going on here?
Avatar
Andrea Giammarchi 23/10/2025 10:26
it works if I refresh, it indeed throws if I close the browser and go back ... this feels like an issue in permission handling on the browser side, we could catch that case though but the thing is that it's stored as permission granted and then it fails ... which is more than undesired
10:27
10:27
you can see the handler is in there, it's just somehow expired ... will check around if it's a known issue ... to fix it (for now) remove that entry in the IndexedDB
10:29
it is possible one of these two settings is making the browser behave like that
10:29
10:30
last, but not least, an issue in PyScript would surely help us working on it
10:37
crickets from the internet ... I'll try to see if I can find the issue and fix it
Avatar
Andrea Giammarchi 23/10/2025 10:55
I think I've fixed it in here: https://github.com/pyscript/pyscript/pull/2395 /cc @ntoll ... fancy a new release? this has been bugging more than a user, the behavior is not docummented but the fix looks and feels correct
Description Apparently, even if a user granted access to a folder, the FileSystemDirectoryHandler works for the current session but it needs to be checked again (the browser will provide options) w...
10:55
you can test that by replacing 2025.10.2 release JS file with https://cdn.jsdelivr.net/npm/@pyscript/core@0.7.8/dist/core.js (edited)
Avatar
Sorry - had my head in code. Lemme look/check ๐Ÿ‘
Avatar
Andrea Giammarchi 23/10/2025 11:46
OK, 0.7.10 is better and tested on both main and worker threads ... it works fine on Edge and Chrome/ium or browsers supporting this feature. (edited)
Avatar
Hmmm... I get the old error again. ๐Ÿ™
11:55
using 0.7.8
Avatar
Thanks for the the debugging, it's over my skills level ๐Ÿ˜‰
Avatar
Latest release should address your issue @f4242 ๐Ÿ‘
15:37
Feedback welcome.
Avatar
Ah, no need to wait for the fix in chromium?
Avatar
There's a work-around. But there is also a bug in Chromium. We've reported it to the upstream devs.
Avatar
I updated my code to use 2025.10.3. version but unfortunately, I still get a (different) exception: Traceback (most recent call last): File "/lib/python313.zip/_pyodide/_base.py", line 597, in eval_code_async await CodeRunner( ...<9 lines>... .run_async(globals, locals) File "/lib/python313.zip/_pyodide/_base.py", line 413, in run_async await coroutine File "<exec>", line 30, in <module> File "<exec>", line 23, in initPyscriptEnv File "/home/pyodide/pyscript/fs.py", line 19, in mount A=await C.idb.get(D);B=await get_handler(A) ^^^^^^^^^^^^^^^^^^^^ File "/home/pyodide/pyscript/fs.py", line 4, in get_handler async def get_handler(details):A=details;B=A.handler;C=A.options;D=await B.queryPermission(C);return B if D=='granted'else _A ^^^^^^^^^ AttributeError: handler On my "offline build" I get another different one ๐Ÿ˜ฎ Traceback (most recent call last): File "/lib/python313.zip/_pyodide/_base.py", line 597, in eval_code_async await CodeRunner( ...<9 lines>... .run_async(globals, locals) File "/lib/python313.zip/_pyodide/_base.py", line 413, in run_async await coroutine File "<exec>", line 39, in <module> File "<exec>", line 32, in initPyscriptEnv File "/home/pyodide/pyscript/fs.py", line 20, in mount if E:B=await A.idb.get(D);C=await get_handler(A,B) ^^^^^^^^^^^^^^^^^^^^^^ File "/home/pyodide/pyscript/fs.py", line 6, in get_handler A=details;B=A[_A];C=A[_B];D=await B.requestPermission(C) ~^^^^ TypeError: 'pyodide.ffi.JsProxy' object is not subscriptable
Avatar
๐Ÿ‘‹ (I'm suposed to be on holiday today, Monday and Tuesday, but replying quickly before I step away from computers for a long weekend) @f4242 ^^^^ this feedback is invaluable, thank you for taking the time to report the errors. I wonder if this is to do with the version of Chrome you're using? As Andrea found, they've broken the API since we originally implemented this feature and so we're in a transitory state as some Chromes work, some Chromes don't and the current Chrome is not in a good place. We can only but report such things to Google and hope they fix things quickly and then our answer is "update to the latest Chrome, and it'll work". Such is the variability of the web. ๐Ÿ™ @Andrea Giammarchi any thoughts..?
Avatar
Avatar
ntoll
๐Ÿ‘‹ (I'm suposed to be on holiday today, Monday and Tuesday, but replying quickly before I step away from computers for a long weekend) @f4242 ^^^^ this feedback is invaluable, thank you for taking the time to report the errors. I wonder if this is to do with the version of Chrome you're using? As Andrea found, they've broken the API since we originally implemented this feature and so we're in a transitory state as some Chromes work, some Chromes don't and the current Chrome is not in a good place. We can only but report such things to Google and hope they fix things quickly and then our answer is "update to the latest Chrome, and it'll work". Such is the variability of the web. ๐Ÿ™ @Andrea Giammarchi any thoughts..?
Andrea Giammarchi 24/10/2025 14:47
my thoughts are: it's not 2025.10.3, it's 2025.10.2 ... the issue is browser cache
14:48
actually, that never landed so the issue is that pyscript is from an unstable channel, aka NPM
14:49
we changed code from A[_A] to A._A (as meta-example) because we caught that on Pyodide side, nothing in 2025.10.3 uses that old accessor, it's all statick and "subscribable" access with latest ... clear your cache, clear in devtools Application the whole storage, hard refresh via Ctrl+R or Cmd+R and you won't see errors anymore, like we cannot see errors anymore with latest. (edited)
14:50
to @ntoll ... "one does not simply suppose to be on vacation" ... go enjoy your time off mate ๐Ÿ‘‹
14:55
be sure you are using 2025.10.3 also
14:56
if you have issues in there too, update your Chrome/ium based browser (edited)
Avatar
Your page indeed work but not mine. I wonder if I'm not stuck because Chrome remember a bad state from the execution on the previous release?
Avatar
Avatar
f4242
Your page indeed work but not mine. I wonder if I'm not stuck because Chrome remember a bad state from the execution on the previous release?
Andrea Giammarchi 24/10/2025 15:07
that's why I've suggested hard refresh after a clean storage state in devtools Application tab
15:07
my demo uses 2025.10.3 ... so should your one
Avatar
yes I got it working (reset permissions and site data). Thanks!
๐Ÿฅณ 1
Avatar
Avatar
f4242
yes I got it working (reset permissions and site data). Thanks!
Andrea Giammarchi 24/10/2025 15:10
apologies I've suggested Ctrl+C instead of Ctrl+R too ... ๐Ÿคฆ
Avatar
Was not working from my local build but I found there is now release 0.7.10 available on npm and I was building 0.7.8. It now work from my offline build too! I wonder, it's maybe a dummy question, but why the versioning scheme is different from npm and your github? Is there a way to map both version?
Avatar
Avatar
f4242
Was not working from my local build but I found there is now release 0.7.10 available on npm and I was building 0.7.8. It now work from my offline build too! I wonder, it's maybe a dummy question, but why the versioning scheme is different from npm and your github? Is there a way to map both version?
Andrea Giammarchi 25/10/2025 05:06
in npm we follow common semver but it's our "dev channel". We use it to test stuff and it's handy because CDNs automatically provide testing URLs we can pass around. Only once we're sure our testing is done we release following calver (mostly for historical reasons) and we distribute it through our official release channel. Anything more complex than this workflow will require more time and provide little value, ihmo, so the rule of thumbs is:
  • do you want a stable release? use https://pyscript.net/releases/{CALVER}/core.js official channel
  • do you want to try a developers preview? grab latest from npm
๐Ÿ‘ 1
Avatar
Original message was deleted or could not be loaded.
Does spam like this actually work? I doubt you'll find any success here.
Avatar
What advice does this group have for debugging a callback-heavy port to pyscript? https://pyscript.com/@janburgy/dc-js/latest is my attempt at porting https://dc-js.github.io/dc.js/ to pyscript. It took sprinkling quite a few to_js and making some callback signatures less strict (*_) because JS doesn't care if you pass 3 argument to a function that only expects 1 (I'm looking at you, Array.prototype.map) The page starts up kind of ok (not sure why #monthly-move-chart and #monthly-volume-chart don't render) then goes of the rails as soon as you click on those incomplete charts with a bunch of Error: <rect> attribute x: Expected length, "NaN". Error: <rect> attribute width: Expected length, "NaN". Error: <path> attribute transform: Expected number, "translate(undefined, 0)".
Avatar
It dawned on me that the 2 broken charts both have .x(d3.scaleTime().domain(to_js([dt.date(1985, 1, 1), dt.date(2012, 12, 31)]))) so I posited that d3 does not like PyProxies so I introduced def default_converter(value, _ignored1, _ignored2): if isinstance(value, dt.date): return js.Date.new(value.year, value.month - 1, value.day) raise value and things look happier now. Still need to sort out some CSS stuff which might be because the original uses bootstrap v2.1.1 whereas mine uses v5.3.8
๐Ÿ‘ 2
Avatar
hey yall im trying to run a cipher for my dnd group but I am having some issues understanding how this all works
03:40
<html> <head> <link rel="stylesheet" href="https://pyscript.net/releases/2025.10.3/core.css"> <script type="module" src="https://pyscript.net/releases/2025.10.3/core.js"></script> </head> <body> <h1>Book of Alexander</h1> <p>Using the magic of Alexander's book you can decipher and cipher Common into and out of Runes.</p> <p>Runes are a magical language used by many highley intelligent users. It's been used to communicate and conjure forth mythical abilities.</p> <py-script src="main.py"></py-script> </body> </html>
03:41
Avatar
@Andrea Giammarchi @ntoll what's the best way to get pyscript/examples#27 looked at?
Avatar
Avatar
INFINITE
Click to see attachment ๐Ÿ–ผ๏ธ
Andrea Giammarchi 28/10/2025 12:49
you cannot access local files without granting a permission ... use pyscript.fs.mount('/tmp') to access your directory then in there you can read files ... PyScript runs on your browser, not on your Operating System
Avatar
Avatar
Jan Burgy
@Andrea Giammarchi @ntoll what's the best way to get pyscript/examples#27 looked at?
Andrea Giammarchi 28/10/2025 12:50
you want that example within the list of our examples?
12:55
anyway, let's discuss in there, thank you for the PR though, appreciated โค๏ธ
๐Ÿ‘ 1
Avatar
Avatar
Neon22
I'm trying to build this using pyscript. https://threejs.org/examples/?q=ammo#physics_ammo_volume It needs the ammo.wasm.wasm module which I can't seem to correctly get on esm.run The older ammo.js version is not ES6. Can anyone help me to get the ammo module loaded in pyscript ? [js_modules.main] "https://esm.run/three" = "THREE" "https://esm.run/three/addons" = "addons" "https://esm.run/ammo" = "ammo" #"https://esm.run/ammo.wasm" = "ammo"
Is there any chance someone can help me work out how to load the wasm version of Ammo.js ? Very keen to get this softbody physics working: https://threejs.org/examples/physics_ammo_volume.html
Avatar
Avatar
Jan Burgy
@Andrea Giammarchi @ntoll what's the best way to get pyscript/examples#27 looked at?
WE have a work item for examples (and docs) rewrite / refactor for this quarter. I've just returned from holiday (today) so playing catch-up. But I'll be on this soon (within a week), and hope to engage the community in the process too, so we have collective ownership and can celebrate the work we're all doing.
Avatar
MLD Phoeniix 29/10/2025 13:27
Hello Guys, iโ€™m French and i want to learn Python language for do web site or application in Python. Where should i start ? I work in IT and i have basic on develpment web
Carlos Araujo started a thread. 11/11/2025 22:25
Exported 6,887 message(s)
Timezone: UTC+0