Spaces:
Runtime error
Runtime error
Upload 4 files
Browse files- pages/Level_1 +0 -0
- pages/Level_2 +0 -0
- pages/Level_3 +0 -0
- pages/The_Leaderboard.py +157 -0
pages/Level_1
ADDED
|
File without changes
|
pages/Level_2
ADDED
|
File without changes
|
pages/Level_3
ADDED
|
File without changes
|
pages/The_Leaderboard.py
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import requests
|
| 5 |
+
import streamlit as st
|
| 6 |
+
from dotenv import load_dotenv
|
| 7 |
+
|
| 8 |
+
from modules.utils import set_sidebar
|
| 9 |
+
|
| 10 |
+
load_dotenv()
|
| 11 |
+
|
| 12 |
+
PANTRY_ID = os.environ.get("PANTRY_ID")
|
| 13 |
+
PANTRY_BASKET = os.environ.get("PANTRY_BASKET")
|
| 14 |
+
assert (
|
| 15 |
+
PANTRY_ID is not None and PANTRY_BASKET is not None
|
| 16 |
+
), "Pantry ID and basket name must be set in .env file."
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
PAGE_TITLE = "The Leaderboard"
|
| 20 |
+
|
| 21 |
+
pd.set_option("future.no_silent_downcasting", True)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def _user_passed_level(df: pd.DataFrame, name: str, level: str) -> bool:
|
| 25 |
+
return df.loc[df["Name"] == name, level].values[0] == "✅"
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def main():
|
| 29 |
+
st.set_page_config(
|
| 30 |
+
page_title=PAGE_TITLE,
|
| 31 |
+
page_icon="assets/effixis_logo.ico",
|
| 32 |
+
layout="centered",
|
| 33 |
+
)
|
| 34 |
+
set_sidebar()
|
| 35 |
+
|
| 36 |
+
st.title(PAGE_TITLE)
|
| 37 |
+
|
| 38 |
+
st.markdown(
|
| 39 |
+
"""
|
| 40 |
+
### *Welcome to the Leaderboard!*
|
| 41 |
+
Here you can submit your keys and see how you compare to others!
|
| 42 |
+
"""
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
# Display leaderboard
|
| 46 |
+
leaderboard_url = (
|
| 47 |
+
f"https://getpantry.cloud/apiv1/pantry/{PANTRY_ID}/basket/{PANTRY_BASKET}"
|
| 48 |
+
)
|
| 49 |
+
leaderboard_response = requests.get(leaderboard_url)
|
| 50 |
+
if leaderboard_response.status_code == 200:
|
| 51 |
+
leaderboard_json = leaderboard_response.json()
|
| 52 |
+
print(f"Leaderboard data: {leaderboard_json}")
|
| 53 |
+
leaderboard_data = (
|
| 54 |
+
pd.DataFrame(leaderboard_json)
|
| 55 |
+
.transpose()
|
| 56 |
+
.rename(
|
| 57 |
+
columns={
|
| 58 |
+
"level 0": "Level 1",
|
| 59 |
+
"level 1": "Level 2",
|
| 60 |
+
"level 2": "Level 3",
|
| 61 |
+
},
|
| 62 |
+
)[["Level 1", "Level 2", "Level 3"]]
|
| 63 |
+
.fillna(False)
|
| 64 |
+
.map(lambda x: "✅" if x else "❌")
|
| 65 |
+
.assign(
|
| 66 |
+
# Weighted sum of the levels
|
| 67 |
+
Score=lambda df: df.apply(
|
| 68 |
+
lambda x: sum(
|
| 69 |
+
[int(passing == "✅") * (i + 1) for i, passing in enumerate(x)]
|
| 70 |
+
),
|
| 71 |
+
axis=1,
|
| 72 |
+
)
|
| 73 |
+
)
|
| 74 |
+
.sort_values(by="Score", ascending=False)
|
| 75 |
+
.reset_index()
|
| 76 |
+
.rename(columns={"index": "Name"})
|
| 77 |
+
)
|
| 78 |
+
st.dataframe(leaderboard_data)
|
| 79 |
+
else:
|
| 80 |
+
st.error("An error occurred while fetching the leaderboard.")
|
| 81 |
+
st.stop()
|
| 82 |
+
|
| 83 |
+
# Submit keys
|
| 84 |
+
with st.form("leaderboard"):
|
| 85 |
+
key = st.text_input("Enter your key here:")
|
| 86 |
+
email = st.text_input("Enter your email here:")
|
| 87 |
+
display_name = st.text_input("Enter your leaderboard display name here:")
|
| 88 |
+
st.markdown(
|
| 89 |
+
"*Note: Your email will not be displayed on the leaderboard, it is only used to contact you if you win!*"
|
| 90 |
+
)
|
| 91 |
+
submit = st.form_submit_button("Submit")
|
| 92 |
+
|
| 93 |
+
if submit and key and email and display_name:
|
| 94 |
+
if (
|
| 95 |
+
display_name in leaderboard_json.keys()
|
| 96 |
+
and email != leaderboard_json[display_name]["email"]
|
| 97 |
+
):
|
| 98 |
+
st.error(
|
| 99 |
+
"This display name is already taken, please choose another one."
|
| 100 |
+
)
|
| 101 |
+
else:
|
| 102 |
+
if key not in {
|
| 103 |
+
os.environ.get("LEVEL_1_KEY"),
|
| 104 |
+
os.environ.get("LEVEL_2_KEY"),
|
| 105 |
+
os.environ.get("LEVEL_3_KEY"),
|
| 106 |
+
}:
|
| 107 |
+
st.error("Invalid key!")
|
| 108 |
+
st.stop()
|
| 109 |
+
|
| 110 |
+
if display_name not in leaderboard_json.keys():
|
| 111 |
+
data = {
|
| 112 |
+
display_name: {
|
| 113 |
+
"email": email,
|
| 114 |
+
"level 0": key == os.environ.get("LEVEL_1_KEY"),
|
| 115 |
+
"level 1": key == os.environ.get("LEVEL_2_KEY"),
|
| 116 |
+
"level 2": key == os.environ.get("LEVEL_3_KEY"),
|
| 117 |
+
}
|
| 118 |
+
}
|
| 119 |
+
else:
|
| 120 |
+
data = {
|
| 121 |
+
display_name: {
|
| 122 |
+
"email": email,
|
| 123 |
+
"level 0": (
|
| 124 |
+
key == os.environ.get("LEVEL_1_KEY")
|
| 125 |
+
or _user_passed_level(
|
| 126 |
+
leaderboard_data, display_name, "Level 1"
|
| 127 |
+
)
|
| 128 |
+
),
|
| 129 |
+
"level 1": (
|
| 130 |
+
key == os.environ.get("LEVEL_2_KEY")
|
| 131 |
+
or _user_passed_level(
|
| 132 |
+
leaderboard_data, display_name, "Level 2"
|
| 133 |
+
)
|
| 134 |
+
),
|
| 135 |
+
"level 2": (
|
| 136 |
+
key == os.environ.get("LEVEL_3_KEY")
|
| 137 |
+
or _user_passed_level(
|
| 138 |
+
leaderboard_data, display_name, "Level 3"
|
| 139 |
+
)
|
| 140 |
+
),
|
| 141 |
+
}
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
try:
|
| 145 |
+
updated_data = leaderboard_json | data
|
| 146 |
+
print(f"Updated data: {updated_data}")
|
| 147 |
+
_ = requests.post(leaderboard_url, json=leaderboard_json | data)
|
| 148 |
+
|
| 149 |
+
st.success(
|
| 150 |
+
"You should soon be able to see your name and your scores on the leaderboard! 🎉"
|
| 151 |
+
)
|
| 152 |
+
except Exception as e:
|
| 153 |
+
st.error(f"An error occurred while submitting your key: {e}")
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
if __name__ == "__main__":
|
| 157 |
+
main()
|