IJulia.jl icon indicating copy to clipboard operation
IJulia.jl copied to clipboard

Support Cell Macros (similar to IPython cell magics but easily user-definable)

Open cduck opened this issue 5 years ago • 2 comments

Adds the ability to apply macros to the contents of an entire cell in IJulia. The use case that prompted me to implement this can be seen here.

Simple use case (assume soft scope is disabled):

### Cell 1 ###
using SoftGlobalScope

### Cell 2 ###
@@softscope
x=0
for i in 1:10 x+=i end
x

Equivalent to:

@softscope begin  # :toplevel block
    x=0
    for i in 1:10 x+=i end
    x
end

Features:

  • Runs any number of macros listed at the beginning of a cell. The lowest macro is run first.
  • Expressions on the same line as a cell macro are included as arguments. The cell contents are the last argument.
  • Automatically runs the macros in IJulia.cell_macros afterward. If soft scope is enabled, var"@softscope" is included here.
  • Include @@noauto in the cell to disable the automatic macros for that cell. (Useful for debugging or recovering from pushing a bad macro.)
  • Tab completion works just like for regular macros.

I'm relatively new to Julia so please give me suggestions if my code style or macro usage could be improved. I can add tests and documentation if needed.

cduck avatar Jul 20 '20 08:07 cduck

Bump.

cduck avatar May 06 '21 15:05 cduck

I'm not sure we want to encourage people to use syntax in Jupyter notebooks that can't be copied-and-pasted to other Julia code. They can always use regular macros, string macros, etcetera…

stevengj avatar Oct 19 '22 22:10 stevengj