Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Running Matlab in Jupyter Notebooks in VS Code (on Windows) #41

Closed
tylerlekang opened this issue Jun 29, 2023 · 9 comments
Closed

Running Matlab in Jupyter Notebooks in VS Code (on Windows) #41

tylerlekang opened this issue Jun 29, 2023 · 9 comments
Assignees
Labels
answered Issue has been answered demo question Further information is requested

Comments

@tylerlekang
Copy link

tylerlekang commented Jun 29, 2023

There is currently a VS Code extension for Matlab https://github.com/mathworks/MATLAB-extension-for-vscode/ so I'm not sure if this request/issue should be here or there? But that just allows you to edit a .m file fairly nicely, not run it -- in the sense of a notebook with inline outputs for each code sections. (I strongly disagree that Live Scripts give an equivalent experience to a notebook. It's not the same, and I don't particularly care for it.)

The ultimate would be to be able to run Matlab in a Jupyter Notebook in VS Code, with a nice environment (similar to Python with the Python & Pylance extensions).

Will this be upcoming? Pretty please. :) Thank you!

@rashedmyt rashedmyt self-assigned this Jul 4, 2023
@prabhakk-mw
Copy link
Member

prabhakk-mw commented Jul 6, 2023

Hi @tylerlekang

VSCode provides multiple ways to run Jupyter notebooks through VSCode.

Currently the jupyter-matlab-proxy package depends on the presence of a running Jupyter Notebook Server to relay messages to and from MATLAB.

You can however start a jupyter notebook server, and then connect to it via VSCode and continue notebook development from VSCode.

  • Start a Jupyter Lab Session
jupyter lab 

<snip>
    To access the server, open this file in a browser:
        file:///home/prabhakk/.local/share/jupyter/runtime/jpserver-1012796-open.html
    Or copy and paste one of these URLs:
        http://localhost:8888/lab?token=f5c87aa6e805384a82a8774ec366938bc554cd7e8462e1b7
     or http://127.0.0.1:8888/lab?token=f5c87aa6e805384a82a8774ec366938bc554cd7e8462e1b7
<snip>
  • Click on the link to access Jupyter. Save this link for future use.

  • Start MATLAB via the "MATLAB Kernel" or the "Open MATLAB " buttons

  • Carry out the licensing step.

  • This will save the licensing options used, and future jupyter servers on this machine will not need the last two steps.

  • Now, navigate to VSCode, and open any notebook and click on the "Select Kernel" button
    screenshot_1688619796

  • Select existing jupyter server:
    screenshot_1688619814
    Enter the link to your Jupyter Server here.

  • Select MATLAB kernel
    screenshot_1688619672

  • Start using Notebook with MATLAB
    screenshot_1688620273

We are working on integrating with VSCode more, and look to enable more modes of access through Jupyter later this year.

Hope this helps!

Adding video demo of this process:

2023-11-17.15-27-38.mp4

@tylerlekang
Copy link
Author

tylerlekang commented Jul 6, 2023

@prabhakk-mw that works perfectly! I did not know that was an option! Maybe in the future there can be a MATLAB for Notebook extension that takes care of even needing to open the JL server in the first place, but that really is a minor inconvenience. In fact, VS Code was able to auto-detect the correct URL w/ token instead of even needing to copy/paste it from the JL terminal.

