Pluto: Get current notebook file path

I think the only way you could have knowledge about the caller scope is by using a macro.

Quoting the Julia documentation:

In addition to the given argument list, every macro is passed extra arguments named __source__ and __module__ .

The argument __source__ provides information (in the form of a LineNumberNode object) about the parser location of the @ sign from the macro invocation. This allows macros to include better error diagnostic information, and is commonly used by logging, string-parser macros, and docs, for example, as well as to implement the @__LINE__ , @__FILE__ , and @__DIR__ macros.

The location information can be accessed by referencing __source__.line and __source__.file

So you could do something like this:

macro export_to_jupyter(...)
  notebook_name = __source__.file
  return quote
    ... # code using $notebook_name
  end
end
3 Likes