Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,93 +1,65 @@
|
|
1 |
-
import os
|
2 |
-
import pandas as pd
|
3 |
import streamlit as st
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
# Read the file into a DataFrame
|
66 |
-
data = pd.read_csv("SharedState.csv")
|
67 |
-
|
68 |
-
# Check if the user wants to include their name in the output
|
69 |
-
if st.checkbox("π» Include Username in Output", value=True):
|
70 |
-
# Add the username to the DataFrame
|
71 |
-
data["DiceRollerName"] = st.session_state.username
|
72 |
-
# Check if there is a bonus match and add the bonus match column to the DataFrame
|
73 |
-
if bonus_match:
|
74 |
-
data["BonusMatchToDiceName"] = bonus_match
|
75 |
-
data["BonusMatchToDiceEmoji"] = bonus_emoji
|
76 |
-
# Check if the user wants to download the file
|
77 |
-
if download_roll_history:
|
78 |
-
st.download_button("π₯ Download Roll History", data.to_csv(index=False), f"shared_state_{st.session_state.username}.csv", "text/csv")
|
79 |
-
else:
|
80 |
-
# Remove the username from the DataFrame
|
81 |
-
data = data.drop(columns=["DiceRollerName"])
|
82 |
-
# Check if there is a bonus match and add the bonus match column to the DataFrame
|
83 |
-
if bonus_match:
|
84 |
-
data["BonusMatchToDiceName"] = bonus_match
|
85 |
-
data["BonusMatchToDiceEmoji"] = bonus_emoji
|
86 |
-
# Check if the user wants to download the file
|
87 |
-
if download_roll_history:
|
88 |
-
st.download_button("π₯ Download Roll History", data.to_csv(index=False), "shared_state.csv", "text/csv")
|
89 |
-
else:
|
90 |
-
st.write("π No roll history available.")
|
91 |
|
92 |
|
93 |
st.write("""
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import numpy as np
|
3 |
+
import pandas as pd
|
4 |
+
import plotly.express as px
|
5 |
+
|
6 |
+
st.title('Random Dice Game')
|
7 |
+
|
8 |
+
dice_types = [{'name': 'Six-sided Dice', 'sides': 6, 'emoji': 'π²'},
|
9 |
+
{'name': 'Twenty-sided Dice', 'sides': 20, 'emoji': 'π―'},
|
10 |
+
{'name': 'Thirty-sided Dice', 'sides': 30, 'emoji': 'π―'},
|
11 |
+
{'name': 'One Hundred-sided Dice', 'sides': 100, 'emoji': 'π²'}]
|
12 |
+
|
13 |
+
if 'name' not in st.session_state:
|
14 |
+
st.session_state.name = ''
|
15 |
+
if 'dice_roll_history' not in st.session_state:
|
16 |
+
st.session_state.dice_roll_history = pd.DataFrame()
|
17 |
+
|
18 |
+
dice_type = st.selectbox('Choose a type of dice', dice_types, format_func=lambda d: f"{d['name']} {d['emoji']}")
|
19 |
+
num_rolls = st.slider('How many times do you want to roll the dice?', 1, 1000000, 1000)
|
20 |
+
|
21 |
+
rolls = np.random.randint(1, dice_type['sides'] + 1, num_rolls, dtype=np.uint64)
|
22 |
+
roll_counts = pd.Series(rolls).value_counts().sort_index()
|
23 |
+
|
24 |
+
fig = px.sunburst(names=[f'Roll {i}' for i in roll_counts.index],
|
25 |
+
parents=['Dice Rolls'] * dice_type['sides'],
|
26 |
+
values=roll_counts.values,
|
27 |
+
color=[f'Roll {i}' for i in roll_counts.index],
|
28 |
+
color_discrete_sequence=px.colors.qualitative.Dark24,
|
29 |
+
maxdepth=2)
|
30 |
+
fig.update_layout(title='Dice Roll Distribution', margin=dict(l=20, r=20, t=40, b=20), width=800, height=600)
|
31 |
+
|
32 |
+
show_labels = st.checkbox('Show Labels', value=True)
|
33 |
+
if not show_labels:
|
34 |
+
fig.update_traces(textinfo='none')
|
35 |
+
fig.show()
|
36 |
+
|
37 |
+
bonus_match = False
|
38 |
+
for dice in dice_types:
|
39 |
+
if rolls[0] == dice['sides']:
|
40 |
+
bonus_match = True
|
41 |
+
bonus_dice_type = dice['name']
|
42 |
+
bonus_dice_emoji = dice['emoji']
|
43 |
+
break
|
44 |
+
|
45 |
+
dice_roll_history = st.session_state.dice_roll_history
|
46 |
+
new_roll_data = pd.DataFrame({'Roll': rolls,
|
47 |
+
'Count': np.ones(num_rolls, dtype=np.uint64),
|
48 |
+
'DiceNumberOfSides': [dice_type['sides']] * num_rolls,
|
49 |
+
'DiceRollerName': [st.session_state.name] * num_rolls})
|
50 |
+
if bonus_match:
|
51 |
+
new_roll_data['BonusMatchToDiceName'] = [bonus_dice_type] * num_rolls
|
52 |
+
new_roll_data['BonusMatchToDiceEmoji'] = [bonus_dice_emoji] * num_rolls
|
53 |
+
dice_roll_history = dice_roll_history.append(new_roll_data, ignore_index=True)
|
54 |
+
st.session_state.dice_roll_history = dice_roll_history
|
55 |
+
|
56 |
+
if st.button('Download Results'):
|
57 |
+
filename = f'dice_roll_history_{st.session_state.name}.csv'
|
58 |
+
st.download_button(label='Download CSV', data=dice_roll_history.to_csv(index=False), file_name=filename, mime='text/csv')
|
59 |
+
|
60 |
+
if st.session_state.dice_roll_history.shape[0] > 0:
|
61 |
+
st.markdown('### Dice Roll History')
|
62 |
+
st.dataframe(st.session_state.dice_roll_history)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
|
65 |
st.write("""
|