Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -3,113 +3,70 @@ import numpy as np
|
|
3 |
import pandas as pd
|
4 |
import plotly.express as px
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
|
|
|
|
|
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
'DiceRollerName': [name] * num_rolls
|
17 |
-
})
|
18 |
-
if bonus_dice_type:
|
19 |
-
new_roll_data['BonusMatchToDiceName'] = [bonus_dice_type] * num_rolls
|
20 |
-
new_roll_data['BonusMatchToDiceEmoji'] = [bonus_dice_emoji] * num_rolls
|
21 |
-
dice_roll_history = dice_roll_history.append(new_roll_data, ignore_index=True)
|
22 |
-
return dice_roll_history
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
st.download_button(
|
27 |
-
label='Download CSV',
|
28 |
-
data=dataframe.to_csv(index=False),
|
29 |
-
file_name=filename,
|
30 |
-
mime='text/csv'
|
31 |
-
)
|
32 |
|
33 |
-
|
34 |
-
|
35 |
|
36 |
-
|
37 |
-
|
38 |
-
{'name': 'Twenty-sided Dice', 'sides': 20, 'emoji': '๐ฏ'},
|
39 |
-
{'name': 'Thirty-sided Dice', 'sides': 30, 'emoji': '๐ฏ'},
|
40 |
-
{'name': 'One Hundred-sided Dice', 'sides': 100, 'emoji': '๐ฒ'}
|
41 |
-
]
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
if 'dice_roll_history' not in st.session_state:
|
46 |
-
st.session_state.dice_roll_history = pd.DataFrame()
|
47 |
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
format_func=lambda d: f"{d['name']} {d['emoji']}"
|
52 |
-
)
|
53 |
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
|
|
60 |
|
61 |
-
|
|
|
62 |
|
63 |
-
|
64 |
-
|
65 |
-
parents=['Dice Rolls'] * dice_type['sides'],
|
66 |
-
values=roll_counts.values,
|
67 |
-
color=[f'Roll {i}' for i in roll_counts.index],
|
68 |
-
color_discrete_sequence=px.colors.qualitative.Dark24,
|
69 |
-
maxdepth=2
|
70 |
-
)
|
71 |
-
fig.update_layout(
|
72 |
-
title='Dice Roll Distribution',
|
73 |
-
margin=dict(l=20, r=20, t=40, b=20),
|
74 |
-
width=800, height=600
|
75 |
-
)
|
76 |
|
77 |
-
|
78 |
-
|
79 |
-
fig.update_traces(textinfo='none')
|
80 |
-
st.plotly_chart(fig)
|
81 |
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
rolls,
|
97 |
-
num_rolls,
|
98 |
-
dice_type,
|
99 |
-
bonus_dice_type,
|
100 |
-
bonus_dice_emoji
|
101 |
-
)
|
102 |
-
st.session_state.dice_roll_history = dice_roll_history
|
103 |
-
|
104 |
-
if st.button('Download Results'):
|
105 |
-
download_results(dice_roll_history, st.session_state.name)
|
106 |
-
|
107 |
-
if dice_roll_history.shape[0] > 0:
|
108 |
-
st.markdown('### Dice Roll History')
|
109 |
-
st.dataframe(dice_roll_history)
|
110 |
-
|
111 |
-
st.write("""
|
112 |
-
๐ Bread, ๐ฅ Croissant, ๐ฅ Baguette Bread, ๐ซ Flatbread, ๐ฅจ Pretzel, ๐ฅฏ Bagel, ๐ฅ Pancakes, ๐ง Waffle, ๐ง Cheese Wedge, ๐ Meat on Bone, ๐ Poultry Leg, ๐ฅฉ Cut of Meat, ๐ฅ Bacon, ๐ Hamburger, ๐ French Fries, ๐ Pizza, ๐ญ Hot Dog, ๐ฅช Sandwich, ๐ฎ Taco, ๐ฏ Burrito, ๐ซ Tamale, ๐ฅ Stuffed Flatbread, ๐ง Falafel, ๐ฅ Egg, ๐ณ Cooking, ๐ฅ Shallow Pan of Food, ๐ฒ Pot of Food, ๐ซ Fondue, ๐ฅฃ Bowl with Spoon, ๐ฅ Green Salad, ๐ฟ Popcorn, ๐ง Butter, ๐ง Salt, ๐ฅซ Canned Food, ๐ฑ Bento Box, ๐ Rice Cracker, ๐ Rice Ball, ๐ Cooked Rice, ๐ Curry Rice, ๐ Steaming Bowl, ๐ Spaghetti, ๐ Roasted Sweet Potato, ๐ข Oden, ๐ฃ Sushi, ๐ค Fried Shrimp, ๐ฅ Fish Cake with Swirl, ๐ฅฎ Moon Cake, ๐ก Dango, ๐ฅ Dumpling, ๐ฅ Fortune Cookie, ๐ฅก Takeout Box, ๐ฆช Oyster, ๐ฆ Soft Ice Cream, ๐ง Shaved Ice, ๐จ Ice Cream, ๐ฉ Doughnut, ๐ช Cookie, ๐ Birthday Cake, ๐ฐ Shortcake, ๐ง Cupcake, ๐ฅง Pie, ๐ซ Chocolate Bar, ๐ฌ Candy, ๐ญ Lollipop, ๐ฎ Custard, ๐ฏ Honey Pot, ๐ผ Baby Bottle, ๐ฅ Glass of Milk, โ Hot Beverage, ๐ซ Teapot, ๐ต Teacup Without Handle, ๐ถ Sake, ๐พ Bottle with Popping Cork, ๐ท Wine Glass, ๐ธ Cocktail Glass, ๐น Tropical Drink, ๐บ Beer Mug, ๐ป Clinking Beer Mugs, ๐ฅ Clinking Glasses, ๐ฅ Tumbler Glass, ๐ซ Pouring Liquid, ๐ฅค Cup with Straw, ๐ง Bubble Tea, ๐ง Beverage Box, ๐ง Mate, ๐ง Ice, ๐ฅข Chopsticks, ๐ฝ๏ธ Fork and Knife with Plate, ๐ด Fork and Knife, ๐ฅ Spoon, ๐ซ Jar"
|
113 |
-
""")
|
114 |
|
115 |
-
|
|
|
|
|
|
|
|
3 |
import pandas as pd
|
4 |
import plotly.express as px
|
5 |
|
6 |
+
# Define the types of dice with their number of sides and emojis
|
7 |
+
dice_types = {
|
8 |
+
'Six-sided Dice': {'sides': 6, 'emoji': '๐ฒ'},
|
9 |
+
'Twenty-sided Dice': {'sides': 20, 'emoji': '๐ฏ'},
|
10 |
+
'Thirty-sided Dice': {'sides': 30, 'emoji': '๐ฒ'},
|
11 |
+
'One Hundred-sided Dice': {'sides': 100, 'emoji': '๐ฒ'}
|
12 |
+
}
|
13 |
|
14 |
+
# Use session state to store the user name and dice roll history
|
15 |
+
if 'name' not in st.session_state:
|
16 |
+
st.session_state.name = ''
|
17 |
+
if 'dice_roll_history' not in st.session_state:
|
18 |
+
st.session_state.dice_roll_history = pd.DataFrame()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
+
# Render the app title with a dice emoji
|
21 |
+
st.title('๐ฒ Random Dice Game')
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
+
# Let the user enter their name
|
24 |
+
name = st.text_input('๐ค Enter your name')
|
25 |
|
26 |
+
# Let the user select a type of dice
|
27 |
+
dice_type = st.selectbox('๐ฒ Choose a type of dice', list(dice_types.keys()), format_func=lambda d: f"{d} {dice_types[d]['emoji']}")
|
|
|
|
|
|
|
|
|
28 |
|
29 |
+
# Let the user choose how many times to roll the dice
|
30 |
+
num_rolls = st.slider('๐ข How many times do you want to roll the dice?', 1, 1000000, 1000)
|
|
|
|
|
31 |
|
32 |
+
# Roll the dice and count the results
|
33 |
+
rolls = np.random.randint(1, dice_types[dice_type]['sides'] + 1, num_rolls, dtype=np.uint64)
|
34 |
+
roll_counts = pd.Series(rolls).value_counts().sort_index()
|
|
|
|
|
35 |
|
36 |
+
# Plot a sunburst chart of the dice roll distribution
|
37 |
+
fig = px.sunburst(names=[f'Roll {i}' for i in roll_counts.index],
|
38 |
+
parents=['Dice Rolls'] * dice_types[dice_type]['sides'],
|
39 |
+
values=roll_counts.values,
|
40 |
+
color=[f'Roll {i}' for i in roll_counts.index],
|
41 |
+
color_discrete_sequence=px.colors.qualitative.Dark24,
|
42 |
+
maxdepth=2)
|
43 |
+
fig.update_layout(title='Dice Roll Distribution', margin=dict(l=20, r=20, t=40, b=20), width=800, height=600)
|
44 |
|
45 |
+
# Let the user choose whether to show labels on the sunburst chart
|
46 |
+
show_labels = st.checkbox('๐ Show Labels', value=True)
|
47 |
|
48 |
+
if not show_labels:
|
49 |
+
fig.update_traces(textinfo='none')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
+
# Display the sunburst chart
|
52 |
+
st.plotly_chart(fig)
|
|
|
|
|
53 |
|
54 |
+
# Append the new dice roll data to the roll history
|
55 |
+
dice_roll_history = st.session_state.dice_roll_history
|
56 |
+
new_roll_data = pd.DataFrame({
|
57 |
+
'Roll': rolls,
|
58 |
+
'Count': np.ones(num_rolls, dtype=np.uint64),
|
59 |
+
'DiceNumberOfSides': [dice_types[dice_type]['sides']] * num_rolls,
|
60 |
+
'DiceRollerName': [name] * num_rolls
|
61 |
+
})
|
62 |
+
dice_roll_history = dice_roll_history.append(new_roll_data, ignore_index=True)
|
63 |
+
st.session_state.dice_roll_history = dice_roll_history
|
64 |
+
|
65 |
+
# Add a button to download the roll history as a CSV file
|
66 |
+
if st.button('๐ฅ Download Results'):
|
67 |
+
filename = f'dice_roll_history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
|
69 |
+
|
70 |
+
st.write("""
|
71 |
+
๐ Bread, ๐ฅ Croissant, ๐ฅ Baguette Bread, ๐ซ Flatbread, ๐ฅจ Pretzel, ๐ฅฏ Bagel, ๐ฅ Pancakes, ๐ง Waffle, ๐ง Cheese Wedge, ๐ Meat on Bone, ๐ Poultry Leg, ๐ฅฉ Cut of Meat, ๐ฅ Bacon, ๐ Hamburger, ๐ French Fries, ๐ Pizza, ๐ญ Hot Dog, ๐ฅช Sandwich, ๐ฎ Taco, ๐ฏ Burrito, ๐ซ Tamale, ๐ฅ Stuffed Flatbread, ๐ง Falafel, ๐ฅ Egg, ๐ณ Cooking, ๐ฅ Shallow Pan of Food, ๐ฒ Pot of Food, ๐ซ Fondue, ๐ฅฃ Bowl with Spoon, ๐ฅ Green Salad, ๐ฟ Popcorn, ๐ง Butter, ๐ง Salt, ๐ฅซ Canned Food, ๐ฑ Bento Box, ๐ Rice Cracker, ๐ Rice Ball, ๐ Cooked Rice, ๐ Curry Rice, ๐ Steaming Bowl, ๐ Spaghetti, ๐ Roasted Sweet Potato, ๐ข Oden, ๐ฃ Sushi, ๐ค Fried Shrimp, ๐ฅ Fish Cake with Swirl, ๐ฅฎ Moon Cake, ๐ก Dango, ๐ฅ Dumpling, ๐ฅ Fortune Cookie, ๐ฅก Takeout Box, ๐ฆช Oyster, ๐ฆ Soft Ice Cream, ๐ง Shaved Ice, ๐จ Ice Cream, ๐ฉ Doughnut, ๐ช Cookie, ๐ Birthday Cake, ๐ฐ Shortcake, ๐ง Cupcake, ๐ฅง Pie, ๐ซ Chocolate Bar, ๐ฌ Candy, ๐ญ Lollipop, ๐ฎ Custard, ๐ฏ Honey Pot, ๐ผ Baby Bottle, ๐ฅ Glass of Milk, โ Hot Beverage, ๐ซ Teapot, ๐ต Teacup Without Handle, ๐ถ Sake, ๐พ Bottle with Popping Cork, ๐ท Wine Glass, ๐ธ Cocktail Glass, ๐น Tropical Drink, ๐บ Beer Mug, ๐ป Clinking Beer Mugs, ๐ฅ Clinking Glasses, ๐ฅ Tumbler Glass, ๐ซ Pouring Liquid, ๐ฅค Cup with Straw, ๐ง Bubble Tea, ๐ง Beverage Box, ๐ง Mate, ๐ง Ice, ๐ฅข Chopsticks, ๐ฝ๏ธ Fork and Knife with Plate, ๐ด Fork and Knife, ๐ฅ Spoon, ๐ซ Jar"
|
72 |
+
""")
|