1- # type: ignore
21import asyncio
32import io
43import inspect
109import threading
1110import time
1211
13- from typing import Optional
12+ from typing import Optional , Any
1413from typing_extensions import Literal
1514
1615from werkzeug .serving import make_server
1716
18-
1917try :
20- from IPython import get_ipython
21- from IPython .display import IFrame , display , Javascript
22- from IPython .core .display import HTML
23- from IPython .core .ultratb import FormattedTB
24- from retrying import retry
25- from comm import create_comm
26- import nest_asyncio
18+ from IPython import get_ipython # type: ignore[attr-defined]
19+ from IPython .display import IFrame , display , Javascript # type: ignore[import-not-found]
20+ from IPython .core .display import HTML # type: ignore[import-not-found]
21+ from IPython .core .ultratb import FormattedTB # type: ignore[import-not-found]
22+ from retrying import retry # type: ignore[import-untyped]
23+ from comm import create_comm # type: ignore[import-not-found]
24+ import nest_asyncio # type: ignore[import-untyped]
2725
28- import requests
26+ import requests # type: ignore[import-untyped]
2927
30- _dash_comm = create_comm (target_name = "dash" )
28+ _dash_comm = create_comm (target_name = "dash" ) # type: ignore[misc]
3129 _dep_installed = True
3230except ImportError :
3331 _dep_installed = False
34- _dash_comm = None
35- get_ipython = lambda : None
32+ _dash_comm = None # type: ignore[assignment]
33+
34+ # Stub implementations for when dependencies are not installed
35+ def get_ipython (): # type: ignore[misc]
36+ return None
37+
38+ # pylint: disable=unused-argument
39+ def retry (* args : Any , ** kwargs : Any ): # type: ignore[misc]
40+ def decorator (func : Any ) -> Any :
41+ return func
42+
43+ return decorator
44+
45+ # pylint: disable=unused-argument,too-few-public-methods
46+ class IFrame : # type: ignore[no-redef]
47+ def __init__ (self , * args : Any , ** kwargs : Any ) -> None :
48+ pass
49+
50+ # pylint: disable=unused-argument,too-few-public-methods
51+ def display (* args : Any , ** kwargs : Any ) -> None : # type: ignore[misc]
52+ pass
53+
54+ # pylint: disable=unused-argument,too-few-public-methods
55+ class Javascript : # type: ignore[no-redef]
56+ def __init__ (self , * args : Any , ** kwargs : Any ) -> None :
57+ pass
58+
59+ # pylint: disable=unused-argument,too-few-public-methods
60+ class HTML : # type: ignore[no-redef]
61+ def __init__ (self , * args : Any , ** kwargs : Any ) -> None :
62+ pass
63+
64+ # pylint: disable=unused-argument,too-few-public-methods
65+ class FormattedTB : # type: ignore[no-redef]
66+ def __init__ (self , * args : Any , ** kwargs : Any ) -> None :
67+ pass
68+
69+ def __call__ (self , * args : Any , ** kwargs : Any ) -> None :
70+ pass
71+
72+ # pylint: disable=unused-argument,too-few-public-methods
73+ class _RequestsModule : # type: ignore[misc]
74+ class ConnectionError (Exception ):
75+ pass
76+
77+ def get (self , * args : Any , ** kwargs : Any ) -> Any :
78+ return None
79+
80+ requests = _RequestsModule () # type: ignore[assignment]
81+
82+ # pylint: disable=unused-argument,too-few-public-methods
83+ class _NestAsyncioModule : # type: ignore[misc]
84+ @staticmethod
85+ def apply (* args : Any , ** kwargs : Any ) -> None :
86+ pass
87+
88+ nest_asyncio = _NestAsyncioModule () # type: ignore[assignment]
3689
3790JupyterDisplayMode = Literal ["inline" , "external" , "jupyterlab" , "tab" , "_none" ]
3891
@@ -44,7 +97,7 @@ def _get_skip(error: Exception):
4497
4598 tb = error .__traceback__
4699 skip = 1
47- while tb .tb_next is not None :
100+ while tb is not None and tb .tb_next is not None :
48101 skip += 1
49102 tb = tb .tb_next
50103 if tb .tb_frame .f_code is _invoke_callback .__code__ :
@@ -89,9 +142,9 @@ def convert(name, locals=locals, formatarg=formatarg, formatvalue=formatvalue):
89142 return "(\n " + ",\n " .join (specs ) + "\n )"
90143
91144
92- _jupyter_config = {}
145+ _jupyter_config : Any = {}
93146
94- _caller = {}
147+ _caller : Any = {}
95148
96149
97150def _send_jupyter_config_comm_request ():
@@ -102,9 +155,10 @@ def _send_jupyter_config_comm_request():
102155 ipython is not None
103156 and hasattr (ipython , "kernel" )
104157 and ipython .kernel is not None
158+ and _dash_comm is not None
105159 ):
106160 _caller ["parent" ] = ipython .kernel .get_parent ()
107- _dash_comm .send ({"type" : "base_url_request" })
161+ _dash_comm .send ({"type" : "base_url_request" }) # type: ignore[attr-defined]
108162
109163
110164def _jupyter_comm_response_received ():
@@ -121,19 +175,19 @@ def _request_jupyter_config(timeout=2):
121175 _send_jupyter_config_comm_request ()
122176
123177 # Get shell and kernel
124- shell = get_ipython ()
125- kernel = shell .kernel
178+ shell = ipython
179+ kernel = shell .kernel # type: ignore[attr-defined]
126180
127181 # Start capturing shell events to replay later
128182 captured_events = []
129183
130184 def capture_event (stream , ident , parent ):
131185 captured_events .append ((stream , ident , parent ))
132186
133- kernel .shell_handlers ["execute_request" ] = capture_event
187+ kernel .shell_handlers ["execute_request" ] = capture_event # type: ignore[attr-defined]
134188
135189 # increment execution count to avoid collision error
136- shell .execution_count += 1
190+ shell .execution_count += 1 # type: ignore[attr-defined]
137191
138192 # Allow kernel to execute comms until we receive the jupyter configuration comm
139193 # response
@@ -181,7 +235,7 @@ class JupyterDash:
181235 alive_token = str (uuid .uuid4 ())
182236 inline_exceptions : bool = True
183237
184- _servers = {}
238+ _servers : Any = {}
185239
186240 def infer_jupyter_proxy_config (self ):
187241 """
@@ -343,7 +397,7 @@ def run_app(
343397 except ImportError :
344398 pass
345399
346- err_q = queue .Queue ()
400+ err_q : Any = queue .Queue ()
347401
348402 server = make_server (host , port , app .server , threaded = True , processes = 0 )
349403 logging .getLogger ("werkzeug" ).setLevel (logging .ERROR )
@@ -422,7 +476,7 @@ def wait_for_app():
422476 @staticmethod
423477 def _display_in_colab (dashboard_url , port , mode , width , height ):
424478 # noinspection PyUnresolvedReferences
425- from google .colab import output # pylint: disable=E0401,E0611,C0415
479+ from google .colab import output # type: ignore[import-not-found] # pylint: disable=E0401,E0611,C0415
426480
427481 if mode == "inline" :
428482 output .serve_kernel_port_as_iframe (port , width = width , height = height )
@@ -444,13 +498,14 @@ def _display_in_jupyter(dashboard_url, port, mode, width, height):
444498 elif mode == "jupyterlab" :
445499 # Update front-end extension
446500 # FIXME valid only in jupyterlab but accepted in regular notebooks show nothing.
447- _dash_comm .send (
448- {
449- "type" : "show" ,
450- "port" : port ,
451- "url" : dashboard_url ,
452- }
453- )
501+ if _dash_comm is not None :
502+ _dash_comm .send ( # type: ignore[attr-defined]
503+ {
504+ "type" : "show" ,
505+ "port" : port ,
506+ "url" : dashboard_url ,
507+ }
508+ )
454509
455510 @staticmethod
456511 def serve_alive ():
0 commit comments