Sitemap

Snowflake Cortex Analyst + Plotly: A Seamless Way to Visualize Data

3 min readNov 9, 2024

For business teams, waiting on traditional dashboards can mean missed opportunities and delayed decisions. Snowflake’s Cortex Analyst offers a new way forward — empowering users to ask questions in plain English and receive immediate, meaningful insights from their data. By integrating Cortex Analyst with Plotly (OR Any Data Visualization Library), your team can create dynamic, self-service dashboards that evolve with your needs

What is Snowflake Cortex Analyst?

Cortex Analyst, built with powerful AI models like Meta’s Llama and Mistral, is designed for structured data interaction. It empowers business users to access insights without SQL, providing a natural language interface for fast, accurate answers. By integrating Cortex Analyst with Plotly, we can produce dynamic charts directly from natural language queries, transforming insights into interactive visuals.

Walkthrough

Let’s dive into the core elements of the code, how we set up Cortex Analyst, and how Plotly enables visualizations.

Setting Up Cortex Analyst

Our setup script, Cortex-Analyst-Setup.sql, initializes Cortex Analyst on Snowflake, creating the necessary structures and configuring the semantic model. The script includes the following key elements:

-- Setting up Cortex Analyst and semantic models
CREATE OR REPLACE MODEL cortex_model USING
(
MODEL_TYPE = 'llama', -- Specify AI model
TARGET_SCHEMA = 'data_schema',
TARGET_TABLE = 'data_table'
);

This block initiates a Cortex model, using llama as the model type. The TARGET_SCHEMA and TARGET_TABLE parameters specify the schema and table containing the data.

Streamlit & Plotly for Visualizations

With Cortex Analyst configured, we move to the Python-based interface powered by Streamlit and Plotly. In Streamlit_SiS.py, we capture user input and generate the visual output with Plotly:

import streamlit as st
import plotly.express as px
import pandas as pd

# Load data and create interactive plot
df = pd.read_csv("Medical_books_clean.csv") # Load our sample dataset
fig = px.scatter(df, x="Author", y="Price", color="Genre") # Example plot
st.plotly_chart(fig)

In this snippet:

  1. We load the dataset (Medical_books_clean.csv) to visualize it.
  2. Using Plotly’s scatter method, we create an interactive scatter plot, displaying price by author and grouping by genre.
  3. Streamlit’s plotly_chart renders the Plotly figure on the dashboard, making it easy to explore the data interactively.

Connecting Natural Language Queries with Cortex Analyst

One of Cortex Analyst’s strengths is its text-to-SQL capability. The model translates queries into SQL, pulling relevant data for visualization. Here’s how a query might look in Streamlit:

query = st.text_input("Enter your query:")
if query:
sql_query = generate_sql(query) # function to translate natural language to SQL
results = run_query(sql_query) # function to execute SQL on Snowflake
st.write(results)

With generate_sql, natural language queries are translated into SQL. The run_query function then runs the query on Snowflake, fetching the data to be displayed in Streamlit.

Why This Matters

Cortex Analyst + Plotly brings a new level of simplicity to data exploration. The setup reduces the time needed to get insights from data by automating complex SQL translation and providing an interactive, no-code interface. This means teams can focus on insights rather than on the technicalities of data retrieval and visualization.

Conclusion

Combining Cortex Analyst and Plotly through Streamlit offers a powerful, streamlined experience for business users and data teams alike. Try it yourself to see how this integration can simplify your data workflows and bring your insights to life!

--

--

Sarathi Balakrishnan
Sarathi Balakrishnan

Written by Sarathi Balakrishnan

Cloud Data & AI/ML Leader with deep technical roots. I write about Gen AI, LLMs, data platforms, and how AI solves real-world business and healthcare problems.

Responses (2)