Skip to content

Instantly share code, notes, and snippets.

@thuwarakeshm
Created May 11, 2022 16:22
Show Gist options
  • Save thuwarakeshm/0cd6746437f558ebf3ca4ed99b678165 to your computer and use it in GitHub Desktop.
Save thuwarakeshm/0cd6746437f558ebf3ca4ed99b678165 to your computer and use it in GitHub Desktop.
streamlit2
# Imports
# -----------------------------------------------------------
import os
import streamlit as st
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings")
application = get_wsgi_application()
from django.contrib.auth import authenticate
def check_password():
"""Returns `True` if the user had a correct password."""
def password_entered():
"""Checks whether a password entered by the user is correct."""
user = authenticate(
username=st.session_state["username"], password=st.session_state["password"]
)
if user is not None:
st.session_state["password_correct"] = True
del st.session_state["password"] # don't store username + password
del st.session_state["username"]
# Add the user object to Streamlit session
st.session_state.user = user
else:
st.session_state["password_correct"] = False
if "password_correct" not in st.session_state:
# First run, show inputs for username + password.
st.text_input("Username", on_change=password_entered, key="username")
st.text_input(
"Password", type="password", on_change=password_entered, key="password"
)
return False
elif not st.session_state["password_correct"]:
# Password not correct, show input + error.
st.text_input("Username", on_change=password_entered, key="username")
st.text_input(
"Password", type="password", on_change=password_entered, key="password"
)
st.error("😕 User not known or password incorrect")
return False
else:
# Password correct.
return True
if check_password():
with st.sidebar:
report = st.selectbox(
"Choose a dashboard",
("Data Science Team KPI", "Finance Team KPI"),
)
st.write(f"Hello, {st.session_state.user.username}!")
# Data Science Dashboard Section
if report == "Data Science Team KPI":
if st.session_state.user.groups.filter(
name__in=["Manager", "Data Science Team"]
).exists():
st.title("Data Science KPI Dashboard")
else:
st.write("Sorry, you don't have permissions to view this dashboard!")
# Finance Dashboard Section
elif "Finance Team KPI":
if st.session_state.user.groups.filter(
name__in=["Manager", "Finance Team"]
).exists():
st.title("Finance KPI Dashboard")
else:
st.write("Sorry, you don't have permissions to view this dashboard!")
# Section when no dashboard is selected
else:
st.write("Please select a Dashboard")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment