Boost your visibility with Snowflake Native Apps

Experience the power of Snowflake Native Apps to drive new leads to your business.

Snowflake native apps dashboard

Note
As of February 2024, Snowflake Native Apps are only available on AWS.

Snowflake Native Applications enable developers to package their creations for easy consumption by other Snowflake users. The Snowflake Marketplace is a central hub where users can discover and install these innovative applications.

After developing your app, you can share it with your existing customers and leverage the Snowflake Marketplace to increase brand exposure and discoverability. This not only makes your app accessible to consumers but also generates new leads for your business.

As a consumer, is it safe to use Native Apps?

Yes, Snowflake Native Apps are designed with security in mind. They leverage the same robust security features and protocols as the Snowflake Data Cloud platform, ensuring your data and applications are protected.
Improvement:

The app will explicitly request access to your data and resources. For example, you must approve an application requesting UPDATE permissions. It’s essential to pay attention to the permissions the app is seeking. It’s best not to grant them if they seem unnecessary or inappropriate.

Rest assured that all your data remains within your environment. The provider cannot transfer your data to any other location. This ensures the security and integrity of your information.

What my app should do?

This depends on the nature of your business, but here are a few examples to consider:
- For a data security company, create an app to audit consumer data.
- For a vehicle parts supplier company, develop an app that scans car inventories and recommends replacements based on the lifespan of parts.

Explore the Snowflake Native App Marketplace for inspiration and to discover apps that could be valuable for your business needs.
https://app.snowflake.com/marketplace?shareType=application&sortBy=date_desc

Writing your app

As an app provider, let’s develop a straightforward application we can publish on Snowflake’s Marketplace later. This will allow consumers to install it on their own Snowflake accounts easily.

Let’s start by creating a simple project structure. You can view an example by cloning a simple project from Snowflake labs

git clone https://github.com/Snowflake-Labs/sfguide-getting-started-with-native-apps.git
Project structure

Let’s see how we can create a fresh app:

Under your project, create the following hierarchy:
app folder
→ data folder (will not be used in this demo)
→ src folder
→ test folder (will not be used in this demo)

Fresh project strucutre

Also, create the following files:
.gitignore

connection.json
.DS_Store # For MAC

LICENSE

                                 Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/

README.MD

# Getting Started with Snowflake Native Apps

Under the src folder create two additional folders:
libraries
scripts

Also, create the following file:

manifest.yml

#version identifier
manifest_version: 1 # this is the Snowflake defined manifest file version. If there are new configuration options or additions, the version number will change.

version:
name: V1 # this is a user-defined version for the application. This version identifier is used when creating the app package.
label: Version One
comment: The first version of the application

#artifacts that are distributed from this version of the package
artifacts: # this contains options and definitions for where various parts of our package is located. In particular the setup_script option is required.
setup_script: scripts/setup.sql
default_streamlit: app_instance_schema.streamlit
extension_code: true

#runtime configuration for this version
configuration: # this is used to define what logging we want to use in our application. During development we will want a log level of debug.
log_level: debug
trace_level: off

Under the scripts folder create setup.xml

-- ==========================================
-- This script runs when the app is installed
-- ==========================================

-- Create Application Role and Schema
create application role if not exists app_instance_role;
create or alter versioned schema app_instance_schema;

-- Create Streamlit app
create or replace streamlit app_instance_schema.streamlit from '/libraries' main_file='streamlit.py';


create or replace procedure app_instance_schema.update_reference(ref_name string, operation string, ref_or_alias string)
returns string
language sql
as $$
begin
case (operation)
when 'ADD' then
select system$set_reference(:ref_name, :ref_or_alias);
when 'REMOVE' then
select system$remove_reference(:ref_name, :ref_or_alias);
when 'CLEAR' then
select system$remove_all_references();
else
return 'Unknown operation: ' || operation;
end case;
return 'Success';
end;
$$;

-- Grant usage and permissions on objects
grant usage on schema app_instance_schema to application role app_instance_role;
grant usage on streamlit app_instance_schema.streamlit to application role app_instance_role;
grant usage on procedure app_instance_schema.update_reference(string, string, string) to application role app_instance_role;

Under the libraries folder create environment.yml

name: sf_env
channels:
- snowflake
dependencies:
- snowflake-native-apps-permission

