4

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?

For a variable var, using

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

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.

#%% [markdown]
Out[10]
Clay
  • 2,584
  • 1
  • 28
  • 63

1 Answers1

0

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. However, you can circumvent this problem by displaying Markdown or HTML within the Python cell. 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.

It does not answer this problem nevertheless gives the same result.

Suat Atan PhD
  • 1,152
  • 13
  • 27