在元数据显示中配置 Jupyter 日期格式,例如最后检查点日期

configure Jupyter date format in metadata display, e.g. last checkpoint date

在我的笔记本顶部,我目前显示“最后检查点:2022 年 1 月 25 日(自动保存)”。我想要 civilised readable 不同的格式。但是我找不到更改此选项的选项。

模板配置文件(使用 jupyter notebook --generate-config 生成)中唯一提及 date 的是:

## The date format used by logging formatters for %(asctime)s
#  Default: '%Y-%m-%d %H:%M:%S'
# c.Application.log_datefmt = '%Y-%m-%d %H:%M:%S'

## The date format used by logging formatters for %(asctime)s
#  See also: Application.log_datefmt
# c.JupyterApp.log_datefmt = '%Y-%m-%d %H:%M:%S'

## The date format used by logging formatters for %(asctime)s
#  See also: Application.log_datefmt
# c.NotebookApp.log_datefmt = '%Y-%m-%d %H:%M:%S'

所有这些似乎都指的是日志记录而不是显示的日期。配置文件没有提及语言环境或语言。 None 我试过的环境变量有任何影响(包括 LANGLC_ALLLC_TIME 等),尽管它们确实会影响 python的 locale.getdefaultlocale().

这是 jupyter --version 的输出值:

jupyter  --version
Selected Jupyter core packages...
IPython          : 8.0.1
ipykernel        : 6.7.0
ipywidgets       : 7.6.5
jupyter_client   : 7.1.1
jupyter_core     : 4.9.1
jupyter_server   : not installed
jupyterlab       : not installed
nbclient         : 0.5.10
nbconvert        : 6.4.0
nbformat         : 5.1.3
notebook         : 6.4.6
qtconsole        : 5.2.2
traitlets        : 5.1.1
  • 现在 运行 jupyter notebook 我确实看到 YYYY-DD-MM 格式的日期,例如 Last Checkpoint: 01/13/2022 (autosaved)
  • 您可以通过将浏览器设置更改为“英语(英国)”而不是美式英语来更新此设置(请参阅说明 here

一些提示 - 我是怎么想出来的?

  1. 转到 GitHub for jupyter notebook - https://github.com/jupyter/notebook
  2. 在存储库中搜索“最后一个检查点:”
  3. 我们可以找到相关代码here
  4. 我们可以看到它使用 var chkd = moment(this._checkpoint_date); ...; chkd.calendar();,这意味着它在浏览器中使用 MomentJS library
  5. 进行了格式化
  6. 从文档中我们可以看到显示格式是使用 moment.locale(...) 配置的 - 因此我们可以在代码中搜索此片段。我们可以看到 here - moment.locale(_uiLang())
  7. 查看 _uiLang 我们可以看到它只是浏览器语言首选项的包装器 (source) - _uiLang = function() { return navigator.language.toLowerCase(); }
  8. 我检查了 MomentJS 的本地化文件,我们可以看到英式英语确实指定了“正确”的日期格式 (source)