Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -2,72 +2,39 @@ import os
|
|
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 |
-
|
12 |
-
if 'username' not in st.session_state:
|
13 |
-
st.session_state.username = st.text_input("π€ Username", max_chars=50)
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
"π² Twenty-sided dice": {"sides": 20, "name": "d20", "emoji": "π²"},
|
20 |
-
"π² One hundred-sided dice": {"sides": 100, "name": "d100", "emoji": "π―"},
|
21 |
-
}
|
22 |
|
23 |
-
# Create a selectbox to choose the dice type
|
24 |
dice_type = st.selectbox("π² Select a dice type", options=list(dice_types.keys()), format_func=lambda x: x.split(' ')[-2])
|
25 |
-
|
26 |
-
# Create a slider to choose the number of rolls
|
27 |
num_rolls = st.slider("π² How many times do you want to roll the dice?", 1, 1000000, 100)
|
28 |
-
|
29 |
-
# Create a string with the rolls for the selected dice type
|
30 |
-
#orig: rolls = [str(pd.Series([dice_types[dice_type]["sides"]] * num_rolls).apply(pd.Series.sample, replace=True))]
|
31 |
-
#replaced:
|
32 |
-
rolls = pd.DataFrame([dice_types[dice_type]["sides"]] * num_rolls)[0].apply(pd.Series.sample, replace=True).tolist()
|
33 |
-
|
34 |
-
|
35 |
-
#rolls = [str(pd.Series([dice_types[dice_type]["sides"]] * num_rolls).apply(pd.Series.sample, replace=True))]
|
36 |
rolls = rolls[0].strip('[]').split('\n')
|
37 |
|
38 |
-
# Create a button to roll the dice
|
39 |
if st.button("π² Roll Dice"):
|
40 |
-
# Display the rolls
|
41 |
display_roll = [int(i) for i in rolls]
|
42 |
st.write(f"{dice_types[dice_type]['emoji']} You rolled {len(display_roll)} {dice_types[dice_type]['name']} dice: {', '.join(map(str, display_roll))}.")
|
43 |
-
|
44 |
-
# Check if the count is a multiple of the dice sides to see if there's a bonus match
|
45 |
-
bonus_match = None
|
46 |
-
count = sum(display_roll)
|
47 |
if count % dice_types[dice_type]['sides'] == 0:
|
48 |
bonus_match = dice_types[dice_type]['name']
|
49 |
-
bonus_emoji = dice_types[dice_type]['emoji']
|
50 |
st.write(f"π Bonus match! You rolled a multiple of {dice_types[dice_type]['sides']}.")
|
51 |
-
|
52 |
-
# Create a DataFrame with the rolls, count, and dice number of sides
|
53 |
df = pd.DataFrame({'Roll': rolls, 'Count': count, 'DiceNumberOfSides': dice_types[dice_type]['sides']})
|
54 |
-
|
55 |
-
# Check if the file exists
|
56 |
file_exists = os.path.exists("SharedState.csv")
|
|
|
|
|
57 |
|
58 |
-
# Append the new rolls to the file
|
59 |
-
if file_exists:
|
60 |
-
df.to_csv("SharedState.csv", mode='a', header=False, index=False)
|
61 |
-
# If the file doesn't exist, create it and write the headers
|
62 |
-
else:
|
63 |
-
df.to_csv("SharedState.csv", mode='w', header=True, index=False)
|
64 |
-
|
65 |
-
# Create a checkbox to download the roll history
|
66 |
download_roll_history = st.checkbox("π₯ Download Roll History", value=True)
|
67 |
-
|
68 |
-
# Check if the file exists and display it as a download link
|
69 |
if os.path.exists("SharedState.csv"):
|
70 |
-
|
|
|
|
|
|
|
71 |
|
72 |
st.write("""
|
73 |
|
|
|
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 'username' not in st.session_state: st.session_state.username = st.text_input("π€ Username", max_chars=50)
|
|
|
|
|
9 |
|
10 |
+
dice_types = {"π² Six-sided dice": {"sides": 6, "name": "d6", "emoji": "π²"},
|
11 |
+
"π² Thirty-sided dice": {"sides": 30, "name": "d30", "emoji": "π²"},
|
12 |
+
"π² Twenty-sided dice": {"sides": 20, "name": "d20", "emoji": "π²"},
|
13 |
+
"π² One hundred-sided dice": {"sides": 100, "name": "d100", "emoji": "π―"}}
|
|
|
|
|
|
|
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 = rolls[0].strip('[]').split('\n')
|
19 |
|
|
|
20 |
if st.button("π² Roll Dice"):
|
|
|
21 |
display_roll = [int(i) for i in rolls]
|
22 |
st.write(f"{dice_types[dice_type]['emoji']} You rolled {len(display_roll)} {dice_types[dice_type]['name']} dice: {', '.join(map(str, display_roll))}.")
|
23 |
+
bonus_match, count = (None, sum(display_roll))
|
|
|
|
|
|
|
24 |
if count % dice_types[dice_type]['sides'] == 0:
|
25 |
bonus_match = dice_types[dice_type]['name']
|
|
|
26 |
st.write(f"π Bonus match! You rolled a multiple of {dice_types[dice_type]['sides']}.")
|
|
|
|
|
27 |
df = pd.DataFrame({'Roll': rolls, 'Count': count, 'DiceNumberOfSides': dice_types[dice_type]['sides']})
|
|
|
|
|
28 |
file_exists = os.path.exists("SharedState.csv")
|
29 |
+
if file_exists: df.to_csv("SharedState.csv", mode='a', header=False, index=False)
|
30 |
+
else: df.to_csv("SharedState.csv", mode='w', header=True, index=False)
|
31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
download_roll_history = st.checkbox("π₯ Download Roll History", value=True)
|
|
|
|
|
33 |
if os.path.exists("SharedState.csv"):
|
34 |
+
if download_roll_history:
|
35 |
+
with open("SharedState.csv", "rb") as f:
|
36 |
+
st.download_button(label="π₯ Download Roll History", data=f, file_name="SharedState.csv", mime="text/csv")
|
37 |
+
else: st.warning("No roll history found.")
|
38 |
|
39 |
st.write("""
|
40 |
|