Execute long running function after dialog close

I have been smashing my headover this for longer then I would like to admit. Essentially I am using a dialog to popup and confirm what is about to get deployed, when the user clicks confirm in the dialog it is to close and start running the function. Here is the relevant code:

@st.experimental_dialog("Confirm Deployment", width="large")
def confirm_deployment(selected_dashboards, selected_dashboard_permissions, change_request_number):
    source_env_upper = str(asset_manager.source_env).upper()
    target_env_upper = str(asset_manager.target_env).upper()

    st.write(f"You are about to deploy dashboards from **{source_env_upper}** to **{target_env_upper}** with the following details:")
    st.write("")
    for dashboard in selected_dashboards:
        st.markdown(f"**{dashboard['name']} ({dashboard['id']})**")
        permissions = selected_dashboard_permissions.get(dashboard["id"], [])
        if "existing" in permissions:
            st.markdown("Permissions:")
            st.markdown("    - Use Existing Groups")
        else:
            st.markdown("Permissions:")
            for group in permissions:
                st.markdown(f"    - {group}")
    if st.button("Confirm", type="primary", key="confirm_dashboard_deployment"):
        st.session_state["deployment"] = {
            "selected_dashboards": selected_dashboards,
            "selected_dashboard_permissions": selected_dashboard_permissions,
            "change_request_number": change_request_number,
        }
        st.rerun()


def display_dashboard_deployment_ui() -> None:
    change_request_number = get_change_request_number()

    if change_request_number:
        source_env, target_env = get_selected_accounts()
        asset_manager.set_source_env(source_env)
        asset_manager.set_target_env(target_env)

        dashboards = get_source_dashboards(asset_manager.source_env)
        groups = get_target_groups(asset_manager.target_env)
        dashboard_display_names, selected_dashboard_display_names = select_dashboards(dashboards)

        if selected_dashboard_display_names:
            selected_dashboards, selected_dashboard_permissions = display_permissions(
                dashboards, dashboard_display_names, selected_dashboard_display_names, groups
            )
            if all_keys_have_non_empty_values(selected_dashboard_permissions):
                if "deployment" in st.session_state:
                    deployment_info = st.session_state["deployment"]
                    with st.status("**Deploying Dashboards**", expanded=False) as status:
                        deploy_selected_dashboards(
                            deployment_info["selected_dashboards"], deployment_info["selected_dashboard_permissions"], deployment_info["change_request_number"]
                        )
                        status.update(label="**Completed Deploying Dashboards**", state="complete", expanded=True)
                else:
                    if st.button("Deploy Dashboards", type="primary", key="submit_dashboard_deployment"):
                        confirm_deployment(selected_dashboards, selected_dashboard_permissions, change_request_number)


def main() -> None:
    display_navigation_bar()
    display_dashboard_deployment_ui()


if __name__ == "__main__":
    main()

This works but the dialog does not close on its own. If i comment out the line:

deploy_selected_dashboards(
                            deployment_info["selected_dashboards"], deployment_info["selected_dashboard_permissions"], deployment_info["change_request_number"]
                        )

it works. I have tried so many ways to make this work that I am asking you experts for help please :slight_smile:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.