-
DescriptionI am trying to include Python code blocks in Quarto documents that
However, this only works for specifying the Steps to reproduce
# Works: Print code with filename & without result
```{.python filename="my.py"}
sum = 0
i = 1
while i <= 100:
if i % 2 == 0:
sum += i
i += 1
print('The sum of all even numbers between 1 and 100 is:', sum)
```
# Works: Print Python code with result & without filename
```{python}
sum = 0
i = 1
while i <= 100:
if i % 2 == 0:
sum += i
i += 1
print('The sum of all even numbers between 1 and 100 is:', sum)
```
# Does not work (why?): Print code with result & with filename
Specifically, the **result is printed, but the `filename` option is being ignored**. Why?
```{python filename="my.py"}
sum = 0
i = 1
while i <= 100:
if i % 2 == 0:
sum += i
i += 1
print('The sum of all even numbers between 1 and 100 is:', sum)
```
# Works: Specify filename as "special comment" instead
If I specify the `filename` execution option as a "special comment", it works, however,
i.e., source code, result, and filename are all being printed:
```{python}
#| filename: my.py
sum = 0
i = 1
while i <= 100:
if i % 2 == 0:
sum += i
i += 1
print('The sum of all even numbers between 1 and 100 is:', sum)
```
Why do the last two examples behave differently?I guess more generally, I am confused about the difference between specifying execution options as
I am guessing that this is not really a bug, but due to some fundamental difference between these two syntax options that I do not understand. In general, I find the latter a little easier to read (which is why I'd prefer to use it), but clearly there are differences between these two? Are these documented anywhere on https://quarto.org? What is the reason for these differences? Output of
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
Could you properly format your post using code blocks for code and terminal outputs? Thanks. |
Beta Was this translation helpful? Give feedback.
-
Let me try to clarify the context
this syntax is not related to execution. So this is not execution options. The attributes you can set on those code cells are not the same as the ones for execution. Some can be common, mainly the one you can set on the execution cell that maps to a source code attribute (like
This syntax is from Quarto and is used for execution. It is a different syntax. In Quarto, options for execution are passed using special comments with YAML. You can't pass options to execution using
Do you have an example using a Python cell where this is working for execution options? Regarding the
They are not processed the same. For computation, you typically set much more options than in the source code. Some attributes like
Hope it helps clarify; Happy to give more details if needed. |
Beta Was this translation helpful? Give feedback.
Let me try to clarify the context
this syntax is not related to execution. So this is not execution options.
This syntax is for source code cell and they can take language as class, and some attribute. This is inherited from Pandoc and its
fenced_code_attributes
extension.The attributes you can set on those code cells are not the same as the ones for execution. Some can be common, mainly the one you can set on the execution cell that maps to a source code attribute (like
filename
)This syntax is from Quarto and is used for execution. It is a different syntax. In Quarto, options for execution are passed using special comments with YAML.
Y…