4

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.

1
  • 1
    See Inline Code from Quarto Doc. What you want is not exactly possible in the case of python, since Jupyter, Knitr, and Julia each of them has its own way (i.e. syntax) to evaluate inline code Commented Nov 18, 2022 at 12:19

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

Comments

1

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}'")`.


    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.