Map keyboard shortcut to code snippet in Jupyter Lab

时间:2018-03-22 23:28:28

标签: jupyter-lab

Does anyone know if there a way to bind a code snippet to a keyboard shortcut in Jupyter Lab? For example in R Studio you can use Ctrl+Shift+M to write the pipe operator (%>%) quickly and I got used to that functionality so I would like to replicate it.

I looked at the Keyboard Shortcut menu under Settings but I'm not sure how to use the JSON schema to write such an Override (if it is even possible from there), and the documentation wasn't very clear.

2 个答案:

答案 0 :(得分:3)

好吧,我对Jupyter Lab没有很好的答案,但是对于旧的Jupyter笔记本,只需运行下面的单元格即可提供所需的信息:

IRdisplay::display_javascript("Jupyter.keyboard_manager.edit_shortcuts.add_shortcut('Ctrl-Shift-M', {
    help : 'add pipe symbol',
    help_index : 'zz',
    handler : function (event) {
        var target = Jupyter.notebook.get_selected_cell()
        var cursor = target.code_mirror.getCursor()
        var before = target.get_pre_cursor()
        var after = target.get_post_cursor()
        target.set_text(before + ' %>% ' + after)
        cursor.ch += 5
        target.code_mirror.setCursor(cursor)
        return false;
    }}
);")

答案 1 :(得分:0)

在 JupyterLab 2.1+ 中,您可以使用以下内容为 R 配置键盘快捷键:

from kafka import KafkaConsumer
# Import sys module
import sys

# Import json module to serialize data
import json

# Initialize consumer variable and set property for JSON decode
consumer = KafkaConsumer ('JSONtopic',bootstrap_servers = ['localhost:9092'],
value_deserializer=lambda m: json.loads(m.decode('utf-8')))
for message in consumer:
 print("Consumer records:\n")
 print(message)
 print("\nReading from JSON data\n")
 print("Name:",message[6]['name'])
 print("Email:",message[6]['email'])
 # Terminate the script
 sys.exit()

{ "shortcuts": [ { "command": "apputils:run-first-enabled", "selector": "body", "keys": ["Alt -"], "args": { "commands": [ "console:replace-selection", "fileeditor:replace-selection", "notebook:replace-selection", ], "args": {"text": "<- "} } }, { "command": "apputils:run-first-enabled", "selector": "body", "keys": ["Accel Shift M"], "args": { "commands": [ "console:replace-selection", "fileeditor:replace-selection", "notebook:replace-selection", ], "args": {"text": ">%> "} } } ] } 在 Linux 和 Windows 上表示 Ctrl,在 Mac 上表示 Command

只需转到 Accel -> Advanced Settings Editor,粘贴并保存:

illustration

JupyterLab 存储库中的相关问题:#4519#7908#10114

或者,对于更高级的功能,例如特定于内核的快捷方式或自动填充,您可以使用 jupyterext-text-shortcuts 扩展名。