And streamlit.py

import streamlit as st
import pandas as pd
import altair as alt
from snowflake.snowpark.context import get_active_session
import snowflake.permissions as permission
from sys import exit

st.set_page_config(layout="wide")
session = get_active_session()

st.title("Hello World!")

Now let’s create our App Package

Login to your Snowflake account and run the following queries

CREATE APPLICATION PACKAGE IDENTIFIER('"NATIVE_APP_QUICKSTART_PACKAGE"') 
COMMENT = 'TESTING NATIVE APPS' DISTRIBUTION = 'INTERNAL';

Notice that a new database was created to hold all of your app information

app db

Now run

-- when we create an app package, snowflake creates a database with 
-- the same name. This database is what we use
-- to store all of our packaged snowflake objects.
USE DATABASE NATIVE_APP_QUICKSTART_PACKAGE;

CREATE OR REPLACE SCHEMA NATIVE_APP_QUICKSTART_SCHEMA;
USE SCHEMA NATIVE_APP_QUICKSTART_SCHEMA;

-- we will upload our project files here, directly into our app package
CREATE OR REPLACE STAGE NATIVE_APP_QUICKSTART_STAGE
DIRECTORY = ( ENABLE = true )
COMMENT = '';

Navigate to Data -> Databases
Select NATIVE_APP_QUICKSTART_PACKAGE -> NATIVE_APP_QUICKSTART_SCHEMA -> Stages -> NATIVE_APP_QUICKSTART_STAGE

And click the ‘+ Files’ button

+ Files

Upload the manifest

Upload manifest

Upload the setup file to the scripts folder

Upload setup

Do the same for uploading streamlit.py

You might need to REFRESH but by the end, you should see the new structure

Files structure

To view our App Package in the Snowflake UI:

  1. Login to your Snowflake account and navigate to Apps
  2. In the top navigation, click Packages
  3. Click NATIVE_APP_QUICKSTART_PACKAGE
Packages view

Now let’s add our first version using our previously uploaded code.

Click Add first version.

Version

Type V1 for the version name.

Select NATIVE_APP_QUICKSTART_PACKAGE for the database.

Select NATIVE_APP_QUICKSTART_STAGE for the stage.

Select / for the directory.

V1

Create the first version.

Create V1

Click the DONE button

Install the App

The initial step to utilizing the application is to install it in the account. Ordinarily, this process involves clicking an install button in the Snowflake Marketplace. However, as we’re constructing the application and utilizing a single account to showcase both provider and consumer experiences, you’ll need to execute the following SQL to install the application in the account.

-- ################################################################
-- INSTALL THE APP IN THE ACCOUNT
-- ################################################################

USE DATABASE NATIVE_APP_QUICKSTART_DB;
USE SCHEMA NATIVE_APP_QUICKSTART_SCHEMA;
USE WAREHOUSE NATIVE_APP_QUICKSTART_WH;

-- This executes "setup.sql"; This is also what gets executed when installing the app
CREATE APPLICATION NATIVE_APP_QUICKSTART_APP
FROM application package NATIVE_APP_QUICKSTART_PACKAGE
using version V1 patch 0;
-- This might take a minute

-- At this point you should see the app NATIVE_APP_QUICKSTART_APP listed under Apps
SHOW APPLICATIONS;

Run the App

Navigate again to Apps->Your new app and click it

Hello world! is shown in Streamlit

In case you would like to update your streamlit.py

# Update your code
st.title("Hello World V2.1!")

And Upload the files that you changed

Patch/Version
Let’s now create a patch (or a new version to our app)

Navigate to Apps->Packages

Packages

And click your app name

Packages

Then click on your latest Package (in our case V1)

V1

You will see all of your patches

Patches

If you never did a patch, you will just see Patch 0

Click the + Patch button

Select the / for the path as before and click the Create button and Done

You will now be able to view the new patch

New patch

From our worksheet, let’s install the new patch by

-- After patch/release
DROP APPLICATION NATIVE_APP_QUICKSTART_APP;

CREATE APPLICATION NATIVE_APP_QUICKSTART_APP
FROM application package NATIVE_APP_QUICKSTART_PACKAGE
using version V1 patch 1;

Refreshing the Stramlit view should give you the new app version

