How to read data in an app

Hi my question is very simple I would like to create a Streamlit app where (for example) i can read a SQL database with password/username hided.

When i tried to deploy this application, I got an error message

ModuleNotFoundError: This app has encountered an error. The original error message is redacted to prevent data leaks. Full error details have been recorded in the logs (if you're on Streamlit Cloud, click on 'Manage app' in the lower right of your app).
Traceback:
File "/mount/src/test/app.py", line 1, in <module>
    from sqlalchemy import create_engine

BUT it works fine when i run it locally !!!

How can i do this to work ???

Thanks for your help

environment.yml file contains:

streamlit==1.24.1
pandas==2.2.3
SQLAlchemy==2.0.35

For that I have created an app.py where i do (I just want to read my data) :

from sqlalchemy import create_engine
import pandas as pd
import streamlit as st

@st.cache_data
# Fonction permettant de lire la base de données SQL
def lecture_bdd(DATABASE, table, colonnes=None, condition=None):
    SERVER = 'XXXXXXXXX.database.windows.net'
    USERNAME = 'XXXXXXXXXX'
    PASSWORD = 'XXXXXXXXXXXX'
    DRIVER = 'ODBC+Driver+17+for+SQL+Server'
    connection_string = f'mssql+pyodbc://{USERNAME}:{PASSWORD}@{SERVER}:1433/{DATABASE}?driver={DRIVER}'
    engine = create_engine(connection_string)
    # Requête SQL
    query = f"SELECT {', '.join(colonnes) if colonnes else '*'} FROM {table}"
    if condition:
        query += f" WHERE {condition}"
    # Lire et concaténer les chunks de manière progressive
    df_final = pd.DataFrame()
    for chunk in pd.read_sql(query, engine, dtype_backend='pyarrow', chunksize=100000):
        # Filtrer les colonnes vides dans le chunk
        chunk_filtered = chunk.dropna(how='all', axis=1)
        df_final = pd.concat([df_final, chunk_filtered], ignore_index=True)
    return df_final

df = lecture_bdd("data", "main")
df

You have to fill in your dependencies in the requirements.txt file.

1 Like