Streamlit and MongoDB: Storing Your Data in the Cloud | by Alan Jones | Aug, 2023

Streamlit lets you deploy your public apps to their cloud for free, but any files or databases you create locally will cease to exist when the app finishes. This may not be the behaviour that you want, so we are going to explore a solution using MongoDB.

For many apps losing this data is no problem. For example, if you have designed a dashboard that reads data from an external source any data that you generate is likely to be temporary and only needed while the app is running.

But, as I noted when developing my app for the article, Simple Surveys with Streamlit, if the app itself is generating data that needs to be stored, it’s not so straightforward. In that app, I stored the data in a local file but in a cloud-based deployment these will cease to exist when the app stops running — the proper solution is to use an external data store.

We are going to see how we can achieve this with MongoDB, but there are alternatives.

What are the choices?

In the Streamlit documentation, there are guides for connecting to various databases and cloud storage suppliers. They basically break down into three areas: data buckets such as AWS S3 and Google Cloud Storage, where you can store anything; SQL databases like Microsoft’s SQL Server, MySQL, PostgreSQL; and NoSQL databases of which Firestore and MongoDB are examples. For each type, you obviously need access to a server that hosts that particular database.

To be perfectly honest, I’m not the biggest fan of SQL. It seems to me that there is a disjoint between SQL code and Python that feels uncomfortable. (Having said that I definitely appreciate the power and convenience of SQL and have written about it here, here and here.)

But, NoSQL databases like MongoDB feel like they fit more neatly into the Python way of doing things.

I’m sure there are all sorts of arguments about speed, efficiency, ease of use, security and I don’t know what else. But I’m not going there…

Source link

Leave a Comment