Shortcut / key combination to access Jupyter notebook menu bar - jupyter-notebook

I prefer the convenience and speed of the keyboard over using the trackpad/mouse whenever possible. Is there a means to access the Jupyter menu bar with the keyboard. For example the Edit menu:
On Linux we could access a top-level item as Alt-E | D for Edit | Delete cells . On macos it is a headache but still possible: <Access Menubar shortcut>| E | D.
Is there any way to achieve that on Jupyter?

So you want to bind custom keyboard shortcuts...
The documentation tell you to use the JavaScript API, where the documentation is in the source code.
So you end up with something like this in custom.js (it's convenient that the first characters of all the menus are distinct)
for(let id of ["filelink", "editlink", "viewlink", "insertlink", "celllink", "kernellink"]) {
const element=document.getElementById(id)
const actionName=Jupyter.notebook.keyboard_manager.actions.register(function (env) { element.click() },
`open-menu-${id[0]}`, "custom")
const shortcut='alt-'+id[0]
Jupyter.keyboard_manager.command_shortcuts.add_shortcut(shortcut, actionName)
Jupyter.keyboard_manager.edit_shortcuts.add_shortcut(shortcut, actionName)
}
This code binds Alt+E to clicking edit menu.
It's rather version-dependent (as it depends on the ID of the links on the menu) -- this is tested on Jupyter notebook version 6.1.6.
If you use a for loop to generate the shortcuts (like in the code above), it's very important that you register actions manually (and have different names for them) instead of add_shortcut(shortcut, handler) because otherwise the handler would generate the action name from the string representation of the handler, and that would be all the same. No JavaScript closure can help you.
If you also want to use Enter key to select a menu, you have to do a little more -- because by default it's bound to enter edit mode:
{
Jupyter.keyboard_manager.command_shortcuts.add_shortcut("enter", function(env){
const element=document.activeElement
if(element.getAttribute("role")==="menuitem")
element.click()
else{
env.notebook.edit_mode();
// alternatively:
//env.notebook.keyboard_manager.actions.call("jupyter-notebook:enter-edit-mode")
}
})
}
Modify it a little more if you want to add shortcut for the 2 other menus, as they don't conveniently have an ID.

Related

Change order and labels of local tasks in Drupal 9

I've got local tasks displayed as tabs on my users' profile page.
Those local tasks are added by various modules.
I need to change the order of those tabs, and change some of their labels.
I can't figure out how to do that.
I tried using the menu_tokens module to replace those tabs by a real, customizable menu, but unfortunately it's not working correctly in Drupal 9 yet and breaks the whole website. Maybe there's another way which I'm not aware of?
Use hook_local_tasks_alter hook inside your custom module. I've checked and it's still valid in d9.
Inside your module_name.module file place a function named according to the convention function module_name_local_tasks_alter(&$local_tasks) {
Place there a breakpoint or simply var dump the local tasks, you will see, how many local tasks tab goes through this function.
In the end you will find entity.user.canonical, just overwrite the item weight in similar manner:
$local_tasks['entity.user.canonical']['weight'] = -3;
$local_tasks['entity.user.edit_form']['weight'] = -2;
Source: https://codimth.com/blog/web/drupal/how-override-local-task-drupal-8

How can I use 'selectedPanel` in storybook?

I noticed a property in Storybooks Options Docs called selectedPanel which I assume will allow me to pre-select an addon panel.
I'm unclear on how to use it. The example is:
options: { selectedPanel: 'storybook/a11y/panel' }
What I don't understand is where the 'storybook/a11y/panel' string comes from. What if I want to preselect the 'Source' panel?
For anyone looking to default to the knobs panel: selectedPanel: 'storybookjs/knobs/panel' appears to work!
I've encountered the same issue and managed to find out that the panelId can at least be found in the addon's register source code step. For example, I wanted to open Readme tab for certain stories.
I ended up finding the id of the panel in registerWithPanelTitle.js, and then using it with the storiesOf API like this:
.addParameters({
options: { selectedPanel: 'REACT_STORYBOOK/readme/panel' },
})
For a11y, it can be found in constants.ts.
Although, I've searched for those in the distributed node_modules versions in my case.
P.S. If you want to reorder the panels for all of the stories globally, the list that the addons are imported in handles it.
I was using the #storybook/addons-essential in main.js and simply added #storybook/addon-controls to the front of the addons array option, like so:
module.exports = {
addons: ['#storybook/addon-controls', '#storybook/addon-essentials'],
...
}
This puts the controls tab first, which is auto-selected.

Disable Jupyter Notebook automatic hyperlink

In my notebook, I print some data from scraped web pages. Some of these are hyperlinks without tags e.g. https://stackoverflow.com. Unfortunately, Notebook prints these out as an actual hyperlink (i.e. wraps it in tags) on the output page and shortens it. (So the final result in HTML looks like this: https://stacko....) The field is set to code, but this still happens. Is there a way to disable this behaviour?
Solution:
Enter the following text in the empty cell of your Jupyter notebook:
%%javascript
Jupyter.utils.autoLinkUrls = function (txt) {
return txt;
}
Explanation:
The ability to locate URLs in text output and convert them to hyperlinks appeared in IPython notebook (a Jupyter's predecessor) as a result of a merge request in Oct' 2012. Since then every piece of output is scanned for URLs and each found URL is replaced with an anchor <a href=.../>. There's no easy way to alter this behavior because function autoLinkUrls(...) doesn't provide any configuration parameters.
So, the only way to disable URL "autolinking" is to simply replace JavaScript function autoLinkUrls, which is exposed through global Jupyter object, and %%javascript magic command makes the job done.

Qt, use of "&" in tr

I'm going through the Qt5 Application tutorial. In the creation of actions, I keep seeing the usage of "&" like the follwoing:
newAct = new QAction(QIcon(":/images/new.png"), tr("&New"), this)
...
openAct = new QAction(QIcon(":/images/open.png"), tr("&Open..."), this);
...
enter code exitAct = new QAction(tr("E&xit"), this);
...
I can't seem to find why these "&" are used, the Qt documentations on tr uses these notations without explaining what they are for. Further, when I deleted the "&" symbol, the resulting application doesn't seem to change at all.
As far as tr is concerned & is just another character in the string and has no special meaning.
However it does have a special meaning to QAction: It sets the following character as the entry's shortcut within a menu, so when you have the "File" menu open, pressing the n key will activate the "New" entry, x will active "Exit" and so on. Depending on the OS, the character after the & may also be underlined (on Windows it's only underlined when you press the Alt key).
& is used to indicate a character that should be used for the Alt-key shortcut for a given menu item. Assuming these actions are being put into a menu named &File, pressing Alt-F, then N would activate the New action.
It's for shortcut (in many cases it's Alt and the character).
If you code doesn't have & it will be like this:
now if you have & before the key you want to use for shortcut, it will look like this:
Here I assume you already have these actions added to a menu as in the pictures above, and you also have & for the menu name, something like this:
fileMenu = menuBar()->addMenu("&File");

How can you extend the default behavior of Tridion.Cme.Commands.Open.prototype._execute()?

I have written a GUI extension which adds an additional tab to many of the Item views in the SDL Tridion CME (e.g. Component, Page and Schema etc.). I have also written some JavaScript which loads that tab directly if when the view is loaded with a tab name is specified in the URL.
The result is that if a page is loaded with the tab name added as follows:
http://localhost/WebUI/item.aspx?tcm=64#id=tcm:1-48-64&tab=InfoTab
Rather than the default of
http://localhost/WebUI/item.aspx?tcm=64#id=tcm:1-48-64
The Info Tab will be loaded on top, instead of the General Tab. This is performed with the following code snippet and works very well:
$evt.addEventHandler($display, "start", onDisplayStarted);
// This callback is called when any view has finished loading
function onDisplayStarted() {
$evt.removeEventHandler($display, "start", onDisplayStarted);
var tabname = $url.getHashParam("tab");
if (tabname != '') {
var tabControl = $controls.getControl($("#MasterTabControl"), "Tridion.Controls.TabControl");
tabControl.selectItem(tabname);
}
}
Now I would like to make a context menu item to open items and link to the tabs using my new functionality. My first thought was to construct the Item URL myself and simply open a new window in my execute method. So I looked at the default functionality in the standard Open.prototype_execute() functionality of the GUI. This is stored in the navigation.js file of the CME, and is performed by the Tridion.Cme.Commands.Open.prototype._execute method. The code is a lot more complicated than I had anticipated as it deals with shared items, and permissions etc.
Rather than just copying all of this code to my own function, I was wondering if there is a way to elegantly extend the existing Open.prototype_execute() function and append my “&tab=MyTab” to the $cme.Popups.OPEN_ITEM_OPTIONS.URL constant for my own functions.
Any advice would be greatly appreciated.
At the end the Open command uses $config.getEditorUrl(item_type) to get the url for the item view (item_type - $const.ItemType.COMPONENT, etc). There are no extension points for this part of the functionality, but you could always try to overwrite it on your own risk.

Resources