How do I change edit mode shortcuts in Jupyter notebooks? - jupyter-notebook

In a Jupyter Notebook I can click on Help -> Edit Keyboard Shortcuts to change Command Mode shortcuts. However, I don't see how I can change Edit Mode shortcuts. How can I do this?

You are correct that Help -> Edit Keyboard Shortcuts will only change Command Mode shortcuts (at least as of Nov 2020). In fact, at the bottom of the edit keyboard shortcuts modal for Jupyter Notebook it states "Changing the keybindings of edit mode is not currently available."
So to get to the "edit" shortcuts I had to go into the notebook config. Documentation here: https://jupyter-notebook.readthedocs.io/en/stable/extending/keymaps.html
For me, the notebook config was located here "~/.jupyter/nbconfig/notebook.json". Once there, you can bind (set new shortcuts) or unbind (remove existing shortcuts).
Here is the structure of my notebook.json file:
{
"Cell": {
"cm_config": {
"lineNumbers": false
}
},
"keys": {
"command": {
"bind": {
"ctrl-enter": "jupyter-notebook:run-cell"
}
},
"edit": {
"bind": {
"ctrl-enter": "jupyter-notebook:run-cell"
}
}
}
}
Notice how I want to run cells with Ctrl-enter rather than Cmd-enter, so I am binding Ctrl-enter to run cells in command mode and edit mode. I'm on a Mac but have previously gotten used to Ctrl-enter to run cells, so I wanted to change things back.
Once you have modified your notebook.json file, restart Jupyter Notebook and your shortcuts should work!
If you are wondering where to find the code syntax name for each action, there is a command palette (tiny keyboard at the top middle right of Jupyter Notebook). After you click into it, hover over the command mode key on the right side and it will give you a little tooltip with the code syntax name.

Related

Jupyter Lab Shortcut

How do I add a custom shortcut from the kernelmenu that uses the restart and run up to selected cell button?
I looked at another shortcut in the menu from the JSON Settings Editor and found one also using the kernelmenu. I added this shortcut below using that as my “template”.
This did not work.
Is it the selector, data-jp-kernel-user? Does it need to be data-jp-kernel something?
{
“args”: {},
“command”: “kernelmenu:restart-and-run-up-to-selected-cell”,
“keys”: [
“Ctrl Shift J”
],
“selector”: “[data-jp-kernel-user]:focus”
}

Jupyter notebook slideshow, how can the appearance of a new fragment trigger a property change on a previous fragment?

Using Jupyter Notebook to prepare a slideshow,
I would like when a new fragment becomes visible, to trigger a property change (for example text color or font-size) on a previously shown fragment.
If it requires addEventListener could you provide a working example for a jupyter notebook?
This is what I found:
EDIT: How do (can) I use a custom.js file under Jupyter notebook?
Tag the target element and the trigger element with an id:
code:
<p id="target"> This text will become red and then green.</p>
<p class="fragment" id="trigger"> When this appears, the previous fragment will change color. When it disappears it will change color again</p>
screenshot:
We need to add an event listener that listens to the 'fragmentshown' event.
The eventlistener can not be in the Jupyter cell since cell contents are loaded before Reveal is initialized.
Next convert to slides (for the notebook called test.ipynb):
jupyter-nbconvert test.ipynb --to slides --post serve
Next open the slides file test.slides.html with a text editor. At the bottom of the file we find the require() function that contains the Reveal initialization. Inside there (after the initialization) append the following commands:
code:
Reveal.addEventListener('fragmentshown', function( event ) {
if(event.fragment.id === 'trigger') { document.getElementById("target").style.color = "red";}
} );
Reveal.addEventListener('fragmenthidden', function( event ) {
if(event.fragment.id === 'trigger') { document.getElementById("target").style.color = "green";}
} );

Add proper selector for keybinding event in Atom editor

I develop a package, which allows user to use Home button to toggle cursor position in soft-wrapped lines, as it is in Komodo Edit editor.
In my toggle() function I try to get active text editor from Atom Workspace using getActiveTextEditor function and then I do my logic.
atom.workspace.getActiveTextEditor()
I bind a Home key with a selector atom-text-editor.editor, and it mainly works fine until triggered inside Search&Replace pane or Command Palette.
"atom-text-editor.editor": {
"home": "toggle-home:toggle"
}
In this case, getActiveTextEditor returns always a currently edited file's text editor. It leads to a situation, when a cursor is moved inside an edited file's pane, but not inside focused field. When I use a Home inside a text field in Settings pane, Atom throws an exception, as it can't find any active text editor.
I did a research through Atom Docs, Atom API, even community packages, but all I found, was adding 'mini' to my event selector, to narrow event scope a bit.
"atom-text-editor.editor:not([mini])": {
"home": "toggle-home:toggle"
}
Nevertheless it still causes exception or misbehaviour in text fields without 'mini' (ie. textarea of Git/GitHub package).
What I want to achieve is:
1) to find a proper keybinding selector, that would fire only inside currently edited file pane;
OR
2) to find a method to get focused instance (Search&Replace, Command Palette or any other field) for further processing.
Solution
As DamnedScholar mentioned here:
the selector atom-workspace-axis.vertical atom-pane-container atom-text-editor:not([mini]) will do it.
Already tested, works fine.

Atom - disable single key binding

How can I disable a single key binding such as ctrl-alt-= in Atom?
I'm using the QWERTZ keyboard layout, so that I execute pane:increase-size when I type a '\'.
Open settings with File > Settings
Click Keybindings
Filter the list by typing ctrl-alt-= in the search box.
Click the clipboard icon next to the shortcut. This will copy the shortcut definition to your clipboard.
Click the hyperlinked text your keymap file.
Paste in the contents of the clipboard.
Replace 'pane:increase-size' with the value 'unset!'
Now ctrl-alt-= will not do anything.
EDIT: 'unset!' was previously null, see this atom discussion for details.
EDIT2: To fix issues with many non-QWERTY keyboard layouts, check out the keyboard-localization package for Atom.
Fix from #Monkey worked, here's the code from my keymap.cson for fixing backslash.
'atom-workspace atom-text-editor:not([mini])':
'ctrl-alt-[': 'unset!'
EDIT:
I'm using the QWERTZ keyboard layout.

read-only cells in ipython/jupyter notebook

Is there a way to mark a cell in the ipython/jupyter notebook readonly using the json format in the ipynb file? (E.g., a cell attribute "readonly":false or some such.) If not, is there a jquery hack to find suppress the double click event in the cell?
#Richard Ackon's answer requires adjustments for JupyterLab:
Open the Property Inspector.
Focus the cell you want to lock.
Add the following lines to the Cell Metadata:
{
"trusted": true,
"editable": false,
"deletable": false
}
Click on the tick to save the metadata... Tadah!, your cell can't be modified or deleted.
The Property Inspector comes built-in since JupyterLab 2.0 (note it was moved to the right sidebar by default in JupyterLab 3.0). For older JupyterLab versions you would need to modify the notebook file manually.
Unfortunately, the outputs can still be cleared by intentionally selecting that option in the menu bar (Edit > Clear Ouputs). Of course that can only happen if you DO WANT to clear the outputs and not just update them by running the cell.
Source
Yes, Use the steps below:
Select view on the menubar
Point to Cell Toolbar and select Edit Metadata
An "Edit Metadata" button will appear at the top-right corner of the cell.
Click on that button and edit the json that pops up. Set the editable key to true or false to get the desired effect.
The JSON will look something like this:
{
"deletable": false,
"editable": false,
"scrolled": true,
"trusted": true
}
There is an extension for IPython that is supposed to that:
Read Only Cell extension.
Getting it to work is something else, but it is there.

Resources