Streamlit database access error from snowflake native apps

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.

New contributor

Saiprasath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật