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

Consider supporting opening in jupyterlab #51

Open
akhmerov opened this issue Mar 1, 2018 · 7 comments
Open

Consider supporting opening in jupyterlab #51

akhmerov opened this issue Mar 1, 2018 · 7 comments

Comments

@akhmerov
Copy link

akhmerov commented Mar 1, 2018

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
Copy link

JoranDox commented Mar 28, 2018

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
Copy link
Owner

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
Copy link

cuchoi commented Apr 6, 2018

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

@francisco-dlp
Copy link

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
Copy link

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
Copy link

Nestak2 commented Jan 20, 2020

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants