Skip to content

Add Restart Session button to reset Python namespace#37

Merged
giswqs merged 1 commit intomainfrom
add-restart-session
Feb 18, 2026
Merged

Add Restart Session button to reset Python namespace#37
giswqs merged 1 commit intomainfrom
add-restart-session

Conversation

@giswqs
Copy link
Copy Markdown
Member

@giswqs giswqs commented Feb 18, 2026

Summary

  • Adds a "Restart" button (amber/warning color) to the main toolbar
  • Clicking it prompts for confirmation, then:
    • Clears all user-defined variables from the namespace
    • Re-imports default modules (QGIS, processing, numpy, pandas, os, sys, etc.)
    • Updates autocomplete in all cells with the fresh namespace
  • Useful when you want to start fresh without reloading the notebook

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

  • Open a notebook and run some cells that define variables
  • Click "Restart" button
  • Confirm in the dialog
  • Verify status bar shows "Session restarted"
  • Run a cell that uses a previously-defined variable — should get NameError
  • Verify pre-imported modules (iface, QgsProject, etc.) still work

- 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
Copilot AI review requested due to automatic review settings February 18, 2026 19:14
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Copy link

Copilot AI Feb 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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()

Copilot uses AI. Check for mistakes.
@giswqs giswqs merged commit 84e1077 into main Feb 18, 2026
7 checks passed
@giswqs giswqs deleted the add-restart-session branch February 18, 2026 19:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants