posit-dev / py-shiny

Shiny for Python
https://shiny.posit.co/py/
MIT License
1.2k stars 69 forks source link

Add example for nested modules #1303

Open ME-researchgroup opened 5 months ago

ME-researchgroup commented 5 months ago

Hi!

I am writing an application and would like to split my code into different modules to keep it organized.

For my use case, the user can press a button who's logic lives in "app.py" with its own ui and server. Clicking the button will open a modal who's logic lives in its own file with its own ui and server, the modal introduces new buttons and each button has its own logic in their respective files with their own ui and server components.

I can get the modal working, but when I go one layer deeper i can't seem to get it to work; nothing happens when the buttons are clicked. I guess I am confused about how the namespace is resolved/how i should handle IDs and found the documentation lacking for more complex architectures.

Any chance we could add an example for nested modules? In the meantime it would really help me if someone could give a code example of how i could get this to work.

Thanks in advance!

gadenbuie commented 4 months ago

Hi @ME-researchgroup! Do you think you could create a small example using the pattern you're trying to get working? It could be a question of getting the nested modules set up correctly, or it could be an issue with the modal logic.

ME-researchgroup commented 4 months ago

Hi @gadenbuie , i'll try to write an example this week. The code works if i put everything in app.py, so I think the logic should be fine. Also, I've now got it to work by using resolve_id() so I am pretty sure it's a namespace issue. So it works for now, but since resolve_id is defined in the _namespaces folder, I don't think that's what I'm supposed to do :)

teristam commented 1 month ago

Hi, is there any update on this? I am having similar problems creating nested modules. Specifically, I don't really know where to put the server functions of the sub-module. I tried to create it in the parent modules but it the server function requires reactive input and shiny complains about not having a reactive context when the app starts.