py5coding / py5generator

Meta-programming project that creates the py5 library code.
https://py5coding.org/
GNU General Public License v3.0
52 stars 13 forks source link

Pylance support in Imported Mode #313

Closed smlpt closed 1 year ago

smlpt commented 1 year ago

It currently seems that Pylance does not detect the methods and attributes that are provided by the py5 Jupyter kernel. The code cells are executed successfully, but all methods are highlighted by Pylance as "not defined". Is there an easy way to enable support for Pylance so that its more convenient to work with Imported Mode in Jupyter Notebooks?

My setup:

hx2A commented 1 year ago

Interesting. I use the py5 kernel and I use Jupyter Notebooks in VSCode, but I've never used Jupyter Notebooks with the py5 kernel in VSCode. I just tried it now and I can't get it to work. It hangs on the "Kernel Detection Tasks Running" state with spinning arrows, as if it is stuck trying to start up the kernel. How do you get VSCode to run a notebook with the py5 kernel? Maybe I am missing a Jupyter addon. Which ones do you have?

I can still investigate this issue though by creating a notebook with py5 Imported Mode code.

Can you try creating a cell at the top of the notebook with this:

from py5 import *

That should take care of it...can you test that and tell me if it works for you? Also, does it get the syntax highlighting colors right, or is the py5 stuff all white?

This feels like a bit of a hack, albeit a small one. There might be some PyLance configuration setting you can adjust to let it know about from py5 import *. I just looked at VSCode's settings and I don't see anything relevant. However, I know that Juputer Notebooks can be configured to import frequently used libraries automatically, and I would be surprised if there was no way to keep PyLance from flagging that code as errors. Can you do some poking around and let me know what you find?

smlpt commented 1 year ago

Hey, thanks for the quick response.

Your Kernel Detection issue sounds weird. The addons I have installed are Python and Pylance, as well as the Jupyter addon (which automatically installs 4 addons under the hood).

I first ran %pip install py5[jupyter] in a cell, then !python -m py5jupyter.kernels.py5.install --sys-prefix to install the kernel. Then I restarted VS Code (it seems the kernel detection only works after the restart).

I tried these exact same steps on a different machine right now and I get the same results as on the other machine.

Creating a cell at the top with from py5 import * does indeed seem to fix it. No cell execution necessary, it automatically detects all functions. Now they are also highlighted correctly. Previously, they were all white. Interestingly, auto-completion was still possible in the earlier case, even when Pylance didn't detect the functions. So I assume the auto-completion might be a feature of the Python addon.

As for auto-import, I think this is usually done by creating a startup script that can be executed at the beginning. But this only really makes sense if there's more than one library (as is the case for me here).

hx2A commented 1 year ago

Interestingly, auto-completion was still possible in the earlier case, even when Pylance didn't detect the functions. So I assume the auto-completion might be a feature of the Python addon.

Yes. That's because VSCode is communicating with the Kernel for the auto-completion values. The Jupyter Kernel framework is quite interesting, all communication between the client (web client, VSCode, etc) and the server (Jupyter Kernel) goes through a handful of JSON messages. If you are making a Kernel, you can optionally implement the one for code completion. I didn't have to do any of that for the py5 kernel though because the py5 kernel inherits from the IPython kernel. Happily, the IPython kernel is super-configurable and could adapt to py5's needs without needing to override complicated methods.

Creating a cell at the top with from py5 import * does indeed seem to fix it. No cell execution necessary, it automatically detects all functions. Now they are also highlighted correctly. Previously, they were all white.

Good to know! I can't think of a way for further improvement unless there is a way to tell Pylance to assume that from py5 import * has been executed. But if that was possible, it would probably mess up your regular Python notebooks, which is a bad outcome.

smlpt commented 1 year ago

Alright, thanks for the insights! I will close the issue then.

hx2A commented 1 year ago

Thanks for closing this, and thanks for using py5!

Before you go, can you tell me what you are using py5 for? I find it motivating to hear what people are doing with py5.

smlpt commented 1 year ago

Sure! I personally use the Processing IDE in Python mode to create generative art. But now I am supposed to write a notebook for student interns to get started with simple 2D simulations and interactive art, so Py5 seemed the easiest way to achieve this. The only other Python library that would run in notebooks is P5, but it requires the installation of GLFW and messing around with environment variables, so I felt like Py5 was the easier choice for students to get started.

hx2A commented 1 year ago

Sounds great. Very happy that py5 is being used in an educational setting. If it helps, there are great beginner tutorials available on the website.

smlpt commented 1 year ago

Thanks, I'll take a look!