Deploying from terminal

You may have noticed that manually uploading files from the UI isn’t very convenient. A much better approach is to use the Snowflake CLI. Below are the commands for macOS, but similar commands can be found in the Snowflake documentation for other systems.

Installation:

brew tap Snowflake-Labs/snowcli
brew install snowcli
snow --help
│ app Manage Native Apps in Snowflake │
│ connection Manages connections to Snowflake. │
│ object Manages Snowflake objects like warehouses and stages │
│ snowpark Manage procedures and functions. │
│ spcs Manages Snowpark services, pools, jobs, image registries, and image repositories. │
│ sql Executes Snowflake query. │
│ streamlit Manages Streamlit in Snowflake.

Configuring the connection:

snow connection add

Or you can add it by editing the configuration file. You can specify your password. But it is safer to use keypair for authentication.

sudo vi ~/.snowflake/config.toml

[options]
default_connection = "dev"

[connections]
[connections.dev]
account = "XXXXXXXX"
user = "YYYYY"
authenticator = "SNOWFLAKE_JWT"
private_key_path = "/Users/xxxxxxx/.ssh/snowflake_rsa_key.p8"
warehouse = "WH_XS"
export PRIVATE_KEY_PASSPHRASE=<your keypair passphrase>

Test your connection by:

snow connection test --connection "dev"

Deploy and run the app:

snow app run --connection dev

Upgrading existing application INFOSTRUX_APP_xxxx.
Your application (INFOSTRUX_APP_xxxx) is now live:
https://app.snowflake.com/XXXXX/yyyyy/#/apps/application/INFOSTRUX_APP_xxxx

Clicking on the link (press Command on Mac) would open a new tab with your app on Snowflake

Adding some stuff to your app

Let’s add some graphics to our app

First of all, let’s edit our main script streamlit.py

def run_streamlit():
# Import python packages
import streamlit as st
from snowflake.snowpark.context import get_active_session

import plotly.express as px
from skimage import io

img = io.imread('logo.jpeg')
fig = px.imshow(img)
fig.update_traces(hovertemplate = None,hoverinfo = "skip")
fig.update_layout(width=300, height=100, margin=dict(l=10, r=10, b=10, t=10))
fig.update_xaxes(showticklabels=False)
fig.update_yaxes(showticklabels=False)
fig.update_xaxes(showgrid=False, zeroline=False)
fig.update_yaxes(showgrid=False, zeroline=False)

st.plotly_chart(fig,config={'displayModeBar': False})

st.title('Data Checking')
st.divider()
st.header('Understand :orange[your] data')

st.subheader('Do you know all of your databases?')

options = [
"Yes",
"No"
]

know_data = st.radio(
"",
options,
index=0,
)

know_data = options.index(know_data)

st.divider()

message = 'Knowing your data = ' + str(know_data)
st.subheader(':orange[' + message + ' ]')

if __name__ == '__main__':
run_streamlit()

Add the libraries to your environment.yml file:

name: sf_env
channels:
- snowflake
dependencies:
- snowflake-native-apps-permission
- plotly
- scikit-image

Add your logo.jpeg image to the same folder where your py code is

Run your app:

snow app run --connection dev
App screenshot

Check out Infostrux’s GitHub repository; we’ll be publishing some examples of native app code there soon!

Summary

In this blog post, I detailed the process of crafting a basic Native App in Snowflake and instructions on updating it. For a hands-on experience showcasing data querying and dynamic chart visualization, check out the excellent lab provided by Snowflake at this link: https://quickstarts.snowflake.com/guide/getting_started_with_native_apps/#12

Useful Links
Background: https://www.snowflake.com/dca-thankyou/snowflake-native-app-bootcamp

Labs:
https://quickstarts.snowflake.com/guide/getting_started_with_native_apps/#0
https://quickstarts.snowflake.com/guide/native-app-chairlift/#0

To stay updated on more Snowflake-related posts, follow me at my Medium profile: Eylon’s Snowflake Articles.

I’m Eylon Steiner, Engineering Manager for Infostrux Solutions. You can follow me on LinkedIn.

Subscribe to Infostrux Medium Blog at https://blog.infostrux.com for the most interesting Data Engineering and Snowflake news. Follow Infostrux’s open-source efforts through GitHub.

--

--