Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -5,6 +5,7 @@ import pandas as pd
|
|
5 |
import plotly.express as px
|
6 |
|
7 |
st.set_page_config(layout='wide')
|
|
|
8 |
st.title('π² Random Dice Game')
|
9 |
|
10 |
dice_types = [{'name': 'Six-sided Dice', 'sides': 6, 'emoji': 'π²'},
|
@@ -12,12 +13,9 @@ dice_types = [{'name': 'Six-sided Dice', 'sides': 6, 'emoji': 'π²'},
|
|
12 |
{'name': 'Thirty-sided Dice', 'sides': 30, 'emoji': 'π―'},
|
13 |
{'name': 'One Hundred-sided Dice', 'sides': 100, 'emoji': 'π―'}]
|
14 |
|
15 |
-
if 'username' not in st.session_state:
|
16 |
-
st.session_state.username = ''
|
17 |
-
if 'dice_roll_history' not in st.session_state:
|
18 |
-
st.session_state.dice_roll_history = pd.DataFrame()
|
19 |
-
|
20 |
def username_input():
|
|
|
|
|
21 |
st.text_input('π€ Username', key='username')
|
22 |
|
23 |
def display_dice_roll_distribution():
|
@@ -47,66 +45,29 @@ def display_bonus_match(bonus_match, bonus_dice_type, bonus_dice_emoji):
|
|
47 |
st.success(f'π You rolled a {bonus_dice_type} {bonus_dice_emoji} on the first roll!')
|
48 |
|
49 |
def update_dice_roll_history(dice_type, rolls):
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
if bonus_match is not None:
|
69 |
-
history['BonusMatchToDiceName'] = bonus_match
|
70 |
-
history['BonusMatchToDiceEmoji'] = bonus_emoji
|
71 |
-
|
72 |
-
# Add username to history
|
73 |
-
history['Username'] = st.session_state.username
|
74 |
-
|
75 |
-
st.session_state.dice_roll_history = history
|
76 |
-
return history
|
77 |
-
|
78 |
-
|
79 |
-
def display_dice_roll_results():
|
80 |
-
download_history = st.checkbox('π₯ Download Roll History', value=True)
|
81 |
-
|
82 |
-
if download_history:
|
83 |
-
csv_filename = f'dice_roll_history_{st.session_state.username}.csv'
|
84 |
-
st.markdown(f'π₯ [Download roll history]({csv_filename})')
|
85 |
-
dice_roll_history = st.session_state.dice_roll_history[['Roll', 'Count', 'DiceNumberOfSides']]
|
86 |
-
if st.checkbox('π Show Bonus Matches', value=True):
|
87 |
-
dice_roll_history = dice_roll_history.join(
|
88 |
-
st.session_state.dice_roll_history[['BonusMatchToDiceName', 'BonusMatchToDiceEmoji']])
|
89 |
-
if st.checkbox('π€ Show Username', value=True):
|
90 |
-
dice_roll_history['Username'] = st.session_state.dice_roll_history['Username']
|
91 |
-
dice_roll_history.to_csv(csv_filename, index=False)
|
92 |
-
|
93 |
-
dice_roll_history_files = [f for f in os.listdir() if f.startswith('dice_roll_history_') and f.endswith('.csv')]
|
94 |
-
if len(dice_roll_history_files) > 0:
|
95 |
-
st.write('π Roll History Files:')
|
96 |
-
for history_file in dice_roll_history_files:
|
97 |
-
st.markdown(f'[Download {history_file}]({history_file})')
|
98 |
-
|
99 |
-
st.sidebar.title('π§ Settings')
|
100 |
-
username_input()
|
101 |
-
|
102 |
-
if st.button('π² Roll Dice'):
|
103 |
-
dice_type, rolls = display_dice_roll_distribution()
|
104 |
-
display_bonus_match(update_dice_roll_history(dice_type, rolls), dice_type['name'], dice_type['emoji'])
|
105 |
-
|
106 |
-
display_dice_roll_results()
|
107 |
-
|
108 |
|
|
|
109 |
|
|
|
|
|
110 |
|
111 |
|
112 |
st.write("""
|
|
|
5 |
import plotly.express as px
|
6 |
|
7 |
st.set_page_config(layout='wide')
|
8 |
+
|
9 |
st.title('π² Random Dice Game')
|
10 |
|
11 |
dice_types = [{'name': 'Six-sided Dice', 'sides': 6, 'emoji': 'π²'},
|
|
|
13 |
{'name': 'Thirty-sided Dice', 'sides': 30, 'emoji': 'π―'},
|
14 |
{'name': 'One Hundred-sided Dice', 'sides': 100, 'emoji': 'π―'}]
|
15 |
|
|
|
|
|
|
|
|
|
|
|
16 |
def username_input():
|
17 |
+
if 'username' not in st.session_state:
|
18 |
+
st.session_state.username = ''
|
19 |
st.text_input('π€ Username', key='username')
|
20 |
|
21 |
def display_dice_roll_distribution():
|
|
|
45 |
st.success(f'π You rolled a {bonus_dice_type} {bonus_dice_emoji} on the first roll!')
|
46 |
|
47 |
def update_dice_roll_history(dice_type, rolls):
|
48 |
+
bonus_match = False
|
49 |
+
for dice in dice_types:
|
50 |
+
if rolls[0] == dice['sides']:
|
51 |
+
bonus_match = True
|
52 |
+
bonus_dice_type = dice['name']
|
53 |
+
bonus_dice_emoji = dice['emoji']
|
54 |
+
break
|
55 |
+
|
56 |
+
dice_roll_history = st.session_state.dice_roll_history
|
57 |
+
new_roll_data = pd.DataFrame({'Roll': rolls,
|
58 |
+
'Count': np.ones(len(rolls), dtype=np.uint64),
|
59 |
+
'DiceNumberOfSides': [dice_type['sides']] * len(rolls),
|
60 |
+
'Username': [st.session_state.username] * len(rolls)})
|
61 |
+
if bonus_match:
|
62 |
+
new_roll_data['BonusMatchToDiceName'] = [bonus_dice_type] * len(rolls)
|
63 |
+
new_roll_data['BonusMatchToDiceEmoji'] = [bonus_dice_emoji] * len(rolls)
|
64 |
+
dice_roll_history = dice_roll_history.append(new_roll_data, ignore_index=True)
|
65 |
+
st.session_state.dice_roll_history = dice_roll_history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
+
return bonus_match, bonus_dice_type, bonus_dice_emoji
|
68 |
|
69 |
+
def download_dice_roll_history(include_name_column):
|
70 |
+
dice_roll
|
71 |
|
72 |
|
73 |
st.write("""
|