awacke1 commited on
Commit
ddb3454
ยท
1 Parent(s): 7badadb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +111 -71
app.py CHANGED
@@ -1,84 +1,124 @@
1
- import os
2
- import pandas as pd
3
  import streamlit as st
 
 
 
4
 
5
- # Set page title and icon
6
- st.set_page_config(page_title="Dice Roller", page_icon="๐ŸŽฒ")
7
-
8
- # Set page title
9
- st.title("๐ŸŽฒ Dice Roller")
10
-
11
- # Check if username is already set, if not, create a text input to set it
12
- if 'username' not in st.session_state:
13
- st.session_state.username = st.text_input("๐Ÿ‘ค Username", max_chars=50)
14
-
15
- # Define the dice types
16
- dice_types = {
17
- "๐ŸŽฒ Six-sided dice": {"sides": 6, "name": "d6", "emoji": "๐ŸŽฒ"},
18
- "๐ŸŽฒ Thirty-sided dice": {"sides": 30, "name": "d30", "emoji": "๐ŸŽฒ"},
19
- "๐ŸŽฒ Twenty-sided dice": {"sides": 20, "name": "d20", "emoji": "๐ŸŽฒ"},
20
- "๐ŸŽฒ One hundred-sided dice": {"sides": 100, "name": "d100", "emoji": "๐Ÿ’ฏ"},
21
- }
22
-
23
- # Create a function to build the rolls pandas series to be a list of dictionaries
24
  def build_rolls(dice_type, num_rolls):
25
- sides = dice_types[dice_type]["sides"]
26
- rolls = pd.Series([sides] * num_rolls).apply(pd.Series.sample, replace=True, axis=1)
27
- rolls = rolls.tolist()
28
- count = sum(rolls)
29
- return [{"Roll": roll, "Count": count, "DiceNumberOfSides": sides} for roll in rolls]
30
-
31
-
32
- # Create a selectbox to choose the dice type
33
- dice_type = st.selectbox("๐ŸŽฒ Select a dice type", options=list(dice_types.keys()), format_func=lambda x: x.split(' ')[-2])
34
-
35
- # Create a slider to choose the number of rolls
36
- num_rolls = st.slider("๐ŸŽฒ How many times do you want to roll the dice?", 1, 1000000, 100)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
- # Create a button to roll the dice
39
- if st.button("๐ŸŽฒ Roll Dice"):
40
- # Build the rolls list of dictionaries
41
- rolls = build_rolls(dice_type, num_rolls)
42
-
43
- # Display the rolls
44
- display_roll = [roll["Roll"] for roll in rolls]
45
- st.write(f"{dice_types[dice_type]['emoji']} You rolled {len(display_roll)} {dice_types[dice_type]['name']} dice: {', '.join(map(str, display_roll))}.")
46
-
47
- # Check if the count is a multiple of the dice sides to see if there's a bonus match
48
- bonus_match = None
49
- count = rolls[0]["Count"]
50
- if count % dice_types[dice_type]['sides'] == 0:
51
- bonus_match = dice_types[dice_type]['name']
52
- bonus_emoji = dice_types[dice_type]['emoji']
53
- st.write(f"๐ŸŽ‰ Bonus match! You rolled a multiple of {dice_types[dice_type]['sides']}.")
54
-
55
- # Create a DataFrame with the rolls, count, and dice number of sides
56
- df = pd.DataFrame(rolls)
57
-
58
- # Check if the file exists
59
- file_exists = os.path.exists("SharedState.csv")
60
-
61
- # Append the new rolls to the file
62
- if file_exists:
63
- df.to_csv("SharedState.csv", mode='a', header=False, index=False)
64
- # If the file doesn't exist, create it and write the headers
65
- else:
66
- df.to_csv("SharedState.csv", mode='w', header=True, index=False)
67
-
68
- # Create a checkbox to download the roll history
69
- download_roll_history = st.checkbox("๐Ÿ“ฅ Download Roll History", value=True)
70
 
71
- # Check if the file exists and display it as a download link
72
- if os.path.exists("SharedState.csv") and download_roll_history:
73
- with open("SharedState.csv", "r") as f:
74
- file_contents = f.read()
75
- st.markdown(f"<a href='data:file/csv;base64,{file_contents.encode().b64encode()}'>๐Ÿ“ฅ Download Roll History</a>", unsafe_allow_html=True)
76
 
77
 
78
 
79
 
80
  st.write("""
81
-
82
  ๐Ÿž 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")
83
  """)
84
 
 
 
 
 
 
1
  import streamlit as st
2
+ 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
 
116
 
117
 
118
 
119
  st.write("""
 
120
  ๐Ÿž 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")
121
  """)
122
 
123
+ if name == 'main':
124
+ main()