jupyter / dashboards_server

[RETIRED] Server that runs and renders Jupyter notebooks as interactive dashboards
Other
181 stars 48 forks source link

A way to test if the dashboard is deployed to dashboard server? #293

Closed mschmill closed 7 years ago

mschmill commented 7 years ago

Is there a way to test if the notebook is deployed to a dashboard server or not? I have tried some hacks using Javascript and kernel calls but these just seem to break the dashboard.

The reason that we want to do this is that we want to be able to generate files for download and display a link in the dashboard. The problem is that the URL path changes relative to the cwd depending on whether the dashboard is deployed on a dashboard server or not.

jhpedemonte commented 7 years ago

Not sure I fully understand the issue, but I wonder: can you not make the URL absolute, rather than relative? It seems like that would be a much simpler/cleaner solution than having different paths for dashboard vs non-dashboard instances.

But if you must go down this route: in the JavaScript that runs in a dashboard page, we do define the global window.jupyter_dashboard. You could conceivably check for the existence of that.

But for code executed in cells, we don't define anything special for the dashboards case. The kernel gateway itself does set the environment variable KERNEL_GATEWAY, though.

mschmill commented 7 years ago

But the absolute URL is different depending on the notebook's location relative to the cwd, or depending on which port number you use, etc.

Using a relative path works easily and intuitively when running from Jupyter notebook (the relative URL path and file path are the same) but it not when deployed (you have to go up 4 levels ../../../../ in the URL to get to the cwd).

It seems to me like it would be useful to have the kernel set a variable when deployed as a dashboard not just for this case but for debugging/logging/output purposes.

parente commented 7 years ago

It seems to me like it would be useful to have the kernel set a variable when deployed as a dashboard not just for this case but for debugging/logging/output purposes.

The kernel is pretty unaware that it's being used by a dashboard, a notebook, or any other client. That said, your code running on the kernel can look for the KERNEL_GATEWAY environment variable like @jhpedemonte. If you want to know if you're in the dashboard server on the frontend, you can look at window.jupyter_dashboard in JS or a CSS selector I forgot at the moment.

mschmill commented 7 years ago

Thanks, the environment variable seems to do the trick.