Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,72 +1,165 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
import numpy as np
|
3 |
import pandas as pd
|
4 |
import plotly.express as px
|
5 |
-
|
6 |
-
|
7 |
-
dice_types = {
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
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 |
-
|
21 |
-
|
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'] *
|
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 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
|
|
|
|
|
|
55 |
dice_roll_history = st.session_state.dice_roll_history
|
56 |
-
new_roll_data = pd.DataFrame({
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
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
|
72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Code Destiny and Density is equal to one hundred.
|
2 |
+
|
3 |
+
#st.Markdown("TODO: A code density slider bar shows and adjusts the size of lines. ')
|
4 |
+
#st.Markdown('Any ๐ฒDice๐ฒ way to compress the lines yet make it readable as optimal list of sets will do. Match language from good math books.')
|
5 |
+
|
6 |
+
import os
|
7 |
import streamlit as st
|
8 |
import numpy as np
|
9 |
import pandas as pd
|
10 |
import plotly.express as px
|
11 |
+
st.set_page_config(layout='wide')
|
12 |
+
st.title('Any ๐ฒDice๐ฒ Way - STEM Math and Data Science Numerical Potluck')
|
13 |
+
dice_types = [{'name': 'Six-sided Dice', 'sides': 6, 'emoji': '๐ฒ'},
|
14 |
+
{'name': 'Twenty-sided Dice', 'sides': 20, 'emoji': '๐ฟ'},
|
15 |
+
{'name': 'Thirty-sided Dice', 'sides': 30, 'emoji': '๐ฅ'},
|
16 |
+
{'name': 'One Hundred-sided Dice', 'sides': 100, 'emoji': '๐'}]
|
17 |
+
if 'username' not in st.session_state:
|
18 |
+
st.session_state.username = ''
|
|
|
|
|
|
|
|
|
19 |
if 'dice_roll_history' not in st.session_state:
|
20 |
st.session_state.dice_roll_history = pd.DataFrame()
|
21 |
+
dice_type = st.selectbox('Choose a type of dice', dice_types, format_func=lambda d: f"{d['name']} {d['emoji']}")
|
22 |
+
num_rolls = st.slider('How many times do you want to roll the dice?', 1, 1000000, 1000)
|
23 |
+
rolls = np.random.randint(1, dice_type['sides'] + 1, num_rolls, dtype=np.uint64)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
roll_counts = pd.Series(rolls).value_counts().sort_index()
|
|
|
|
|
25 |
fig = px.sunburst(names=[f'Roll {i}' for i in roll_counts.index],
|
26 |
+
parents=['Dice Rolls'] * dice_type['sides'],
|
27 |
values=roll_counts.values,
|
28 |
color=[f'Roll {i}' for i in roll_counts.index],
|
29 |
color_discrete_sequence=px.colors.qualitative.Dark24,
|
30 |
maxdepth=2)
|
31 |
fig.update_layout(title='Dice Roll Distribution', margin=dict(l=20, r=20, t=40, b=20), width=800, height=600)
|
32 |
+
show_labels = st.checkbox('Show Labels', value=True)
|
|
|
|
|
|
|
33 |
if not show_labels:
|
34 |
fig.update_traces(textinfo='none')
|
35 |
+
fig.show()
|
36 |
+
bonus_match = False
|
37 |
+
for dice in dice_types:
|
38 |
+
if rolls[0] == dice['sides']:
|
39 |
+
bonus_match = True
|
40 |
+
bonus_dice_type = dice['name']
|
41 |
+
bonus_dice_emoji = dice['emoji']
|
42 |
+
break
|
43 |
dice_roll_history = st.session_state.dice_roll_history
|
44 |
+
new_roll_data = pd.DataFrame({'Roll': rolls,
|
45 |
+
'Count': np.ones(num_rolls, dtype=np.uint64),
|
46 |
+
'DiceNumberOfSides': [dice_type['sides']] * num_rolls,
|
47 |
+
'Username': [st.session_state.username] * num_rolls})
|
48 |
+
if bonus_match:
|
49 |
+
new_roll_data['BonusMatchToDiceName'] = [bonus_dice_type] * num_rolls
|
50 |
+
new_roll_data['BonusMatchToDiceEmoji'] = [bonus_dice_emoji] * num_rolls
|
51 |
dice_roll_history = dice_roll_history.append(new_roll_data, ignore_index=True)
|
52 |
st.session_state.dice_roll_history = dice_roll_history
|
53 |
+
include_name_column = st.checkbox('Include Username Column in Downloaded CSV', value=True)
|
54 |
+
if st.button('Download Results'):
|
55 |
+
filename = f'dice_roll_history_{st.session_state.username}{dice_type["emoji"]}.csv'
|
56 |
+
if not include_name_column:
|
57 |
+
dice_roll_history = dice_roll_history.drop(columns=['Username'])
|
58 |
+
filename = f'dice_roll_history{dice_type["emoji"]}.csv'
|
59 |
+
st.download_button(label='Download CSV', data=dice_roll_history.to_csv(index=False), file_name=filename, mime='text/csv')
|
60 |
+
csv_files = [f for f in os.listdir('.') if os.path.isfile
|
61 |
+
f.endswith('.csv') and 'dice_roll_history' in f]
|
62 |
+
if csv_files:
|
63 |
+
st.write('Downloaded files:')
|
64 |
+
for csv_file in csv_files:
|
65 |
+
st.markdown(f'[Download {csv_file}]({csv_file})')
|
66 |
+
st.write('---')
|
67 |
+
st.subheader('Settings')
|
68 |
+
st.session_state.username = st.text_input('Username', st.session_state.username)
|
69 |
+
st.write('---')
|
70 |
+
st.subheader('Dice Roll History')
|
71 |
+
st.write(dice_roll_history)
|
72 |
|
|
|
|
|
|
|
|
|
|
|
73 |
st.write("""
|
74 |
+
๐ Bread
|
75 |
+
๐ฅ Croissant
|
76 |
+
๐ฅ Baguette Bread
|
77 |
+
๐ซ Flatbread
|
78 |
+
๐ฅจ Pretzel
|
79 |
+
๐ฅฏ Bagel
|
80 |
+
๐ฅ Pancakes
|
81 |
+
๐ง Waffle
|
82 |
+
๐ง Cheese Wedge
|
83 |
+
๐ Meat on Bone
|
84 |
+
๐ Poultry Leg
|
85 |
+
๐ฅฉ Cut of Meat
|
86 |
+
๐ฅ Bacon
|
87 |
+
๐ Hamburger
|
88 |
+
๐ French Fries
|
89 |
+
๐ Pizza
|
90 |
+
๐ญ Hot Dog
|
91 |
+
๐ฅช Sandwich
|
92 |
+
๐ฎ Taco
|
93 |
+
๐ฏ Burrito
|
94 |
+
๐ซ Tamale
|
95 |
+
๐ฅ Stuffed Flatbread
|
96 |
+
๐ง Falafel
|
97 |
+
๐ฅ Egg
|
98 |
+
๐ณ Cooking
|
99 |
+
๐ฅ Shallow Pan of Food
|
100 |
+
๐ฒ Pot of Food
|
101 |
+
๐ซ Fondue
|
102 |
+
๐ฅฃ Bowl with Spoon
|
103 |
+
๐ฅ Green Salad
|
104 |
+
๐ฟ Popcorn
|
105 |
+
๐ง Butter
|
106 |
+
๐ง Salt
|
107 |
+
๐ฅซ Canned Food
|
108 |
+
๐ฑ Bento Box
|
109 |
+
๐ Rice Cracker
|
110 |
+
๐ Rice Ball
|
111 |
+
๐ Cooked Rice
|
112 |
+
๐ Curry Rice
|
113 |
+
๐ Steaming Bowl
|
114 |
+
๐ Spaghetti
|
115 |
+
๐ Roasted Sweet Potato
|
116 |
+
๐ข Oden
|
117 |
+
๐ฃ Sushi
|
118 |
+
๐ค Fried Shrimp
|
119 |
+
๐ฅ Fish Cake with Swirl
|
120 |
+
๐ฅฎ Moon Cake
|
121 |
+
๐ก Dango
|
122 |
+
๐ฅ Dumpling
|
123 |
+
๐ฅ Fortune Cookie
|
124 |
+
๐ฅก Takeout Box
|
125 |
+
๐ฆช Oyster
|
126 |
+
๐ฆ Soft Ice Cream
|
127 |
+
๐ง Shaved Ice
|
128 |
+
๐จ Ice Cream
|
129 |
+
๐ฉ Doughnut
|
130 |
+
๐ช Cookie
|
131 |
+
๐ Birthday Cake
|
132 |
+
๐ฐ Shortcake
|
133 |
+
๐ง Cupcake
|
134 |
+
๐ฅง Pie
|
135 |
+
๐ซ Chocolate Bar
|
136 |
+
๐ฌ Candy
|
137 |
+
๐ญ Lollipop
|
138 |
+
๐ฎ Custard
|
139 |
+
๐ฏ Honey Pot
|
140 |
+
๐ผ Baby Bottle
|
141 |
+
๐ฅ Glass of Milk
|
142 |
+
โ Hot Beverage
|
143 |
+
๐ซ Teapot
|
144 |
+
๐ต Teacup Without Handle
|
145 |
+
๐ถ Sake
|
146 |
+
๐พ Bottle with Popping Cork
|
147 |
+
๐ท Wine Glass
|
148 |
+
๐ธ Cocktail Glass
|
149 |
+
๐น Tropical Drink
|
150 |
+
๐บ Beer Mug
|
151 |
+
๐ป Clinking Beer Mugs
|
152 |
+
๐ฅ Clinking Glasses
|
153 |
+
๐ฅ Tumbler Glass
|
154 |
+
๐ซ Pouring Liquid
|
155 |
+
๐ฅค Cup with Straw
|
156 |
+
๐ง Bubble Tea
|
157 |
+
๐ง Beverage Box
|
158 |
+
๐ง Mate
|
159 |
+
๐ง Ice
|
160 |
+
๐ฅข Chopsticks
|
161 |
+
๐ฝ๏ธ Fork and Knife with Plate
|
162 |
+
๐ด Fork and Knife
|
163 |
+
๐ฅ Spoon
|
164 |
+
๐ซ Jar
|
165 |
+
""")
|