Streamlit web App using SnowPark

sanjay kumar
4 min readJan 9, 2023

--

Idea behind the app is simple to create a portal for the students where they can provide feedback for their schools and labs

Before we start lets go through the basic but important elements of streamlit

“st.set_page_config” : Needs to be the very first code of block in the program , used to configure the title of the page .You can explore more about config here

“st.markdown” : This allow us to execute HTML,CSS and JAVASCRIPT in our streamlit web page . You can explore more about markdown here.

set unsafe_allow_html=True

“st.empty()” : First of all it creates a empty streamlit element which later on can be replaced with any element like sidebar, text_input e.t.c. as streamlit does not provide any delete or remove functionalities it can be used in place of them. You can explore more about empty() here.

Libraries to Import

import time
import streamlit as st
import streamlit.components.v1 as comp
import snowflake.connector
from snowflake.connector.pandas_tools import write_pandas

#Configure page_title for your app
st.set_page_config(
page_title="MyApp")

you can install snowflake connector from here : snowflake-connector .To use snowflake connector you must have a snowflake account you can signup here for a trial account : snowflake trial account

Setup Snowflake connection for the backend

#Snowflake connection
c=snowflake.connector.connect(
user='your_username',
password='your_password',
account='account_name', #mw25963.ap-south-1.aws something like this will be your account name
warehouse='default_warehouse'
)
cx=c.cursor() #cursor to execute snowflake commands
cx.execute('use role accountadmin') #you can use any other roles and database
cx.execute('CREATE database IF NOT EXISTS data')
cx.execute('use database data')
cx.execute('use schema public')

Creating tables in the database using snowpark

#function to create table and insert users data in the tables
def add_userdata(username,password):
cx.execute('use warehouse PC_MATILLION_WH')
cx.execute('CREATE TABLE IF NOT EXISTS userstable(username varchar,password varchar)')
cx.execute('INSERT INTO userstable(username,password) VALUES (%s, %s)', (username,password))
c.commit()

def login_user(username,password):
cx.execute('use warehouse PC_MATILLION_WH')

cx.execute('SELECT * FROM userstable WHERE username =%s AND password =%s',(username,password))
data = cx.fetchall()
print(data)
return data

Removing streamlit logo and rerun bar

#removing streamlit bottom logo and rerun bar
st.markdown("""
<style>
.css-9s5bis.edgvbvh3,
.css-1q1n0ol.egzxvld0,
.css-6awftf.e19lei0e1
{
visibility: hidden;
}
</style>
""",unsafe_allow_html=True)

Creating Home page and login section

def main():
teamtitle = st.empty()
teamt_clcik = teamtitle.markdown("<h1 style='text-align: center; color:black;'>MyApp</h1>",unsafe_allow_html=True)

menu = ["Home","Student Login","Student SignUp"]
#you can add more pages/choices if you want
choice = st.sidebar.selectbox("Menu",menu)

if choice == "Home":
st.image("home.jpg") #place your choice of background

elif choice == "Student Login":
#css style to display feedback form in the middle

live_style = """

<style>
.css-10trblm e16nr0p30
{
width: 100px;
position: relative;
display: flex;
flex: 1 1 0%;
flex-direction: column;
gap: 1rem;
position: relative;
top:80px;
left: 30px;
}
</style>

"""

if teamt_clcik:
teamtitle.empty()
secmsg = st.empty()
secmsg_click = secmsg.subheader("LoginSection")
if secmsg_click:
secmsg.empty()

username = st.sidebar.text_input("User Name")
password = st.sidebar.text_input("Password",type='password')


if st.sidebar.checkbox("Login"):
#css style to display feedback form in the middle
live_style = """

<style>
.css-1n76uvr
{
width: 100px;
position: relative;
display: flex;
flex: 1 1 0%;
flex-direction: column;
gap: 1rem;
position: relative;
top:-120px;
left: -50px;
}
</style>

"""
st.markdown(live_style, unsafe_allow_html=True)
#notice both live_style are adressing different classes
st.markdown(live_style, unsafe_allow_html=True)

# if password == '12345':

result = login_user(username,password)
if result:
sucmsg = st.empty()
sucmsg_click = sucmsg.success("Logged In as {}".format(username))
if sucmsg_click:
#replacing success message with empty because we want that message as pop up
#not as a label or header
sucmsg.empty()
time.sleep(1)

Creating feedback form after clicking on login button

def create_table():
cx.execute(
'CREATE TABLE IF NOT EXISTS feedback(date_submitted DATE, Q1 TEXT, Q2 TEXT, Q3 NUMBER, Q4 TEXT, Q5 TEXT, Q6 TEXT, Q7 TEXT, Q8 TEXT)')

def add_feedback(date_submitted, Q1, Q2, Q3, Q4, Q5, Q6, Q7, Q8):
cx.execute(
'INSERT INTO feedback (date_submitted,Q1, Q2, Q3, Q4, Q5, Q6, Q7, Q8) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s)',
(date_submitted, Q1, Q2, Q3, Q4, Q5, Q6, Q7, Q8))
c.commit()

st.markdown("<h1 style='text-align: center; color:black;'>Student Feedback Form</h1>", unsafe_allow_html=True)
#creating feedack form and framing questions you edit these questions according to your need
d = st.date_input("Today's date", None, None, None, None)

question_1 = st.text_input('what is your name')
question_2 = st.text_input('which state do you belong?')
question_3 = st.slider(
'Overall, how happy are you with the lesson? (1 very negative,2 negative,3 neutral ,4 positive,5 very positive)', 1,
5, 1)
question_4 = st.text_input('How is the teaching?')
question_5 = st.text_input('what do you think about the course content')
question_6 = st.text_input('How is the examination pattern?')
question_7 = st.text_input('How do you feel about the facilities in the lab?')
question_8 = st.text_input('What could have been better?', max_chars=100)

if st.button("Submit feedback"):
create_table()
add_feedback(d, question_1, question_2, question_3, question_4, question_5, question_6, question_7,
question_8)
st.success("Feedback submitted")

else:
st.warning("Incorrect Username/Password")
else:
st.header("You must login to view Feedback form ")

And at last we will design our sign up page

 elif choice == "Student SignUp":
#css style to display signup page in the middle
live_style = """

<style>
.css-1n76uvr
{
width: 100px;
position: relative;
display: flex;
flex: 1 1 0%;
flex-direction: column;
gap: 1rem;
position: relative;
top:10px;
left: -50px;
}
</style>

"""

st.markdown(live_style, unsafe_allow_html=True)
st.subheader("Create New Account")
new_user = st.text_input("Username")
new_password = st.text_input("Password",type='password')

if st.button("Signup"):

add_userdata(new_user,new_password)
st.success("You have successfully created a valid Account")
st.info("Go to Login Menu to login")



if __name__ == '__main__':
main()

Source code here

--

--