Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -3,13 +3,38 @@ import numpy as np
|
|
3 |
import pandas as pd
|
4 |
import plotly.express as px
|
5 |
|
6 |
-
st.title('Random
|
7 |
|
8 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
num_rolls = st.slider('How many times do you want to roll the dice?', 1, 1000000, 1000)
|
10 |
|
11 |
# Generate dice rolls
|
12 |
-
rolls = np.random.randint(1,
|
13 |
|
14 |
# Count the number of occurrences of each roll
|
15 |
roll_counts = pd.Series(rolls).value_counts().sort_index()
|
@@ -17,7 +42,7 @@ roll_counts = pd.Series(rolls).value_counts().sort_index()
|
|
17 |
# Create a sunburst chart of the roll counts
|
18 |
fig = px.sunburst(
|
19 |
names=[f'Roll {i}' for i in roll_counts.index],
|
20 |
-
parents=['Dice Rolls'] *
|
21 |
values=roll_counts.values,
|
22 |
color=[f'Roll {i}' for i in roll_counts.index],
|
23 |
color_discrete_sequence=px.colors.qualitative.Dark24,
|
@@ -38,15 +63,17 @@ if not show_labels:
|
|
38 |
fig.update_traces(textinfo='none')
|
39 |
fig.show()
|
40 |
|
41 |
-
# Allow user to download roll data as CSV file
|
42 |
if st.button('Download Roll Data as CSV'):
|
43 |
roll_data = pd.DataFrame({
|
44 |
'Roll': rolls,
|
45 |
-
'Count': np.ones(num_rolls)
|
|
|
46 |
})
|
|
|
47 |
st.download_button(
|
48 |
label='Download CSV',
|
49 |
data=roll_data.to_csv(index=False),
|
50 |
-
file_name=
|
51 |
mime='text/csv'
|
52 |
-
)
|
|
|
3 |
import pandas as pd
|
4 |
import plotly.express as px
|
5 |
|
6 |
+
st.title('Random Dice Game')
|
7 |
|
8 |
+
# Define the available types of dice
|
9 |
+
dice_types = [
|
10 |
+
{
|
11 |
+
'name': 'Six-sided Dice ๐ฒ',
|
12 |
+
'sides': 6,
|
13 |
+
'emoji': '๐ฒ'
|
14 |
+
},
|
15 |
+
{
|
16 |
+
'name': 'Twenty-sided Dice ๐ฏ',
|
17 |
+
'sides': 20,
|
18 |
+
'emoji': '๐ฏ'
|
19 |
+
},
|
20 |
+
{
|
21 |
+
'name': 'Thirty-sided Dice ๐ฏ',
|
22 |
+
'sides': 30,
|
23 |
+
'emoji': '๐ฏ'
|
24 |
+
},
|
25 |
+
{
|
26 |
+
'name': 'One Hundred-sided Dice ๐ฒ',
|
27 |
+
'sides': 100,
|
28 |
+
'emoji': '๐ฒ'
|
29 |
+
}
|
30 |
+
]
|
31 |
+
|
32 |
+
# Get user input for the type of dice and number of rolls
|
33 |
+
dice_type = st.selectbox('Choose a type of dice', dice_types)
|
34 |
num_rolls = st.slider('How many times do you want to roll the dice?', 1, 1000000, 1000)
|
35 |
|
36 |
# Generate dice rolls
|
37 |
+
rolls = np.random.randint(1, dice_type['sides']+1, num_rolls)
|
38 |
|
39 |
# Count the number of occurrences of each roll
|
40 |
roll_counts = pd.Series(rolls).value_counts().sort_index()
|
|
|
42 |
# Create a sunburst chart of the roll counts
|
43 |
fig = px.sunburst(
|
44 |
names=[f'Roll {i}' for i in roll_counts.index],
|
45 |
+
parents=['Dice Rolls'] * dice_type['sides'],
|
46 |
values=roll_counts.values,
|
47 |
color=[f'Roll {i}' for i in roll_counts.index],
|
48 |
color_discrete_sequence=px.colors.qualitative.Dark24,
|
|
|
63 |
fig.update_traces(textinfo='none')
|
64 |
fig.show()
|
65 |
|
66 |
+
# Allow user to download roll data as CSV file with their name and dice type
|
67 |
if st.button('Download Roll Data as CSV'):
|
68 |
roll_data = pd.DataFrame({
|
69 |
'Roll': rolls,
|
70 |
+
'Count': np.ones(num_rolls),
|
71 |
+
'DiceNumberOfSides': [dice_type['sides']] * num_rolls
|
72 |
})
|
73 |
+
filename = f'roll_data_{dice_type["name"].replace(" ", "_")}_{st.session_state.name}.csv'
|
74 |
st.download_button(
|
75 |
label='Download CSV',
|
76 |
data=roll_data.to_csv(index=False),
|
77 |
+
file_name=filename,
|
78 |
mime='text/csv'
|
79 |
+
)
|