awacke1 commited on
Commit
6b5d290
ยท
1 Parent(s): a566d88

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -98
app.py CHANGED
@@ -3,113 +3,70 @@ import numpy as np
3
  import pandas as pd
4
  import plotly.express as px
5
 
6
- def build_rolls(dice_type, num_rolls):
7
- rolls = np.random.randint(1, dice_type['sides'] + 1, num_rolls, dtype=np.int64)
8
- roll_counts = pd.Series(rolls).value_counts().sort_index()
9
- return rolls, roll_counts
 
 
 
10
 
11
- def update_dice_roll_history(dice_roll_history, name, rolls, num_rolls, dice_type, bonus_dice_type='', bonus_dice_emoji=''):
12
- new_roll_data = pd.DataFrame({
13
- 'Roll': rolls,
14
- 'Count': np.ones(num_rolls, dtype=np.int64),
15
- 'DiceNumberOfSides': [dice_type['sides']] * num_rolls,
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
- def download_results(dataframe, name):
25
- filename = f'dice_roll_history_{name}.csv'
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
- def main():
34
- st.title('Random Dice Game')
35
 
36
- dice_types = [
37
- {'name': 'Six-sided Dice', 'sides': 6, 'emoji': '๐ŸŽฒ'},
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
- if 'name' not in st.session_state:
44
- st.session_state.name = 'Username'
45
- if 'dice_roll_history' not in st.session_state:
46
- st.session_state.dice_roll_history = pd.DataFrame()
47
 
48
- dice_type = st.selectbox(
49
- 'Choose a type of dice',
50
- dice_types,
51
- format_func=lambda d: f"{d['name']} {d['emoji']}"
52
- )
53
 
54
- num_rolls = st.slider(
55
- 'How many times do you want to roll the dice?',
56
- 1, 1000000, 1000,
57
- value=1000,
58
- step=1
59
- )
 
 
60
 
61
- rolls, roll_counts = build_rolls(dice_type, num_rolls)
 
62
 
63
- fig = px.sunburst(
64
- names=[f'Roll {i}' for i in roll_counts.index],
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
- show_labels = st.checkbox('Show Labels', value=True)
78
- if not show_labels:
79
- fig.update_traces(textinfo='none')
80
- st.plotly_chart(fig)
81
 
82
- bonus_match = False
83
- bonus_dice_type = ''
84
- bonus_dice_emoji = ''
85
- for dice in dice_types:
86
- if rolls[0] == dice['sides']:
87
- bonus_match = True
88
- bonus_dice_type = dice['name']
89
- bonus_dice_emoji = dice['emoji']
90
- break
91
-
92
- dice_roll_history = st.session_state.dice_roll_history
93
- dice_roll_history = update_dice_roll_history(
94
- dice_roll_history,
95
- st.session_state.name,
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
- runitforme = main()
 
 
 
 
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
+ """)