Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jupyter (Lab): Browsing the remote file system inside a notebook

I have several Jupyter notebooks which perform analysis on datasets. Right now, a dataset is specified by its filename. Every time the user wants to perform analysis on a new dataset, she/he has to edit the appropriate line in the notebook and modify dataset path string. The datasets can be located in different directories. The notebooks can also be located in different directories. In each notebook I would like to provide a widget that allows the user to browse the remote file system and pick the dataset he/she wants to analyse.

  • Are there any open source projects that support the above functionality? I am looking for something that is still active/supported and has some basic documentation. I did quick search on Google and surprisingly I didn't find anything.

Then I realised that JupyterLab, the evolution of Jupyter, has something very similar to what I want. It already has a very capable file browser but it is a bit "isolated" from everything else.

  • Is it possible somehow to get the relative (to the currently opened notebook) path of the selected file in the JupyterLab file browser?

Thank you.

like image 366
AstrOne Avatar asked Jan 02 '18 06:01

AstrOne


People also ask

How do I browse files in Jupyter notebook?

To find files in the Jupyter Notebook dashboard, you can click on the name of a directory (e.g. ea-bootcamp-day-1 ), and the dashboard will update to show you the contents of the directory. You can click on the name of directory in the Jupyter Notebook Dashboard to navigate into that directory and see the contents.

How do I access remote Jupyter notebook?

Go to http://localhost:9999 . You should be able to select your notebook and you'll be good to go. Show activity on this post. you can run jupyter notebook --no-browser --ip="<remote server ip>" on your remote machine terminal.

How do I connect to remote Jupyter lab?

Connecting and running Jupyterlab from a laptop is straightforward. You simply type jupyter lab into your terminal and Jupyterlab will open in your browser, with the Notebook server running in your terminal.


Video Answer


1 Answers

Here's code for a server-side file browsing widget. Only tested in regular Jypter notebook - not Jupyter Lab. Also, must use a fairly recent version. Hope this helps.

import sys
import os
import ipywidgets as ui
from IPython.display import display

class PathSelector():

    def __init__(self,start_dir,select_file=True):
        self.file        = None 
        self.select_file = select_file
        self.cwd         = start_dir
        self.select      = ui.SelectMultiple(options=['init'],value=(),rows=10,description='') 
        self.accord      = ui.Accordion(children=[self.select]) 

        self.accord.selected_index = None # Start closed (showing path only)
        self.refresh(self.cwd)
        self.select.observe(self.on_update,'value')

    def on_update(self,change):
        if len(change['new']) > 0:
            self.refresh(change['new'][0])

    def refresh(self,item):
        path = os.path.abspath(os.path.join(self.cwd,item))

        if os.path.isfile(path):
            if self.select_file:
                self.accord.set_title(0,path)  
                self.file = path
                self.accord.selected_index = None
            else:
                self.select.value = ()

        else: # os.path.isdir(path)
            self.file = None 
            self.cwd  = path

            # Build list of files and dirs
            keys = ['[..]']; 
            for item in os.listdir(path):
                if item[0] == '.':
                    continue
                elif os.path.isdir(os.path.join(path,item)):
                    keys.append('['+item+']'); 
                else:
                    keys.append(item); 

            # Sort and create list of output values
            keys.sort(key=str.lower)
            vals = []
            for k in keys:
                if k[0] == '[':
                    vals.append(k[1:-1]) # strip off brackets
                else:
                    vals.append(k)

            # Update widget
            self.accord.set_title(0,path)  
            self.select.options = list(zip(keys,vals)) 
            with self.select.hold_trait_notifications():
                self.select.value = ()

f = PathSelector('/some/data')
display(f.accord)
like image 183
aliasid Avatar answered Nov 15 '22 15:11

aliasid