Pyscript now supports interactive development with Jupyter

I just released a new version of Pyscript. Pyscript allows you to write Python functions and scripts that can implement a wide range of automation, logic and triggers in a very simple manner. Pyscript is available as a custom integration in HACS.

This new release provides an optional kernel that supports the Jupyter front-ends (notebook, lab, console). That allows fully interactive development of code, functions, triggers and automations, which is much easier than the old workflow of saving a pyscript file, calling the reload service, and looking at the log for errors. It also supports autocompletion (with TAB), so you can easily explore state variables, call services etc from Jupyter.

Check out this Jupyter notebook tutorial, and here is more about pyscript and the Jupyter kernel interface.

Enjoy!

Craig

3 Likes

Hey, I’ve been curious how to interact with this when my HA instance runs in Docker. I can install Pyscript fine, but I’m running into confusion; I can’t force the hass_pyscript_kernel library to install in the Docker, and I’m not sure I’m supposed to, but at least under Windows (which therein may lie the problem) I can’t get the Pyscript kernel to show up. It keeps telling me:

PS C:\Users\Kay\code> jupyter pyscript install
updated pyscript kernel in C:\Users\Kay\AppData\Roaming\Python\Python38\site-packages\ipykernel\pyscript
PS C:\Users\Kay\code> jupyter kernelspec list
Available kernels:
python3    C:\Users\Kay\AppData\Roaming\Python\Python38\site-packages\ipykernel\resources
PS C:\Users\Kay\code> jupyter pyscript info
hass_pyscript_kernel version 1.0.0 installed
No installed kernel named pyscript found

Any suggestions?

EDIT: This is also after changing the configuration file to refer to my Home Assistant instance via its hostname and https address; if HTTPS is unsupported for this I can change it to the direct 8123 port instead.

I haven’t tested this on Windows, so it looks like something is broken. Unfortunately I don’t have access to a windows machine currently.

Yes, for some reason jupyter can’t see the installed pyscript kernel. It does look like it is installed alongside the python3 one, although I’m not sure why the python3 kernel is in a directory called resources - on my machine the directory name matches the kernel name.

Perhaps jupyter on windows stores the kernel information in a different manner?

What’s in the resources directory for the python3 kernel?

Literally nothing but 2 icon files, haha. Sure enough, I tried it under my Fedora laptop and it’s working fine. Now to see if I can’t just run it from inside WSL and connect using ST3 in Windows instead of the Linux build via X410. Will try later, but that being mentioned helps.

More than likely it’s something weird about how there’s some stuff stored in $HOME\AppData\Local\Programs\Python\Python38\Scripts and some stuff in $HOME\AppData\Roaming\Python\Python38\Scripts and I cannot, for the life of me, figure out how that’s butchered because aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa. I suspect maybe because I specified --user when it’s technically a local install of Python anyway.

Sounds like some progress, but not yet a solution. The Jupyter docs say the kernels on Windows are in %PROGRAMDATA%\jupyter\kernels (for system) and %APPDATA%\jupyter\kernels (for user).

But those don’t match the paths you got when you ran jupyter kernelspec list.

In order to get this working under windows, I used WSL2. From there, I was able to install the pyscript kernel. Then I could either launch notebook from WSL2, or I could us VSCode from a directory in WSL2.

Sadly, Windows isn’t the best environment for this. WSL2 helps, but, ultimately, is not nearly as easy for most development work as using a Mac or Linux based workstation.

I tried for quite a while to install the kernel without WSL2 (mostly to provide instructions for others since I’m perfectly happy just using my Mac) and failed miserably.

Seems dated or for C:\python setups. In any case I was able to fix it! I uninstalled Python, removed all folders related, reinstalled, and this time since technically it’s already a local install I just left it as-was without specifing --user to run pip install hass_pyscript_kernel notebook jupyterlab, installing notebook and jupyterlab formally for ensuring it’s all there, and it worked fine. I did have to configure Helium (Jupyter for Sublime Text) for the correct path to Jupyter, %LOCALAPPDATA%\Programs\Python\Python39\share\jupyter in my case since I’d installed Python 3.9, but it worked perfectly after that from Windows.

1 Like

Hi!
I cannot connect to to the Jupiter kernel from my laptop and after some troubleshooting I realised that the issue is that I use SSL (https) for my Home Assistant access. If I temporarily disabled https it worked fine. Does anyone know how I should config Jupyter to allow it to connect to the Pyscript kernel over https? Or will I need to set up some kind of proxy?
Isac

Try the hass_proxy configuration setting in pyscript.conf.

I’d recommend doing this anyway to get https remotely and http locally.

Ok! Do you have a link to some guide to follow on setting up a proxy for Hass.io? :slight_smile:

No, because hass.io doesn’t exist anymore for more than a year now :wink:
Install the NGINX add-on and follow the instructions there.

:rofl: of course! I meant Home Assistant :slight_smile: Thanks for the tip!