Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

add qt config option to clear_on_kernel_restart #1681

Merged
merged 4 commits into from

5 participants

Paul Ivanov Fernando Perez Thomas Kluyver Min RK SamuelDeleglise
Paul Ivanov
Owner

Previously, restarting a kernel in the qtconsole always cleared the
entire session. Since qtconsole has a %clear command, it makes sense to
allow users to decouple the two activities - the restarting of kernels,
and clearing the console. This PR adds a configuration option which allows for the restarting of kernels without losing console history. The default value for clear_on_kernel_restart is set to True to stay consistent with the way things worked before this functionality was added.

To test, run:

ipython qtconsole --IPythonWidget.clear_on_kernel_restart=False

Here's what it looks like when the user restarts the kernel on In[3]:

Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41)
Type "copyright", "credits" or "license" for more information.

IPython 0.13.dev -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.
%guiref   -> A brief reference about the graphical user interface.

In [1]: 0x42
Out[1]: 66

In [2]: 042
Out[2]: 34

In [3]: # restarting kernel...
#------------------------------------------

In [1]:
Paul Ivanov
Owner

I could use feedback if I should go ahead and remove the messagebox logic on after line 522 to also use this new option.

Fernando Perez
Owner

No time to review right now (gotta catch a flight early tomorrow, will be offline for a bit), but I wanted to say thank you for this. I've been wanting this for a long time.

Paul Ivanov ivanov add qt config option to clear_on_kernel_restart
Previously, restarting a kernel in the qtconsole always cleared the
entire session. Since qtconsole has a %clear command, it makes sense to
allow users to decouple the two activities - the restarting of kernels,
and clearing the console.  This commit adds a configuration option which
allows for the restarting of kernels without losing console history. The
default value for clear_on_kernel_restart is set to True to stay
consistent with the way things worked before this functionality was
added.

To test, run:

    ipython qtconsole --IPythonWidget.clear_on_kernel_restart=False

Here's what it looks like when the user restarts the kernel on In[3]:

 Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41)
 Type "copyright", "credits" or "license" for more information.

 IPython 0.13.dev -- An enhanced Interactive Python.
 ?         -> Introduction and overview of IPython's features.
 %quickref -> Quick reference.
 help      -> Python's own help system.
 object?   -> Details about 'object', use 'object??' for extra details.
 %guiref   -> A brief reference about the graphical user interface.

 In [1]: 0x42
 Out[1]: 66

 In [2]: 042
 Out[2]: 34

 In [3]: # restarting kernel...
 #------------------------------------------
 In [1]:
90356a4
Thomas Kluyver
Owner

IIRC, we're using a rich text widget - is it easy to draw a proper horizontal line, rather than an ASCII art -----?

Paul Ivanov
Owner

ok, now using html <hr> instead of ASCII ----, took me a bit to get to the bottom of it, so @takluyver better be happy with the results! ;)

Thomas Kluyver
Owner

Thanks Paul, I've just tested, and I like the way it's looking now.

Min RK
Owner

Looks good to me. Any reason not to merge?

Paul Ivanov
Owner

@minrk, actually, yeah, hang on, found a minor problem with this

Paul Ivanov ivanov move clear_on_kernel_restart back inside reset()
I originally made a new method (reset_on_restart) to accommodate this
new functionality, but it turns out I ended up needing to replicate
almost everything that reset() already does inside this new method. In
particular, reset_on_restart did not work properly when restarting a
kernel that was in debug mode.

Everything should now work, and we don't have another method to deal
with.
a9c55d5
Paul Ivanov
Owner

ok @minrk and @takluyver - I think it's ready to merge again:

I originally made a new method (reset_on_restart) to accommodate this
new functionality, but it turns out I ended up needing to replicate
almost everything that reset() already does inside this new method. In
particular, reset_on_restart did not work properly when restarting a
kernel that was in debug mode.

Everything should now work, and we don't have another method to deal
with.

Paul Ivanov
Owner

having second thoughts about calling the parameter force, does clear make more sense? or force_clear?

Min RK
Owner

clear seems most appropriate, since that's the switch you are flipping.

Thomas Kluyver
Owner

I agree that clear makes more sense - force sounds like "don't ask for confirmation".

Other than that, it looks good, and I think putting it inside the reset method makes sense. I assume you've tested it again in both modes.

Paul Ivanov
Owner

ok, thanks for being patient with me, guys. Renamed it to clear, ready to merge, and yes, tested it in both modes.

Paul Ivanov ivanov merged commit cffb287 into from
SamuelDeleglise

Hi all,

I am not sure if this is the best place for this comment, but I will try anyway:
I use ipython notebook (IPython 2.1.0) on windows. The options are interpreted correctly (for instance launching the notebook with --IPythonWidget.confirm_restart=False has the correct effect), however, the option IPythonWidget.clear_on_kernel_restart=True has no effect. This is particularly cumbersome since there is a small bug associated with restarting the kernel without clearing the screen: the console doesn't scroll down to the first command prompt (it scrolls down only to the horizontal line, and I have to hit enter once to have it scroll to the new command prompt).

Are these 2 bugs (scrolling problem + no way to disable clear_on_kernel_restart) windows related and is there a way to solve them? I use the pythonxy distribution and these problems have been appearing on all the computers I have tried so far.

Thanks for your help,
Samuel

Thomas Kluyver
Owner

Hi Samuel - it looks like the code to check that option was taken out somewhere along the line. If you can work out where it should be checked, please make a pull request to do so.

SamuelDeleglise

