Closed nathanrboyer closed 3 months ago
You can simply load it by changing the variable to the appropriate format, such as my function to save the workspace, similar to jldsave("example.jld2"; z=x, x=y, y=3)
function save_workspace(outname="workspace.jld2")
variable_names = Base.names(Main)
workspace = Dict{Symbol, Any}()
for var_name in variable_names
if (~isa((@eval Main.$(Symbol(var_name))),Function) && (~(@eval Main.$(Symbol(var_name)) in [nothing,Main,Base,Core]))
&& !isa((@eval Main.$(Symbol(var_name))), Module) && !isstructtype(@eval Main.$(Symbol(var_name))))
try
workspace[Symbol(var_name)] = eval(Meta.parse(string(var_name)))
catch e
println("Cannot save variable $var_name: $e")
end
end
end
varstring = join(
["$k=$(typeof(workspace[k]) == String ? "\"$(workspace[k])\"" : workspace[k])" for k in keys(workspace)],
","
)
savestring="JLD2.jldsave(\"$outname\","*varstring*")"
@eval Main Meta.parse(savestring)
end
Use Save workspace and load workspace
save_workspace()
@load "workspace.jld2"
I hope the developer will improve this workspace saving function into the project, many people have this demand
This functionality has existed for many years in @save filename
(without further args) and @load "path/to/file"
.
However, not every object can be stored with JLD2 and there are conceptual problems. (only works interactively and not in module / no functions / no modules / no struct defs) Due to this, there was a constant stream of complaints for years before this functionality was eventually removed from documentation.
I do not see a way to load variables without specifying new variables names. The variable names are already in the file!
I am currently using this workaround, and think something like this should have a convenient function in the package.
I mostly use this package when I need to restart my REPL but don't want to lose progress on the variables I've generated so far.