Hi, I would like to know if there is a function that returns the name of the file where the function is evaluated, much like pwd() does for the directory.
I can't use @__FILE__ as that returns the name of the file where the function containing the @__FILE__ is, so it doesn't work if I call my function containig @__FILE__ from somewhere else.
Some more details on my use-case
Say I have a julia file in path X that calls my utility function know_filename()
defined in path Y and I would like know_filename()
to be aware of the file-name of X during its processing.
I know that I could pass @__FILE__ as an argument to the function from the caller, but I would like to avoid having to explicitly pass this as argument if possible.
I would need this this for storing pasted images to serve on Pluto notebooks.
I often finds the need to directly paste images inside Pluto notebooks.
To do so, I practically want to automatically move the pasted image in a subfolder of where my Pluto notebook is located.
More specifically, since I may have more than one notebook in the same folder, I would like images to be stored in a subdirectory structure like this
notebook_folder/pastedImages/notebook_name/img_name.png
I was already using @__DIR__ inside my function to get the working directory, but I just realized that indeed that was providing the directory of where my utility function is defined, rather than the directry where the notebook is located.
That can easily be fixed using pwd() inside the utility function, but is there a similar way to obtain the filename of the notebook calling the function without the need to pass it as an argument?
That is only possible using macros. In this case I guess you can use a macro like this (pseudo code):
@pass_filename f(args...)
that just calls e.g.
f(args...; file = ...)
string(stacktrace(backtrace())[2].file)
might give you the answer but it reaches into internals and I'm not sure how reliable it is.
Thanks, indeed in the end I relied on stacktrace() and on the fact that the filename (in the stacktrace) of function call where the Pluto cell output evaluation is happening always ends with notebook-name#==#CELL-UUID
.
So I can extract the notebook name by looking for that pattern
Last updated: Nov 22 2024 at 04:41 UTC