Issues with alerts

Hello there,

I am currently observing a really weird behavior of the alert function. I have been fighting with it now for days to get it solved and tries many different setups in my app. Unfortunately, I am not able to share my app due to confidentiality and was not able to reproduce it in a shareable application.

However, alerts are only working when being used for the first time in an app session. So something like:

alert(‘This is an alert!”)
alert(‘This is alert #2!”)

does not work. The first alert is shown as expected, but after clicking OK, which closes the alert successfully, nothing happens anymore. There is no Error or Exception showing up, it simply stops executing the code then… So even the code right after this snipped is not being executed.

Has anyone of you experienced the same or similar issues?

Thanks in advance!

Sorry I don’t know that I fully understand. Can you share the code that you are using?

It sounds like you are opening two alerts at the same time? If so, Anvil will only handle one alert at a time… but… if you do some thing like this, the alerts pop up one after each other.

Just make sure you have the alert returning to a variable ie foo = alert("First alert")

Or even better:

    result = alert(content="Choose Yes or No",
                  title="First Alert",
                  large=True,
                  buttons=[
                    ("Yes", "YES"),
                    ("No", "NO"),
                    ("Neither", None)
                  ])
    
    result2 = alert(content="Choose Yes or No",
               title="Second Alert",
               large=True,
               buttons=[
                 ("Yes", "YES"),
                 ("No", "NO"),
                 ("Neither", None)
               ])

You can then also use the result to action some code based on an if statement. The key is to capture the alert return so that it holds the code until the first alert finishes executing (ie the button is clicked) - I think I have explained that right.

Check out this:
https://anvil.works/docs/client/python/alerts-and-notifications#custom-popup-styles

Hi @rickhurlbatt,

thank you for your answer!

Actually, it should also work with the simple alert("test") call because it will always include the “OK”-button that is waiting for feedback, right? At least it works exactly as expected when I tried to rebuild only the alert part:

https://anvil.works/build#clone:ZBKN6JFQRJANFJGE=55UTIX6J3OFORBUNUL4HIKOY

My goal is not to show one alert after another in my final app. However, after one alert was raised in an app session, it is not possible to raise another one later on in the same session…
I also tried to integrate your code into my app. However, the same behavior… to make it more precise. When I use the following code:

    result = alert(content="Choose Yes or No",
              title="First Alert",
              large=True,
              buttons=[
                ("Yes", "YES"),
                ("No", "NO"),
                ("Neither", None)
              ])
    print('First alert was triggered: ', result)
    result2 = alert(content="Choose Yes or No",
           title="Second Alert",
           large=True,
           buttons=[
             ("Yes", "YES"),
             ("No", "NO"),
             ("Neither", None)
           ])
    print('Second alert was triggered: ', result2)

… the output is only:
First alert was triggered: NO

Thus, the second alert is not triggered anymore. It does not show up and also the code right after is not executed anymore. However, there is no Exception thrown that would help me to further evaluate the issue.
Unfortunately, I am not able to share the original app due to confidential content…

Is there any way of debugging this behavior?

Here’s a clone example of a minimum app that shows two alerts in sequence: https://anvil.works/build#clone:3CN2WTCZSRF2HUQP=536LXTF2S3JWM74RZPDGAUFV

Try running that and seeing if the two alerts show (they do for me). If they don’t, then it’s likely to be a browser issue. If they do, then it’s something else that’s going on in your app. Hard to say what, though, without knowing what other features your app is using.

Maybe try building the sample app up into something more like what your app does without the confidential content, until you start seeing the issue?

Thank you guys for your help! @jshaffstall, your code works as expected when I run the copy of your app…

However, I created an app based on my environment, where the issue occurs as well. The two alerts are called in the form_show function.

https://anvil.works/build#clone:BLJ2I2KFFGHPBXFY=E6A7GVHZ6HLZLH2TTI5KEJ2A

I can confirm the issue happens in your version. It’s likely something in your added Javascript libraries in Assets is conflicting with something that Anvil depends on. You’ve got both Bootstrap and JQuery in there, when Anvil already includes versions of those.

After clicking a button on the first alert, this error is showing up in the Javascript console:

Failed to execute 'postMessage' on 'DOMWindow': The target origin provided ('https://anvil.works') does not match the recipient window's origin ('https://sglnzf5azlybagmx.anvil.app').

That’s way outside my area of experience, though, but maybe it’ll trigger something for someone with more experience with the Javascript side of Anvil.

3 Likes

Hi @jshaffstall,

this might definitely be the issue. It has something to do with bootstrap 4… Anvil is using Bootstrap 3…Just read a view entries here in the forum about Bootstrap 4 and Anvil. Seems to be quite tricky. I will try my luck by using the Anvil versions from now on. Hope that will work…

Thanks!