André Costa, Just checking in to see if you had got a chance to see the previous response by Amira Bedhiafi. If the answer helped (pointed, you in the right direction) > please click Accept Answer - it will benefit the community to find the answers quickly.
Just to clarify, are you leveraging Azure App Service WebApp?
Adding to Amira's response, based on my understanding of your issue description, that the issue consistently occurs on the initial access or after some idle time, but after 2-3 refreshes, the page loads without any issues, and the app works fine moving forward.
To isolate the issue if it's happening at WebApp end or Database end and fetch more details about the issue, you may try these:
For App Service Webapp, enable Always On: From Azure Portal – Your WebApp > Configuration blade (> General settings).
When Always On isn't turned on (default), the app is unloaded after 20 minutes without any incoming requests. The unloaded app can cause high latency for new requests because of its warm-up time. When Always On is turned on, the front-end load balancer sends a GET request to the application root every five minutes. The continuous ping prevents the app from being unloaded.
You may leverage App Service diagnostics from Azure Portal> Navigate to your App Service app in the Azure Portal. (screenshot below). https://learn.microsoft.com/azure/app-service/overview-diagnostics
If you haven't done this already, As suggested, you may check the connection pool settings in your Flask app, and double check if it's configured to maintain a persistent connection to the database.
Also, you to isolate further, try increasing the timeout value for the connection pool.
Kindly let us know how it goes, I'll follow-up with you further.