nbviewer icon indicating copy to clipboard operation
nbviewer copied to clipboard

400 Errors for static files when using base_url flag

Open ghost opened this issue 7 years ago • 3 comments

I am attempting to use nbviewer to display notebooks with Bokeh plots. I have the plots saved as separate Javascript files, which are loaded into the notebook via a script tag embedded in an HTML object.

Directory structure is roughly this:

.
|-- bokeh_plots
|   |-- plot_0.js
|   `-- plot_1.js
`-- notebook.ipynb

When running my nbviewer at the root (so the primary access point is ip:port) this setup works. The notebook is accessible at ip:port/urls/domain/path/notebook.ipynb, and all of the Bokeh plots are rendered.

However, I am running this behind a proxy, so I would like to use the base_url parameter to change the root to "/nbviewer". With this flag set, I can successfully access the home page at ip:port/nbviewer, with all of the static content of that page rendered successfully. However, when I try to access my notebook at ip:port/nbviewer/urls/domain/path/notebook.ipynb, the notebook is returned successfully, but the request for the Javascript files returns a 400 error.

Example Error Message:

  [I 180504 13:01:07 client:90] Fetched https://domain/path/bokeh_plots/test_plot_6.js in 17.08 ms
  [E 180504 13:01:07 base:626] Failed to render file from url: https://domain/path/test_plot_6.js

    Traceback (most recent call last):
      File "/usr/local/lib/python3.4/dist-packages/nbformat/reader.py", line 14, in parse_json
        nb_dict = json.loads(s, **kwargs)
      File "/usr/lib/python3.4/json/__init__.py", line 318, in loads
        return _default_decoder.decode(s)
      File "/usr/lib/python3.4/json/decoder.py", line 343, in decode
        obj, end = self.raw_decode(s, idx=_w(s, 0).end())
      File "/usr/lib/python3.4/json/decoder.py", line 361, in raw_decode
        raise ValueError(errmsg("Expecting value", s, err.value)) from None
    ValueError: Expecting value: line 1 column 1 (char 0)

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "/srv/nbviewer/nbviewer/providers/base.py", line 623, in finish_notebook
        nb = reads(json_notebook, current_nbformat)
      File "/usr/local/lib/python3.4/dist-packages/nbformat/__init__.py", line 74, in reads
        nb = reader.reads(s, **kwargs)
      File "/usr/local/lib/python3.4/dist-packages/nbformat/reader.py", line 58, in reads
        nb_dict = parse_json(s, **kwargs)
      File "/usr/local/lib/python3.4/dist-packages/nbformat/reader.py", line 17, in parse_json
        raise NotJSONError(("Notebook does not appear to be JSON: %r" % s)[:77] + "...")
    nbformat.reader.NotJSONError: Notebook does not appear to be JSON: '(function() {\n  var fn = function() {\...

[W 180504 13:01:07 web:1618] 400 GET /nbviewer/urls/domain/path/bokeh_plots/test_plot_6.js (10.31.115.83): Error reading JSON notebook
[W 180504 13:01:07 log:53] 400 GET /nbviewer/urls/domain/path/bokeh_plots/test_plot_6.js (10.31.115.83) 1425.55ms referer="http://ip:port/nbviewer/urls/domain/path/notebook.ipynb" user-agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36"

So it looks like it's trying to render the Javascript files as notebooks and fails (because they're obviously not notebooks). But without base_url, it never attempts this, it simply injects the script into the loaded notebook as expected.

TL;DR: Without base_url, nbviewer can load static content in the notebook's directory, and with base_url it cannot.

Note that for this example that I am not going through a proxy, I am still hitting the server at the IP hosting the service directly. So this isn't a proxy issue. I've also omitted the actual domain and path because they are private, but the logs seem to indicate it is finding the resources just fine, just not handling them correctly.

Currently running this in the latest jupyter/nbviewer Docker container, using Python 3.4.

If you need more information please let me know. Thanks!

ghost avatar May 04 '18 13:05 ghost

I think the problem might be in this if-statement (https://github.com/jupyter/nbviewer/blob/master/nbviewer/providers/url/handlers.py#L57) which does not take a non-blank base_url.

parente avatar Jul 08 '18 01:07 parente

Does anyone can merge this issue to master and provide docker image ? Or, is there any workaround ?

tkang007 avatar Jan 21 '19 17:01 tkang007

Hi,

I am sharing broken url at index.html page when using base_url=/nbviewer

Broken:
https://.../github/ipython/ipython/blob/6.x/examples/IPython%20Kernel/Index.ipynb

Fixed url with base_url:
https://.../nbviewer/github/ipython/ipython/blob/6.x/examples/IPython%20Kernel/Index.ipynb

tkang007 avatar Jan 21 '19 21:01 tkang007