How to use __import__

I have many, many forms I need to switch between in my content_panel (material design template). I have taken to creating a “routing” module but find I am creating tons of functions for each form load.

What I mean is this :

def show_dashboard(): 
  from Dashboard import Dashboard as new_form
  get_open_form().content_panel.clear()
  get_open_form().content_panel.add_component(new_form())

and I call it from my forms thusly :

routing.show_dashboard()

It makes the forms much tidier but it comes at a cost, which is every time I must cut/paste the above function, changing the names. I think I’m up to about 50 odd now, and the routing module is somewhat unwieldy.

What I want to do is create a function like this :

routing.show_form("Dashboard")

where the function can take the parameter and dynamically import the form and load it into the content panel. Problem is, I can’t use __import__ and nor can I use importlib (neither, I assume, being supported by Skulpt’s version 2-ish of Python).

Is there any way in which I can achieve what I want? Or has anyone got a better suggestion?

Good question. I tried eval but I get a not implemented yet error. I had a dirty feeling about using that anyway…

…shudder…

And before anyone suggests a massive if/else block - don’t. Just don’t. Firstly, because it’s just wrong, and secondly, I’ve already done it. And it’s just as wrong in the flesh as it was in my head…

1 Like

I ended up throwing the form-constructors (classes) into a global dict. That means one module has a very long list of import statements. On the other hand, that module can look up any needed form by name.

Similar to my long list of if/elif. It works but I just can’t help thinking there’s a neater way.

It’s one of those things - it doesn’t matter really, but it just niggles at me …

I also use the “dirty great big dictionary” approach so that module has all the nastiness in one place.