Using Vanna with a Streamlit front end

Ashish Singal
Vanna AI
Published in
2 min readJul 9, 2023

While we can use Vanna’s Python endpoints directly, it may be easier to use Streamlit as Vanna’s front end. Streamlit is a popular open source Python library that allows users to build front ends easily, in pure Python. It’s especially useful for data apps, like Vanna.

Check out the Vanna Streamlit app on Streamlit Cloud, and the code on Github.

The version on Streamlit cloud allows you to connect to our Snowflake instance and use a pre-trained Vanna organization to immediately start asking questions.

You can run Streamlit in your own Python environment, so it’s secure and doesn’t send any data back to Vanna.

Stepping through the code

Let’s go through important bits of the code (app.py). We primarily highlight the interactions with Vanna below.

First, we need to set our Vanna API key and organization. Details are in Getting started with Vanna.

vn.api_key = st.secrets['vanna_api_key']
vn.set_org('demo-sales')

After a new question is asked, the first step is to generate the SQL —

with st.spinner('Generating SQL...'):
sql = vn.generate_sql(question=my_question)

Next, we connect to our Snowflake and run the SQL generated above —

with st.spinner('Running SQL...'):
conn = snowflake.connector.connect(
user=st.secrets['snowflake_user'],
password=st.secrets['snowflake_password'],
account=st.secrets['snowflake_account'],
database=st.secrets['snowflake_default_database'],
)

cs = conn.cursor()

df = vn.get_results(cs, st.secrets['snowflake_default_database'], sql)

And finally, we generate the Plotly code and execute it.

with st.spinner('Generating Plotly Code...'):
plotly_code = vn.generate_plotly_code(question=my_question, sql=sql, df=df)

with st.spinner('Running Chart...'):
fig = vn.get_plotly_figure(plotly_code=plotly_code, df=df)

Feel free to modify the Streamlit app to suit your needs!

--

--