I would like to create in my notebook a custom cell, that looks and acts just like normal one, but the code within is parsed to an Expr
tree that I can examine before Julia runs it. Can that be achieved using Pluto/PlutoUI API? Thanks.
Why not just write a macro and put @mymacro
in front of the code in your cell?
I aim to make it harder for a user of the notebook to mess it up inadvertently. It shouldn’t be possible to remove macro call while editing the cell. Ideally, it shouldn’t be visible to a user.
BonitoBook let’s you extend the cell editor widget in Julia
It’s not fully documented yet, but there are a few examples in the intro notebook
I would prefer to remain within Pluto ecosystem, because porting all existing work into the new notebook format would entail a great deal of work.
I think Pluto generally takes the “transparency” route: no hidden state, no hidden transformations. Even the markdown cells are just m"..."
string macro calls. I don’t think Pluto allows plugging extra implicit transformation…
Although ping @fonsp.
Hi @Mikhail_Kagalenko !
I’m curious about your project, sounds cool! @aplavin’s comment is correct, we think that Julia’s macro syntax is quite cool and flexible, I think you could use that here!
Like @simeonschaub suggested, you can write a macro like @inspect
, and you can place it in front of any cell. See e.g. @steps
in Turtles – introduction . You could use a one-letter macro to make it more subtle, like @x
.
Future feature?
We are considering a feature called Filters (like snapchat) where a “top-level wrapping macro” like @steps
or md"""
can be selected with a GUI, and you only see the inner content in code. This would allow for “markdown cells” and “python cells”, or a “timed cell”, etc. It sounds like this is what you are looking for, but don’t expect this to be released anytime soon.
I’m also considering a “Display transformation” feature, where for any cell, you can write a bit of extra (hidden) code that transforms the cell’s result before displaying it. This allows you to write simple code to show to the reader, with a bit of hidden code to make it display more nicely.
Hello, thank you for the meaningful answer. I am using Pluto
together with PlutoTeachingTools
to check student answers in an exercise notebook. When they are asked to write a function, I want to implement an automatic check that a previously written function is employed, instead of cutting and pasting code. That seems to requre an access to the Expr
tree for their answer. ( I suppose I could try some hacky check, like substituting an error-generating function in a local scope and testing that an exception is being raised)
That’s exactly what I am looking for. Is that feature coming soon?
Hey! Yes that sounds like a good use case, I had the similar situation before.
The low-tech solution is to track calls to the function: (if you wrote it yourself)
You can make this a bit fancier with a macro in front of you_should_use_me
that automatically creates the ref.
I also tried to use Julia introspection tools, like Base.uncompressed_ast(methods(cool)[1])
. This strategy works for simple functions, but I found that when the student function gets more complex (like an anonymous function), then I don’t know how to properly scan the CodeInfo object for usage of your function. I’m a Julia introspection beginner though, so it might make more sense to ask this in a separate thread. You could ask: “is there a way to use Julia’s introspection tools to check whether a function a
uses a function b
in its definition?”
I’m sure this is possible, with tools like Cthulu.jl, Infiltrator.jl, or with base Julia functionality. But I’m the wrong person to ask
I don’t have any timeline for the Display Transformation feature. It’s quite a big feature though…
I don’t write it myself. There’s a sequence of assignments, where a student writes a simple function, then uses it to write a slightly more complex function. The name of a function is fixed,
so checking for its presence in an expression tree should not be hard.
You could ask: “is there a way to use Julia’s introspection tools to check whether a function
a
uses a functionb
in its definition?”