Skip to content

Commit 699c041

Browse files
authored
feat(array): auto render in colab and jupyter (#40)
1 parent 934d57d commit 699c041

File tree

3 files changed

+49
-10
lines changed

3 files changed

+49
-10
lines changed

docarray/array/mixins/io/csv.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ def save_embeddings_csv(self, file: Union[str, TextIO], **kwargs) -> None:
2626
file_ctx = nullcontext(file)
2727
else:
2828
file_ctx = open(file, 'w')
29-
np.savetxt(file_ctx, self.embeddings, **kwargs)
29+
with file_ctx:
30+
np.savetxt(file_ctx, self.embeddings, **kwargs)
3031

3132
def save_csv(
3233
self,

docarray/array/mixins/plot.py

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os.path
44
import tempfile
55
import threading
6+
import time
67
import warnings
78
from collections import Counter
89
from math import sqrt, ceil, floor
@@ -220,21 +221,57 @@ def _get_fastapi_app():
220221
kwargs=dict(app=app, port=port, log_level='error'),
221222
daemon=True,
222223
)
223-
t_m.start()
224224
url_html_path = (
225225
f'http://localhost:{port}/static/index.html?config={config_fn}'
226226
)
227+
t_m.start()
227228
try:
228-
import webbrowser
229-
230-
webbrowser.open(url_html_path, new=2)
229+
_env = str(get_ipython()) # noqa
230+
if 'ZMQInteractiveShell' in _env:
231+
_env = 'jupyter'
232+
elif 'google.colab' in _env:
233+
_env = 'colab'
231234
except:
232-
pass # intentional pass, browser support isn't cross-platform
233-
finally:
234-
print(
235-
f'You should see a webpage opened in your browser, '
236-
f'if not, you may open {url_html_path} manually'
235+
_env = 'local'
236+
if _env == 'jupyter':
237+
warnings.warn(
238+
f'Showing iframe in cell, you may want to open {url_html_path} in a new tab for better experience. '
239+
f'Also, `localhost` may need to be changed to the IP address if your jupyter is running remotely. '
240+
f'Click "stop" button in the toolbar to move to the next cell.'
241+
)
242+
time.sleep(
243+
1
244+
) # jitter is required otherwise encouter werid `strict-origin-when-cross-origin` error in browser
245+
from IPython.display import IFrame, display # noqa
246+
247+
display(IFrame(src=url_html_path, width="100%", height=600))
248+
elif _env == 'colab':
249+
from google.colab.output import eval_js # noqa
250+
251+
colab_url = eval_js(f'google.colab.kernel.proxyPort({port})')
252+
colab_url += f'/static/index.html?config={config_fn}'
253+
warnings.warn(
254+
f'Showing iframe in cell, you may want to open {colab_url} in a new tab for better experience. '
255+
f'Click "stop" button in the toolbar to move to the next cell.'
237256
)
257+
time.sleep(
258+
1
259+
) # jitter is required otherwise encouter werid `strict-origin-when-cross-origin` error in browser
260+
from IPython.display import IFrame, display
261+
262+
display(IFrame(src=colab_url, width="100%", height=600))
263+
elif _env == 'local':
264+
try:
265+
import webbrowser
266+
267+
webbrowser.open(url_html_path, new=2)
268+
except:
269+
pass # intentional pass, browser support isn't cross-platform
270+
finally:
271+
print(
272+
f'You should see a webpage opened in your browser, '
273+
f'if not, you may open {url_html_path} manually'
274+
)
238275
t_m.join()
239276
return path
240277

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
'scipy',
5353
'av',
5454
'fastapi',
55+
'uvicorn',
5556
]
5657
},
5758
classifiers=[

0 commit comments

Comments
 (0)