[Done] Importlib or __import__() on client

How can you import forms by their string name on the client?

I’d like to create a dynamic navigator without having to hardcode all the imports:

form_import = __import__(form_name, fromlist=[form_name])
open_form(form_import.getattr(form_name)(**properties))

__import__ is not found here, importlib or buildins cannot be imported either.

Hi @toon.sevrin,

We don’t currently support __import__ for dynamic navigation.

Moving to feature requests!

To simplify doing the imports manually, you could create a ‘routing’ module as per David’s post here.

1 Like

Time to uncomment my dictionary navigator then :slight_smile:

Expressing support for the feature request. I ended up here because my standard importlib code didn’t work on the client.

Hi,
I found a way, but it works only on the top level of Forms. Let’s say you have Form1 and Form2 that are direct children of your_app_name. Then in Form1, you can do:

 def link_1_click(self, **event_args):
    """This method is called when the link is clicked"""
    name = 'Form2'  #or get it there however you want
    pathname = 'your_app_name.' + name           #the dot after your_app_name is vital
    module = sys.modules.get(pathname)
    func=getattr(module, name)
    f= func() 
    open_form(f)
1 Like

__import__ is now implemented on the client
while it works, it’s not the most usable api - and python doesn’t recommend it.

But it does mean we were able to implement:

from anvil_extras.utils import import_module

Which attempts to replicate importlib.import_module

docs: Utils — Anvil Extras documentation
source: source

(Currently only available on the Development branch, but it will be part of the next anvil_extras release)

2 Likes