I have made streamlit under snowflake native apps, and the streamlit is working fine locally, but while going through snowflake native apps, it is not working, like it is not able to use the desired database that I wanted.
Here is the app.py
import streamlit as st
import pandas as pd
from snowflake.snowpark import Session
from snowflake.snowpark.context import get_active_session
def get_snowflake_session() -> Session:
try:
# Attempt to get the active session provided by the environment
session = get_active_session()
return session
except Exception as e:
st.error(f"Error getting active Snowflake session: {e}")
return None
def show_data_page():
st.title("Snowflake Data Viewer")
# Ensure credentials are stored in session_state
if 'user' in st.session_state:
try:
session = get_snowflake_session()
if session:
st.success("Successfully connected to Snowflake")
# Display current database and schema
st.write(f"Database: {st.session_state['database']}")
st.write(f"Schema: {st.session_state['schema']}")
# Perform a query to get tables in the specified schema
query1 = f"""
USE DATABASE {st.session_state['database']};
"""
query2 = f"""
SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'PUBLIC';
"""
st.write(f"Executing query: {query1}")
st.write(f"Executing next query: {query2}")
try:
# Execute the SQL query and retrieve the result directly into a Pandas DataFrame
st.write("Database Selection Query")
# session.sql(query1)._execute_and_get_query_id()
# Convert the result to a Pandas DataFrame
st.write("Table Creation:")
tables_df = session.sql(query2).to_pandas()
st.write("Table:")
st.write(tables_df)
# tables_df = result.to_pandas()
# st.write("Tables in Schema:", tables_df)
# Allow the user to select a table and display its contents
table_name = st.selectbox("Select a table to view data", tables_df['TABLE_NAME'])
if table_name:
table_query = f"SELECT * FROM {st.session_state['database']}.{st.session_state['schema']}.{table_name} LIMIT 10"
st.write(f"Executing query: {table_query}")
table_df = session.sql(table_query).to_pandas()
st.write(f"Data in {table_name}:", table_df)
except Exception as e:
st.error(f"Error executing query: {e}")
st.write("Debug Info: Could not retrieve table names or table data.")
session.close()
except Exception as e:
st.error(f"Error using Snowflake session: {e}")
else:
st.warning("You are not logged in. Please log in first.")
if __name__ == "__main__":
# Streamlit sidebar inputs for Snowflake credentials
st.sidebar.header("Snowflake Credentials")
user = st.sidebar.text_input("User")
password = st.sidebar.text_input("Password", type="password")
account = st.sidebar.text_input("Account")
warehouse = st.sidebar.text_input("Warehouse")
database = st.sidebar.text_input("Database")
schema = st.sidebar.text_input("Schema")
if st.sidebar.button("Connect to Snowflake"):
if user and password and account and warehouse and database and schema:
st.session_state['user'] = user
st.session_state['password'] = password
st.session_state['account'] = account
st.session_state['warehouse'] = warehouse
st.session_state['database'] = database
st.session_state['schema'] = schema
show_data_page()
else:
st.warning("Please enter all Snowflake credentials")
else:
st.warning("Please enter your Snowflake credentials and click the button to connect.")
# Show data page if connected
if 'user' in st.session_state:
show_data_page()
but in this the query part, when I mention a database or the session database it is throwing an error as database not exists or unauthorized, but it is showing snowflake is connected. And the same query is working under snowflake worksheets.
Can someone please guide me on how to proceed?
I was trying to do a login with the credentials and accept it and show the tables from the credentials, but it is not doing.
Saiprasath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.