Call a script when the view changes and refresh the UI via an external script


I’m new to Home Assistant and want to use it for my Bachelor Thesis.
What I want is to dynamically generate the UI when I select a view. It should be done by some Python script which edits the the ui-lovelace.yaml file. Afterwards the Script should reload the UI or if needed thee whole configuration.
Restarting Home Assistant would take too long for me and I’d prefer to do it as described above.

I need two things:

Automatically refresh the UI.
I need the functionality from the “Refresh” button of the three-dot-menu in the upper right corner.
Can this be scripted somehow?

Trigger a script when the view changed.
When I change the view I want to do something and gather some data.
Based on that data I want to rewrite the UI and refresh it.

I also would be fine if I would not refresh the UI but reload the whole configuration.

What would be the best way to achieve that? Is there an API I can call from an add-on for what I want?
I would prefer writing an external module instead of changing the main code base if possible.

Thanks for your help :slight_smile: