Choose SQL as default cell magics for Jupyter Notebook

浪子不回头ぞ 提交于 2019-12-20 10:37:29

问题


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?

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.


回答1:


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).

So, in ~/.jupyter/nbconfig/notebook.json paste in the following code:

{
    "CodeCell": {
        "cm_config": {
            "value": "%%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.



来源:https://stackoverflow.com/questions/34415452/choose-sql-as-default-cell-magics-for-jupyter-notebook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!