NameError: name 'get_active_session' is not defined

Getting NameError: name ‘get_active_session’ is not defined error while running my code on streamlit:

import streamlit as st

from snowflake.snowpark.context import get_active_session

#import get_active_session

from snowflake.snowpark.functions import col

Write directly to the app

st.title(“:cup_with_straw: Customize Your Smoothie! :cup_with_straw:”)
st.write(
“”“Choose the fruits you want in your custom Smoothie!
“””)
#import streamlit as st

name_on_order = st.text_input(“Name on Smoothie:”)
st.write(“The name on your Smoothie will be:”, name_on_order)

from snowflake.snowpark.functions import col

#def get_active_session():
session = get_active_session()
my_dataframe = session.table(“SMOOTHIES.PUBLIC.FRUIT_OPTIONS”).select(col(‘FRUIT_NAME’))
st.dataframe(data=my_dataframe, use_container_width=True)

ingredients_list = st.multiselect(‘Choose up to 5 ingredients:’
, my_dataframe, max_selections=5
)

if ingredients_list:
ingredients_string = ‘’

for fruit_chosen in ingredients_list:
    ingredients_string += fruit_chosen + ' '

#st.write(ingredients_string)

my_insert_stmt = """ insert into smoothies.public.orders(ingredients, name_on_order) 
values ('""" + ingredients_string + """','""" + name_on_order + """' )"""

time_to_insert = st.button('Submit Order')
if time_to_insert:
    session.sql(my_insert_stmt).collect()

    st.success('Your Smoothie is ordered!', icon="✅")

st.write(my_insert_stmt)
st.stop()
##if ingredients_string:
 #   session.sql(my_insert_stmt).collect()
  #  st.success('Your Smoothie is ordered!', icon="✅")

cnx = st.connection(“snowflake”)
session = cnx.session()

Please help

Hi @lalit_phougat, welcome to the forum!

That get_active_session method will only work if your app is deployed on Snowflake, but the st.connection method you do at the end should work fine either locally, or on Snowflake, or on Community Cloud.

Can you try moving that to the top of your app, instead of using get_active_session?

Also, as a side note, when you post your code on the forum, can yo uplease put it in a code block, by doing this, except with no spaces between the ` marks:

` ` `
Put code here
` ` `

This will make it easier for others to read and run your code.

Thanks Blackary, it is working fine now.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.