Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -2,41 +2,79 @@ import os
|
|
2 |
import pandas as pd
|
3 |
import streamlit as st
|
4 |
|
|
|
5 |
st.set_page_config(page_title="Dice Roller", page_icon="π²")
|
|
|
|
|
6 |
st.title("π² Dice Roller")
|
7 |
|
8 |
-
if
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
14 |
|
|
|
15 |
dice_type = st.selectbox("π² Select a dice type", options=list(dice_types.keys()), format_func=lambda x: x.split(' ')[-2])
|
16 |
-
num_rolls = st.slider("π² How many times do you want to roll the dice?", 1, 1000000, 100)
|
17 |
-
#rolls = [str(pd.Series([dice_types[dice_type]["sides"]] * num_rolls).apply(pd.Series.sample, replace=True))]
|
18 |
-
rolls = pd.Series([dice_types[dice_type]["sides"]] * num_rolls).apply(pd.Series.sample, replace=True).astype(str).tolist()
|
19 |
|
20 |
-
|
|
|
21 |
|
|
|
22 |
if st.button("π² Roll Dice"):
|
23 |
-
|
|
|
|
|
|
|
|
|
24 |
st.write(f"{dice_types[dice_type]['emoji']} You rolled {len(display_roll)} {dice_types[dice_type]['name']} dice: {', '.join(map(str, display_roll))}.")
|
25 |
-
|
|
|
|
|
|
|
26 |
if count % dice_types[dice_type]['sides'] == 0:
|
27 |
bonus_match = dice_types[dice_type]['name']
|
|
|
28 |
st.write(f"π Bonus match! You rolled a multiple of {dice_types[dice_type]['sides']}.")
|
29 |
-
|
|
|
|
|
|
|
|
|
30 |
file_exists = os.path.exists("SharedState.csv")
|
31 |
-
if file_exists: df.to_csv("SharedState.csv", mode='a', header=False, index=False)
|
32 |
-
else: df.to_csv("SharedState.csv", mode='w', header=True, index=False)
|
33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
download_roll_history = st.checkbox("π₯ Download Roll History", value=True)
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
40 |
|
41 |
st.write("""
|
42 |
|
|
|
2 |
import pandas as pd
|
3 |
import streamlit as st
|
4 |
|
5 |
+
# Set page title and icon
|
6 |
st.set_page_config(page_title="Dice Roller", page_icon="π²")
|
7 |
+
|
8 |
+
# Set page title
|
9 |
st.title("π² Dice Roller")
|
10 |
|
11 |
+
# Check if username is already set, if not, create a text input to set it
|
12 |
+
if 'username' not in st.session_state:
|
13 |
+
st.session_state.username = st.text_input("π€ Username", max_chars=50)
|
14 |
+
|
15 |
+
# Define the dice types
|
16 |
+
dice_types = {
|
17 |
+
"π² Six-sided dice": {"sides": 6, "name": "d6", "emoji": "π²"},
|
18 |
+
"π² Thirty-sided dice": {"sides": 30, "name": "d30", "emoji": "π²"},
|
19 |
+
"π² Twenty-sided dice": {"sides": 20, "name": "d20", "emoji": "π²"},
|
20 |
+
"π² One hundred-sided dice": {"sides": 100, "name": "d100", "emoji": "π―"},
|
21 |
+
}
|
22 |
|
23 |
+
# Create a function to build the rolls pandas series to be a list of dictionaries
|
24 |
+
def build_rolls(dice_type, num_rolls):
|
25 |
+
sides = dice_types[dice_type]["sides"]
|
26 |
+
rolls = pd.Series([sides] * num_rolls).apply(pd.Series.sample, replace=True)
|
27 |
+
rolls = rolls.tolist()
|
28 |
+
count = sum(rolls)
|
29 |
+
return [{"Roll": roll, "Count": count, "DiceNumberOfSides": sides} for roll in rolls]
|
30 |
|
31 |
+
# Create a selectbox to choose the dice type
|
32 |
dice_type = st.selectbox("π² Select a dice type", options=list(dice_types.keys()), format_func=lambda x: x.split(' ')[-2])
|
|
|
|
|
|
|
33 |
|
34 |
+
# Create a slider to choose the number of rolls
|
35 |
+
num_rolls = st.slider("π² How many times do you want to roll the dice?", 1, 1000000, 100)
|
36 |
|
37 |
+
# Create a button to roll the dice
|
38 |
if st.button("π² Roll Dice"):
|
39 |
+
# Build the rolls list of dictionaries
|
40 |
+
rolls = build_rolls(dice_type, num_rolls)
|
41 |
+
|
42 |
+
# Display the rolls
|
43 |
+
display_roll = [roll["Roll"] for roll in rolls]
|
44 |
st.write(f"{dice_types[dice_type]['emoji']} You rolled {len(display_roll)} {dice_types[dice_type]['name']} dice: {', '.join(map(str, display_roll))}.")
|
45 |
+
|
46 |
+
# Check if the count is a multiple of the dice sides to see if there's a bonus match
|
47 |
+
bonus_match = None
|
48 |
+
count = rolls[0]["Count"]
|
49 |
if count % dice_types[dice_type]['sides'] == 0:
|
50 |
bonus_match = dice_types[dice_type]['name']
|
51 |
+
bonus_emoji = dice_types[dice_type]['emoji']
|
52 |
st.write(f"π Bonus match! You rolled a multiple of {dice_types[dice_type]['sides']}.")
|
53 |
+
|
54 |
+
# Create a DataFrame with the rolls, count, and dice number of sides
|
55 |
+
df = pd.DataFrame(rolls)
|
56 |
+
|
57 |
+
# Check if the file exists
|
58 |
file_exists = os.path.exists("SharedState.csv")
|
|
|
|
|
59 |
|
60 |
+
# Append the new rolls to the file
|
61 |
+
if file_exists:
|
62 |
+
df.to_csv("SharedState.csv", mode='a', header=False, index=False)
|
63 |
+
# If the file doesn't exist, create it and write the headers
|
64 |
+
else:
|
65 |
+
df.to_csv("SharedState.csv", mode='w', header=True, index=False)
|
66 |
+
|
67 |
+
# Create a checkbox to download the roll history
|
68 |
download_roll_history = st.checkbox("π₯ Download Roll History", value=True)
|
69 |
+
|
70 |
+
# Check if the file exists and display it as a download link
|
71 |
+
if os.path.exists("SharedState.csv") and download_roll_history:
|
72 |
+
with open("SharedState.csv", "r") as f:
|
73 |
+
file_contents = f.read()
|
74 |
+
st.markdown(f"<a href='data:file/csv;base64,{file_contents.encode().b64encode()}'>π₯ Download Roll History</a>", unsafe_allow_html=True)
|
75 |
+
|
76 |
+
|
77 |
+
|
78 |
|
79 |
st.write("""
|
80 |
|