Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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 |
-
|
26 |
-
|
27 |
-
rolls
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
|
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()
|