Skip to content
This repository was archived by the owner on Aug 29, 2025. It is now read-only.

Commit 469a2f1

Browse files
authored
Add Colab support (#27)
* Add Colab support * Set default iframe width (for Colab and regular Jupyter) to 100% * Update to version 0.3.0rc1
2 parents f338c5c + 4c751d0 commit 469a2f1

File tree

7 files changed

+101
-12
lines changed

7 files changed

+101
-12
lines changed

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,5 @@
55
/dist
66
/build/
77
/extensions/jupyterlab/dist/
8-
/jupyter_dash/labextension/dist/
9-
/jupyter_dash/labextension/package.json
108
.ipynb_checkpoints
119
/extensions/jupyterlab/README.md

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
All notable changes to `jupyter-dash` will be documented in this file.
33
This project adheres to [Semantic Versioning](http://semver.org/).
44

5+
## 0.3.0 - ???
6+
### Added
7+
- Added suport for using JupyterDash in Google Colab ([#27](https://github.com/plotly/jupyter-dash/pull/27))
8+
- Added support for installing JupyterDash from git using pip: (e.g. `pip install git+https://github.com/plotly/jupyter-dash.git@master`)
9+
10+
### Changed
11+
- The default display width in `mode='inline'` is now `100%` to fill the screen width.
12+
513
## 0.2.1 - 2020-05-19
614
### Added
715
- Remove f-strings to support Python 3.5

jupyter_dash/jupyter_app.py

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import re
1010
import sys
1111
import inspect
12+
import warnings
1213

1314
from IPython import get_ipython
1415
from IPython.display import IFrame, display
@@ -37,6 +38,7 @@ class JupyterDash(dash.Dash):
3738
)
3839
default_server_url = None
3940
_in_ipython = get_ipython() is not None
41+
_in_colab = "google.colab" in sys.modules
4042
_token = str(uuid.uuid4())
4143

4244
@classmethod
@@ -63,14 +65,31 @@ def infer_jupyter_proxy_config(cls):
6365
see what JupyterLab extensions are installed by running the following command:
6466
$ jupyter labextension list
6567
"""
66-
if not JupyterDash._in_ipython:
67-
# No op when not running in a Jupyter context
68+
if not JupyterDash._in_ipython or JupyterDash._in_colab:
69+
# No op when not running in a Jupyter context or when in Colab
6870
return
69-
70-
_request_jupyter_config()
71+
else:
72+
# Assume classic notebook or JupyterLab
73+
_request_jupyter_config()
7174

7275
def __init__(self, name=None, server_url=None, **kwargs):
7376
""""""
77+
# Strip unsupported properties and warn
78+
if JupyterDash._in_colab:
79+
unsupported_colab_props = [
80+
'requests_pathname_prefix',
81+
'routes_pathname_prefix',
82+
'url_base_pathname'
83+
]
84+
for prop in unsupported_colab_props:
85+
if prop in kwargs:
86+
kwargs.pop(prop)
87+
warnings.warn(
88+
"The {prop} argument is ignored when running in Colab".format(
89+
prop=prop
90+
)
91+
)
92+
7493
# Call superclass constructor
7594
super(JupyterDash, self).__init__(name=name, **kwargs)
7695

@@ -105,6 +124,9 @@ def __init__(self, name=None, server_url=None, **kwargs):
105124
if domain_base:
106125
# Dash Enterprise set PLOTLY_DASH_DOMAIN_BASE environment variable
107126
server_url = 'https://' + domain_base
127+
elif JupyterDash._in_colab:
128+
warnings.warn("The server_url argument is ignored when running in Colab")
129+
server_url = None
108130

109131
self.server_url = server_url
110132

@@ -126,7 +148,7 @@ def alive():
126148

127149
def run_server(
128150
self,
129-
mode=None, width=800, height=650, inline_exceptions=None,
151+
mode=None, width="100%", height=650, inline_exceptions=None,
130152
**kwargs
131153
):
132154
"""
@@ -166,7 +188,10 @@ def run_server(
166188
kwargs['port'] = port
167189

168190
# Validate / infer display mode
169-
valid_display_values = ["jupyterlab", "inline", "external"]
191+
if JupyterDash._in_colab:
192+
valid_display_values = ["inline", "external"]
193+
else:
194+
valid_display_values = ["jupyterlab", "inline", "external"]
170195

171196
if mode is None:
172197
mode = JupyterDash.default_mode
@@ -293,6 +318,21 @@ def wait_for_app():
293318

294319
wait_for_app()
295320

321+
if JupyterDash._in_colab:
322+
self._display_in_colab(dashboard_url, port, mode, width, height)
323+
else:
324+
self._display_in_jupyter(dashboard_url, port, mode, width, height)
325+
326+
def _display_in_colab(self, dashboard_url, port, mode, width, height):
327+
from google.colab import output
328+
if mode == 'inline':
329+
output.serve_kernel_port_as_iframe(port, width=width, height=height)
330+
elif mode == 'external':
331+
# Display a hyperlink that can be clicked to open Dashboard
332+
print("Dash app running on:")
333+
output.serve_kernel_port_as_window(port, anchor_text=dashboard_url)
334+
335+
def _display_in_jupyter(self, dashboard_url, port, mode, width, height):
296336
if mode == 'inline':
297337
display(IFrame(dashboard_url, width, height))
298338
elif mode == 'external':
3.19 KB
Binary file not shown.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"name": "jupyterlab-dash",
3+
"version": "0.2.0",
4+
"description": "A JupyterLab extensions for rendering Plotly Dash apps",
5+
"keywords": [
6+
"jupyter",
7+
"jupyterlab",
8+
"jupyterlab-extension"
9+
],
10+
"homepage": "https://github.com/plotly/jupyter-dash",
11+
"bugs": {
12+
"url": "https://github.com/plotly/jupyter-dash/issues"
13+
},
14+
"license": "MIT",
15+
"author": "Plotly",
16+
"files": [
17+
"lib/**/*.{d.ts,eot,gif,html,jpg,js,js.map,json,png,svg,woff2,ttf}",
18+
"style/**/*.{css,eot,gif,html,jpg,json,png,svg,woff2,ttf}"
19+
],
20+
"main": "lib/index.js",
21+
"types": "lib/index.d.ts",
22+
"repository": {
23+
"type": "git",
24+
"url": "git+https://github.com/plotly/jupyter-dash.git"
25+
},
26+
"scripts": {
27+
"build": "tsc",
28+
"clean": "rimraf lib",
29+
"prepare": "npm run clean && npm run build",
30+
"prettier": "prettier --write '{!(package),src/**,!(lib)/**}{.js,.jsx,.ts,.tsx,.css,.json,.md}'",
31+
"watch": "tsc -w"
32+
},
33+
"dependencies": {
34+
"@jupyterlab/application": "^2.0.0",
35+
"@jupyterlab/notebook": "^2.0.0",
36+
"@jupyterlab/console": "^2.0.0"
37+
},
38+
"devDependencies": {
39+
"prettier": "2.0.5",
40+
"rimraf": "3.0.2",
41+
"typescript": "3.9.3"
42+
},
43+
"jupyterlab": {
44+
"extension": true
45+
}
46+
}

jupyter_dash/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.2.1.post1"
1+
__version__ = "0.3.0rc1"

setup.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,5 @@ def readme():
153153
],
154154
cmdclass=dict(
155155
build_js=BuildLabextension,
156-
build_py=js_prerelease(build_py),
157-
egg_info=js_prerelease(egg_info),
158-
sdist=js_prerelease(sdist),
159156
)
160157
)

0 commit comments

Comments
 (0)