Skip to content

Commit

Permalink
Add dashbaord functions, configs, fake secrets.
Browse files Browse the repository at this point in the history
  • Loading branch information
argythana committed Jun 16, 2024
1 parent 4daeb63 commit 4589d17
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/.streamlit/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Git ignore everything in this directory
*
# Except these files
!.gitignore
!config.toml

# In production, this file should not be pushed to Github.
!secrets.toml
31 changes: 31 additions & 0 deletions src/.streamlit/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[browser]
gatherUsageStats = false

[client]
toolbarMode = "viewer"

# default theme
[theme]
base="dark"
#primaryColor="#F63366"

backgroundColor="#000000" # Black

#secondaryBackgroundColor="#F0F2F6"
#textColor="#262730"

## Primary accent for interactive elements
#primaryColor = '#7792E3'

## Background color for the main content area
#backgroundColor = '#152630'

## Background color for sidebar and most interactive widgets
#secondaryBackgroundColor = '#bfbfbf'

## Color used for almost all text
#textColor = '#0693e3'

## Font family for all text in the app, except code blocks
# Accepted values (serif | sans serif | monospace)
font = "sans serif"
3 changes: 3 additions & 0 deletions src/.streamlit/secrets.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Add in gitignore in production
username = "r4m_worth"
password = "$millions"
43 changes: 43 additions & 0 deletions src/public_dashboard.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""
Create a version of a "Challenge" Dashboard for the public demo.
"""

import sys

import streamlit as st

from streamlit_auth import check_password

# Import modules from src when run from a folder next to src.
sys.path.append("../src")

from utils.all_plots_from_df import create_all_charts_from_feather_file
from utils.dashboard_plot_configs import (
hide_streamlit_instructions,
hide_streamlit_style,
)

if __name__ == "__main__":
# Request user authentication
if "authenticated" not in st.session_state:
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
st.session_state["authenticated"] = False
hide_streamlit_instructions()

# Check password
check_password()
# Initialize session state
if st.session_state["authenticated"]:
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
st.markdown(
"<h5 style='text-align: center; color: #0693e3;'>Run4More <strong>analytics</strong> Demo</h5>",
unsafe_allow_html=True,
)

# Dashboard from feather file
create_all_charts_from_feather_file("./data/acts_by_date_trend.feather")

else:
st.info(
"Please login by clicking on the Login button. Username: `r4m_worth`, Pwd: `$millions`."
)
59 changes: 59 additions & 0 deletions src/streamlit_auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import hashlib

import streamlit as st

from utils.dashboard_plot_configs import hide_streamlit_instructions


def hash_password(password: str) -> str:
"""
Hash the password.
Args: password: string.
Return: string.
"""
return hashlib.sha256(str.encode(password)).hexdigest()


def check_password() -> None:
"""
Check the password for the public dashboard.
return: None
"""

# Define your username and password hash
hide_streamlit_instructions()
correct_username = st.secrets["username"]
correct_password_hash = hash_password(st.secrets["password"])

# Create placeholders for the login inputs and button
username_placeholder = st.empty()
password_placeholder = st.empty()
button_placeholder = st.empty()
custom_message_placeholder = st.empty()

# Only display login inputs and button if the user is not authenticated
if not st.session_state.get("authenticated", False):
hide_streamlit_instructions()
# Input fields
username = username_placeholder.text_input("Username", key="username_input")
password = password_placeholder.text_input(
"Password",
key="password_input",
type="password",
)

# Check the password
if button_placeholder.button("Login"):
if (
username == correct_username
and hash_password(password) == correct_password_hash
):
st.session_state["authenticated"] = True
# Clear the placeholders after a successful login
username_placeholder.empty()
password_placeholder.empty()
button_placeholder.empty()
custom_message_placeholder.empty()
else:
st.error("Incorrect username or password.")
return None

0 comments on commit 4589d17

Please sign in to comment.