I am thinking of using Pluto as a sort of Electronic Lab Notebook from where I could drive an experiment and do analysis on the fly. To avoid running a command (e.g. measuring the transmission of a device with a VNA) several times, I was thinking of turning a cell into markdown after it has been executed. Ideally, I would like to add a timestamp and place the command in between backticks so that it’s nicely formatted. Is there a way to do that automatically, for instance using a macro? If not, is there a way to define keyboard shortcuts?
Any suggestion to make this cell conversion to markdown would be appreciated.
Thanks for your input but I don’t think it fits what I’m looking for. I’d like to be able to mix instrument driving/data taking and analysis. To clarify, I made a mock-up of what I have in mind: suppose I have a command measure_transmission that drives an instrument and there’s a ‘magic’ macro that does the cell conversion I’m thinking of. Before running the cell, it would look like that:
After my little playing with Pluto, I’m not sure what you seek is possible and if I’m allowed to speculate I believe it goes against the very intentions of the tool.
I believe you’d have more luck trying to get Jupyter notebooks to do something like this.
Not sure why this would be against the intentions of the tool. All I want is a quick and easy way to turn a cell into markdown (easy with a ctrl+m) but in addition I’d like to have the code surrounded by backticks so that it is displayed as a code block in markdown.
I’d like to keep using Pluto because I actually really like the reactivity for the analysis part and I also like that it directly adds packages that are not installed yet.
Thanks for the links, this is exactly what I had in mind. The issues mentioned in that thread (e.g. unintentional reactivity) are the reasons why I was thinking of turning the cells into markdown: I would keep track of past commands without running the risk of having these commands re-run if I restart the notebook.
Should be doable by making a macro return a block where the first expression execute the Julia code and the second expression (the one that is sent to the Pluto output) returns an Html with Javascript inside a script tag that modifies the cell to markdown and then rerun it…
Not at the computer to try it now though
Edit:
I did have a chance to try this and indeed it is possible:
And here is the link to the notebook code for testing:
Note that this is somehow a hacky solution which might not work anymore when Pluto changes to CM6 (don’t know how long it’ll take), or in general with new versions of Pluto.
But overall it still highlight the amazing capabilities of Pluto!
Hi, any updates on this? It doesn’t seem to work anymore.
I’m using ImageClipboard.jl to copy images into the notebook but I need the images to be persistent.
Is there a way to just never re-run a pluto cell?
Things changed a bit with recent CodeMirror updates in Pluto since the answer.
The original macro code can be simply modified to work again by changing this line:
const cm = cell.querySelector('pluto-input .CodeMirror').CodeMirror
into this other line
const cm = cell.querySelector('pluto-input .cm-editor').CodeMirror
with the new macro code being:
macro run_and_log(ex::Expr)
ex = esc(ex)
blk = Expr(:block) # Create the block
push!(blk.args,ex) # Add the input expression for execution
# Here we create the javascript snippet that will be sent to the output and thus be executed.
script = :(@htl("""
<script>
// Get the current timestamp
let currentdate = new Date();
let dateTime = currentdate.toLocaleString();
// Change the cell input contents
const cell = currentScript.closest('pluto-cell')
const cm = cell.querySelector('pluto-input .cm-editor').CodeMirror
let new_text = cm.getValue().replace('@run_and_log ','')
const newVal = `md\"\"\"
timestamp: \${dateTime}
\\`\\`\\`julia
\${new_text}
\\`\\`\\`
\"\"\"`
cm.setValue(newVal)
// Simulate pressing the run button
cell.querySelector('.runcell').click()
</script>
"""))
push!(blk.args,script)
blk
end
That being said, this code will not really work directly as is to copy an image persistently to the notebook, as it is only storing the raw text of the expression provided as input to it.
You need to modify it to actually store in the cell code the raw HTML necessary to show the image (which you can extract in string form using repr(MIME"text/html"(), image)).
I can’t try it out in Pluto now to create a solution but you should be able to experiment with it to get a working solution based on the pointers above
Thanks for the reply!
I’m having trouble getting the cell output (my javascript is a bit rusty).
I don’t understand how cell.querySelector('pluto-input .cm-editor').CodeMirror works, specifically why does the result of querySelector have the property CodeMirror? What does getting it do?
I’ve tried using cell.querySelector('pluto-output div').innerHTML and sticking that to the cell input but all I get in the cell is