Have only 1 database connection per user at a time (Dash)

Hi everyone, I’m working on a multi-page dash app. On the backend I have a postregresql database, and on the front end I have several pages where users can visualize data. In each of these files, in the beginning I open a connection, and then do all the querying in that file using that connection. Thus, I have multiple connections, so when a user uses the website, they will likely have many connections open.

I thought about closing the connections after they are used, but I’m unsure how to do this. On the index page, there are no connections opened, since that is just used for switching to different tabs/pages. I was thinking of somehow closing the connection when the user switches to another page. Can someone point me in the right direction of implementing this?

Basically the structure is like
index.py
→ A.py
→ B.py
→ C.py

Each of A, B, C all open connections in the beginning, but currently don’t close them. Looking at A in particular, it opens a connection to the database in the beginning, but I’m unsure where exactly to close the connection, since index.py is the file which “switches” pages. Or is there a way to make the connection in index.py and not in the A, B, C files?

Thank you!