Solved

How to pass Snowflake credentials to DevReady environment?

  • 2 April 2024
  • 1 reply
  • 12 views

Badge

Hey @DataOps.live ,

I am curious to know more about how I can “connect” or pass my snowflake credentials to the DevReady environment and in particular a streamlit app that I am using.

 

Would you kindly share more?

 

Thanks

icon

Best answer by luke 2 April 2024, 16:29

View original

1 reply

Hey Mincho!

Thanks for your question.

DevReady can access your CICD variables and are included in the environment as variables.

You can include any git controlled variable in the DevReady workspace by adding it to:

~/.dataops/develop/env/snowflake-oauth.env

As a Streamlit developer, you can use Python to access these variables authentication credentials.

Note: This requires the credentials to be set in the gitpod environment OR set as CICD variables in the project.

 

Here is an example Streamlit app that imports the environment variables:

import os
from snowflake.snowpark import Session
import streamlit as st




st.set_page_config(
page_title="Example Streamlit",
layout="wide",
)




def connect_to_snowflake():
## Try to import required account credentials.
try:
username = os.environ["DATAOPS_SNOWFLAKE_USER"]
password = os.environ["DATAOPS_SNOWFLAKE_PASSWORD"]
account = os.environ["DATAOPS_SNOWFLAKE_ACCOUNT"]
database_name = os.environ["DATAOPS_DATABASE"]
except KeyError:
raise Exception("Could not find one or more required environment variables")


## Arrange the Snowflake connection values.
## ACTION REQUIRED: Update the `role`/`warehouse`/`database`/`schema` to match your solution.
## See dataops/snowflake/ for the current configuration.
connection_parameters = {
"account": account,
"user": username,
"password": password,
"role": f"DATA_SCIENTIST",
"warehouse": f"DS_WH",
"database": database_name,
"schema": "PUBLIC",
}


## Create the `session` object used to interact with Snowpark.
session = Session.builder.configs(connection_parameters).create()
return session




def main():
## Entrypoint to the Streamlit app.


## Add some page content.
st.title("Hello Snowflake - Streamlit Edition")
st.write(
"""The following data is from the accounts table in the application package.
However, the Streamlit app queries this data from a view called
code_schema.accounts_view.
"""
)


## Connect to Snowflake account.
session = connect_to_snowflake()


## Get the customer data.
data_frame = session.sql("SELECT * FROM example_schema.customer;")


## Execute the query and convert it into a Pandas data frame.
queried_data = data_frame.to_pandas()


## Display the Pandas data frame as a Streamlit data frame.
st.dataframe(queried_data, use_container_width=True)




if __name__ == "__main__":
main()

 

Note: All variables can be accessed through the command line tool gp env​ or just env​.

 

I hope this helps!

 

Luke

Reply