Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

include file containing module #2849

Open
dorn-gerhard opened this issue Mar 13, 2024 · 4 comments
Open

include file containing module #2849

dorn-gerhard opened this issue Mar 13, 2024 · 4 comments

Comments

@dorn-gerhard
Copy link
Contributor

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?

2024-03-13.20.31.25.mp4
@dorn-gerhard
Copy link
Contributor Author

dorn-gerhard commented Mar 13, 2024

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

2024-03-13.20.44.28.mp4

@dorn-gerhard
Copy link
Contributor Author

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
Copy link
Collaborator

Related to #1795.

@disberd
Copy link
Contributor

disberd commented Mar 14, 2024

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants