Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added context menu option to reset IPython namespace #2997

Merged
merged 7 commits into from
Feb 24, 2016
Merged

Added context menu option to reset IPython namespace #2997

merged 7 commits into from
Feb 24, 2016

Conversation

webcsm
Copy link
Contributor

@webcsm webcsm commented Feb 19, 2016

Fixes #2986
Fixes #3000


Hi @ccordoba12! This is my first attempt to help you guys. Tell me if I need to fix anything else in the PR. If this goes through, maybe I'll tackle the vertical lines thing. #2986

@webcsm webcsm changed the title Added context menu option to reset IPython namespace. #2986 Added context menu option to reset IPython namespace Feb 19, 2016
@ccordoba12 ccordoba12 added this to the v3.0beta4 milestone Feb 20, 2016
@ccordoba12
Copy link
Member

@webcsm, thanks a lot for your contribution. I have a couple of comments after a quick review:

  1. Please remove your changes to our spyderlib//defaults/defaults-*.ini files. There's no need to touch those files at all.
  2. Please remove all blank space changes you introduced in widgets/ipython.py. Those changes make things harder to review and introduce hard to resolve conflicts for other pull requests.
  3. Please remove the new icon you introduced. There's no need for all actions to have an icon, but even if it's considered necessary to add one later on, we want to be able to select it ourselves :-)

@webcsm
Copy link
Contributor Author

webcsm commented Feb 20, 2016

Thank you for your patience in showing me the problems with my PR. I was not sure about which files I should change and well... about the removed trailing spaces, it ended up being a configuration of my Spyder that I had to turn off.

Another thing is that I put "%reset -f" in the function, which forces the reset without asking for confirmation. That's what I would expect from this option. What do you think about that?

@ccordoba12
Copy link
Member

Thank you for your patience in showing me the problems with my PR

No problem :-) I think you've done a pretty good job.

Another thing is that I put "%reset -f" in the function

I'd prefer to ask for confirmation from the user. @goanpeca and @Nodd, what do you think?

@ccordoba12
Copy link
Member

I mean, resetting your namespace is quite a big deal, especially if you've loaded a lot of data. So if you select this new option by mistake, you could be very angry :-)

@goanpeca
Copy link
Member

I agree with @ccordoba12, destructive actions should always have a question :-p, something like

        reply = QMessageBox.question(
            None,
            "Clear namespace for console XX",
            "Are you sure you want to clean the namespace?",
            QMessageBox.Yes | QMessageBox.No,
            )

        if reply == QMessageBox.No:
            return

None,
_("Reset IPython namespace"),
_("All user-defined variables will be removed."
"<br>Are you sure you want to clean the namespace?"),
Copy link
Member

Choose a reason for hiding this comment

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

Please change clean here to reset (to be in line with the %reset magic).

Copy link
Member

Choose a reason for hiding this comment

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

I mean, this line should read

Are you sure you want to reset the namespace?

@webcsm
Copy link
Contributor Author

webcsm commented Feb 22, 2016

Thank you for your suggestions @goanpeca and @ccordoba12. I've fixed the problems in the last commit.

@goanpeca
Copy link
Member

👍 thanks for the work on this :-) @webcsm

@ccordoba12 ccordoba12 modified the milestones: v3.0beta3, v3.0beta4 Feb 23, 2016
@ccordoba12
Copy link
Member

Thanks lot for keep working on this @webcsm. Everything is shaping nicely!

There's only one little thing before merging: the new shortcut (Ctrl+R) is not working. For that you need to create the shortcut explicitly (instead of just adding it to the context menu action).

For that you need to check how we add other shortcuts, in the create_shortcuts method of widgets/ipython.py (current line 250 of that file).

@webcsm
Copy link
Contributor Author

webcsm commented Feb 23, 2016

Hey guys, I had to create the shortcut and then call get_shortcut in add_actions_to_context_menu. Please, check if I did everything right.

@ccordoba12
Copy link
Member

Now things are failing with this error:

Traceback (most recent call last):
  File "/home/carlos/Projects/spyder/github-repo/spyderlib/plugins/ipythonconsole.py", line 855, in create_new_client
    menu_actions=self.menu_actions)
  File "/home/carlos/Projects/spyder/github-repo/spyderlib/widgets/ipython.py", line 415, in __init__
    local_kernel=False)
  File "/home/carlos/Projects/spyder/github-repo/spyderlib/widgets/ipython.py", line 188, in __init__
    self.shortcuts = self.create_shortcuts()
  File "/home/carlos/Projects/spyder/github-repo/spyderlib/widgets/ipython.py", line 256, in create_shortcuts
    reset_namespace = create_shortcut(self.reset_namespace, context='Console',
AttributeError: 'IPythonShellWidget' object has no attribute 'reset_namespace'

This mean that you need to move the current reset_namespace method from IPythonClient to IPythonShellWidget.

For the contex menu action to work, you also need to define a new reset_namespace method in IPythonClient, that would be something like

def reset_namespace(self):
    self.shellwidget.reset_namespace()

I know this is a little bit confuse, but the idea is that you need two reset_namespace methods: one in IPythonShellWidget to make the shortcut work, and one in IPythonClient to show the context menu action (and which calls the first one for simplicity).

If you read the code you will notice that this is what we do for clear_console :-)

