Use Jupyter Notebook on my local computer to run code on a remote computer - jupyter

I use Jupyter Notebook to run bioinformatic analyses, and I love it. However, it only really plays nice when I run it on my personal computer. However, I regularly do analysis using a remote computer with multiple cores to reduce processing time. I'd like to be able to use the Jupyter Notebook interface on my personal computer while everything is actually running on the remote computer. I generally do this via ssh access to the remote computer within the shell and execute all commands at the command line. I'd love to do this from the Jupyter notebook on my personal computer, rather than from the shell on my personal computer. It is relevant that I don't have sudo access on the remote computer.
So far, I've installed miniconda and jupyter notebook on the remote computer like this:
wget https://repo.continuum.io/miniconda/Miniconda-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh
Once conda is installed properly, I install jupyter notebook via miniconda with this line:
conda install jupyter
This installs successfully. I can then start a jupyter notebook session on the remote machine with the line:
jupyter notebook --no-browser
So far, so good. My next question: How do I have my local jupyter notebook connect to the remote machine, so that I can execute commands on the remote machine using my local jupyter notebook? There is some documentation here, however i have been trying different things for hours, but have failed to succeed.
Can anyone give a straight forward method to connect to my remote server, given that I am this far along? I feel like it should just be a matter of entering url addresses and passwords into my local Jupyter notebook (all of this is so easy via ssh in the shell).

Follow the steps below:
Enable port forwarding on remote machine
ssh -N -f -L 127.0.0.1:8898:127.0.0.1:8898 user#remote-machine.com
Do ssh to your remote machine and then run following command on remote machine
jupyter-notebook --no-browser --port=8898
you will see some thing as shown below
Copy/paste this URL into your browser when you connect for the first time,
to login with a token:
http://localhost:8898/token=eaf2f51f9c053f43d8bd093e76f0cc6301b545549c998fa2&token=eaf2f51f9c053f43d8bd093e76f0cc6301b545549c998fa2
Copy and paste the URL in your local machine browser.

If you want to access Jupyter/Ipython notebook running on a VPS remotely, I wrote a tutorial on the digital ocean community site.
As shown in the guide, after installing and running Ipython Notebook using command line on the server, you can connect to the notebook using SSH tunnelling with Putty (on windows) or the ssh -L command on Unix-like systems (ie Mac and Linux)

Related

Matlab connection to a remote server

Is there a way to connect to a host through matlab using SSH? The way that can be done in vscode for example.
I have script and data in the remote machine but matlab license in my local machine. I would like to run the matlab scripts using matlab installed on my laptop.
Alternatively, I clone the scripts on my laptop run them while processing the data located in the remote machine.
https://en.wikipedia.org/wiki/SSHFS
helps you mount a remote filesystem via ssh. So if you can login from your matlab laptop to the data pc via ssh , then sshfs is one step more. From there your data is a path on the matlab laptop.

How to run initialization commands after SSH in VS Code Remote?

