Reference
Contents
Index
MinimalWorkingExamples.MWEResultMinimalWorkingExamples.mweMinimalWorkingExamples.mwe_invertMinimalWorkingExamples.mwe_rescueMinimalWorkingExamples.previewMinimalWorkingExamples.set_defaults!MinimalWorkingExamples.@mwe
MinimalWorkingExamples.@mwe — Macro
@mwe begin
code
end [venue=:gh] [temp=true] [newprocess=true] [manifest=false]
[advertise=nothing] [versioninfo=nothing] [preview=nothing]
[packagespecs=PackageSpec[]] [manifest_path=nothing] [verbose=false]
[julia_args=""] [plot_dir="MWEPlots"]Generate a Minimal Working Example (MWE) formatted as Markdown, then copy it to the clipboard.
The code is rendered as a copy-pasteable Julia script with the value of each statement shown as a #> comment, alongside any print/logging output. Assignments and definitions (function, struct, module, etc.) are not echoed.
Keyword arguments
venue=:gh: output format —:ghfor GitHub-Flavored Markdown (default),:discordfor Discord (same as:ghbut the advertisement note uses Discord's-#subtext syntax instead of<sup>),:slackfor Slack (strips the language identifier from the code fence).temp=true: create a temporary isolated environment and auto-add packages fromusing/import. Whenfalse, code runs in the current environment without auto-adding packages (to avoid polluting the user's project).newprocess=true: run the MWE in a fresh Julia process; startup files are disabled to ensure reproducibility. Ifnewprocess=false, the MWE runs in the current session. If alsotemp=true, a temporary project is activated for the execution and then restored.manifest=false: append theManifest.tomlin a collapsible<details>block.advertise=nothing: append a footer noting the date, this package, and Julia version used. Ifnothing(the default), this isfalsefor:slackandtrueotherwise.versioninfo=nothing: whether to append a collapsible "Environment" block showing the output ofversioninfo(). Ifnothing(the default), this istruefor:ghandfalseotherwise.preview=nothing: which viewer shows the rendered result (seepreview) —:editor(the host editor's viewer panel),:browser, orfalse(don't preview). Ifnothing(the default), this isfalsein non-interactive sessions, otherwise:editorwhen an editor viewer panel is available and:browserotherwise.packagespecs=PackageSpec[]: vector ofPkg.PackageSpecs for packages that need a specific version, git revision, URL, or local path.manifest_path=nothing: path to an existingManifest.tomlto use as-is. Mutually exclusive withpackagespecs.verbose=false: iftrue, show Pkg output (downloads, resolver messages) during environment setup.julia_args="": extra command-line flags passed through to the isolated Julia process, e.g."-t 4"or"--check-bounds=no". Only valid whennewprocess=true.plot_dir="MWEPlots": directory in which plots produced by the code are saved as PNGs. A visible**Insert plot here: ...**placeholder marks each plot's position in the Markdown. End a line with;to suppress capture for that expression.
The code block is rebuilt from its parsed AST, so comments and exact formatting are not preserved in the output. Use mwe if you need to preserve your code's formatting and comments.
The defaults above (except packagespecs and manifest_path) can be changed persistently with set_defaults!.
Examples
@mwe begin
using Statistics
x = [1, 2, 3, 4, 5]
mean(x)
end versioninfo=falseProduces (copied to clipboard):
```julia
using Statistics
x = [1, 2, 3, 4, 5]
mean(x)
#> 3.0
```
<sup>Created on <date> with [MinimalWorkingExamples v<pkg-version>](https://github.com/BjarkeHautop/MinimalWorkingExamples.jl) using Julia <version></sup>Pin a package to a specific version:
using Pkg
@mwe begin
using Example
Example.hello("World")
end packagespecs=[PackageSpec(name="Example", version="0.5.3")]MinimalWorkingExamples.mwe — Function
mwe([code]; kwargs...)Function form of @mwe. Accepts code as a plain string. If code is omitted, reads Julia source from the clipboard.
Keyword arguments
Accepts the same keyword arguments (with the same defaults) as @mwe.
Examples
# Run code already copied to the clipboard:
mwe()
# Format for Slack:
mwe(; venue=:slack)
# Run an explicit string:
mwe("""
using Statistics
mean([1, 2, 3])
""")
# Comments are preserved:
mwe("""
1+1 # This comment is preserved
""")MinimalWorkingExamples.mwe_rescue — Function
mwe_rescue([transcript]; kwargs...)Rescue a copy-pasted Julia REPL transcript into an MWE: strips the julia> prompt, dedents continuation lines, discards printed output, then runs the remaining code through mwe.
If transcript is omitted, it is read from the clipboard.
Keyword arguments
Accepts the same keyword arguments (with the same defaults) as mwe.
Examples
mwe_rescue("""
julia> x = 1 + 1
2
julia> x * 2
4
"""; advertise=false)Produces the same MWE as mwe("x = 1 + 1\nx * 2"; advertise=false).
MinimalWorkingExamples.mwe_invert — Function
mwe_invert([text])Invert a rendered MWE back into plain code: drop every line that starts with #> (the output lines emitted by mwe/@mwe) and return what's left, unexecuted.
If text is omitted, it is read from the clipboard — e.g. paste in the code block copied from a GitHub issue. The inverted code is copied back to the clipboard when possible.
Examples
mwe_invert("""
1 + 1
#> 2
""")Returns "1 + 1".
MinimalWorkingExamples.MWEResult — Type
MWEResultWraps the Markdown string produced by @mwe. Displays silently in the REPL — access the Markdown string via .md.
MinimalWorkingExamples.set_defaults! — Function
set_defaults!(; kwargs...)Persistently override the default keyword arguments of @mwe and mwe using Preferences.jl.
Any of venue, temp, newprocess, manifest, advertise, verbose, versioninfo, julia_args, plot_dir, and preview may be set. Passing nothing for a key clears it, reverting to the built-in default.
Examples
# Always format for Slack and skip the isolated environment:
set_defaults!(venue=:slack, temp=false)
# Go back to the built-in venue default:
set_defaults!(venue=nothing)MinimalWorkingExamples.preview — Function
preview(result::MWEResult; target::Union{Symbol,Nothing}=nothing)Render the Markdown of an MWEResult to HTML, to check what the MWE will look like before posting it. Plot placeholders are replaced by the actual saved image files.
target picks the viewer: :editor for the host editor's viewer panel, :browser for the default browser, or nothing (the default) to use the editor panel when available and fall back to the browser otherwise.
Returns the path of the generated HTML file.
Examples
result = mwe()
preview(result)
preview(result; target = :browser)