fonsp / Pluto.jl

🎈 Simple reactive notebooks for Julia
https://plutojl.org/
MIT License
4.91k stars 284 forks source link

include file containing module #2849

Open dorn-gerhard opened 3 months ago

dorn-gerhard commented 3 months ago

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

dorn-gerhard commented 3 months ago

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

dorn-gerhard commented 3 months ago

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 :)

image

Pangoraw commented 3 months ago

Related to #1795.

disberd commented 3 months ago

@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