Hi Thomas, Thanks for the quick reply, I have been looking around in the code, especially in
IPython\qt\console\frontend_widget.py
Hoping for a commented line or something as trivial as that, but after one hour of digging, I must admit my incompetence. I tried to add a self.reset(True) in restart_kernel(..) but I have the feeling it's actually restarting the kernel twice since the restart command is taking very long...
Sorry for not being more helpful,
Cheers,
Samuel

Thomas Kluyver
Owner

It certainly looks like sticking self.reset(True) in the right part of restart_kernel (about where it adds the "Restarting kernel..." HTML) should work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 2, 2012
  1. Paul Ivanov

    add qt config option to clear_on_kernel_restart

    ivanov authored
    Previously, restarting a kernel in the qtconsole always cleared the
    entire session. Since qtconsole has a %clear command, it makes sense to
    allow users to decouple the two activities - the restarting of kernels,
    and clearing the console.  This commit adds a configuration option which
    allows for the restarting of kernels without losing console history. The
    default value for clear_on_kernel_restart is set to True to stay
    consistent with the way things worked before this functionality was
    added.
    
    To test, run:
    
        ipython qtconsole --IPythonWidget.clear_on_kernel_restart=False
    
    Here's what it looks like when the user restarts the kernel on In[3]:
    
     Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41)
     Type "copyright", "credits" or "license" for more information.
    
     IPython 0.13.dev -- An enhanced Interactive Python.
     ?         -> Introduction and overview of IPython's features.
     %quickref -> Quick reference.
     help      -> Python's own help system.
     object?   -> Details about 'object', use 'object??' for extra details.
     %guiref   -> A brief reference about the graphical user interface.
    
     In [1]: 0x42
     Out[1]: 66
    
     In [2]: 042
     Out[2]: 34
    
     In [3]: # restarting kernel...
     #------------------------------------------
     In [1]:
Commits on May 5, 2012
  1. Paul Ivanov
Commits on May 14, 2012
  1. Paul Ivanov

    move clear_on_kernel_restart back inside reset()

    ivanov authored
    I originally made a new method (reset_on_restart) to accommodate this
    new functionality, but it turns out I ended up needing to replicate
    almost everything that reset() already does inside this new method. In
    particular, reset_on_restart did not work properly when restarting a
    kernel that was in debug mode.
    
    Everything should now work, and we don't have another method to deal
    with.
Commits on May 15, 2012
  1. Paul Ivanov
This page is out of date. Refresh to see the latest.
Showing with 23 additions and 7 deletions.
  1. +23 −7 IPython/frontend/qt/console/frontend_widget.py
30 IPython/frontend/qt/console/frontend_widget.py
View
@@ -95,6 +95,9 @@ class FrontendWidget(HistoryConsoleWidget, BaseFrontendMixin):
enable_calltips = Bool(True, config=True,
help="Whether to draw information calltips on open-parentheses.")
+ clear_on_kernel_restart = Bool(True, config=True,
+ help="Whether to clear the console when the kernel is restarted")
+
# Emitted when a user visible 'execute_request' has been submitted to the
# kernel from the FrontendWidget. Contains the code to be executed.
executing = QtCore.Signal(object)
@@ -516,6 +519,8 @@ def _handle_shutdown_reply(self, msg):
if reply == QtGui.QMessageBox.Yes:
self.exit_requested.emit(self)
else:
+ # XXX: remove message box in favor of using the
+ # clear_on_kernel_restart setting?
reply = QtGui.QMessageBox.question(self, title,
"Kernel has been reset. Clear the Console?",
QtGui.QMessageBox.Yes,QtGui.QMessageBox.No)
@@ -529,7 +534,7 @@ def _started_channels(self):
""" Called when the KernelManager channels have started listening or
when the frontend is assigned an already listening KernelManager.
"""
- self.reset()
+ self.reset(clear=True)
#---------------------------------------------------------------------------
# 'FrontendWidget' public interface
@@ -563,9 +568,13 @@ def interrupt_kernel(self):
self._append_plain_text('Kernel process is either remote or '
'unspecified. Cannot interrupt.\n')
- def reset(self):
- """ Resets the widget to its initial state. Similar to ``clear``, but
- also re-writes the banner and aborts execution if necessary.
+ def reset(self, clear=False):
+ """ Resets the widget to its initial state if ``clear`` parameter or
+ ``clear_on_kernel_restart`` configuration setting is True, otherwise
+ prints a visual indication of the fact that the kernel restarted, but
+ does not clear the traces from previous usage of the kernel before it
+ was restarted. With ``clear=True``, it is similar to ``%clear``, but
+ also re-writes the banner and aborts execution if necessary.
"""
if self._executing:
self._executing = False
@@ -573,8 +582,15 @@ def reset(self):
self._reading = False
self._highlighter.highlighting_on = False
- self._control.clear()
- self._append_plain_text(self.banner)
+ if self.clear_on_kernel_restart or clear:
+ self._control.clear()
+ self._append_plain_text(self.banner)
+ else:
+ self._append_plain_text("# restarting kernel...")
+ self._append_html("<hr><br>")
+ # XXX: Reprinting the full banner may be too much, but once #1680 is
+ # addressed, that will mitigate it.
+ #self._append_plain_text(self.banner)
# update output marker for stdout/stderr, so that startup
# messages appear after banner:
self._append_before_prompt_pos = self._get_cursor().position()
@@ -693,7 +709,7 @@ def _process_execute_error(self, msg):
self._append_plain_text(traceback)
def _process_execute_ok(self, msg):
- """ Process a reply for a successful execution equest.
+ """ Process a reply for a successful execution request.
"""
payload = msg['content']['payload']
for item in payload:
Something went wrong with that request. Please try again.