- May 29, 2024
- Posted by: Samuel R
- Category: Snowflake
Introduction:
This blog provides instructions for Introduction of Snowflake CLI, Installation process, Connecting to Snowflake and deploy the Streamlit app using snowflake CLI.
- What is Snowflake CLI?
- How does Snowflake CLI differ from SnowSQL?
- How to Install snowflake CLI?
- How to add credentials using a Snowflake CLI connection command?
- How to test a connection?
- Creating a Streamlit app
- Create the project definition for a Streamlit app
- How to deploy a Streamlit app?
- How to get the URL for a deployed Streamlit app
- How to share a Streamlit app?
What is Snowflake CLI?
Snowflake CLI is an open-source command-line tool explicitly designed for developer-centric workloads in addition to SQL operations. It is a flexible and extensible tool that can accommodate modern development practices and technologies.
With Snowflake CLI, developers can create, manage, update, and view apps running on Snowflake across workloads such as Streamlit in Snowflake, the Snowflake Native App Framework, Snowpark Container Services, and Snowpark. It supports a range of Snowflake features, including user-defined functions, stored procedures, Streamlit in Snowflake, and SQL execution.
How does Snowflake CLI differ from SnowSQL?
SnowSQL is the command-line client for connecting to Snowflake to execute SQL queries and perform all DDL and DML operations, including loading data into and unloading data out of database tables.
The Snowflake CLI command-line client, in contrast, focuses primarily on managing workloads and applications that connect to Snowflake. Snowflake CLI lets you locally run and debug Snowflake apps, with the following benefits:
- You can search, create, and upload Python packages that might not be supported in Anaconda yet.
- Snowflake CLI supports Snowpark Python user-defined functions and stored procedures, warehouses, and Streamlit apps.
- You can define packages by using requirements.txt, with dependencies automatically added through integration with Anaconda at deployment time.
- Snowflake CLI can include packages that are identified in requirements.txt—but aren’t yet in Anaconda—in the application package deployed to Snowflake. (This feature only works with packages that don’t rely on native libraries).
- When you update existing applications, code and dependencies are automatically altered as needed.
- Deployment artifacts are automatically managed and uploaded to Snowflake stages.
Snowflake plans to continue enhancing Snowflake CLI to provide developers a robust tool for leveraging all the SnowSQL capabilities in a new open source CLI.
How to install Snowflake CLI using pip?
Note: To install Snowflake CLI using pip, you must have Python version 3.8 or later installed.
- Run the command python –version to make sure you have the python version 3.8 or later.
- To install the python packages separately, create the python virtual environment using the below command.
python -m venv .streamlit_venv
- To activate the virtual environment, go to the below path and run activate.
- To install Snowflake CLI using pip, Run the following shell command:
pip install snowflake-cli-labs
- To verify that the software was installed successfully, run the following command:
snow –help
How to add credentials using a Snowflake CLI connection command?
Note: Create snowflake account before you are trying to add the connection in snowflake CLI.
- Enter the following shell command:
snow connection add
- When prompted, supply the required connection, account, username parameters, and any desired optional parameters:
Note:
- You can give any name in “Name for this connection”
- “Snowflake account name” should be included with the region name
example – cjb67246.us-east-1
- “Snowflake account name” should be your Login username of snowflake cloud.
- Other options are optional to give.
- Name for this connection: <connection-name>
- Snowflake account name: <account-name>
- Snowflake username: <user-name>
- Snowflake password [optional]: <password-value>
- Role for the connection [optional]: <role-name>
- Warehouse for the connection [optional]: <warehouse-name>
- Database for the connection [optional]: <database-name>
- Schema for the connection [optional]: <schema-name>
- Connection host [optional]: <host-name>
- Connection port [optional]: <port-value>
- Snowflake region [optional]: <region-name>
How to test a connection?
- Give the below command to test the connection, replace your connection name in double codes.
snow connection test –connection=”Streamlit1″
Creating a Streamlit app:
- Use the below command to create the Streamlit app, Give your Streamlit app name Inside the double codes.
snow streamlit init “my-streamlit-app”
- To set your default connection, use the below command.
snow connection set-default “Streamlit2”
Note: To find the list of connection – “snow connection list”
Create the project definition for a Streamlit app:
- Once you created the Streamlit app, the below folder will be generated in your python virtual environment.
- Now you have the modify the snowflake.yml file.
The following shows a sample snowflake.yml project definition file:
- In “name”, you must give the Streamlit app name.
- In “stage”, you must give the named stage name for deploy the Streamlit app.
- In “query_warehouse”, you must give the warehouse name to process the code.
- In “main_file”, you have to give the python main program file name.
- Copy the below sample code and paste inside the streamlit_app.py python file for code deployment.
import streamlit as st
import numpy as np
import pandas as pd
if st.checkbox(‘Show dataframe’):
chart_data = pd.DataFrame(
np.random.randn(20, 3),
columns=[‘a’, ‘b’, ‘c’])
chart_data
How to deploy a Streamlit app?
- Run run the below command to deploy the app.
snow streamlit deploy
- After completing the deployment, it will show you the URL to access the same in browser.
How to get the URL for a deployed Streamlit app?
To get an app URL, do the following:
- Ensure your connection specifies the database and schema where your app is deployed.
- Enter a command similar to the following, use your app name instead of my_streamlit_app
snow streamlit get-url my_streamlit_app
You can use the command to return the URL and open the app automatically in your default browser by using the –open option, similar to the following:
snow streamlit get-url my_streamlit_app –open
How to share a Streamlit app?
Privileges required to create and use a Streamlit app.
Streamlit apps are schema-level objects.
- To create and edit a Streamlit app by using Streamlit in Snowflake, you must use a role that has either the OWNERSHIP privilege on the schema, or both of the following privileges:
Granted on the database that contains the Streamlit app:
- USAGE
Granted on the schema that contains the Streamlit app:
- USAGE
- CREATE STREAMLIT
- CREATE STAGE
Creating the Role:
- CREATE ROLE APP_DEVELOPER;
Assigned the role to the User:
- GRANT ROLE APP_DEVELOPER TO USER SAMUELRAJKUMAR
Assigned the required Privileges to that role:
- GRANT USAGE ON SCHEMA SNOWFLAKE_CLI.STREAMLIT_APP TO ROLE APP_DEVELOPER;
- GRANT USAGE ON DATABASE SNOWFLAKE_CLI TO ROLE APP_DEVELOPER;
- GRANT CREATE STREAMLIT ON SCHEMA SNOWFLAKE_CLI.STREAMLIT_APP TO ROLE APP_DEVELOPER;
- GRANT CREATE STAGE ON SCHEMA SNOWFLAKE_CLI.STREAMLIT_APP TO ROLE APP_DEVELOPER;
- To share a Streamlit app from the stage, enter the following command in snowflake CLI:
snow streamlit share my-app APP_DEVELOPER
To manage the Streamlit App:
- To list the Streamlit apps for which you have access, use the “snow object list Streamlit” command.
- To display details about a Streamlit app, use the “snow object describe streamlit app_name” command.
- To delete a Streamlit app, use the “snow object drop streamlit app_name” command.
CONCLUSION:
In this use case, we have seen about snowflake CLI and how it differs from Snow SQL, and the installation process of snowflake CLI. Adding the snowflake connection in Snowflake CLI and test the connection for the same. And detail explanation about creating the Streamlit app along with the deployment process. Overall, using snowflake CLI we can do our application deployment easily from Streamlit to snowflake.
Cittabase is a select partner with Snowflake. Please feel free to contact us regarding your Snowflake solution needs. Our snowflake solutions encompass a suite of services for your data integration and migration needs. We are committed to providing personalized assistance and support customized to your requirements.