# Pluto: Get current notebook file path

**URL:** <https://discourse.julialang.org/t/pluto-get-current-notebook-file-path/60543>\
**Category:** General Usage\
**Tags:** filesystem, pluto\
**Created:** [May 4, 2021, 8:16pm UTC](https://discourse.julialang.org/t/pluto-get-current-notebook-file-path/60543 "2021-05-04T20:16:36Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![maajdl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maajdl/32/22838_2.png) [@maajdl](https://discourse.julialang.org/u/maajdl)\
**Post date:** [May 4, 2021, 8:16pm UTC](https://discourse.julialang.org/t/pluto-get-current-notebook-file-path/60543/1 "2021-05-04T20:16:36Z")

</div>

Hello,

I would like to get the filepath for the current Pluto notebook.  
Is there a way to get this information?

Thanks

Michel

PS:  
My purpose is to write a SaveAsIpynb function that would keep a Jupyter notebook copy of the current Pluto notebook.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [May 5, 2021, 9:37am UTC](https://discourse.julialang.org/t/pluto-get-current-notebook-file-path/60543/2 "2021-05-05T09:37:16Z")

</div>

You can get the folder that the notebook is in with `@ __DIR__ ()`, and you can get files in that directory with `readdir`, but I don’t know how to get the name of the notebook directly.

---

<div class="post-metadata">

**Author:** ![Pangoraw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pangoraw/32/24719_2.png) [@Pangoraw](https://discourse.julialang.org/u/Pangoraw)\
**Post date:** [May 5, 2021, 10:50am UTC](https://discourse.julialang.org/t/pluto-get-current-notebook-file-path/60543/3 "2021-05-05T10:50:28Z")

</div>

You can use the `@ __FILE__ ` macro and remove the trailing characters after the `#==#<...>`. By doing something like:

```julia
notebookpath() = replace(@ __FILE__ , r"#==#.*" => "")

```

---

<div class="post-metadata">

**Author:** ![maajdl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maajdl/32/22838_2.png) [@maajdl](https://discourse.julialang.org/u/maajdl)\
**Post date:** [May 5, 2021, 7:58pm UTC](https://discourse.julialang.org/t/pluto-get-current-notebook-file-path/60543/4 "2021-05-05T19:58:44Z")

</div>

Thanks both!  
I also found pathof and splitdir to help.  
It is not yet clear how I will do it.  
I’ll come back if I am stuck!  
Michel

---

<div class="post-metadata">

**Author:** ![maajdl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maajdl/32/22838_2.png) [@maajdl](https://discourse.julialang.org/u/maajdl)\
**Post date:** [May 5, 2021, 9:15pm UTC](https://discourse.julialang.org/t/pluto-get-current-notebook-file-path/60543/5 "2021-05-05T21:15:46Z")

</div>

> [@Pangoraw](#):
>
> `@ __FILE__ `

Just tested.  
In a Jupyter notebook, @ **FILE** returns “In[12]” , the cell in the notebook.

In Pluto it rerturn something like:  
“C:\Users\myPC\Pluto notebooks\myPackage\examples\Pluto\example.jl#==#06f9b601-36bf-440d-812f-a858b101fb2a”

So, for Pluto this would be enough, but not for Jupyter!  
I hope there is a way to have that working for both Jupyet and Pluto.  
I want to have this code inside the package.

Thanks

Michel

---

<div class="post-metadata">

**Author:** ![josePereiro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josepereiro/32/17322_2.png) [@josePereiro](https://discourse.julialang.org/u/josePereiro)\
**Post date:** [May 5, 2021, 10:47pm UTC](https://discourse.julialang.org/t/pluto-get-current-notebook-file-path/60543/6 "2021-05-05T22:47:22Z")

</div>

This is an old unsolved [issue](https://github.com/ipython/ipython/issues/10123) for jupyter notebooks.

---

<div class="post-metadata">

**Author:** ![maajdl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maajdl/32/22838_2.png) [@maajdl](https://discourse.julialang.org/u/maajdl)\
**Post date:** [May 6, 2021, 8:07am UTC](https://discourse.julialang.org/t/pluto-get-current-notebook-file-path/60543/7 "2021-05-06T08:07:59Z")

</div>

> [@Pluto: Get current notebook file path](https://discourse.julialang.org/t/pluto-get-current-notebook-file-path/60543/3):
>
> You can use the @\_\_FILE\_\_ macro and remove the trailing characters after the #==#\<...\>. By doing something like: notebookpath() = replace(@\_\_FILE\_\_, r"#==#.\*" =\> "")

Hello Pangoraw

I just realised that @ **FILE** returns the path of the file where the macro is used.  
Actually, I need the path of the notebook (Pluto), but the macro will be in a package used by the notebook.  
Is there a way to find the path of the topmost “caller”, in a sense ?

Thanks

Michel

---

<div class="post-metadata">

**Author:** ![Pangoraw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pangoraw/32/24719_2.png) [@Pangoraw](https://discourse.julialang.org/u/Pangoraw)\
**Post date:** [May 10, 2021, 5:10pm UTC](https://discourse.julialang.org/t/pluto-get-current-notebook-file-path/60543/8 "2021-05-10T17:10:02Z")

</div>

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

Quoting the [Julia documentation](https://docs.julialang.org/en/v1/manual/metaprogramming/#Macro-invocation):

> 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__ `](https://docs.julialang.org/en/v1/base/base/#Base.@ __LINE__ ), [`@ __FILE__ `](https://docs.julialang.org/en/v1/base/base/#Base.@ __FILE__ ), and [`@ __DIR__ `](https://docs.julialang.org/en/v1/base/base/#Base.@ __DIR__ ) macros.
> 
> The location information can be accessed by referencing ` __source__.line` and ` __source__.file`

So you could do something like this:

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

```

---

<div class="post-metadata">

**Author:** ![maajdl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maajdl/32/22838_2.png) [@maajdl](https://discourse.julialang.org/u/maajdl)\
**Post date:** [May 11, 2021, 6:22am UTC](https://discourse.julialang.org/t/pluto-get-current-notebook-file-path/60543/9 "2021-05-11T06:22:43Z")

</div>

Thanks Pangoraw!

A one liner to wrap my function and that’s is, thanks to you:

```julia
macro toJupiter()
    toJupiter(String(split(String( __source__.file),"#==#")[1]))
end

```

Now I can convert a Pluto nb to Jupyter without thinking.

From Jupyter to Pluto it will not work, because I do not get the path, I get the cell instead.  
But that’s less important for me.

Thanks

Michel

---

<div class="post-metadata">

**Author:** ![floswald](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/floswald/32/195_2.png) [@floswald](https://discourse.julialang.org/u/floswald)\
**Post date:** [December 22, 2021, 11:00am UTC](https://discourse.julialang.org/t/pluto-get-current-notebook-file-path/60543/10 "2021-12-22T11:00:32Z")

</div>

that does not seem to be the case anymore. how can i get the `pwd` from which I started the julia session that called `Pluto.run()`?

It’s not this directory:

 ![Screenshot 2021-12-22 at 11.59.05](https://global.discourse-cdn.com/julialang/original/3X/8/5/85b2b178a034bf9dee0598c77ec34463e139692f.png)

I tried something like this

```julia
﻿﻿julia> Pluto.Configuration.Options(server=Pluto.Configuration.ServerOptions(root_url = pwd()))
Pluto.Configuration.Options(Pluto.Configuration.ServerOptions("/Users/florian.oswald/.julia/pluto_notebooks", "127.0.0.1", nothing, true, false, true, "/Users/florian.oswald/git/ClimateNotebooks/notebooks/", false, false, 0.4, false, nothing, false, 0.0), Pluto.Configuration.SecurityOptions(true, true), Pluto.Configuration.EvaluationOptions(true, true, false), Pluto.Configuration.CompilerOptions(nothing, nothing, nothing, nothing, nothing, "no", "no", 2))

julia> Pluto.run(Pluto.Configuration.Options(server=Pluto.Configuration.ServerOptions(root_url = pwd())))
ERROR: AssertionError: endswith(session.options.server.root_url, "/")

```

---

<div class="post-metadata">

**Author:** ![Pangoraw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pangoraw/32/24719_2.png) [@Pangoraw](https://discourse.julialang.org/u/Pangoraw)\
**Post date:** [December 23, 2021, 1:25pm UTC](https://discourse.julialang.org/t/pluto-get-current-notebook-file-path/60543/11 "2021-12-23T13:25:35Z")

</div>

> [@floswald](#):
>
> that does not seem to be the case anymore. how can i get the `pwd` from which I started the julia session that called `Pluto.run()` ?

This discussion is about getting the file path to the actual notebook file. Since each notebook runs in a different Julia process, they each get their own `pwd()`. You can use an environment variable to get the working directory of the Pluto process:

```julia-repl
julia> withenv("PLUTO_ROOT_DIR" => pwd()) do
           Pluto.run() # launch Pluto with PLUTO_ROOT_DIR in the env
       end

```

and then in the notebook you can get it with `ENV["PLUTO_ROOT_DIR"]`. Also note that `root_url` refers to the url of the Pluto webserver and is not related to the directory in which the draft notebooks are created.

---

<div class="post-metadata">

**Author:** ![floswald](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/floswald/32/195_2.png) [@floswald](https://discourse.julialang.org/u/floswald)\
**Post date:** [December 23, 2021, 1:39pm UTC](https://discourse.julialang.org/t/pluto-get-current-notebook-file-path/60543/12 "2021-12-23T13:39:17Z")

</div>

Great thanks! That’s what I need. I could not make this work with the advised solution of using `Base.current_project()` because it would always look for a project at `~`. When trying to use a local project file that is shared with a local package.
