Reputation: 163
I want to access a python parameter inside a standard Markdown paragraph. Think dynamically updated values in a text report. I don't want to do an f-string inside a code block outside the paragraph.
E.g. "... After [code1] years, we found [code2] instances of XYZ occurrences..."
A more detailed example of the expected behavior is in this RMarkdown Documentation.
I tried the RMarkdown syntax `python var_name`
where var_name
is a float and variations on that syntax with no success. Quarto is treating it like a code-formatted text block (not evaluating the code).
How do I do this in a Quarto .qmd file running the Jupyter Kernel in VS Code?
Edit:
Partial workarounds here (what I'm using now) and here. The first option requires string formatting for rounded floats, because float formatting leaves trailing zeros for some reason. I couldn't find documentation to make the second option more extendible.
Upvotes: 3
Views: 2167
Reputation: 595
Inline code is now available starting Quarto version 1.4
https://quarto.org/docs/computations/inline-code.html
You will need to use brackets for specifying the programming language: `{python} var_name`
From the documentation:
```{python}
radius = 5
```
The radius of the circle is `{python} radius`
Will result in
The radius of the circle is 5
Upvotes: 3
Reputation: 94237
The easiest way to do this as of today seems to be to use an r
inline expression that gets the value using reticulate
. For example:
```{python}
value = 99 * 99
```
Python value is `r reticulate::py_eval("value")`.
Will render as "Python value is 9801"
If you want to format something inline with an f-string, you can do that too:
```{python}
x = 3.141592699999
```
Python value is `r reticulate::py_eval("f'{x=:.3}'")`.
You will need to make sure the R kernel (or knitr? or something deep in quarto's guts? idk) is doing the work by including an empty R chunk at the start. Here's a complete example:
---
title: Testing
date: now
format: html
---
```{r}
```
```{python}
x = 3.141592699999
```
Python value is `r reticulate::py_eval("f'{x=:.3}'")`.
Upvotes: 1