IPython更改整个会话的输入单元语法突出显示逻辑 [英] IPython change input cell syntax highlighting logic for entire session

查看:118
本文介绍了IPython更改整个会话的输入单元语法突出显示逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以在IPython中使用扩展名或 display 帮助程序,以便在输出单元格上突出显示您想要的任何语法。

You can use extensions or display helpers in IPython to make whatever syntax highlighting you'd like on output cells.

对于某些特殊的单元格魔法,例如 %% javascript ,您还可以看到输入单元格本身是使用该语言的自然语法高亮显示的。

For some special cell magics, like %%javascript you can also see the input cell itself is rendered with that language's natural syntax highlighting.

如何使用一些选定的非Python语法高亮显示每个输入单元格(无论单元格上是否使用任何魔法,无论单元格是否包含Python代码,其他语言) 。

How can you cause every input cell to be displayed with some chosen, non-Python syntax highlighting (regardless of any magics used on a cell, regardless of whether the cell embodies Python code, some other language).

在我的情况下,我正在使用专有语言的定制单元魔法。 %% javascript 语法突出显示适用于此语言,但如果我有自己的 %% proprietarylang 魔术函数,我显然无法使用 %% javascript 来帮助我了解单元格的显示方式。

In my case I am working with a custom-made cell magic for a proprietary language. The %%javascript syntax highlighting works well for this language, but if I have my own %%proprietarylang magic function, I obviously can't use %%javascript to help me with how the cell is displayed.

是否有设置我可以在启动笔记本时启用,或者 ipython 对象本身的某些属性,可以在我的魔术函数内以编程方式设置,这将导致相同的显示逻辑好像它是 %% javascript

Is there a setting I can enable when I launch the notebook, or some property of the ipython object itself that can be programmatically set inside of my magic function, which will cause the same display logic to happen as if it was %%javascript.

我知道通用的动态语法突出显示目前笔记本不支持。我特别询问如何使用预先存在的语法高亮效果,例如 %% javascript

I know that general-purpose on-the-fly syntax highlighting is not supported by the notebook currently. I'm asking specifically about how to make use of pre-existing syntax highlighting effects, such as that of %%javascript.

我见过某些 文档,指的是 IPython .config.cell_magic_highlight 但这似乎不存在了。是否有标准替代品?

I've seen some documentation referring to IPython.config.cell_magic_highlight but this does not seem to exist anymore. Is there a standard replacement for it?

推荐答案

替换 IPython.config.cell_magic_highlight ,你可以使用类似的东西

To replace IPython.config.cell_magic_highlight, you can use something like

import IPython
js = "IPython.CodeCell.config_defaults.highlight_modes['magic_fortran'] = {'reg':[/^%%fortran/]};"
IPython.core.display.display_javascript(js, raw=True)

so so哪个以 %% fortran开头将以FORTRAN语法突出显示。 (但是,如果只做这个,它们仍将被评估为python。)

so cells which begin with %%fortran will be syntax-highlighted like FORTRAN. (However, they will still be evaluated as python if you do only this.)

这篇关于IPython更改整个会话的输入单元语法突出显示逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