Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Implement execute-dir #184

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions src/QuartoNotebookWorker/src/refresh.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,31 @@
function refresh!(path, original_options, options = original_options)
# Current directory should always start out as the directory of the
# notebook file, which is not necessarily right initially if the parent
# process was started from a different directory to the notebook.
cd(dirname(path))
# We check the `execute-dir` key in the options,
if haskey(options, "project") && haskey(options["project"], "execute-dir")
ed = options["project"]["execute-dir"]
if ed == "directory"
cd(dirname(path))
elseif ed == "project"

Check warning on line 7 in src/QuartoNotebookWorker/src/refresh.jl

View check run for this annotation

Codecov / codecov/patch

src/QuartoNotebookWorker/src/refresh.jl#L3-L7

Added lines #L3 - L7 were not covered by tests
Comment on lines +5 to +7
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only see a description of project here, https://quarto.org/docs/projects/code-execution.html#working-dir, not directory. Is that documented elsewhere. It also doesn't mention that you can pass a path, is that described somewhere else?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just looked this up - Quarto's project file schema defines execute-dir as an enum: [file, project], so we should probably only support those two arguments.

https://github.com/quarto-dev/quarto-cli/blob/6b363d996e31c65826c2e8186c14a5da6019af96/src/resources/schema/project.yml#L16-L24

# TODO: this doesn't seem right. How does one get the root path of the project here?
# Maybe piggyback on `options` with some ridiculous identifier?
# We can't rely on `pwd`, because the notebook can change that.
if isfile(NotebookState.PROJECT[])
cd(dirname(NotebookState.PROJECT[]))
elseif isdir(NotebookState.PROJECT[])
cd(NotebookState.PROJECT[])
elseif isdir(ed)
cd(ed)

Check warning on line 16 in src/QuartoNotebookWorker/src/refresh.jl

View check run for this annotation

Codecov / codecov/patch

src/QuartoNotebookWorker/src/refresh.jl#L11-L16

Added lines #L11 - L16 were not covered by tests
else
@warn "Project path not found: $(NotebookState.PROJECT[])"

Check warning on line 18 in src/QuartoNotebookWorker/src/refresh.jl

View check run for this annotation

Codecov / codecov/patch

src/QuartoNotebookWorker/src/refresh.jl#L18

Added line #L18 was not covered by tests
end
else
cd(abspath(ed))

Check warning on line 21 in src/QuartoNotebookWorker/src/refresh.jl

View check run for this annotation

Codecov / codecov/patch

src/QuartoNotebookWorker/src/refresh.jl#L21

Added line #L21 was not covered by tests
end
else
# Current directory should always start out as the directory of the
# notebook file, which is not necessarily right initially if the parent
# process was started from a different directory to the notebook.
cd(dirname(path))

Check warning on line 27 in src/QuartoNotebookWorker/src/refresh.jl

View check run for this annotation

Codecov / codecov/patch

src/QuartoNotebookWorker/src/refresh.jl#L27

Added line #L27 was not covered by tests
end

# Reset back to the original project environment if it happens to
# have changed during cell evaluation.
Expand Down
7 changes: 7 additions & 0 deletions src/server.jl
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ function _extract_relevant_options(file_frontmatter::Dict, options::Dict)
julia_default = get(file_frontmatter, "julia", nothing)

params_default = get(file_frontmatter, "params", Dict{String,Any}())
project_default = get(file_frontmatter, "project", Dict{String,Any}())

if isempty(options)
return _options_template(;
Expand All @@ -191,6 +192,7 @@ function _extract_relevant_options(file_frontmatter::Dict, options::Dict)
julia = julia_default,
daemon = daemon_default,
params = params_default,
project = project_default,
)
else
format = get(D, options, "format")
Expand Down Expand Up @@ -218,6 +220,8 @@ function _extract_relevant_options(file_frontmatter::Dict, options::Dict)
cli_params = get(options, "params", Dict())
params_merged = _recursive_merge(params_default, params, cli_params)

project = get(metadata, "project", Dict())

return _options_template(;
fig_width,
fig_height,
Expand All @@ -229,6 +233,7 @@ function _extract_relevant_options(file_frontmatter::Dict, options::Dict)
julia = julia_merged,
daemon,
params = params_merged,
project,
)
end
end
Expand All @@ -244,6 +249,7 @@ function _options_template(;
julia,
daemon,
params,
project,
)
D = Dict{String,Any}
return D(
Expand All @@ -261,6 +267,7 @@ function _options_template(;
"metadata" => D("julia" => julia),
),
"params" => D(params),
"project" => D(project),
)
end

Expand Down
Loading