PumasAI / QuartoNotebookRunner.jl

MIT License
63 stars 10 forks source link

PythonCall #185

Open jkrumbiegel opened 1 month ago

jkrumbiegel commented 1 month ago

Played around with the ast module to try and fix the problems of https://github.com/PumasAI/QuartoNotebookRunner.jl/pull/180, mainly that the "Python" code there actually had to be valid Julia syntax, too, and that it wouldn't work with multiple expressions or any more complex thing.

Python only allows eval to be called on certain expressions and we can use ast to analyze whether we got those. Otherwise we call exec. One more special case is assignments, where we first exec and then eval the variable out.

I got this working so far:

---
engine: julia
execute: 
  error: true
---

```{julia}
using PythonCall
a = 1 + 3
b = range(1, 10)
a
def myfunc(x):
    return x + 2

myfunc(b[0])


<img width="688" alt="image" src="https://github.com/user-attachments/assets/5e00efc0-6cb8-47eb-9e24-90f03a262832">