@@ -68,6 +68,7 @@ def start(self) -> CodeBoxStatus:
6868 out = None
6969 else :
7070 out = subprocess .PIPE
71+ self ._check_installed ()
7172 try :
7273 python = Path (sys .executable ).absolute ()
7374 self .jupyter = subprocess .Popen (
@@ -123,6 +124,20 @@ def _check_port(self) -> None:
123124 self .port += 1
124125 self ._check_port ()
125126
127+ def _check_installed (self ) -> None :
128+ """if jupyter-kernel-gateway is installed"""
129+ import pkg_resources # type: ignore
130+
131+ try :
132+ pkg_resources .get_distribution ("jupyter-kernel-gateway" )
133+ except pkg_resources .DistributionNotFound :
134+ print (
135+ "Make sure 'jupyter-kernel-gateway' is installed "
136+ "when using without a CODEBOX_API_KEY.\n "
137+ "You can install it with 'pip install jupyter-kernel-gateway'."
138+ )
139+ raise
140+
126141 async def astart (self ) -> CodeBoxStatus :
127142 os .makedirs (".codebox" , exist_ok = True )
128143 self .session = aiohttp .ClientSession ()
@@ -132,18 +147,27 @@ async def astart(self) -> CodeBoxStatus:
132147 out = None
133148 else :
134149 out = asyncio .subprocess .PIPE
150+ self ._check_installed ()
135151 python = Path (sys .executable ).absolute ()
136- self .jupyter = await asyncio .create_subprocess_exec (
137- python ,
138- "-m" ,
139- "jupyter" ,
140- "kernelgateway" ,
141- "--KernelGatewayApp.ip='0.0.0.0'" ,
142- f"--KernelGatewayApp.port={ self .port } " ,
143- stdout = out ,
144- stderr = out ,
145- cwd = ".codebox" ,
146- )
152+ try :
153+ self .jupyter = await asyncio .create_subprocess_exec (
154+ python ,
155+ "-m" ,
156+ "jupyter" ,
157+ "kernelgateway" ,
158+ "--KernelGatewayApp.ip='0.0.0.0'" ,
159+ f"--KernelGatewayApp.port={ self .port } " ,
160+ stdout = out ,
161+ stderr = out ,
162+ cwd = ".codebox" ,
163+ )
164+ except Exception as e :
165+ print (e )
166+ raise ModuleNotFoundError (
167+ "Jupyter Kernel Gateway not found, please install it with:\n "
168+ "`pip install jupyter_kernel_gateway`\n "
169+ "to use the LocalBox."
170+ )
147171 while True :
148172 try :
149173 response = await self .session .get (self .kernel_url )
0 commit comments