Other Functions
This package also exports some additional convenience macros for simplifying package development aided by Pluto notebooks.
Additionally, the non-exported function PlutoDevMacros.hide_this_log can be used for sending javascript code through logs to Pluto and hide the corresponding log from view (without stopping the javascript code to execute)
Utilities Macros
PlutoDevMacros.@addmethod — Macro@addmethod func(args...;kwargs...) = body
@addmethod function func(args...;kwargs...)
body
endThis simple macro modifies a function definition expression (only when called from a Pluto notebook) to prepend the name of the module defining the function (here called DefiningModule) to the method definition.
So the code
@addmethod func(args...;kwargs...) = somethingis simply translated to
DefiningModule.func(args...;kwargs...) = somethingwhen called from a Pluto notebook, and to:
func(args...;kwargs...) = somethingwhen called outside of Pluto.
This is useful to avoid multiple definition errors inside Pluto but has the caveat that defining a method with @addmethod does not trigger a reactive run of all cells that call the modified function. This also mean that when removing the cell with the @addmethod call, the actual method added to the DefiningModule will not be automatically erased by Pluto and will still accessible until it is not overwritten with another method with the same signature.
This is easy to fix in the case of adding methods to modules loaded with @frompackage/@fromparent as reloading the module is sufficient to remove the hanging method.
See this video for an example:
See also: @frompackage, @fromparent
PlutoDevMacros.@only_in_nb — Macroonly_in_nb(ex)Executes the expression ex only if the macro is called from a running Pluto instance and ran directly from the source notebook file.
This is more strict than PlutoHooks.@skip_as_script as including a notebook with @skip_as_script ex from another notebook would still execute ex.@only_in_nb ex instead only evaluates ex if the calling notebook is the original source notebook file.
See also: @only_out_nb. PlutoHooks.@skip_as_script.
PlutoDevMacros.@only_out_nb — MacroPlutoDevMacros.@current_pluto_cell_id — Macro@current_pluto_cell_id()Returns the cell_id (as a string) of the cell where the macro is called. If not ran from within the pluto notebook containing the call, returns an empty string
PlutoDevMacros.@current_pluto_notebook_file — Macro@current_pluto_notebook_file()Returns the path of the notebook file of the cell where the macro is called. If not ran from within the pluto notebook containing the call, returns an empty string
Utilities Functions
PlutoDevMacros.hide_this_log — Functionhide_this_log(html_content::AbstractString = "")
hide_this_log(html::Docs.HTML)Simple function that returns a Docs.HTML object which when sent to Pluto logs with e.g. @info (or any other loggin macro), will hide the log from view.
It is mostly intended to send some javascript to the logs for execution but avoid having an empty log box hanging around below the cell. The output of this function contains a script which will hide the specific log that contains it, and if no other logs are present, will also hide the log container in Pluto.
The function also optionally accept some content that will be inserted just before the script for hiding the log. The custom input can be provided as an AbstractString, or directly as an HTML object of type Docs.HTML, in which case it will simply extract the HTML contents as String from the contents field. The provided content will be directly interpreted as HTML, meaning that any script will have to be surrounded by <script> tags.
This function is used inside PlutoDevMacros to create the reload @fromparent button also via logs for redundancy.
Example
Suppose you are inside a Pluto notebook and you want to execute some custom javascript from a cell, but for some reason you don't want to have the javascript to be the final output of your cell. You can exploit logs for this (assuming you didn't disable logging for the cell in question).
The following snippet can be inserted in a cell to send a custom message on the javascript developer console while still having a non-javascript cell output.
julia_output = let
@info PlutoDevMacros.hide_this_log(html"<script>console.log('message')</script>")
3
endWhich will correctly send the message to the console even if the cell output is not the javascript script: