Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Generate "All magics..." menu live #956

Merged
merged 5 commits into from

2 participants

@Carreau
Owner

Hi everyone,

I'm trying to catch up with the merging of the qtconsole. For now constructing the "all magic menu" by querying the kernel about get_ipython().lsmagic() through user_expressions. I thought it might be a good idea.

It does work as long as the action is not automatically done at startup, otherwise there is no prompt.
( try adding :self.window.pop.trigger() just before self.app._exec() in qtconsole.py )
You can trigger the action by hand through the Magics > Populate All Magic Menu Menu, which create a second All Magics Menu.

I don't know why, it's not working at startup, and i'm not sure adding a timer to execute this action later is a good idea.
I was also thinking of updating at each new prompt, or every time the user click on Magic Menu.

If can get a little help on this ...

Otherwise, still not 100% back, still digging in my backups to recover some data, but will do my best.
--
Matthias

@Carreau
Owner

Hi,

Found what I was looking for. I've boud the action to update the menu to a first_response signal.
The menu is now populated with all the magics from %lsmagic.

I've also append '?' to the end of some magic which failed when invoked alone.

@Carreau
Owner

rebased on master, and forced push..

@fperez
Owner

Glad to have you back @Carreau. I'll try to review this over the weekend, if nobody beats me to it.

@fperez
Owner

I found a bit of time to do it now :)

I like this and I think it's useful, as it will encourage users to browse around and try some magics they might not know about. It also seems to work perfectly on my system. I'll add some inline comments that are fairly minor code tidying issues and should be pretty easy to address.

IPython/frontend/qt/console/frontend_widget.py
@@ -137,6 +138,7 @@ class FrontendWidget(HistoryConsoleWidget, BaseFrontendMixin):
self._input_splitter = self._input_splitter_class(input_mode='cell')
self._kernel_manager = None
self._request_info = {}
+ self._callback_dict=dict([])
@fperez Owner
fperez added a note

Create the emtpy dict with {} like the line above, for consistency.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
IPython/frontend/qt/console/frontend_widget.py
@@ -311,6 +313,42 @@ class FrontendWidget(HistoryConsoleWidget, BaseFrontendMixin):
cursor.movePosition(QtGui.QTextCursor.Left, n=len(text))
self._complete_with_items(cursor, rep['content']['matches'])
+ def _silent_exec_callback(self,expr,callback):
@fperez Owner
fperez added a note

pep-8: use a space after each comma

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
IPython/frontend/qt/console/frontend_widget.py
@@ -311,6 +313,42 @@ class FrontendWidget(HistoryConsoleWidget, BaseFrontendMixin):
cursor.movePosition(QtGui.QTextCursor.Left, n=len(text))
self._complete_with_items(cursor, rep['content']['matches'])
+ def _silent_exec_callback(self,expr,callback):
+ """ silently execute a function in the kernel and send the reply to the callback
@fperez Owner
fperez added a note

Docstring formatting: Capitalize properly all sentences, no extra spaces at the beginning of each sentence, and use the formatting guidelines for docstring parameters. We follow the numpy docstring standard, as documented in our developer guide.

Once ytou get used to following proper formatting conventions, they become second nature. And since we follow the same standard as numpy, scipy and many other projects in the 'scipy ecosystem', it will be a good habit to pick up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@fperez fperez commented on the diff
IPython/frontend/qt/console/frontend_widget.py
@@ -311,6 +313,42 @@ class FrontendWidget(HistoryConsoleWidget, BaseFrontendMixin):
cursor.movePosition(QtGui.QTextCursor.Left, n=len(text))
self._complete_with_items(cursor, rep['content']['matches'])
+ def _silent_exec_callback(self,expr,callback):
+ """ silently execute a function in the kernel and send the reply to the callback
+
+ expr should be a valid string to be executed by the kernel.
+
+ callback a function accepting one argument (str)
+
+ the callback is called with the 'repr()' of the result as first argument.
+ to get the object, do 'eval()' on the passed value.
+ """
+
+ # generate uuid, which would be used as a indication of wether or not
+ # the unique request originate from here (can use msg id ?)
+ local_uuid = str(uuid.uuid1())
+ msg_id = self.kernel_manager.shell_channel.execute('',
@fperez Owner
fperez added a note

No need to break up this call into so many extra lines. You should keep individual lines to less than 80 characters, but otherwise you can finish up the dict and closing parens all in one line, for example:

msg_id = self.kernel_manager.shell_channel.execute('', 
            silent=True, user_expressions={ local_uuid:expr } )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
IPython/frontend/qt/console/frontend_widget.py
((8 lines not shown))
+
+ callback a function accepting one argument (str)
+
+ the callback is called with the 'repr()' of the result as first argument.
+ to get the object, do 'eval()' on the passed value.
+ """
+
+ # generate uuid, which would be used as a indication of wether or not
+ # the unique request originate from here (can use msg id ?)
+ local_uuid = str(uuid.uuid1())
+ msg_id = self.kernel_manager.shell_channel.execute('',
+ silent=True,
+ user_expressions={ local_uuid:expr,
+ }
+ )
+ self._callback_dict[local_uuid]=callback
@fperez Owner
fperez added a note

