- IPython Interactive Computing and Visualization Cookbook(Second Edition)
- Cyrille Rossant
- 542字
- 2021-07-02 16:23:33
Configuring the Jupyter Notebook
Many aspects of the Jupyter Notebook can be configured. We covered the configuration of the IPython kernel in the Mastering IPython's configuration system recipe in Chapter 1, A Tour of Interactive Computing with Jupyter and IPython. In this recipe, we show how to configure the Jupyter application and the Jupyter Notebook frontend.
How to do it...
- Let's check whether the Jupyter Notebook configuration file already exists:
>>> %ls ~/.jupyter/jupyter_notebook_config.py ~/.jupyter/jupyter_notebook_config.py
If it does not, type
!jupyter notebook --generate-config -y
in the notebook. If the file already exists, this command will delete its contents and replace it with the default file. - We can inspect the contents of the file with the following command:
>>> %cat ~/.jupyter/jupyter_notebook_config.py # Configuration file for jupyter-notebook. #------------------------------------------------------- # Application(SingletonConfigurable) configuration #------------------------------------------------------- ## This is an application. ## The date format used by logging formatters #c.Application.log_datefmt = '%Y-%m-%d %H:%M:%S' [...] #------------------------------------------------------- # JupyterApp(Application) configuration #------------------------------------------------------- ## Base class for Jupyter applications ## Answer yes to any prompts. #c.JupyterApp.answer_yes = False ## Full path of a config file. #c.JupyterApp.config_file = '' ...
For example, to change the default name of a new notebook, we can add the following line to this file:
c.ContentsManager.untitled_notebook = 'MyNotebook'
- We now turn to the configuration of the Jupyter Notebook frontend. The configuration files are in the following folder:
>>> %ls ~/.jupyter/nbconfig/ notebook.json tree.json
- Let's inspect the contents of the notebook configuration file (in JSON):
>>> %cat ~/.jupyter/nbconfig/notebook.json { "Cell": { "cm_config": { "lineNumbers": false } }, "Notebook": { "Header": false, "Toolbar": false } }
- There are several ways to configure the Notebook frontend. We can directly edit this JSON file and reload the notebook. We can also do it in the client using JavaScript. For example, here is how we can disable the auto-closing brackets option in code cells:
>>> %%javascript var cell = Jupyter.notebook.get_selected_cell(); var config = cell.config; var patch = { CodeCell:{ cm_config: {autoCloseBrackets: false} } } config.update(patch)
If we reload the notebook, this option will be permanently turned off.
Auto-close brackets
- In fact, this command automatically updates the JSON file:
>>> %cat ~/.jupyter/nbconfig/notebook.json { "Cell": { "cm_config": { "lineNumbers": false } }, "Notebook": { "Header": false, "Toolbar": false }, "CodeCell": { "cm_config": { "autoCloseBrackets": false } } }
- We can also get and change the frontend options from Python:
>>> from notebook.services.config import ConfigManager c = ConfigManager() c.get('notebook').get('CodeCell') {'cm_config': {'autoCloseBrackets': False}} >>> c.update('notebook', {"CodeCell": {"cm_config": {"autoCloseBrackets": True}}}) {'Cell': {'cm_config': {'lineNumbers': False}}, 'CodeCell': {'cm_config': {'autoCloseBrackets': True}}, 'Notebook': {'Header': False, 'Toolbar': False}} >>> %cat ~/.jupyter/nbconfig/notebook.json { "Cell": { "cm_config": { "lineNumbers": false } }, "Notebook": { "Header": false, "Toolbar": false }, "CodeCell": { "cm_config": { "autoCloseBrackets": true } } }
There's more...
The code cell editor used in the Notebook is handled by the CodeMirror JavaScript library. All options are detailed in the CodeMirror documentation.
Here are a few references:
- Notebook configuration at http://jupyter-notebook.readthedocs.io/en/stable/config.html
- Notebook frontend configuration at https://jupyter-notebook.readthedocs.io/en/stable/frontend_conf
- CodeMirror options at https://codemirror.net/doc/manual.html#option_indentUnit
See also
- The Mastering IPython's configuration system recipe in Chapter 1, A Tour of Interactive Computing with Jupyter and IPython
- 大規模數據分析和建模:基于Spark與R
- LibGDX Game Development Essentials
- 大數據技術基礎
- 云計算環境下的信息資源集成與服務
- DB29forLinux,UNIX,Windows數據庫管理認證指南
- Architects of Intelligence
- Learning JavaScriptMVC
- SQL查詢:從入門到實踐(第4版)
- Dependency Injection with AngularJS
- Remote Usability Testing
- Flutter Projects
- 企業級容器云架構開發指南
- SIEMENS數控技術應用工程師:SINUMERIK 840D-810D數控系統功能應用與維修調整教程
- 數據庫查詢優化器的藝術:原理解析與SQL性能優化
- 改進的群智能算法及其應用