evcxr / evcxr

Other
5.59k stars 216 forks source link

Support the VS Code variable inspector for Jupyter notebooks #194

Closed jonasbb closed 2 years ago

jonasbb commented 3 years ago

Visual Studio Code has a Jupyter extension to work on Jupyter notebooks. If the native notebook editor is enabled, the evcxr kernel is also usable. The native editor is opt-in in the stable version, by setting this configuration option.

"jupyter.experiments.optInto": [
    "__NativeNotebookEditor__"
],

VS Code has a variable explorer showing name, type, size, and value of the currently defined variables. It would be nice if evcxr would understand the introspection commands to use the variable explorer.

This inspect method sends an inspect_request message with code being the variable name and a cursor_pos of 0 to the kernel. The result is parsed here https://github.com/microsoft/vscode-jupyter/blob/main/src/client/datascience/jupyter/kernelVariables.ts#L294-L363 I found the links/information in this thread https://github.com/microsoft/vscode-jupyter/issues/1536#issuecomment-683910271.

Returning the variable type seems easy using the variables_and_types function on the CommandContext. I do not know if showing the value or returning the size of collections is easily and quickly possible in evcxr.

davidlattimore commented 3 years ago

Hi @jonasbb. Sorry about the slow reply. That's a neat feature idea! I'd heard that VS code has a Jupyter extension, but haven't as yet tried it out.

You're right that displaying the values in variables and things like collection sizes is a bit tricky. It would require generating some code that then gets compiled and run. The generated code could display each of the values. Evcxr can then capture the displayed values.

I probably don't have the bandwidth to try to implement that myself at the moment, but if you or anyone else wanted to give it a try, I'd be more than happy to provide any guidance that was needed.

jonasbb commented 2 years ago

I tried to implement this but could not make the VS Code side work. Extracting the variable type for a given name is easy enough. The variable viewer of the VS Code Jupyter extension, however, did not fetch the information. It seems it only supports Python code right now, even though the configuration option jupyter.variableQueries is still listed. The configuration does not seem to have any effect.

Since the use case is gone, I close this. Maybe this becomes possible again in the future.