String url calls sqlalchemy and sqlalchemy calls for psycopg2

This code:

import pandas as pd
import plotly.express as px
import streamlit as st

@st.cache(allow_output_mutation=True)
def get_data():
df = pd.read_sql(‘diesel-price-by-regions’, ‘string-LINK-to-cloud-POSTGRES’)
return df

df = get_data()

#prepare thr chart
fig = px.line(df, x=‘date’, y=‘diesel_price’, color=‘region’,
title=‘Diesel price in the US’,
labels={‘date’:‘Date’,
‘diesel_price’:‘Price ($)’, ‘region’:‘Region’})

fig.update_layout(legend=dict(
yanchor=“top”,
y=0.99,
xanchor=“left”,
x=0.01
))

st.plotly_chart(fig)

Returns

File “/home/appuser/venv/lib/python3.10/site-packages/sqlalchemy/engine/create.py”, line 548, in create_engine
dbapi = dialect_cls.dbapi(**dbapi_args)
File “/home/appuser/venv/lib/python3.10/site-packages/sqlalchemy/dialects/postgresql/psycopg2.py”, line 811, in dbapi
import psycopg2
ModuleNotFoundError: No module named ‘psycopg2’

I saw many topics about the same issue but did understand the way to pull data from the cloud postgress without psycopg2.

How to deal with it?

Edit: disregard, I just realized you are talking about the cloud deployment.

Do you have psycopg2 in your requirements.txt?

Even though you’re not using psycopg2 explicitly, that’s what pandas uses under the hood for reading from a postgres db.

no, it is not there. And it is my problem to understand how to overcome this.
I solved the problem by transferring the service to Dash.

If you add the line psycopg2 to a file called requirements.txt, it will automatically install that when you deploy to Streamlit Cloud.

I tried and it seems like Streamlit Cloud rejects any calls related to activities related to hosting DBs.

It does not – we regularly use Streamlit Cloud to read from a wide variety of databases. Could you try following this tutorial here? Connect Streamlit to PostgreSQL - Streamlit Docs

huh, somehow I missed this part of the tutorial. Thanks. I’l play with it when I need quick release to the web

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