pep-8: spaces around = sign missing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
IPython/frontend/qt/console/frontend_widget.py
((11 lines not shown))
+ the callback is called with the 'repr()' of the result as first argument.
+ to get the object, do 'eval()' on the passed value.
+ """
+
+ # generate uuid, which would be used as a indication of wether or not
+ # the unique request originate from here (can use msg id ?)
+ local_uuid = str(uuid.uuid1())
+ msg_id = self.kernel_manager.shell_channel.execute('',
+ silent=True,
+ user_expressions={ local_uuid:expr,
+ }
+ )
+ self._callback_dict[local_uuid]=callback
+ self._request_info['execute'] = self._ExecutionRequest(msg_id, 'silent_exec_callback')
+
+ def _handle_exec_callback(self,msg):
@fperez Owner
fperez added a note

pep8: space after comma

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
IPython/frontend/qt/console/frontend_widget.py
((12 lines not shown))
+ to get the object, do 'eval()' on the passed value.
+ """
+
+ # generate uuid, which would be used as a indication of wether or not
+ # the unique request originate from here (can use msg id ?)
+ local_uuid = str(uuid.uuid1())
+ msg_id = self.kernel_manager.shell_channel.execute('',
+ silent=True,
+ user_expressions={ local_uuid:expr,
+ }
+ )
+ self._callback_dict[local_uuid]=callback
+ self._request_info['execute'] = self._ExecutionRequest(msg_id, 'silent_exec_callback')
+
+ def _handle_exec_callback(self,msg):
+ """ Called when _silent_exec_callback message comme back from the kernel.
@fperez Owner
fperez added a note

Docstring should follow standard, see above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
IPython/frontend/qt/console/frontend_widget.py
((17 lines not shown))
+ local_uuid = str(uuid.uuid1())
+ msg_id = self.kernel_manager.shell_channel.execute('',
+ silent=True,
+ user_expressions={ local_uuid:expr,
+ }
+ )
+ self._callback_dict[local_uuid]=callback
+ self._request_info['execute'] = self._ExecutionRequest(msg_id, 'silent_exec_callback')
+
+ def _handle_exec_callback(self,msg):
+ """ Called when _silent_exec_callback message comme back from the kernel.
+
+ Strip the message comming back from the kernel and send it to the
+ corresponding callback function.
+ """
+ cnt=msg['content']
@fperez Owner
fperez added a note

spaces around = signs here and elsewhere, won't repeat it in every line but check the whole diff and fix everywhere (though read pep8, they are not to be added in keyword args).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
IPython/frontend/qt/console/frontend_widget.py
((19 lines not shown))
+ silent=True,
+ user_expressions={ local_uuid:expr,
+ }
+ )
+ self._callback_dict[local_uuid]=callback
+ self._request_info['execute'] = self._ExecutionRequest(msg_id, 'silent_exec_callback')
+
+ def _handle_exec_callback(self,msg):
+ """ Called when _silent_exec_callback message comme back from the kernel.
+
+ Strip the message comming back from the kernel and send it to the
+ corresponding callback function.
+ """
+ cnt=msg['content']
+ ue=cnt['user_expressions']
+ for i in ue.keys():
@fperez Owner
fperez added a note

A dict can be iterated without calling .keys(), which is both clearer and faster, as it prevents the creation of an unnecessary list. Simply use for i in ue:.

Argh, never mind. I hadn't noticed you were modifying the dict, in that case you do need to create the static keys list. Ignore the above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
IPython/frontend/qt/console/frontend_widget.py
((22 lines not shown))
+ )
+ self._callback_dict[local_uuid]=callback
+ self._request_info['execute'] = self._ExecutionRequest(msg_id, 'silent_exec_callback')
+
+ def _handle_exec_callback(self,msg):
+ """ Called when _silent_exec_callback message comme back from the kernel.
+
+ Strip the message comming back from the kernel and send it to the
+ corresponding callback function.
+ """
+ cnt=msg['content']
+ ue=cnt['user_expressions']
+ for i in ue.keys():
+ if i in self._callback_dict.keys():
+ f= self._callback_dict[i]
+ f(ue[i])
@fperez Owner
fperez added a note

No need to create a separate f variable here just to call it right away, simply write self._callback_dict[i](ue[i])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
IPython/frontend/qt/console/frontend_widget.py
((20 lines not shown))
+ user_expressions={ local_uuid:expr,
+ }
+ )
+ self._callback_dict[local_uuid]=callback
+ self._request_info['execute'] = self._ExecutionRequest(msg_id, 'silent_exec_callback')
+
+ def _handle_exec_callback(self,msg):
+ """ Called when _silent_exec_callback message comme back from the kernel.
+
+ Strip the message comming back from the kernel and send it to the
+ corresponding callback function.
+ """
+ cnt=msg['content']
+ ue=cnt['user_expressions']
+ for i in ue.keys():
+ if i in self._callback_dict.keys():
@fperez Owner
fperez added a note

Never test dict membership via the keys() list, which is an O(n) operation, use instead if i in self._callback_dict. It's shorter to type, clearer and O(1), so potentially much much faster.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
IPython/frontend/qt/console/mainwindow.py
@@ -544,10 +544,44 @@ class MainWindow(QtGui.QMainWindow):
self.kernel_menu.addSeparator()
+ def update_all_magic_menu(self):
+ # first define a callback which will get the list of all magic and put it in the menu.
@fperez Owner
fperez added a note

Docstring for method missing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
IPython/frontend/qt/console/mainwindow.py
@@ -544,10 +544,44 @@ class MainWindow(QtGui.QMainWindow):
self.kernel_menu.addSeparator()
+ def update_all_magic_menu(self):
+ # first define a callback which will get the list of all magic and put it in the menu.
+ def populate_all_magic_menu(val=None):
+ alm_magic_menu = self.all_magic_menu
+ alm_magic_menu.clear()
+ def make_dynamic_magic(i):
+ def inner_dynamic_magic():
@fperez Owner
fperez added a note

Ouch, you have a 4-level deep nested local function definition! That kind of code is very hard to read and analyze, because of the amount of nested scopes one must track. Can you try to simplify this a little bit to use a flatter code, hopefully with zero or at most one locally defined inner function?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
IPython/frontend/qt/console/mainwindow.py
@@ -544,10 +544,44 @@ class MainWindow(QtGui.QMainWindow):
self.kernel_menu.addSeparator()
+ def update_all_magic_menu(self):
+ # first define a callback which will get the list of all magic and put it in the menu.
+ def populate_all_magic_menu(val=None):
+ alm_magic_menu = self.all_magic_menu
+ alm_magic_menu.clear()
+ def make_dynamic_magic(i):
+ def inner_dynamic_magic():
+ self.active_frontend.execute(i)
+ inner_dynamic_magic.__name__ = "dynamics_magic_s"
+ return inner_dynamic_magic
+
+ # list of protected magic that don't like to be called without argument
+ # append '?' to the end to print the docstring when called from the menu
+ protected_magic= ["more","less","load_ext","pycat","loadpy","save"]
@fperez Owner
fperez added a note

If you are going to be testing for membership in this set, you should make it a set() instead of a list with

protected_magic= set(["more","less","load_ext","pycat","loadpy","save"])

which will make the membership checks much faster.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@fperez fperez commented on the diff
IPython/frontend/qt/console/mainwindow.py
((31 lines not shown))
def init_magic_menu(self):
self.magic_menu = self.menuBar().addMenu("&Magic")
self.all_magic_menu = self.magic_menu.addMenu("&All Magics")
-
+
+ # this action should not appear as it will be cleard when menu
+ # will be updated at first kernel response.
+ self.pop = QtGui.QAction("&Update All Magic Menu ",
+ self,
@fperez Owner
fperez added a note

this can fit into the line above, no need to break it up into so many.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
IPython/frontend/qt/console/qtconsoleapp.py
@@ -451,6 +451,10 @@ class IPythonQtConsoleApp(BaseIPythonApplication):
self.window.log = self.log
self.window.add_tab_with_frontend(self.widget)
self.window.init_menu_bar()
+
+ #we need to populate the 'Magic Menu' once the kernel has answer at least once
@fperez Owner
fperez added a note

pep8: Leave a space after the # comment marker.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@fperez
Owner

OK, I've left a bunch of feedback. Most of it is easy to address and mostly code formatting/documentation issues that you'll get used to quickly. There's only one issue that probably will require some code refactoring.

But overall it's a nice improvement, thanks again!

@Carreau
Owner

@fperez,
Just pushed a squashed commit of all the suggestion you made, I was a little lazy about code formating.
If you are ok with that I can push a squashed commit of everything rebased on the last master.

@fperez
Owner

Sure, go ahead and force push a cleaned up commit if you prefer. I need to tune out of ipython for the day, I have some local work I must tend to now. But I'll get back to this as soon as I can, probably tomorrow.

@Carreau
Owner

Squashed, rebased on master, and forced pushed ...

@Carreau
Owner

Rebase on master agin to stay up to date....

IPython/frontend/qt/console/mainwindow.py
((23 lines not shown))
+ def populate_all_magic_menu(self, listofmagic=None):
+ """Clean "All Magics..." menu and repopulate it with `listofmagic`
+
+ `listofmagic` : string, repr() of a list of strings.
+
+ `listofmagic`is a repr() of list because it is fed with the result of
+ a 'user_expression'
+ """
+ alm_magic_menu = self.all_magic_menu
+ alm_magic_menu.clear()
+
+ # list of protected magic that don't like to be called without argument
+ # append '?' to the end to print the docstring when called from the menu
+ protected_magic = set(["more","less","load_ext","pycat","loadpy","save"])
+
+ for magic in eval(listofmagic):
@fperez Owner
fperez added a note

Why are you using eval here? Eval is a fairly dangerous construct that should only be necessary in very specialized places, I don't see why it's needed here...

@Carreau Owner
Carreau added a note

you'r right, it can be done by regExp ... that was because the return value by the kernel was the repr() of the list, so I thought to use eval without trying harder

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
IPython/frontend/qt/console/mainwindow.py
((36 lines not shown))
+ protected_magic = set(["more","less","load_ext","pycat","loadpy","save"])
+
+ for magic in eval(listofmagic):
+ if magic in protected_magic:
+ pmagic = '%s%s%s'%('%',magic,'?')
+ else:
+ pmagic = '%s%s'%('%',magic)
+ xaction = QtGui.QAction(pmagic,
+ self,
+ triggered=self._make_dynamic_magic(pmagic)
+ )
+ alm_magic_menu.addAction(xaction)
+
+ def update_all_magic_menu(self):
+ # first define a callback which will get the list of all magic and put it in the menu.
+ self.active_frontend._silent_exec_callback('get_ipython().lsmagic()',self.populate_all_magic_menu)
@fperez Owner
fperez added a note

pep8, space after comma

@Carreau Owner
Carreau added a note

missed that one...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
IPython/frontend/qt/console/frontend_widget.py
@@ -311,6 +313,45 @@ class FrontendWidget(HistoryConsoleWidget, BaseFrontendMixin):
cursor.movePosition(QtGui.QTextCursor.Left, n=len(text))
self._complete_with_items(cursor, rep['content']['matches'])
+ def _silent_exec_callback(self, expr, callback):
+ """Silently execute `expr` in the kernel and call `callback` with reply
+
+ `expr` : valid string to be executed by the kernel.
@fperez Owner
fperez added a note