Problem
I am trying to connect to my school's computing cluster (aka a linux server with "login node" and "computing node") using VS Code's Remote SSH, but I cannot figure out how to run a command after SSH-ing.
Goal
I simply want to view Python code and test some small lines in a .ipynb jupyter notebook in the computing platform's environment.
Description
Basically, normally in the command line (or mobaXterm of a Windows machine) of my local machine, I first log onto the computing platform's login node with ssh -Y -L PORT:127.0.0.1:PORT username#computing.cluster.ip, and then run srun -t 0-12:00 --pty -p gpu --gres=gpu:1 --x11 --tunnel PORT:PORT /bin/bash to log onto the computing node interactively (shown command allows for port forwarding). The problem is, in VS Code I can only connect to the login node, but after that there's no way for me to run another command and log onto the computing node. The reason I need to get to computing node is that I want to test something with a .ipynb file interactively on VS Code while reading the code, and the login node does not allow me to perform computation.
Failed trials
I've been trying Code-Server, but it does not support .ipynb well (it keeps asking me to install jupyter notebook even though I have installed it in my conda env), possibly because it by default recognizes HPC cluster's Python interpreter which I cannot modify (I can't even select Jupyter kernel in code-server). I also tried to directly use Jupyter Notebook (open Jupyter with port forwarding after getting onto computing node), but reading code on it is much more inconvenient.
Would greatly appreciate your suggestions!

How to know how many sessions are open and in which port using Jupyter notebook on Windows

I am using a windows machine and I have several running sessions of the jupyter notebook. Each session display the content in different ports. I would like to know if there is a way of getting a report including the different sessions I am running and the different ports on which they are displaying the content. I could not find that in the documentation.
Usually what you can do is pass the --help flag to the jupyter notebook command and read the informations in particular here
$ jupyter notebook --help
The Jupyter HTML Notebook.
This launches a Tornado based HTML Notebook Server that serves up an
HTML5/Javascript Notebook client.
Subcommands
-----------
Subcommands are launched as `jupyter-notebook cmd [args]`. For information on
using subcommand 'cmd', do: `jupyter-notebook cmd -h`.
list
List currently running notebook servers.
stop
Stop currently running notebook server for a given port
password
Set a password for the notebook server.
Options
-------
...
Oh ! A list sub command. let's try it:
$ jupyter notebook list
Currently running servers:
http://localhost:8888/ :: /Users/myuser/dev/ipython-website
http://localhost:8889/ :: /Users/myuser
.. well that's on macOS, but you should get the same on Windows.
One of the questions is why do you have multiple sessions as the notebok can browse the filesystem. It is likely confusing.
Enjoy.

Trigger jupyter notebook save

I have a jupyter notebook that I opened on a remote computer (with the kernel also running on that remote computer). I have also opened the notebook on my local computer (connecting to the remote kernel) but there have been changes to the notebook on the remote that haven't been saved.
Is there a way to trigger the remote computer to save the notebook, e.g., by sshing in and issuing some command?
It can be trigged by JavaScript.
IPython.notebook.save_notebook();

ipython notebook on remote server peculiarity

I am taking my first steps with ipython notebook and I installed it successfully on a remote server of mine (over SSH) and I started it using the following command:
ipython notebook --ip='*' ---pylab=inline --port=7777
I then checked on http://myserver.sth:7777/ and the notebook was running just fine. I then wanted to close the SSH connection with the server and keep ipython running in the background. When I did this, I couldn't connect to myserver.sth:7777 anymore. Once I connected again to the remote server by SSH, I could connect again to the notebook. I then tried to use screen to start ipython: I created a new screen by screen -S ipy, I started ipython notebook as above and I used Ctrl+A,D to detach the screen and exit to the TTY. I could still connect remotely to the notebook. I then closed the SSH connection and I got a 404 NOT FOUND error when I tried to access my previously stored notebook and I couldn't see it on the list of notebook at http://myserver.sth:7777/. I tried to create a new notebook, but I got a 500 Internal Server Error.
I also tried running ipython notebook with and without using sudo.
Any ideas?
Rather than use screen, perhaps you could switch to an init script or supervisord to keep IPython notebook up and running.
Let's assume you go the supervisord route:
Install supervisord
Install supervisord using your package manager. For ubuntu it's named supervisor.
apt-get install supervisor
If you decide to install supervisor through pip, you'll have to set up its init.d script yourself.
Write a supervisor configuration file for IPython
The configuration file tells supervisor what to run and how.
After you install supervisor, it should have created /etc/supervisor/supervisord.conf. These lines should exist in the file:
[include]
files = /etc/supervisor/conf.d/*.conf
If they contain these lines, you're in good shape. I only show them to demonstrate where it expects new configuration files. Your configuration file can go there, named something like /etc/supervisor/conf.d/ipynb.conf.
Here's a sample configuration that was generated by Chef by an ipython-notebook-cookbook that runs the notebook in a virtualenv:
[program:ipynb]
command=/home/ipynb/.ipyvirt/bin/ipython notebook --profile=cooked
process_name=%(program_name)s
numprocs=1
numprocs_start=0
autostart=true
autorestart=true
startsecs=1
startretries=3
exitcodes=0,2
stopsignal=QUIT
stopwaitsecs=10
user=ipynb
redirect_stderr=false
stdout_logfile=AUTO
stdout_logfile_maxbytes=50MB
stdout_logfile_backups=10
stdout_capture_maxbytes=0
stdout_events_enabled=false
stderr_logfile=AUTO
stderr_logfile_maxbytes=50MB
stderr_logfile_backups=10
stderr_capture_maxbytes=0
stderr_events_enabled=false
environment=HOME="/home/ipynb",SHELL="/bin/bash",USER="ipynb",PATH="/home/ipynb/.ipyvirt/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games",VIRTUAL_ENV="/home/ipynb/.ipyvirt"
directory=/home/ipynb
serverurl=AUTO
The above supervisor config also relies on an IPython notebook configuration (located at /home/ipynb/.ipython/profile_cooked/ipython_notebook_config.py). This makes configuration much easier (as you can also set up your password hash and many other configurables).:
c = get_config()
# Kernel config
# Make matplotlib plots inline
c.IPKernelApp.pylab = 'inline'
# The IP address the notebook server will listen on.
# If set to '*', will listen on all interfaces.
# c.NotebookApp.ip= '127.0.0.1'
c.NotebookApp.ip='*'
# Port to host on (e.g. 8888, the default)
c.NotebookApp.port = 8888 # If you want it on 80, I recommend iptables rules
# Open browser (probably want False)
c.NotebookApp.open_browser = False
Re-read and update, now that you have the configuration file
supervisorctl reread
supervisorctl update
Reality
In reality, I used to use a Chef cookbook to do the entire installation and configuration. However, using configuration management with tiny stuff like this is a bit of overkill (unless you're orchestrating these in automation).
Nowadays I use Docker images for IPython notebook, orchestrating via JupyterHub or tmpnb.