close

本人為Python初學者,如果有寫錯的地方,還請各位大大留言指正

今天聽說了一個關於Jupyter Notebook還滿好用的功能,那就是multiple cursors,白話的說,就是在同一個cell裡 擁有多個游標。

你可能想知道那這有什麼用呢?  它可以幫助你在同一個cell中,搜尋某個字且同時修改。假設你設定了一個變數名稱,且用到它非常多次,但你突然想改變它的名稱,這時候如果沒有multiple cursors的幫忙,你就需要一個一個慢慢找,慢慢修改,可能還得找到目睭灰灰還沒有改完整,一執行又出error,而有了這功能之後,就可以自動幫你把一樣名稱的選取起來,一次性的修改。

以下教學正式開始

而這功能我是從這裡看到的  http://www.perfectlyrandom.org/2016/03/19/sublime-text-style-multiple-cursors-in-jupyter-notebook/
如果你遇到了一些問題,可以看看這裡面有沒有解答  https://github.com/jupyter/notebook/issues/278

給不想點進去看,或者不想看一堆英文的人
第一步 :
得先找到 custom.js 存在你電腦中的位置,MacOS 和 Linux 會在這個位置 ~/.jupyter/custom/custom.js , 如果你是第一次使用這檔案,它可能不存在,你可以使用以下這段程式碼來找到它應該在的位置

# Print the location of Jupyter's config directory
from jupyter_core.paths import jupyter_config_dir
jupyter_dir = jupyter_config_dir()
print(jupyter_dir)

# Print the location of custom.js
import os.path
custom_js_path = os.path.join(jupyter_dir, 'custom', 'custom.js')
print(custom_js_path)

# Print the contents of custom.js, if it exists.
if os.path.isfile(custom_js_path):
    with open(custom_js_path) as f:
        print(f.read())
else:
    print("You don't have a custom.js file")

第1-2步 : 

python_multi cursor_1.png


如果檔案不存在你就會看到  You don't have a custom.js file  這麼一段話;如果檔案存在,就會把檔案內容印出來。
這時候只要把 C:\Users\你的電腦名稱\.jupyter  這段丟到資料夾路徑上就行了,就可以直達目的地
然後在 .jupyter 這資料夾底下在新增一個名為custom的資料夾
接者在 custom這資料夾中新增一個記事本 取名為custom.js , 在裡面放上這串 alert("hello world from custom.js")
然後重啟你的Jupyter Notebook(如果你是用terminal開的話,可以按ctrl+c)
如果重啟有看到訊息,那恭喜你成功了一半!

 

python_multi cursor_2.png

 

第二步 : 
再次打開cutsom.js這檔案,把剛剛輸入的alert("hello world from custom.js") 使用兩個斜線 // 註解起來,看起來會像這樣 : //alert("hello world from custom.js")
在下面貼上這段文字,然後再一次重啟Jupyter Notebook

require(["codemirror/keymap/sublime", "notebook/js/cell"], function(sublime_keymap, cell) {
    cell.Cell.options_default.cm_config.keyMap = 'sublime';
});

第三步:
是時候嘗試是否成功了!(如果Control+D按下後是刪除該行,那請看第3-2步)
在你的cell內隨意打些字句,反白某個單字後按下 Control+D (on Linux or Windows) 或是 Command+D (on MacOS)。如果有反白出下一個一樣的字的話,恭喜你成功了!注意 反白之後如果按了滑鼠左鍵,選取就會取消了喔。

第3-2步:(給按下Control+D是刪除該行的人)
將第二步的文字改成下面這段文字,
然後再一次重啟Jupyter Notebook,試試是否成功

require(["codemirror/keymap/sublime", "notebook/js/cell", "base/js/namespace"],
function(sublime_keymap, cell, IPython) {
// setTimeout(function(){ // uncomment line to fake race-condition
cell.Cell.options_default.cm_config.keyMap = 'sublime';
var cells = IPython.notebook.get_cells();
for(var c=0; c< cells.length ; c++){
cells[c].code_mirror.setOption('keyMap', 'sublime');
}

// }, 1000)// uncomment line to fake race condition 
}
);

如果還是不行的話,就到一開始提供的兩個網址裡面找找,有沒有人跟你有一樣的問題囉~

 

arrow
arrow
    文章標籤
    Python cursors Jupyter
    全站熱搜

    無心快意 發表在 痞客邦 留言(1) 人氣()