Forms - dynamic import

What I’m trying to do:
I am trying to import appropriate Form based on a string taken from DB. I found and posted a way on the top level (Importlib or __import__() on client - #3 by toon.sevrin)
meaning I can call app->Form2 from app->Form1. But I want to call app->Form2 from app->Form1->ItemTemplate1 (from an item in the RepeatingPanel).

What I’ve tried and what’s not working:
I am trying to change the pathname variable to get through the directory structure to my lovely Form module

Code Sample:

def link_1_click(self, **event_args):
    """This method is called when the link is clicked"""
    name = self.item['task']['object']
    pathname = 'appname.' + name
    module = sys.modules.get(pathname)
    print(module)
    func = getattr(module, name)
    f = func() 
    open_form(f) 

Not an answer to your question, and possibly not applicable to your app, but if you were using hash routing, you could easily build a route out of a string. HashRouting: Routing, navigation with URL Hash

Hash routing isn’t for every app, but I’ve found it simplifies the sort of form to form navigation you’re trying to do quite a bit.

Alternatively, if you can’t work out the import from where it’s at, you can pull your import code out of your repeating panel and back into your form, and have the repeating panel trigger an event that will let the form itself handle things: Dynamically Adding Text To End Of A Text Box Or Text Area - #8 by jshaffstall

1 Like

What about

def link_1_click(self, **event_args):
    """This method is called when the link is clicked"""
    name = self.item['task']['object']
    open_form(name)

open_form also accepts a string.

If you want to refer to an already-created form, you can stash [a reference to] the opened form in a global variable. Then you don’t need to re-create it (unless you really want to).

1 Like