1919import wx
2020
2121from main_window .page import MainPageBase
22- from grass .workflows .directory import JupyterDirectoryManager
23- from grass .workflows .server import JupyterServerInstance , JupyterServerRegistry
22+ from grass .workflows .environment import JupyterEnvironment
2423
2524from .notebook import JupyterAuiNotebook
2625from .toolbars import JupyterToolbar
@@ -47,13 +46,9 @@ def __init__(
4746 self ._giface = giface
4847 self .statusbar = statusbar
4948 self .workdir = workdir
50-
5149 self .SetName ("Jupyter" )
5250
53- self .directory_manager = JupyterDirectoryManager (
54- workdir = self .workdir , create_template = create_template
55- )
56- self .server_manager = JupyterServerInstance (workdir = self .workdir )
51+ self .env = JupyterEnvironment (self .workdir , create_template )
5752
5853 self .toolbar = JupyterToolbar (parent = self )
5954 self .aui_notebook = JupyterAuiNotebook (parent = self )
@@ -72,31 +67,38 @@ def _layout(self):
7267 self .Layout ()
7368
7469 def SetUpNotebookInterface (self ):
75- """Start server and load files available in a working directory."""
76- # Prepare the working directory (find all existing files, copy a template file if needed)
77- self .directory_manager .prepare_files ()
78-
79- # Start the Jupyter server in the specified working directory
80- self .server_manager .start_server ()
81-
82- # Register server to server registry
83- JupyterServerRegistry .get ().register (self .server_manager )
84-
85- # Update the status bar with server info
86- status_msg = _ (
87- "Jupyter server has started at {url} (PID: {pid}) in working directory {dir}"
88- ).format (
89- url = self .server_manager .server_url ,
90- pid = self .server_manager .pid ,
91- dir = self .workdir ,
92- )
93- self .SetStatusText (status_msg , 0 )
70+ """Setup Jupyter notebook environment and load initial notebooks."""
71+ try :
72+ self .env .setup ()
73+ except Exception as e :
74+ wx .MessageBox (
75+ _ ("Failed to start Jupyter environment:\n {}" ).format (str (e )),
76+ _ ("Startup Error" ),
77+ wx .ICON_ERROR ,
78+ )
79+ return
9480
95- # Load all existing files found in the working directory as separate tabs
96- for fname in self .directory_manager .files :
97- url = self .server_manager .get_url (fname .name )
81+ # Load notebook tabs
82+ for fname in self .env .directory .files :
83+ try :
84+ url = self .env .server .get_url (fname .name )
85+ except RuntimeError as e :
86+ wx .MessageBox (
87+ _ ("Failed to get Jupyter server URLt:\n {}" ).format (str (e )),
88+ _ ("Startup Error" ),
89+ wx .ICON_ERROR ,
90+ )
91+ return
9892 self .aui_notebook .AddPage (url = url , title = fname .name )
9993
94+ self .SetStatusText (
95+ _ ("Jupyter server started at {url} (PID: {pid}), directory: {dir}" ).format (
96+ url = self .env .server .server_url ,
97+ pid = self .env .server .pid ,
98+ dir = str (self .workdir ),
99+ )
100+ )
101+
100102 def Switch (self , file_name ):
101103 """
102104 Switch to existing notebook tab.
@@ -114,9 +116,16 @@ def Open(self, file_name):
114116 Open a Jupyter notebook to a new tab and switch to it.
115117 :param file_name: Name of the .ipynb file (e.g., 'example.ipynb') (str).
116118 """
117- url = self .server_manager .get_url (file_name )
118- self .aui_notebook .AddPage (url = url , title = file_name )
119- self .aui_notebook .SetSelection (self .aui_notebook .GetPageCount () - 1 )
119+ try :
120+ url = self .env .server .get_url (file_name )
121+ self .aui_notebook .AddPage (url = url , title = file_name )
122+ self .aui_notebook .SetSelection (self .aui_notebook .GetPageCount () - 1 )
123+ except RuntimeError as e :
124+ wx .MessageBox (
125+ _ ("Failed to get Jupyter server URL:\n {}" ).format (str (e )),
126+ _ ("URL Error" ),
127+ wx .ICON_ERROR ,
128+ )
120129
121130 def OpenOrSwitch (self , file_name ):
122131 """
@@ -136,7 +145,7 @@ def Import(self, source_path, new_name=None):
136145 :param new_name: Optional new name for the imported file (str).
137146 """
138147 try :
139- path = self .directory_manager .import_file (source_path , new_name = new_name )
148+ path = self .env . directory .import_file (source_path , new_name = new_name )
140149 self .Open (path .name )
141150 self .SetStatusText (_ ("File '{}' imported and opened." ).format (path .name ), 0 )
142151 except Exception as e :
@@ -169,7 +178,7 @@ def OnImport(self, event=None):
169178
170179 source_path = Path (dlg .GetPath ())
171180 file_name = source_path .name
172- target_path = self .directory_manager . workdir / file_name
181+ target_path = self .workdir / file_name
173182
174183 # File is already in the working directory
175184 if source_path .resolve () == target_path .resolve ():
@@ -222,7 +231,7 @@ def OnExport(self, event=None):
222231 destination_path = Path (dlg .GetPath ())
223232
224233 try :
225- self .directory_manager .export_file (
234+ self .env . directory .export_file (
226235 file_name , destination_path , overwrite = True
227236 )
228237 self .SetStatusText (
@@ -254,7 +263,7 @@ def OnCreate(self, event=None):
254263 return
255264
256265 try :
257- path = self .directory_manager .create_new_notebook (new_name = name )
266+ path = self .env . directory .create_new_notebook (new_name = name )
258267 except Exception as e :
259268 wx .MessageBox (
260269 _ ("Failed to create notebook:\n {}" ).format (str (e )),
@@ -287,25 +296,25 @@ def OnCloseWindow(self, event):
287296 event .Veto ()
288297 return
289298
290- if self .server_manager :
291- try :
292- # Stop the Jupyter server
293- self .server_manager .stop_server ()
294-
295- # Unregister server from server registry
296- JupyterServerRegistry .get ().unregister (self .server_manager )
297- self .SetStatusText (_ ("Jupyter server has been stopped." ), 0 )
298- except RuntimeError as e :
299- wx .MessageBox (
300- _ ("Failed to stop the Jupyter server:\n {}" ).format (str (e )),
301- _ ("Error" ),
302- wx .ICON_ERROR | wx .OK ,
303- )
304- self .SetStatusText (_ ("Failed to stop Jupyter server." ), 0 )
299+ # Get server info
300+ url = self .env .server .server_url
301+ pid = self .env .server .pid
305302
306- # Clean up the server manager
307- if hasattr (self .GetParent (), "jupyter_server_manager" ):
308- self .GetParent ().jupyter_server_manager = None
309-
310- # Close the notebook panel
303+ # Stop server and close panel
304+ try :
305+ self .env .stop ()
306+ except RuntimeError as e :
307+ wx .MessageBox (
308+ _ ("Failed to stop Jupyter server at {url} (PID: {pid}):\n {err}" ).format (
309+ url = url , pid = pid , err = str (e )
310+ ),
311+ caption = _ ("Error" ),
312+ style = wx .ICON_ERROR | wx .OK ,
313+ )
314+ return
315+ self .SetStatusText (
316+ _ ("Jupyter server at {url} (PID: {pid}) has been stopped" ).format (
317+ url = url , pid = pid
318+ )
319+ )
311320 self ._onCloseWindow (event )
0 commit comments