Open firasm opened 3 years ago
I don't think this is possible (though I agree maybe the Jinja folks would have some ideas?). I think it's a really interesting idea though!
I'll try that - hunting around the Jinja documentation, interestingly, I think I can do simple math:
- {{b}} x {{c}} = {{b*c}}
- {{b}} + {{c}} = {{b+c}}
- {{b}} / {{c}} = {{b / c}}
Which produces:
- 2 x 100 = 200
- 2 + 100 = 102
- 2 / 100 = 0.02
Which is cool. I'll open an issue there to see if they have any suggestions for arbitrary python functions
Good news: It looks like this is possible in Jinja as per this SO answer:
from jinja2 import Template
def custom_function(a):
return a.replace('o', 'ay')
template = Template('Hey, my name is {{ custom_function(first_name) }} {{ func2(last_name) }}')
template.globals['custom_function'] = custom_function
Bad news: The only question that remains is what the equivalent way is in MyST-Parser. I am hopeful but am afraid I'm at the end of my python chops - maybe @chrisjsewell has some ideas?
I've set up a repo here with a minimal example in case it helps...
Interestingly, this does work:
---
substitutions:
a: '{{100*2}}'
b: 2
---
Here are the variables: {{a}} and {{b}}.
Here are the variables: 200 and 2.
but not this:
---
substitutions:
a: {{100*2}}
b: 2
---
Here are the variables: {{a}} and {{b}}.
Exception occurred:
yaml.constructor.ConstructorError: while constructing a mapping
in "<unicode string>", line 2, column 8:
a: {{100*2}}
^
found unhashable key
in "<unicode string>", line 2, column 9:
a: {{100*2}}
^
Is your feature request related to a problem? Please describe.
Is there a way to run code and use myst substitutions (via Jinja) to plug in the result in docs?
Here is a minimal file I'm trying to render:
What I get for this is:
Describe the solution you'd like
What I want is:
If there was a way to run arbitrary python code (random numbers, complex functions, sin/cos), that would be the dream. I am also happy for the python code/function to be in a separate file if that makes this easier.
Additional context
I am trying to create a problem bank with randomized questions using MyST as my base language.
I am now having the feeling I should have opened this issue in the Jinja repo instead...