[英]How to display python variable values in VS Code Python Interactive pane markdown cell output
How can I display the value (as a string) of a python variable in the markdown cell output in the Python Interactive pane of VS Code's MS Python extension?如何在 VS Code 的 MS Python 扩展的 Python 交互窗格的降价单元格输出中显示 Python 变量的值(作为字符串)?
For a variable var
, using对于变量
var
,使用
#%%
from IPython.display import display, Markdown
var = 50 # units
display(Markdown('The value of my variable `var` is: {} units.'.format(str(var)))
in a code cell produces the correct output, but I was looking for a more elegant solution that will allow me to insert the value directly into markdown text.在代码单元格中产生正确的输出,但我正在寻找一个更优雅的解决方案,允许我将值直接插入到降价文本中。
Something similar to the Jupyter notebook extension Python Markdown .类似于 Jupyter 笔记本扩展Python Markdown的东西。
To show output-number [10]
with the following is inconsistent, and when it does work only outputs all of the code text as plain text.显示 output-number
[10]
与以下内容不一致,并且当它起作用时仅将所有代码文本输出为纯文本。
#%% [markdown]
Out[10]
Although there are many ways in RStudio (like usage backtick `` in Markdown) or assigning parameter with glue
method or mustaches {{ var }}
none of them does not work in VSCode for now.尽管 RStudio 中有很多方法(例如 Markdown 中的反引号)或使用
glue
方法或胡须{{ var }}
分配参数,但目前它们都不适用于 VSCode。 However, you can circumvent this problem by displaying Markdown or HTML within the Python cell.但是,您可以通过在 Python 单元格中显示 Markdown 或 HTML 来规避此问题。 Like below:
如下所示:
from IPython.display import IFrame, display, HTML
your_variable = 100
html_skin = f"<h1 style='color:red'>{your_variable }</h1>"
display(HTML(html_skin))
What here we did is show your_variable
within h1 tags and render in the notebook.我们在这里所做的是在 h1 标签中显示
your_variable
并在笔记本中呈现。
It does not answer this problem nevertheless gives the same result.它没有回答这个问题,但给出了相同的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.