Skip to content

Commit

Permalink
code path (#889)
Browse files Browse the repository at this point in the history
  • Loading branch information
tlienart committed Sep 24, 2021
1 parent 5a2840e commit 7dc5564
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
- name: Install Julia
uses: julia-actions/setup-julia@v1
with:
version: 1.5
version: 1.6
- run: julia -e '
using Pkg;
Pkg.add(Pkg.PackageSpec(name="NodeJS"));
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Franklin"
uuid = "713c75ef-9fc9-4b05-94a9-213340da978e"
authors = ["Thibaut Lienart <tlienart@me.com>"]
version = "0.10.57"
version = "0.10.58"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand Down
37 changes: 35 additions & 2 deletions demos/index.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
@def title = "Franklin FAQ"
@def tags = ["index"]
+++
title = "Franklin FAQ"
tags = ["index"]
auto_code_path = true
+++

# Franklin Demos

Expand All @@ -17,6 +20,36 @@ The ordering is reverse chronological but just use the table of contents to guid

\toc

## (017) making cells work in their own path

Currently if you're saving a figure in a code block, you need to specify where to place that figure, if you don't it will go in the current directory which is the main site directory, typically you don't want that, so one trick is to use the `@OUTPUT` macro like so:

```!
using PyPlot
figure(figsize=(8,6))
plot(rand(5), rand(5))
savefig(joinpath(@OUTPUT, "ex_outpath_1.svg"))
```

and that directory is among the ones that are automatically checked when you use the `\fig` command (e.g. `\fig{ex_outpath_1.svg}`):

\fig{ex_outpath_1.svg}

if you're writing tutorials and have lots of such figures and you don't want your readers to see these weird `@OUTPUT` or you can't be bothered to add `#hide` everywhere, you can set a variable `auto_code_path` to `true` (either locally on a page or globally in your config), what this will do is that each time a cell is executed, Julia will first `cd` to the output path. So the above bit of code now reads:

```!
using PyPlot
figure(figsize=(8,6))
plot(rand(5), rand(5), color="red")
savefig("ex_outpath_2.svg")
```

and like before just use `\fig{ex_outpath_2.svg}`:

\fig{ex_outpath_2.svg}

**Note**: since this was meant to be a non-breaking change, `auto_code_path` is set to `false` by default, you must set it yourself to `true` in your `config.md` if you want this to apply everywhere.

## (016) using WGLMakie + JSServe

[This page](/wgl/) shows an example using WGLMakie + JSServe.
Expand Down
5 changes: 5 additions & 0 deletions src/eval/codeblock.jl
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,13 @@ function resolve_code_block(ss::SubString; shortcut=false)::String
# make the output directory available to the code block
# (see @OUTPUT macro)
OUT_PATH[] = cp.out_dir
bk = pwd()
out = ifelse(locvar(:auto_code_path)::Bool, cp.out_dir, bk)
isdir(out) || mkpath(out)
cd(out)
# >> eval the code in the relevant module (this creates output/)
res = run_code(mod, code, cp.out_path; strip_code=false)
cd(bk)
# >> write res to file
# >> this weird thing with QuoteNode is to make sure that the proper
# "show" method is called...
Expand Down
2 changes: 2 additions & 0 deletions src/utils/vars.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ const GLOBAL_VARS_DEFAULT = [
"anchors" => dpair(LittleDict{String,String}()),
# allow md strings in Literate
"literate_mds" => dpair(false),
# each cell runs a cd in/out OUT_PATH
"auto_code_path" => dpair(false),
# -----------------------------------------------------
# LEGACY
"div_content" => dpair(""), # see build_page
Expand Down

2 comments on commit 7dc5564

@tlienart
Copy link
Owner Author

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/45467

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.10.58 -m "<description of version>" 7dc5564f5e68fcd6b7c8f8a53f3e111e5e881a38
git push origin v0.10.58

Please sign in to comment.