Thanks for updating the docstrings, but please make them conforming the numpy standard, these do not. Our development docs have an explanation. While this one in particular is a private method so it can be kept shorter, if you're going to document the arguments (which is great!), then it should be done in compliance with the rest of the codebase.

In short, they need to read

One line description.

more text
if necessary

Parameters
----------
  foo : str
    a string that is ...

  bar : int
    blah blah...

Returns
-------
x : blah...

Just have a look at the link above; once you get used to it they'll become second nature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
IPython/frontend/qt/console/frontend_widget.py
((23 lines not shown))
+ """Execute `callback` corresonding to `msg` reply, after ``_silent_exec_callback``
+
+ `msg` : raw message send by the kernel containing an `user_expressions`
+ and having a 'silent_exec_callback' kind.
+
+ This fonction will look for a `callback` associated with the
+ corresponding message id. Association has been made by
+ ``_silent_exec_callback``. `callback`is then called with the `repr()`
+ of the value of corresponding `user_expressions` as argument.
+ `callback` is then removed from the known list so that any message
+ coming again with the same id won't trigger it.
+ """
+
+ cnt = msg['content']
+ ue = cnt['user_expressions']
+ for i in ue.keys():
@fperez Owner
fperez added a note

You can just iterate on ue itself: for i in ue. The dict that's getting modified is _callback_dict, so no need to build an explicit keys list here. You only need the explicit keys list if you want to simultaneously iterate and modify in-place the same dict.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
IPython/frontend/qt/console/frontend_widget.py
((26 lines not shown))
+ and having a 'silent_exec_callback' kind.
+
+ This fonction will look for a `callback` associated with the
+ corresponding message id. Association has been made by
+ ``_silent_exec_callback``. `callback`is then called with the `repr()`
+ of the value of corresponding `user_expressions` as argument.
+ `callback` is then removed from the known list so that any message
+ coming again with the same id won't trigger it.
+ """
+
+ cnt = msg['content']
+ ue = cnt['user_expressions']
+ for i in ue.keys():
+ if i in self._callback_dict:
+ self._callback_dict[i](ue[i])
+ self._callback_dict.pop(i)
@fperez Owner
fperez added a note

pop returns the popped value, so these two lines should be replaced with just

self._callback_dict.pop(i)(ue[i])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@fperez fperez commented on the diff
IPython/frontend/qt/console/mainwindow.py
((9 lines not shown))
+ return `fun`, function with no parameters
+
+ `fun` execute `magic` an active frontend at the moment it is triggerd,
+ not the active frontend at the moment it has been created.
+
+ This function is mostly used to create the "All Magics..." Menu at run time.
+ """
+ # need to level nested function to be sure to past magic
+ # on active frontend **at run time**.
+ def inner_dynamic_magic():
+ self.active_frontend.execute(magic)
+ inner_dynamic_magic.__name__ = "dynamics_magic_s"
+ return inner_dynamic_magic
+
+ def populate_all_magic_menu(self, listofmagic=None):
+ """Clean "All Magics..." menu and repopulate it with `listofmagic`
@fperez Owner
fperez added a note

Same comments as above on docstring formatting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@fperez
Owner

Thanks, I think we're getting close! Just a few more fixes and we should be good to merge.

Carreau added some commits
@Carreau Carreau Generate "All magics..." menu live
	this use 'user_expression' request to ask for the value of
	'get_ipython().lsmagic()' and populate the menu with the result.

	As is, the action need to be triggerd by hand, once the console
	have appear. Otherwith there is no prompt

	The logic can certainly also be shortend by using msg.id
	instead of uuid

	We should also keep a list of magic that shouldn't be executed without
	argument (eg: %gui) and don't put them in the menu. or appending '?' at the
	end
5881216
@Carreau Carreau triger menu update when kernel started
	bind the update action to shell_channel's first_response event.
	add a list of magic that don't like beeing called alone ( like
	%more , %pycat , ...Etc) and append '?' in the menu to the end
	to show docsctring when clicked
	This list should be completed in :
		IPython/frontend/qt/console/mainwindow.py
e9402d7
@Carreau Carreau protect two more magic in menu ("loadpy" and "save") 81e2cde
@Carreau Carreau fix @fperez suggestions 414941c
@Carreau Carreau fix docstrig, replace eval by regExp
	replace the `eval` to transform the `repr()` of the magic list send by the kernel
	by a regular expression, change the docstring to be numpy complient (I hope so)
81a8174
@Carreau
Owner

Sooooory ... i fixed everything 2 or 3 days ago and forgot to push...
I rebased and foced push...

@fperez
Owner

Great, very nice! Thanks for the excellent contribution, merging now. Everything looks good here.

@fperez fperez merged commit 8fa6544 into from
@fperez fperez referenced this pull request from a commit
@fperez fperez Temporary fix to work around #1057.
Basically it reverts the effect of #956 and goes back to a static list
for the 'all magics' menu.  I tried to mark very clearly the new code
so we can disable it once a proper fix for #1057 is committed, but we
can't have a broken qt console in master.
65546bf
@Carreau Carreau referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
@Carreau Carreau referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
@Carreau Carreau referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
@Carreau Carreau referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
@Carreau Carreau referenced this pull request from a commit in Carreau/ipython
@Carreau Carreau Revert "Temporary fix to work around #1057."
    This reverts commit 65546bf, done to
    temporaly fixed a race condition introduced by #956, next commits should
    fixe this race condition
4573b16
@Carreau Carreau referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
@Carreau Carreau referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
@Carreau Carreau referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
@Carreau Carreau referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
@Carreau Carreau referenced this pull request from a commit in Carreau/ipython
@Carreau Carreau qtconsole: fix race-cond, handle multiple exec
	fix race condition in qtconsole due to a race condition beween the population
	of the magic menu and the first prompt request. Resolved by upgrading the
	logic of the console to handle several executions request in parallel.

	Some namedTuple might still carry redundent information but the fix is the
	first priority

	fixes #1057 , introduced by #956
8553345
@fperez fperez referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
@fperez fperez referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
@ellisonbg ellisonbg referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
@ellisonbg ellisonbg referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
@fperez fperez referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
@mattvonrocketstein mattvonrocketstein referenced this pull request from a commit in mattvonrocketstein/ipython
@fperez fperez Temporary fix to work around #1057.
Basically it reverts the effect of #956 and goes back to a static list
for the 'all magics' menu.  I tried to mark very clearly the new code
so we can disable it once a proper fix for #1057 is committed, but we
can't have a broken qt console in master.
c725d78
@mattvonrocketstein mattvonrocketstein referenced this pull request from a commit in mattvonrocketstein/ipython
@Carreau Carreau Revert "Temporary fix to work around #1057."
    This reverts commit 65546bf, done to
    temporaly fixed a race condition introduced by #956, next commits should
    fixe this race condition
bdbf575
@mattvonrocketstein mattvonrocketstein referenced this pull request from a commit in mattvonrocketstein/ipython
@Carreau Carreau qtconsole: fix race-cond, handle multiple exec
	fix race condition in qtconsole due to a race condition beween the population
	of the magic menu and the first prompt request. Resolved by upgrading the
	logic of the console to handle several executions request in parallel.

	Some namedTuple might still carry redundent information but the fix is the
	first priority

	fixes #1057 , introduced by #956
f447bb0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 24, 2011
  1. @Carreau

    Generate "All magics..." menu live

    Carreau authored
    	this use 'user_expression' request to ask for the value of
    	'get_ipython().lsmagic()' and populate the menu with the result.
    
    	As is, the action need to be triggerd by hand, once the console
    	have appear. Otherwith there is no prompt
    
    	The logic can certainly also be shortend by using msg.id
    	instead of uuid
    
    	We should also keep a list of magic that shouldn't be executed without
    	argument (eg: %gui) and don't put them in the menu. or appending '?' at the
    	end
  2. @Carreau

    triger menu update when kernel started

    Carreau authored
    	bind the update action to shell_channel's first_response event.
    	add a list of magic that don't like beeing called alone ( like
    	%more , %pycat , ...Etc) and append '?' in the menu to the end
    	to show docsctring when clicked
    	This list should be completed in :
    		IPython/frontend/qt/console/mainwindow.py
  3. @Carreau
  4. @Carreau

    fix @fperez suggestions

    Carreau authored
  5. @Carreau

    fix docstrig, replace eval by regExp

    Carreau authored
    	replace the `eval` to transform the `repr()` of the magic list send by the kernel
    	by a regular expression, change the docstring to be numpy complient (I hope so)
This page is out of date. Refresh to see the latest.
View
61 IPython/frontend/qt/console/frontend_widget.py
@@ -4,6 +4,7 @@
from collections import namedtuple
import sys
import time
+import uuid
# System library imports
from pygments.lexers import PythonLexer
@@ -137,6 +138,7 @@ def __init__(self, *args, **kw):
self._input_splitter = self._input_splitter_class(input_mode='cell')
self._kernel_manager = None
self._request_info = {}
+ self._callback_dict = {}
# Configure the ConsoleWidget.
self.tab_width = 4
@@ -311,6 +313,62 @@ def _handle_complete_reply(self, rep):
cursor.movePosition(QtGui.QTextCursor.Left, n=len(text))
self._complete_with_items(cursor, rep['content']['matches'])
+ def _silent_exec_callback(self, expr, callback):
+ """Silently execute `expr` in the kernel and call `callback` with reply
+
+ the `expr` is evaluated silently in the kernel (without) output in
+ the frontend. Call `callback` with the
+ `repr <http://docs.python.org/library/functions.html#repr> `_ as first argument
+
+ Parameters
+ ----------
+ expr : string
+ valid string to be executed by the kernel.
+ callback : function
+ function accepting one arguement, as a string. The string will be
+ the `repr` of the result of evaluating `expr`
+
+ The `callback` is called with the 'repr()' of the result of `expr` as
+ first argument. To get the object, do 'eval()' onthe passed value.
+
+ See Also
+ --------
+ _handle_exec_callback : private method, deal with calling callback with reply
+
+ """
+
+ # generate uuid, which would be used as a indication of wether or not
+ # the unique request originate from here (can use msg id ?)
+ local_uuid = str(uuid.uuid1())
+ msg_id = self.kernel_manager.shell_channel.execute('',
@fperez Owner
fperez added a note

No need to break up this call into so many extra lines. You should keep individual lines to less than 80 characters, but otherwise you can finish up the dict and closing parens all in one line, for example:

msg_id = self.kernel_manager.shell_channel.execute('', 
            silent=True, user_expressions={ local_uuid:expr } )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ silent=True, user_expressions={ local_uuid:expr })
+ self._callback_dict[local_uuid] = callback
+ self._request_info['execute'] = self._ExecutionRequest(msg_id, 'silent_exec_callback')
+
+ def _handle_exec_callback(self, msg):
+ """Execute `callback` corresonding to `msg` reply, after ``_silent_exec_callback``
+
+ Parameters
+ ----------
+ msg : raw message send by the kernel containing an `user_expressions`
+ and having a 'silent_exec_callback' kind.
+
+ Notes
+ -----
+ This fonction will look for a `callback` associated with the
+ corresponding message id. Association has been made by
+ `_silent_exec_callback`. `callback` is then called with the `repr()`
+ of the value of corresponding `user_expressions` as argument.
+ `callback` is then removed from the known list so that any message
+ coming again with the same id won't trigger it.
+
+ """
+
+ user_exp = msg['content']['user_expressions']
+ for expression in user_exp:
+ if expression in self._callback_dict:
+ self._callback_dict.pop(expression)(user_exp[expression])
+
def _handle_execute_reply(self, msg):
""" Handles replies for code execution.
"""
@@ -342,6 +400,9 @@ def _handle_execute_reply(self, msg):
self._show_interpreter_prompt_for_reply(msg)
self.executed.emit(msg)
+ elif info and info.id == msg['parent_header']['msg_id'] and \
+ info.kind == 'silent_exec_callback' and not self._hidden:
+ self._handle_exec_callback(msg)
else:
super(FrontendWidget, self)._handle_execute_reply(msg)
View
111 IPython/frontend/qt/console/mainwindow.py
@@ -20,6 +20,7 @@
# stdlib imports
import sys
+import re
import webbrowser
from threading import Thread
@@ -544,10 +545,89 @@ def init_kernel_menu(self):
self.kernel_menu.addSeparator()
+ def _make_dynamic_magic(self,magic):
+ """Return a function `fun` that will execute `magic` on active frontend.
+
+ Parameters
+ ----------
+ magic : string
+ string that will be executed as is when the returned function is called
+
+ Returns
+ -------
+ fun : function
+ function with no parameters, when called will execute `magic` on the
+ current active frontend at call time
+
+ See Also
+ --------
+ populate_all_magic_menu : generate the "All Magics..." menu
+
+ Notes
+ -----
+ `fun` execute `magic` an active frontend at the moment it is triggerd,
+ not the active frontend at the moment it has been created.
+
+ This function is mostly used to create the "All Magics..." Menu at run time.
+ """
+ # need to level nested function to be sure to past magic
+ # on active frontend **at run time**.
+ def inner_dynamic_magic():
+ self.active_frontend.execute(magic)
+ inner_dynamic_magic.__name__ = "dynamics_magic_s"
+ return inner_dynamic_magic
+
+ def populate_all_magic_menu(self, listofmagic=None):
+ """Clean "All Magics..." menu and repopulate it with `listofmagic`
@fperez Owner
fperez added a note

