Replies: 1 comment
-
|
Hii TrevskiUK, The root cause is that PyScript deliberately doesn't support built in functions like open, read, and write to avoid blocking the main event loop and for security reasons so open() simply isn't available in pyscript context, which is why parsed_data stays None and you get the TypeError. You could use io.open via task.executor import io, json, os
@pyscript_executor
def write_file(path, content):
os.makedirs(os.path.dirname(path), exist_ok=True)
with io.open(path, "w") as f:
f.write(content)
@pyscript_executor
def read_json(path):
with io.open(path, "r", encoding="utf-8") as f:
return json.load(f)
def userproc(action=None, id=None):
write_file("/homeassistant/myah/outbox/userproc_out.myah", "made it this far: 01")
parsed_data = read_json("/homeassistant/.storage/auth")
user_ids = [user["id"] for user in parsed_data["data"]["users"]]
log.info(f"user_ids: {user_ids}")Or put file I/O in a separate native python module in a directory outside config/pyscript/, then import and call it via task.executor. The @pyscript_executor decorator handles wrapping the function in task.executor automatically. Note that allow_all_imports: true must be set in your pyscript config. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi everyone,
New to this add-on, and struggling.
Here is my code:
When run from HA Developer Actions,:
No text file is written "/homeassistant/myah/outbox/userproc_out.myah"
Something is not working with the JSON parsing of the file "/homeassistant/.storage/auth", as I get the following in the log:
This is all very frustrating because my original standalone version of the script works perfectly when executed from the HA terminal.
Aaargh.
Thanks in advance!
Beta Was this translation helpful? Give feedback.
All reactions