选择SQL作为Jupyter Notebook的默认单元魔术 [英] Choose SQL as default cell magics for Jupyter Notebook
问题描述
我正在为数据库课程编写Jupyter笔记本;大多数单元将包含SQL代码,对此我使用 IPython SQL魔术.目前,我必须在每个单元格前添加%%sql
.有没有一种方法可以通过将其设置为默认值来避免在每个单元格之前键入%%sql
?
I'm writing a Jupyter notebook for a database course; most cells will contain SQL code, for which I use the IPython SQL magic. Currently I have to prepend each cell with %%sql
. Is there a way to avoid typing %%sql
before each cell, by making it the default?
我在找到了一个相关的问题 如何设置默认值每个ipython笔记本电脑的电池都有魔力?.但是,我找不到Jupyter的SQL内核.
I found a related question at How do I set up default cell magics for every ipython notebook cell?. However, I couldn't find a SQL kernel for Jupyter.
推荐答案
Jupyter docs 告诉您如何编辑CodeMirror设置. CodeMirror具有名为value
的选项,该选项确定CodeMirror单元的默认值(文档链接).
The Jupyter docs tell you how you can edit the CodeMirror settings. CodeMirror has an option called value
that determines the default value of a CodeMirror cell (doc link).
因此,在~/.jupyter/nbconfig/notebook.json
中粘贴以下代码:
So, in ~/.jupyter/nbconfig/notebook.json
paste in the following code:
{
"CodeCell": {
"cm_config": {
"value": "%%sql"
}
}
}
,现在每个新单元格中都将包含%%sql
.显然,此解决方案适用于任何单元格魔术,甚至适用于您可能希望代码单元具有的任何默认值.
and now every new cell will have %%sql
in it. Obviously this solution applies to any cell magic or indeed any default value you may want your code cells to have.
这篇关于选择SQL作为Jupyter Notebook的默认单元魔术的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!