The major thing that is lacking with this way currently is to have function auto-completion and information appear when you type ("IntelliSense"). This is available using the MATLAB extension when editing an .m file (though doesn't display function documentation as well as Python/Pylance extensions), but currently the only auto-complete options that populate are any existing variables or functions that have already been defined/used in the notebook.

In fact, using this method addresses all three of the issues I raised for using MATLAB in Jupyter Lab

#40
#42
#43

In other words, building the notebook in VSC instead of JL alleviates all three of these issues. (Not sure how, but none of them happen in VSC Notebook).

@tylerlekang
Copy link
Author

tylerlekang commented Jul 12, 2023

@prabhakk-mw one other minor thing for the (hopefully) future, regardless if having to manually connect to a (remote) existing Jupyter server or if VS Code will one day be able to make a direct local connection to a MATLAB kernel, there is also a "Jupyter Powertoys" extension for VS Code which allows any contextual help from the kernel to be displayed in a side panel.

As of now (probably as expected), this only displays the message "No response from kernel" when running with (remote, manually connected) MATLAB kernels.

@Will-Zhao0
Copy link

Hi, I'm wondering how you are doing this, where is the buttons?

  • Start MATLAB via the "MATLAB Kernel" or the "Open MATLAB " buttons

@prabhakk-mw
Copy link
Member

@Will-Zhao0 , I've updated my answer to include a video.
Hopefully that will help answer the questions you may have.
Thanks

@prabhakk-mw prabhakk-mw added question Further information is requested answered Issue has been answered demo labels Nov 17, 2023
@tylerlekang
Copy link
Author

For whatever it is worth, what I do in Windows10 is:

  • launch "Anaconda prompt"
  • activate the environment I want to use (which has jupyterlab and j-ml-proxy installed)
  • launch jupyter lab and copy the server address (just like in the video)
  • in VSC on a tab for a notebook I want to do Matlab, I select the kernel (again just like in the video)
  • first cell I run some kind of version command
  • this kicks off the process of starting up Matlab, checking the license, and connecting it to the notebook. I don't have to do anything in the actual JL browser window, and can in fact close it (or just launch the --no-browser version of JL at command line), but I do use it sometimes to kill the kernels if I'm managing multiple

@prabhakk-mw
Copy link
Member

For whatever it is worth, what I do in Windows10 is:

  • launch "Anaconda prompt"
  • activate the environment I want to use (which has jupyterlab and j-ml-proxy installed)
  • launch jupyter lab and copy the server address (just like in the video)
  • in VSC on a tab for a notebook I want to do Matlab, I select the kernel (again just like in the video)
  • first cell I run some kind of version command
  • this kicks off the process of starting up Matlab, checking the license, and connecting it to the notebook. I don't have to do anything in the actual JL browser window, and can in fact close it (or just launch the --no-browser version of JL at command line), but I do use it sometimes to kill the kernels if I'm managing multiple

Yes @tylerlekang, you are right, one does not need to actually run anything in the JL interface.
However, it seemed like a good thing to do when you are setting up for the first time, as this helps confirm that you have a working environment available outside of VSCode.

Additionally, once licensing has been carried out at least once on a machine through the JL interface, this information is cached and is re-used by the MATLAB kernel for subsequent launches.

Licensing cannot be done through the VSCODE interface using the web screens as there isn't a way for us to route those screens into VSCODE.

@tylerlekang
Copy link
Author

tylerlekang commented Nov 20, 2023

@prabhakk-mw it would be good if somehow it were possible for any external tool (VSC or anything) wanting to do Matlab via Jupyter could connect without having to copy the address every time and re-set the connection. Like if j-ml-proxy had a way to have a permanent ip address/port that would always redirect to the correct/current running Jupyter server. (And then even more ideal, if there were a set of commends you could send to that "middle man layer" that could view and manage the active Jupyter kernels!) I don't know if Jupyter allows such a thing to be possible, though

@prabhakk-mw
Copy link
Member

@prabhakk-mw it would be good if somehow it were possible for any external tool (VSC or anything) wanting to do Matlab via Jupyter could connect without having to copy the address every time and re-set the connection. Like if j-ml-proxy had a way to have a permanent ip address/port that would always redirect to the correct/current running Jupyter server. (And then even more ideal, if there were a set of commends you could send to that "middle man layer" that could view and manage the active Jupyter kernels!) I don't know if Jupyter allows such a thing to be possible, though

We are actively working on improving our product for its usage with vscode to be seamless, and get as close as we can in terms of experience to be similar to other kernels.

Stay tuned. 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
answered Issue has been answered demo question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants