Add Restart Session button to reset Python namespace#37
Conversation
- Adds a "Restart" button to the toolbar (amber/warning color) - Clicking it prompts for confirmation, then clears all user-defined variables and re-imports the default modules (QGIS, processing, numpy, pandas, etc.) - Updates autocomplete in all cells with the fresh namespace
There was a problem hiding this comment.
Pull request overview
This pull request adds a "Restart Session" feature to the QGIS Notebook plugin, allowing users to reset the Python namespace without reloading the entire notebook. The feature is implemented as an amber/warning-styled button in the main toolbar that clears all user-defined variables while re-importing default modules.
Changes:
- Added
_restart_session()method that prompts for confirmation, clears the namespace, re-imports default modules, and updates autocomplete in all cells - Added "warning" button type with amber/orange styling to the button creation system
- Added "Restart" button to the main toolbar with appropriate styling and event handling
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| # Restart session button | ||
| self.restart_btn = self._create_toolbar_button("Restart", "warning") | ||
| self.restart_btn.clicked.connect(self._restart_session) |
There was a problem hiding this comment.
The restart button should be disabled when cells are executing (when self._is_running_all is True) to prevent undefined behavior from clearing the namespace while code is still running. Similar to how the run_all_btn is disabled during execution (see line 2362), the restart_btn should also be disabled and re-enabled in the _run_all_cells and _execute_next_in_queue methods.
| self.restart_btn.clicked.connect(self._restart_session) | |
| self.restart_btn.clicked.connect(self._restart_session) | |
| # Ensure restart is disabled while cells are running (self._is_running_all) | |
| self._restart_btn_timer = QTimer(self.restart_btn) | |
| self._restart_btn_timer.setInterval(200) | |
| self._restart_btn_timer.timeout.connect( | |
| lambda: self.restart_btn.setEnabled( | |
| not getattr(self, "_is_running_all", False) | |
| ) | |
| ) | |
| self._restart_btn_timer.start() |
Summary
Note: Stopping execution mid-run is not implemented as Python's synchronous
exec()cannot be safely interrupted without threading, which would add complexity and potential stability issues with QGIS/PyQt.Test plan