Pluto.jl
Pluto.jl copied to clipboard
include file containing module
Hi,
I wanted to ask for a best practise for including a self-written module MyModule.jl and having all
exported functions (my_function) within the workspace of the Pluto notebook available instead of MyModule.my_function
Workaround would be of course to create a package.
But relating to #115 it would be cool to first load the file ingredient("./MyModule.jl") and then use the module with all exported functions.
Interestingly using .MyModule or using .MyModule: my_function is not working.
When using include("MyModule.jl")
using .MyModule is not working but using .MyModule: my_function is working (see video below).
So I thought what about creating a function that loads all my exported functions (names(MyModule)):
function using_local_module(modulename)
return eval(Meta.parse("using " * string(modulename) * """: """ * join(string.(modulname), ", ")))
end
but that does only work in the same cell.
So what is the best way to use a self-written module saved in a file.jl within Pluto?
https://github.com/fonsp/Pluto.jl/assets/67096719/8040e229-ebcf-452e-baaf-429dcd3eb35a
I just read a post on Zulip about using a module defined in a cell in Pluto.
This also shows a strange behaviour:
as one has to rerun the using .Module: my_function statement everytime the imported function is used:
importing all functions does not work here as well.
maybe there is a function that does
foo1, foo2, ... = Foos.foo1, Foos.foo2, ... for all names(Foos) and replaces the currently using .SelfDefinedModule function
https://github.com/fonsp/Pluto.jl/assets/67096719/9245d2e4-5b68-4631-bca3-3176f6569537
We could replace
using .MyModule by a Macro
macro using_local_modules(MyModule)
return :(eval(Meta.parse(join(string.(names($MyModule)[2:end]), ", ") *
" = " * split(string($MyModule), ".")[end] * "." * join(string.(names($MyModule)[2:end]), ", "*split(string($MyModule), ".")[end] * "."))))
end
and run @using_local_modules MyModule after including the file containing it :)
Related to #1795.
@dorn-gerhard since you are already defining a module for your things, if you are wililng to simply put it inside a package structure you can try @fromparent/@frompackage from https://github.com/disberd/PlutoDevMacros.jl.
You can check the docs for more details or you can write me directly if you need help. At the moment it only supports loading from "packages" (not registered packages but modules defined in a package folder structure) and not directly from single files. I wanted to add support for not formal packages before JuliaCon and would be happy if you are able to try it out :D
@dorn-gerhard I think your use case would now be fully covered by my latest package https://github.com/disberd/SimplePlutoInclude.jl
Have a look and file an issue if you face problems!
Very cool, thank you :) I will have a look in the train to Eindhoven ;) Will you be there as well?