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

Render with Panel button breaks with Jupyterlab root_dir #3170

Closed
govinda18 opened this issue Feb 3, 2022 · 2 comments
Closed

Render with Panel button breaks with Jupyterlab root_dir #3170

govinda18 opened this issue Feb 3, 2022 · 2 comments

Comments

@govinda18
Copy link
Contributor

ALL software version info

Jupyterlab 3.2.8
Panel master

Description of expected behavior and the observed behavior

Hi team, jupyterlab allows you to set a custom root_dir and preferred_dir config for your server app. You can set it using the following:

$ jupyter notebook --generate-config

Then in your ~/.jupyter folder you should find jupyter_notebook_config.py and there we can set a configuration like:

## The directory to use for notebooks and kernels.
#  Default: ''
c.ServerApp.root_dir = '/'
## Preferred starting directory to use for notebooks and kernels.
#  Default: ''
import os
c.ServerApp.preferred_dir = os.getcwd()

Now when you launch jupyter, the jupyter extensions get file path relative to the root_dir. So suppose you launch your lab from /home/gt and root dir is set to / and we try to render a panel preview for a file located at /home/gt/notebook.ipynb, this will break as the path passed to the PanelHandler.get is actually home/gt/notebook.ipynb but the bokeh utility here tries to get abspath which is relative to cwd (which is /home/gt) and it turns out to be /home/gt/home/gt/notebook.ipynb.

Here, what the PanelHandler should be doing is setting the path to bokeh as root_dir + path. A good example is the FileContentsManager of jupyter extension. As you can see here, for all the utility it evaluates the path relative to the root dir.

Complete, minimal, self-contained example code that reproduces the issue

import panel as pn
pn.extension()
pn.pane.Markdown("# Test").servable()

This simple code throws 500 Internal server errors if the following configuration is done -

c.ServerApp.root_dir = '/'
import os
c.ServerApp.preferred_dir = os.getcwd()

and the lab is launched from a directory other than the root_dir. What the above config does is it allows you to access files outside your cwd as well from the lab.

Stack traceback and/or browser JavaScript console output

ERROR 00:22:44 Uncaught exception GET /panel-preview/render/home/totla/Untitled.ipynb (10.218.24.254)

    HTTPServerRequest(protocol='https', host='myhost.com', method='GET', uri='/panel-preview/render/home/totla/Untitled.ipynb', version='HTTP/1.1', remote_ip='10.218.24.254')
    Traceback (most recent call last):
      File "/usr/local/python/python-3.9/std/lib64/python3.9/site-packages/tornado/web.py", line 1704, in _execute
        result = await result
      File "/usr/local/python/python-3.9/std/lib64/python3.9/site-packages/panel/io/jupyter_server_extension.py", line 93, in get
        app = build_single_handler_application(path)
      File "/usr/local/python/python-3.9/std/lib64/python3.9/site-packages/bokeh/command/util.py", line 135, in build_single_handler_application
        raise ValueError("Path for Bokeh server application does not exist: %s" % path)
    ValueError: Path for Bokeh server application does not exist: /home/totla/home/totla/Untitled.ipynb

Proposed Fix

A simple fix here would be similar to what FileContentManager does:

def get_abs_path(self, path):
    try:
        # May be there is a better way to do this
        root_dir = self.application._app.settings['config']['ServerApp']['root_dir']
    except:
        import os
        root_dir = os.getcwd()
    return root_dir + path  

# To be added here https://github.com/holoviz/panel/blob/master/panel/io/jupyter_server_extension.py#L84
path = self.get_abs_path(path)

Similarly, this should be done for PanelWSHandler as well. I was able to make it work with the above change. If this looks fine, I would be happy to raise a PR too. Please let me know if you feel there is a better way to achieve the same.

@philippjfr
Copy link
Member

Thanks @govinda18! Would absolutely love to see a PR with your suggested changes.

govinda18 added a commit to govinda18/panel that referenced this issue Feb 3, 2022
Jupyter sends path relative to the root_dir which can be different from the cwd. This commit fixes the lab extension for panel preview to account for root_dir.

Reference: holoviz#3170
@govinda18
Copy link
Contributor Author

Done.

philippjfr pushed a commit that referenced this issue Feb 3, 2022
Jupyter sends path relative to the root_dir which can be different from the cwd. This commit fixes the lab extension for panel preview to account for root_dir.

Reference: #3170
philippjfr pushed a commit that referenced this issue Mar 26, 2022
Jupyter sends path relative to the root_dir which can be different from the cwd. This commit fixes the lab extension for panel preview to account for root_dir.

Reference: #3170
philippjfr added a commit that referenced this issue Mar 27, 2022
* Fix jinja2 imports (#3258)

* fix: wrong offset when memoryview format is non-byte (#3206)

the offset should be in bytes, not elements. See also bokeh/ipywidgets_bokeh#46 (comment)

* Support jupyter server root_dir with lab extension (#3172)

Jupyter sends path relative to the root_dir which can be different from the cwd. This commit fixes the lab extension for panel preview to account for root_dir.

Reference: #3170

* DOC: add note on enabling panel widget on Jupyter Lab (#3029)

* Remove redundant and confusing JupyterLab install instructions (#3037)

* Remove reference to Python 2 and link to Travis

* Remove redondant install instructions to work with Jupyter

* Update CHANGELOG

* Add setuptools to build requirements

* use https instead of the git:// protocol in GHA (#3092)

* Try to switch to setuptools (#3138)

* Try to switch to setuptools

* Continue on error

* CI: fix Macos tests workflow (#3160)

* attempt to fix CI on macos

* forgot bokeh channel

* remove strict setting

* reset strict and add pyctdev channel

* remove continue_on_error and test py310

* clean up test workflow

* add comment about PYCTDEV_SELF_CHANNEL

* remove py310

* Revert change to build backend

* Update setuptools pin

* Pin setuptools

* Further pin updates

* Pin setuptools (#3265)

* Fix image_url test

* Pin pyvista

* Bump panel.js version

* Fix GH workflows

* Bump panel.js version

Co-authored-by: Maarten Breddels <maartenbreddels@gmail.com>
Co-authored-by: Govinda Totla <govinda.totla.cse16@itbhu.ac.in>
Co-authored-by: Ray Bell <rayjohnbell0@gmail.com>
Co-authored-by: Maxime Liquet <35924738+maximlt@users.noreply.github.com>
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

2 participants