Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,7 @@
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
import numpy as np
|
3 |
import pandas as pd
|
@@ -5,86 +9,61 @@ import plotly.express as px
|
|
5 |
|
6 |
st.title('Random Dice Game')
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
'sides': 6,
|
13 |
-
'emoji': '๐ฒ'
|
14 |
-
},
|
15 |
-
{
|
16 |
-
'name': 'Twenty-sided Dice ๐ฏ',
|
17 |
-
'sides': 20,
|
18 |
-
'emoji': '๐ฏ'
|
19 |
-
},
|
20 |
-
{
|
21 |
-
'name': 'Thirty-sided Dice ๐ฏ',
|
22 |
-
'sides': 30,
|
23 |
-
'emoji': '๐ฏ'
|
24 |
-
},
|
25 |
-
{
|
26 |
-
'name': 'One Hundred-sided Dice ๐ฒ',
|
27 |
-
'sides': 100,
|
28 |
-
'emoji': '๐ฒ'
|
29 |
-
}
|
30 |
-
]
|
31 |
|
32 |
-
# Initialize the user's name if it has not been set yet
|
33 |
if 'name' not in st.session_state:
|
34 |
st.session_state.name = ''
|
|
|
|
|
35 |
|
36 |
-
|
37 |
-
dice_type = st.selectbox('Choose a type of dice', dice_types)
|
38 |
num_rolls = st.slider('How many times do you want to roll the dice?', 1, 1000000, 1000)
|
39 |
|
40 |
-
|
41 |
-
rolls = np.random.randint(1, dice_type['sides']+1, num_rolls)
|
42 |
-
|
43 |
-
# Count the number of occurrences of each roll
|
44 |
roll_counts = pd.Series(rolls).value_counts().sort_index()
|
45 |
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
maxdepth=2
|
54 |
-
)
|
55 |
|
56 |
-
# Customize the chart layout
|
57 |
-
fig.update_layout(
|
58 |
-
title='Dice Roll Distribution',
|
59 |
-
margin=dict(l=20, r=20, t=40, b=20),
|
60 |
-
width=800,
|
61 |
-
height=600
|
62 |
-
)
|
63 |
-
|
64 |
-
# Add UI controls to modify the chart
|
65 |
show_labels = st.checkbox('Show Labels', value=True)
|
66 |
if not show_labels:
|
67 |
fig.update_traces(textinfo='none')
|
68 |
fig.show()
|
69 |
|
70 |
-
|
71 |
-
|
72 |
-
if
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
|
88 |
-
# Add UI control for the user's name
|
89 |
-
name = st.text_input('What is your name?', value=st.session_state.name)
|
90 |
-
st.session_state.name = name
|
|
|
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. Any way to compress the lines yet make it readable as optimal list of sets will do. Match language from good math books.')
|
4 |
+
|
5 |
import streamlit as st
|
6 |
import numpy as np
|
7 |
import pandas as pd
|
|
|
9 |
|
10 |
st.title('Random Dice Game')
|
11 |
|
12 |
+
dice_types = [{'name': 'Six-sided Dice', 'sides': 6, 'emoji': '๐ฒ'},
|
13 |
+
{'name': 'Twenty-sided Dice', 'sides': 20, 'emoji': '๐ฏ'},
|
14 |
+
{'name': 'Thirty-sided Dice', 'sides': 30, 'emoji': '๐ฏ'},
|
15 |
+
{'name': 'One Hundred-sided Dice', 'sides': 100, 'emoji': '๐ฒ'}]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
|
|
17 |
if 'name' not in st.session_state:
|
18 |
st.session_state.name = ''
|
19 |
+
if 'dice_roll_history' not in st.session_state:
|
20 |
+
st.session_state.dice_roll_history = pd.DataFrame()
|
21 |
|
22 |
+
dice_type = st.selectbox('Choose a type of dice', dice_types, format_func=lambda d: f"{d['name']} {d['emoji']}")
|
|
|
23 |
num_rolls = st.slider('How many times do you want to roll the dice?', 1, 1000000, 1000)
|
24 |
|
25 |
+
rolls = np.random.randint(1, dice_type['sides'] + 1, num_rolls, dtype=np.uint64)
|
|
|
|
|
|
|
26 |
roll_counts = pd.Series(rolls).value_counts().sort_index()
|
27 |
|
28 |
+
fig = px.sunburst(names=[f'Roll {i}' for i in roll_counts.index],
|
29 |
+
parents=['Dice Rolls'] * dice_type['sides'],
|
30 |
+
values=roll_counts.values,
|
31 |
+
color=[f'Roll {i}' for i in roll_counts.index],
|
32 |
+
color_discrete_sequence=px.colors.qualitative.Dark24,
|
33 |
+
maxdepth=2)
|
34 |
+
fig.update_layout(title='Dice Roll Distribution', margin=dict(l=20, r=20, t=40, b=20), width=800, height=600)
|
|
|
|
|
35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
show_labels = st.checkbox('Show Labels', value=True)
|
37 |
if not show_labels:
|
38 |
fig.update_traces(textinfo='none')
|
39 |
fig.show()
|
40 |
|
41 |
+
bonus_match = False
|
42 |
+
for dice in dice_types:
|
43 |
+
if rolls[0] == dice['sides']:
|
44 |
+
bonus_match = True
|
45 |
+
bonus_dice_type = dice['name']
|
46 |
+
bonus_dice_emoji = dice['emoji']
|
47 |
+
break
|
48 |
+
|
49 |
+
dice_roll_history = st.session_state.dice_roll_history
|
50 |
+
new_roll_data = pd.DataFrame({'Roll': rolls,
|
51 |
+
'Count': np.ones(num_rolls, dtype=np.uint64),
|
52 |
+
'DiceNumberOfSides': [dice_type['sides']] * num_rolls,
|
53 |
+
'DiceRollerName': [st.session_state.name] * num_rolls})
|
54 |
+
if bonus_match:
|
55 |
+
new_roll_data['BonusMatchToDiceName'] = [bonus_dice_type] * num_rolls
|
56 |
+
new_roll_data['BonusMatchToDiceEmoji'] = [bonus_dice_emoji] * num_rolls
|
57 |
+
dice_roll_history = dice_roll_history.append(new_roll_data, ignore_index=True)
|
58 |
+
st.session_state.dice_roll_history = dice_roll_history
|
59 |
+
|
60 |
+
if st.button('Download Results'):
|
61 |
+
filename = f'dice_roll_history_{st.session_state.name}.csv'
|
62 |
+
st.download_button(label='Download CSV', data=dice_roll_history.to_csv(index=False), file_name=filename, mime='text/csv')
|
63 |
+
|
64 |
+
if st.session_state.dice_roll_history.shape[0] > 0:
|
65 |
+
st.markdown('### Dice Roll History')
|
66 |
+
st.dataframe(st.session_state.dice_roll_history)
|
67 |
+
|
68 |
+
|
69 |
|
|
|
|
|
|