takluyver / nbopen

Open a Jupyter notebook in the best available server
BSD 3-Clause "New" or "Revised" License
304 stars 58 forks source link

Consider supporting opening in jupyterlab #51

Open akhmerov opened 6 years ago

akhmerov commented 6 years ago

I believe quite a bit of machinery should apply to jupyterlab unchanged, specifically finding the best lab server.

Now that jupyterlab has /lab/tree endpoint also opening the notebook (or any other file) in a new lab tab would work.

JoranDox commented 6 years ago

Funny, I have the reverse problem: mine opens in jupyterlab while I don't want that (I prefer just using chrome tabs rather than learning yet another tab switch keybind).

I'm on ubuntu 16.04, what are you on @akhmerov ?

Does this mean this is already solved then or is there a config file we both missed?

Edit: uninstalling jupyterlab with conda (conda remove jupyterlab) worked for me, but is not ideal of course. @akhmerov , what python are you using? Anaconda or other? Your issue might be solved (=workaround) with just anaconda installing jupyterlab in the root environment.

takluyver commented 6 years ago

Nbopen doesn't know anything about Jupyterlab specifically - I would guess that it depends on your config for lab and/or notebook how things are opened.

cuchoi commented 6 years ago

Mine is opening in a new tab and I want it to open in JupyterLab, anybody knows how to do this?

francisco-dlp commented 6 years ago

Same here, it would be great if it could open a new tab in JupyterLab, but it still opens a standard notebook in my case using the lastest version of all the packages.

linuxrider commented 5 years ago

I somehow managed to get it working so that nbopen opens nbs in juypterlab. If jupyterlab is opened in a new tab while another jupyterlab is running it will start a new workspace. And as i found out it is not easy to open in the existing one while browsing the web i decided to have a new firefox profile for jupyterlab. And set the following config parameters with about:config: browser.link.open_newwindow = 1 browser.link.open_newwindow.restriction = 0 The caveat is that the default firefox profile for browsing needs to be started before jupyterlab firefox. Otherwise it has to be started with firefox -no-remote.

jupyter_notebook_config.py c.LabApp.browser = 'firefox -P jupyterlab -no-remote %s'

At some places notebookapp is replaced by labapp nbopen.py

#!/usr/bin/python3

import argparse
import os.path
import webbrowser
from jupyterlab import labapp
from notebook import notebookapp
from notebook.utils import url_path_join, url_escape
import nbformat
from traitlets.config import Config

def find_best_server(filename):
    servers = [si for si in notebookapp.list_running_servers()
               if filename.startswith(si['notebook_dir'])]
    try:
        return max(servers, key=lambda si: len(si['notebook_dir']))
    except ValueError:
        return None

def nbopen(filename):
    filename = os.path.abspath(filename)
    home_dir = os.path.expanduser('~')
    server_inf = find_best_server(filename)
    if server_inf is not None:
        print("Using existing server at", server_inf['notebook_dir'])
        path = os.path.relpath(filename, start=server_inf['notebook_dir'])
        if os.sep != '/':
            path = path.replace(os.sep, '/')
        url = url_path_join(server_inf['url'], 'lab/tree', url_escape(path))
        na = labapp.LabApp.instance()
        na.load_config_file()
        browser = webbrowser.get('firefox -P jupyterlab')
        browser.open(url, new=0)
    else:
        if filename.startswith(home_dir):
            nbdir = home_dir
        else:
            nbdir = os.path.dirname(filename)

        print("Starting new server")
        # Hack: we want to override these settings if they're in the config file.
        # The application class allows 'command line' config to override config
        # loaded afterwards from the config file. So by specifying config, we
        # can use this mechanism.
        cfg = Config()
        cfg.LabApp.file_to_run = os.path.abspath(filename)
        cfg.LabApp.notebook_dir = nbdir
        cfg.LabApp.open_browser = True
        labapp.launch_new_instance(config=cfg,
                                        argv=[],  # Avoid it seeing our own argv
                                        )

def nbnew(filename):
    if not filename.endswith('.ipynb'):
        filename += '.ipynb'
    if os.path.exists(filename):
        msg = "Notebook {} already exists"
        print(msg.format(filename))
        print("Opening existing notebook")
    else:
        nb_version = nbformat.versions[nbformat.current_nbformat]
        nbformat.write(nb_version.new_notebook(),
                       filename)
    return filename

def main(argv=None):
    ap = argparse.ArgumentParser()
    ap.add_argument('-n', '--new', action='store_true', default=False,
                    help='Create a new notebook file with the given name.')
    ap.add_argument('filename', help='The notebook file to open')

    args = ap.parse_args(argv)
    if args.new:
        filename = nbnew(args.filename)
    else:
        filename = args.filename

    nbopen(filename)

if __name__ == '__main__':
    main()
Nestak2 commented 4 years ago

I substituted the content of my nbopen.py file with the content by linuxrider and opening ipynb-files by a double click in jupyterlab worked! Since I am using chrome, not firefox, I had to keep browser = webbrowser.get(na.browser or None) while linuxrider is using browser = webbrowser.get('firefox -P jupyterlab')

try-except commented 3 years ago

If somebody manages to get this working in Windows 10, please post your setup here, thanks in advance