@webcsm
Copy link
Contributor Author

webcsm commented Feb 23, 2016

Don't worry @ccordoba12. I worked on an open-source project using Qt for 7 years and I know how things are. I'm commiting now the fix. I took the liberty to make the clear console method within the client call the method in the shell widget too so that the actual code is encapsulated in just one function.

@webcsm
Copy link
Contributor Author

webcsm commented Feb 23, 2016

One of the tests failed. I don't know why. Is that normal?

@ccordoba12
Copy link
Member

There are still some failures:

  1. You need to use new_shortcut instead of create_shortcut because the last one is used for configurable shortcuts (and we don't have a config option for "Reset namespace" nor we want one for now :-)
  2. The same problem arises when using get_shortcut on the context menu action definition.

I noticed those problem while live testing your branch :-)


In short, this is the diff of the last change you need to do to have everything working:

diff --git a/spyderlib/widgets/ipython.py b/spyderlib/widgets/ipython.py
index 59b5c42..9d6aee7 100644
--- a/spyderlib/widgets/ipython.py
+++ b/spyderlib/widgets/ipython.py
@@ -267,17 +267,16 @@ These commands were executed:
                                   parent=self)
         clear_console = create_shortcut(self.clear_console, context='Console',
                                         name='Clear shell', parent=self)
-        reset_namespace = create_shortcut(self.reset_namespace, context='Console',
-                                        name='Reset namespace', parent=self)

         # Fixed shortcuts
         new_shortcut("Ctrl+T", self, lambda: self.new_client.emit())
+        new_shortcut("Ctrl+R", self, lambda: self.reset_namespace())
         new_shortcut(SHORTCUT_INLINE, self,
                      lambda: self._control.enter_array_inline())
         new_shortcut(SHORTCUT_TABLE, self,
                      lambda: self._control.enter_array_table())

-        return [inspect, clear_console, reset_namespace]
+        return [inspect, clear_console]

     def clean_invalid_var_chars(self, var):
         """
@@ -601,8 +600,7 @@ class IPythonClient(QWidget, SaveHistoryMixin):
                                           icon=ima.icon('editdelete'),
                                           triggered=self.clear_line)
         reset_namespace_action = create_action(self, _("Reset namespace"),
-                                          QKeySequence(get_shortcut('console',
-                                                    'reset namespace')),
+                                          QKeySequence("Ctrl+R"),
                                           triggered=self.reset_namespace)
         clear_console_action = create_action(self, _("Clear console"),
                                              QKeySequence(get_shortcut('console',

@webcsm
Copy link
Contributor Author

webcsm commented Feb 24, 2016

Sorry if it took more time than it should. It's rather difficult to change a code that you don't know very well.

@ccordoba12
Copy link
Member

Don't worry about it :-) It wasn't my intention to be harsh, and I hope my comments don't discourage you to keep contributing to Spyder ;-)

I just noticed that you didn't test your changes after doing them (else you'd have noticed that new errors keep appearing :-)

And to do live testing, most of the core developers start Spyder (from the root of our git clones) with this command:

python bootstrap.py

That avoids installing Spyder in a virtualenv or conda env, and keep re-installing it after every change.

So my advice for next time is: please do live testing and verify that things are working as expected. Our continuous integration testing is not fine-grained enough to detect that things like shortcuts and context-menu actions are working correctly.

And even though @goanpeca and me tried to guide you the best we could, you noticed we also made our blunders, and were not right all the time. So live testing is the cure for that :-)

@ccordoba12
Copy link
Member

And finally, thanks for your contribution!

ccordoba12 added a commit that referenced this pull request Feb 24, 2016
Added context menu option to reset IPython namespace
@ccordoba12 ccordoba12 merged commit c331237 into spyder-ide:master Feb 24, 2016
@webcsm webcsm deleted the reset_option branch February 24, 2016 23:04
@webcsm
Copy link
Contributor Author

webcsm commented Feb 24, 2016

Hey @ccordoba12. Thanks for your tip. I thought that it would give me some trouble to set up the environment for testing... and I'm doing this in my spare time. But following your tip I quickly got everything set up.

@ccordoba12
Copy link
Member

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Shortcuts: reset console / empty namespace Add context menu option for %reset
3 participants