|
3 | 3 | import os.path |
4 | 4 | import tempfile |
5 | 5 | import threading |
| 6 | +import time |
6 | 7 | import warnings |
7 | 8 | from collections import Counter |
8 | 9 | from math import sqrt, ceil, floor |
@@ -220,21 +221,57 @@ def _get_fastapi_app(): |
220 | 221 | kwargs=dict(app=app, port=port, log_level='error'), |
221 | 222 | daemon=True, |
222 | 223 | ) |
223 | | - t_m.start() |
224 | 224 | url_html_path = ( |
225 | 225 | f'http://localhost:{port}/static/index.html?config={config_fn}' |
226 | 226 | ) |
| 227 | + t_m.start() |
227 | 228 | 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' |
231 | 234 | 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.' |
237 | 256 | ) |
| 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 | + ) |
238 | 275 | t_m.join() |
239 | 276 | return path |
240 | 277 |
|
|
0 commit comments