官术网_书友最值得收藏!

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

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

    Note

    A Jupyter configuration file may exist in Python or in JSON (the same location and filename, but different file extension). JSON files have a higher priority. Unlike Python files, JSON files may be edited programmatically.

  2. 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'
  3. 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
  4. 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
      }
    }
  5. 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.

    How to do it...

    Auto-close brackets

  6. 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
        }
      }
    }
  7. 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:

See also

  • The Mastering IPython's configuration system recipe in Chapter 1, A Tour of Interactive Computing with Jupyter and IPython
主站蜘蛛池模板: 任丘市| 自贡市| 云南省| 丹巴县| 山阴县| 都昌县| 保山市| 徐闻县| 吉隆县| 繁峙县| 肥东县| 襄樊市| 广汉市| 化州市| 大埔县| 荣成市| 黄冈市| 鹤庆县| 纳雍县| 北京市| 太原市| 哈尔滨市| 江西省| 黄山市| 商水县| 阜平县| 太湖县| 焦作市| 佛山市| 黎川县| 高邮市| 高尔夫| 开远市| 桂阳县| 原平市| 临高县| 遵化市| 克东县| 剑川县| 高平市| 武陟县|