Same comments as above on docstring formatting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+
+ Parameters
+ ----------
+ listofmagic : string,
+ repr() of a list of strings, send back by the kernel
+
+ Notes
+ -----
+ `listofmagic`is a repr() of list because it is fed with the result of
+ a 'user_expression'
+ """
+ alm_magic_menu = self.all_magic_menu
+ alm_magic_menu.clear()
+
+ # list of protected magic that don't like to be called without argument
+ # append '?' to the end to print the docstring when called from the menu
+ protected_magic = set(["more","less","load_ext","pycat","loadpy","save"])
+ magics=re.findall('\w+', listofmagic)
+ for magic in magics:
+ if magic in protected_magic:
+ pmagic = '%s%s%s'%('%',magic,'?')
+ else:
+ pmagic = '%s%s'%('%',magic)
+ xaction = QtGui.QAction(pmagic,
+ self,
+ triggered=self._make_dynamic_magic(pmagic)
+ )
+ alm_magic_menu.addAction(xaction)
+
+ def update_all_magic_menu(self):
+ """ Update the list on magic in the "All Magics..." Menu
+
+ Request the kernel with the list of availlable magic and populate the
+ menu with the list received back
+
+ """
+ # first define a callback which will get the list of all magic and put it in the menu.
+ self.active_frontend._silent_exec_callback('get_ipython().lsmagic()', self.populate_all_magic_menu)
+
def init_magic_menu(self):
self.magic_menu = self.menuBar().addMenu("&Magic")
self.all_magic_menu = self.magic_menu.addMenu("&All Magics")
-
+
+ # this action should not appear as it will be cleard when menu
+ # will be updated at first kernel response.
+ self.pop = QtGui.QAction("&Update All Magic Menu ",
+ self, triggered=self.update_all_magic_menu)
@fperez Owner
fperez added a note

this can fit into the line above, no need to break it up into so many.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ self.add_menu_action(self.all_magic_menu, self.pop)
+
self.reset_action = QtGui.QAction("&Reset",
self,
statusTip="Clear all varible from workspace",
@@ -583,34 +663,7 @@ def init_magic_menu(self):
statusTip="List interactive variable with detail",
triggered=self.whos_magic_active_frontend)
self.add_menu_action(self.magic_menu, self.whos_action)
-
- # allmagics submenu:
-
- #for now this is just a copy and paste, but we should get this dynamically
- magiclist=["%alias", "%autocall", "%automagic", "%bookmark", "%cd", "%clear",
- "%colors", "%debug", "%dhist", "%dirs", "%doctest_mode", "%ed", "%edit", "%env", "%gui",
- "%guiref", "%hist", "%history", "%install_default_config", "%install_profiles",
- "%less", "%load_ext", "%loadpy", "%logoff", "%logon", "%logstart", "%logstate",
- "%logstop", "%lsmagic", "%macro", "%magic", "%man", "%more", "%notebook", "%page",
- "%pastebin", "%pdb", "%pdef", "%pdoc", "%pfile", "%pinfo", "%pinfo2", "%popd", "%pprint",
- "%precision", "%profile", "%prun", "%psearch", "%psource", "%pushd", "%pwd", "%pycat",
- "%pylab", "%quickref", "%recall", "%rehashx", "%reload_ext", "%rep", "%rerun",
- "%reset", "%reset_selective", "%run", "%save", "%sc", "%sx", "%tb", "%time", "%timeit",
- "%unalias", "%unload_ext", "%who", "%who_ls", "%whos", "%xdel", "%xmode"]
-
- def make_dynamic_magic(i):
- def inner_dynamic_magic():
- self.active_frontend.execute(i)
- inner_dynamic_magic.__name__ = "dynamics_magic_%s" % i
- return inner_dynamic_magic
-
- for magic in magiclist:
- xaction = QtGui.QAction(magic,
- self,
- triggered=make_dynamic_magic(magic)
- )
- self.all_magic_menu.addAction(xaction)
-
+
def init_window_menu(self):
self.window_menu = self.menuBar().addMenu("&Window")
if sys.platform == 'darwin':
View
4 IPython/frontend/qt/console/qtconsoleapp.py
@@ -451,6 +451,10 @@ def init_qt_elements(self):
self.window.log = self.log
self.window.add_tab_with_frontend(self.widget)
self.window.init_menu_bar()
+
+ # we need to populate the 'Magic Menu' once the kernel has answer at least once
+ self.kernel_manager.shell_channel.first_reply.connect(self.window.pop.trigger)
+
self.window.setWindowTitle('Python' if self.pure else 'IPython')
def init_colors(self):
Something went wrong with that request. Please try again.