让 Pygments 为我的细胞结果工作
Getting Pygments to work for my cell results
我使用 Jupyter Notebooks 来实施和记录适用于我们的生产数据库的更改。我在 Python 中编程并向服务器提交典型的 SQL 语句以获取结果并进行更改。
我喜欢打印 sql 作为单元格输出的一部分,以获取详细的更改日志。我希望 pygments 帮助为输出着色,以便 SQL 看起来尽可能漂亮。
似乎 pygments 使用 css 但我不知道如何将其包含在内
from pygments import highlight
from pygments.lexers import SqlLexer
from pygments.formatters import HtmlFormatter
from IPython.display import display, HTML
sql_string = '''
SELECT id, name
FROM users
WHERE name between 'B' AND 'P'
LIMIT 50
'''
display(HTML(data=highlight(sql_string, SqlLexer(), HtmlFormatter())))
display(highlight(sql_string, SqlLexer(), HtmlFormatter()))
display(HtmlFormatter().get_style_defs('.highlight'))
输出
SELECT id, name
FROM users
WHERE name between 'B' AND 'P'
LIMIT 50
u'<div class="highlight"><pre><span></span><span class="k">SELECT</span> <span class="n">id</span><span class="p">,</span> <span class="n">name</span>\n <span class="k">FROM</span> <span class="n">users</span>\n <span class="k">WHERE</span> <span class="n">name</span> <span class="k">between</span> <span class="s1">'B'</span> <span class="k">AND</span> <span class="s1">'P'</span>\n<span class="k">LIMIT</span> <span class="mi">50</span>\n</pre></div>\n'
'.highlight .hll { background-color: #ffffcc }\n.highlight { background: #f8f8f8; }\n.highlight .c { color: #408080; font-style: italic } /* Comment */\n.highlight .err { border: 1px solid #FF0000 } /* Error */\n.highlight .k { color: #008000; font-weight: bold } /* Keyword */\n.highlight .o { color: #666666 } /* Operator */\n.highlight .ch { color: #408080; font-style: italic } /* Comment.Hashbang */\n.highlight .cm { color: #408080; font-style: italic } /* Comment.Multiline */\n.highlight .cp { color: #BC7A00 } /* Comment.Preproc */\n.highlight .cpf { color: #408080; font-style: italic } /* Comment.PreprocFile */\n.highlight .c1 { color: #408080; font-style: italic } /* Comment.Single */\n.highlight .cs { color: #408080; font-style: italic } /* Comment.Special */\n.highlight .gd { color: #A00000 } /* Generic.Deleted */\n.highlight .ge { font-style: italic } /* Generic.Emph */\n.highlight .gr { color: #FF0000 } /* Generic.Error */\n.highlight .gh { color: #000080; font-weight: bold } /* Generic.Heading */\n.highlight .gi { color: #00A000 } /* Generic.Inserted */\n.highlight .go { color: #888888 } /* Generic.Output */\n.highlight .gp { color: #000080; font-weight: bold } /* Generic.Prompt */\n.highlight .gs { font-weight: bold } /* Generic.Strong */\n.highlight .gu { color: #800080; font-weight: bold } /* Generic.Subheading */\n.highlight .gt { color: #0044DD } /* Generic.Traceback */\n.highlight .kc { color: #008000; font-weight: bold } /* Keyword.Constant */\n.highlight .kd { color: #008000; font-weight: bold } /* Keyword.Declaration */\n.highlight .kn { color: #008000; font-weight: bold } /* Keyword.Namespace */\n.highlight .kp { color: #008000 } /* Keyword.Pseudo */\n.highlight .kr { color: #008000; font-weight: bold } /* Keyword.Reserved */\n.highlight .kt { color: #B00040 } /* Keyword.Type */\n.highlight .m { color: #666666 } /* Literal.Number */\n.highlight .s { color: #BA2121 } /* Literal.String */\n.highlight .na { color: #7D9029 } /* Name.Attribute */\n.highlight .nb { color: #008000 } /* Name.Builtin */\n.highlight .nc { color: #0000FF; font-weight: bold } /* Name.Class */\n.highlight .no { color: #880000 } /* Name.Constant */\n.highlight .nd { color: #AA22FF } /* Name.Decorator */\n.highlight .ni { color: #999999; font-weight: bold } /* Name.Entity */\n.highlight .ne { color: #D2413A; font-weight: bold } /* Name.Exception */\n.highlight .nf { color: #0000FF } /* Name.Function */\n.highlight .nl { color: #A0A000 } /* Name.Label */\n.highlight .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */\n.highlight .nt { color: #008000; font-weight: bold } /* Name.Tag */\n.highlight .nv { color: #19177C } /* Name.Variable */\n.highlight .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */\n.highlight .w { color: #bbbbbb } /* Text.Whitespace */\n.highlight .mb { color: #666666 } /* Literal.Number.Bin */\n.highlight .mf { color: #666666 } /* Literal.Number.Float */\n.highlight .mh { color: #666666 } /* Literal.Number.Hex */\n.highlight .mi { color: #666666 } /* Literal.Number.Integer */\n.highlight .mo { color: #666666 } /* Literal.Number.Oct */\n.highlight .sb { color: #BA2121 } /* Literal.String.Backtick */\n.highlight .sc { color: #BA2121 } /* Literal.String.Char */\n.highlight .sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */\n.highlight .s2 { color: #BA2121 } /* Literal.String.Double */\n.highlight .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */\n.highlight .sh { color: #BA2121 } /* Literal.String.Heredoc */\n.highlight .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */\n.highlight .sx { color: #008000 } /* Literal.String.Other */\n.highlight .sr { color: #BB6688 } /* Literal.String.Regex */\n.highlight .s1 { color: #BA2121 } /* Literal.String.Single */\n.highlight .ss { color: #19177C } /* Literal.String.Symbol */\n.highlight .bp { color: #008000 } /* Name.Builtin.Pseudo */\n.highlight .vc { color: #19177C } /* Name.Variable.Class */\n.highlight .vg { color: #19177C } /* Name.Variable.Global */\n.highlight .vi { color: #19177C } /* Name.Variable.Instance */\n.highlight .il { color: #666666 } /* Literal.Number.Integer.Long */'
如您所见,输出已用 class 很好地标记为格式,我可以获取样式的数据,有谁知道如何在笔记本中使用它吗?需要说明的是,问题是我想在单元格结果中看到颜色。
from pygments import highlight
from pygments.lexers import SqlLexer
from pygments.formatters import HtmlFormatter
from IPython.core.display import HTML
sql_string = '''
SELECT id, name
FROM users
WHERE name between 'B' AND 'P'
LIMIT 50
'''
display(HTML("""
<style>
{pygments_css}
</style>
""".format(pygments_css=HtmlFormatter().get_style_defs('.highlight'))))
display(HTML(data=highlight(sql_string, SqlLexer(), HtmlFormatter())))
支持在 Stack Exchange 上进行更深入的挖掘。在关于如何添加到 jupyter 的 css 的未接受问题中找到了东西。
如果您使用的是 Jupyter Notebook,您还可以使用 IPython.display.Code
from IPython.display import Code
Code('print("Hello World")', language='python')
它不会像在 Jupyter Lab 中预期的那样 运行,如 this GitHub issue 中所述,因此您需要 运行 那里的以下位:
from pygments.formatters import HtmlFormatter
from IPython.core.display import HTML
formatter = HtmlFormatter()
display(HTML(f'<style>{ formatter.get_style_defs(".highlight") }</style>'))
您可以直接在笔记本单元格中执行终端命令:
!pygmentize PATH/TO/YOUR/FILE
我使用 Jupyter Notebooks 来实施和记录适用于我们的生产数据库的更改。我在 Python 中编程并向服务器提交典型的 SQL 语句以获取结果并进行更改。
我喜欢打印 sql 作为单元格输出的一部分,以获取详细的更改日志。我希望 pygments 帮助为输出着色,以便 SQL 看起来尽可能漂亮。
似乎 pygments 使用 css 但我不知道如何将其包含在内
from pygments import highlight
from pygments.lexers import SqlLexer
from pygments.formatters import HtmlFormatter
from IPython.display import display, HTML
sql_string = '''
SELECT id, name
FROM users
WHERE name between 'B' AND 'P'
LIMIT 50
'''
display(HTML(data=highlight(sql_string, SqlLexer(), HtmlFormatter())))
display(highlight(sql_string, SqlLexer(), HtmlFormatter()))
display(HtmlFormatter().get_style_defs('.highlight'))
输出
SELECT id, name
FROM users
WHERE name between 'B' AND 'P'
LIMIT 50
u'<div class="highlight"><pre><span></span><span class="k">SELECT</span> <span class="n">id</span><span class="p">,</span> <span class="n">name</span>\n <span class="k">FROM</span> <span class="n">users</span>\n <span class="k">WHERE</span> <span class="n">name</span> <span class="k">between</span> <span class="s1">'B'</span> <span class="k">AND</span> <span class="s1">'P'</span>\n<span class="k">LIMIT</span> <span class="mi">50</span>\n</pre></div>\n'
'.highlight .hll { background-color: #ffffcc }\n.highlight { background: #f8f8f8; }\n.highlight .c { color: #408080; font-style: italic } /* Comment */\n.highlight .err { border: 1px solid #FF0000 } /* Error */\n.highlight .k { color: #008000; font-weight: bold } /* Keyword */\n.highlight .o { color: #666666 } /* Operator */\n.highlight .ch { color: #408080; font-style: italic } /* Comment.Hashbang */\n.highlight .cm { color: #408080; font-style: italic } /* Comment.Multiline */\n.highlight .cp { color: #BC7A00 } /* Comment.Preproc */\n.highlight .cpf { color: #408080; font-style: italic } /* Comment.PreprocFile */\n.highlight .c1 { color: #408080; font-style: italic } /* Comment.Single */\n.highlight .cs { color: #408080; font-style: italic } /* Comment.Special */\n.highlight .gd { color: #A00000 } /* Generic.Deleted */\n.highlight .ge { font-style: italic } /* Generic.Emph */\n.highlight .gr { color: #FF0000 } /* Generic.Error */\n.highlight .gh { color: #000080; font-weight: bold } /* Generic.Heading */\n.highlight .gi { color: #00A000 } /* Generic.Inserted */\n.highlight .go { color: #888888 } /* Generic.Output */\n.highlight .gp { color: #000080; font-weight: bold } /* Generic.Prompt */\n.highlight .gs { font-weight: bold } /* Generic.Strong */\n.highlight .gu { color: #800080; font-weight: bold } /* Generic.Subheading */\n.highlight .gt { color: #0044DD } /* Generic.Traceback */\n.highlight .kc { color: #008000; font-weight: bold } /* Keyword.Constant */\n.highlight .kd { color: #008000; font-weight: bold } /* Keyword.Declaration */\n.highlight .kn { color: #008000; font-weight: bold } /* Keyword.Namespace */\n.highlight .kp { color: #008000 } /* Keyword.Pseudo */\n.highlight .kr { color: #008000; font-weight: bold } /* Keyword.Reserved */\n.highlight .kt { color: #B00040 } /* Keyword.Type */\n.highlight .m { color: #666666 } /* Literal.Number */\n.highlight .s { color: #BA2121 } /* Literal.String */\n.highlight .na { color: #7D9029 } /* Name.Attribute */\n.highlight .nb { color: #008000 } /* Name.Builtin */\n.highlight .nc { color: #0000FF; font-weight: bold } /* Name.Class */\n.highlight .no { color: #880000 } /* Name.Constant */\n.highlight .nd { color: #AA22FF } /* Name.Decorator */\n.highlight .ni { color: #999999; font-weight: bold } /* Name.Entity */\n.highlight .ne { color: #D2413A; font-weight: bold } /* Name.Exception */\n.highlight .nf { color: #0000FF } /* Name.Function */\n.highlight .nl { color: #A0A000 } /* Name.Label */\n.highlight .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */\n.highlight .nt { color: #008000; font-weight: bold } /* Name.Tag */\n.highlight .nv { color: #19177C } /* Name.Variable */\n.highlight .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */\n.highlight .w { color: #bbbbbb } /* Text.Whitespace */\n.highlight .mb { color: #666666 } /* Literal.Number.Bin */\n.highlight .mf { color: #666666 } /* Literal.Number.Float */\n.highlight .mh { color: #666666 } /* Literal.Number.Hex */\n.highlight .mi { color: #666666 } /* Literal.Number.Integer */\n.highlight .mo { color: #666666 } /* Literal.Number.Oct */\n.highlight .sb { color: #BA2121 } /* Literal.String.Backtick */\n.highlight .sc { color: #BA2121 } /* Literal.String.Char */\n.highlight .sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */\n.highlight .s2 { color: #BA2121 } /* Literal.String.Double */\n.highlight .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */\n.highlight .sh { color: #BA2121 } /* Literal.String.Heredoc */\n.highlight .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */\n.highlight .sx { color: #008000 } /* Literal.String.Other */\n.highlight .sr { color: #BB6688 } /* Literal.String.Regex */\n.highlight .s1 { color: #BA2121 } /* Literal.String.Single */\n.highlight .ss { color: #19177C } /* Literal.String.Symbol */\n.highlight .bp { color: #008000 } /* Name.Builtin.Pseudo */\n.highlight .vc { color: #19177C } /* Name.Variable.Class */\n.highlight .vg { color: #19177C } /* Name.Variable.Global */\n.highlight .vi { color: #19177C } /* Name.Variable.Instance */\n.highlight .il { color: #666666 } /* Literal.Number.Integer.Long */'
如您所见,输出已用 class 很好地标记为格式,我可以获取样式的数据,有谁知道如何在笔记本中使用它吗?需要说明的是,问题是我想在单元格结果中看到颜色。
from pygments import highlight
from pygments.lexers import SqlLexer
from pygments.formatters import HtmlFormatter
from IPython.core.display import HTML
sql_string = '''
SELECT id, name
FROM users
WHERE name between 'B' AND 'P'
LIMIT 50
'''
display(HTML("""
<style>
{pygments_css}
</style>
""".format(pygments_css=HtmlFormatter().get_style_defs('.highlight'))))
display(HTML(data=highlight(sql_string, SqlLexer(), HtmlFormatter())))
支持在 Stack Exchange 上进行更深入的挖掘。在关于如何添加到 jupyter 的 css 的未接受问题中找到了东西。
如果您使用的是 Jupyter Notebook,您还可以使用 IPython.display.Code
from IPython.display import Code
Code('print("Hello World")', language='python')
它不会像在 Jupyter Lab 中预期的那样 运行,如 this GitHub issue 中所述,因此您需要 运行 那里的以下位:
from pygments.formatters import HtmlFormatter
from IPython.core.display import HTML
formatter = HtmlFormatter()
display(HTML(f'<style>{ formatter.get_style_defs(".highlight") }</style>'))
您可以直接在笔记本单元格中执行终端命令:
!pygmentize PATH/TO/YOUR/FILE