Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -3,30 +3,25 @@
|
|
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 |
-
|
7 |
import streamlit as st
|
8 |
import numpy as np
|
9 |
import pandas as pd
|
10 |
import plotly.express as px
|
11 |
-
|
12 |
st.title('Random Dice Game')
|
13 |
-
|
14 |
dice_types = [{'name': 'Six-sided Dice', 'sides': 6, 'emoji': '๐ฒ'},
|
15 |
-
{'name': 'Twenty-sided Dice', 'sides': 20, 'emoji': '
|
16 |
-
{'name': 'Thirty-sided Dice', 'sides': 30, 'emoji': '
|
17 |
-
{'name': 'One Hundred-sided Dice', 'sides': 100, 'emoji': '
|
18 |
-
|
19 |
-
|
20 |
-
st.session_state.name = ''
|
21 |
if 'dice_roll_history' not in st.session_state:
|
22 |
st.session_state.dice_roll_history = pd.DataFrame()
|
23 |
-
|
24 |
dice_type = st.selectbox('Choose a type of dice', dice_types, format_func=lambda d: f"{d['name']} {d['emoji']}")
|
25 |
num_rolls = st.slider('How many times do you want to roll the dice?', 1, 1000000, 1000)
|
26 |
-
|
27 |
rolls = np.random.randint(1, dice_type['sides'] + 1, num_rolls, dtype=np.uint64)
|
28 |
roll_counts = pd.Series(rolls).value_counts().sort_index()
|
29 |
-
|
30 |
fig = px.sunburst(names=[f'Roll {i}' for i in roll_counts.index],
|
31 |
parents=['Dice Rolls'] * dice_type['sides'],
|
32 |
values=roll_counts.values,
|
@@ -34,12 +29,10 @@ fig = px.sunburst(names=[f'Roll {i}' for i in roll_counts.index],
|
|
34 |
color_discrete_sequence=px.colors.qualitative.Dark24,
|
35 |
maxdepth=2)
|
36 |
fig.update_layout(title='Dice Roll Distribution', margin=dict(l=20, r=20, t=40, b=20), width=800, height=600)
|
37 |
-
|
38 |
show_labels = st.checkbox('Show Labels', value=True)
|
39 |
if not show_labels:
|
40 |
fig.update_traces(textinfo='none')
|
41 |
fig.show()
|
42 |
-
|
43 |
bonus_match = False
|
44 |
for dice in dice_types:
|
45 |
if rolls[0] == dice['sides']:
|
@@ -47,26 +40,35 @@ for dice in dice_types:
|
|
47 |
bonus_dice_type = dice['name']
|
48 |
bonus_dice_emoji = dice['emoji']
|
49 |
break
|
50 |
-
|
51 |
dice_roll_history = st.session_state.dice_roll_history
|
52 |
new_roll_data = pd.DataFrame({'Roll': rolls,
|
53 |
'Count': np.ones(num_rolls, dtype=np.uint64),
|
54 |
'DiceNumberOfSides': [dice_type['sides']] * num_rolls,
|
55 |
-
'
|
56 |
if bonus_match:
|
57 |
new_roll_data['BonusMatchToDiceName'] = [bonus_dice_type] * num_rolls
|
58 |
new_roll_data['BonusMatchToDiceEmoji'] = [bonus_dice_emoji] * num_rolls
|
59 |
dice_roll_history = dice_roll_history.append(new_roll_data, ignore_index=True)
|
60 |
st.session_state.dice_roll_history = dice_roll_history
|
61 |
-
|
62 |
if st.button('Download Results'):
|
63 |
-
filename = f'dice_roll_history_{st.session_state.
|
|
|
|
|
|
|
64 |
st.download_button(label='Download CSV', data=dice_roll_history.to_csv(index=False), file_name=filename, mime='text/csv')
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
st.
|
69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
st.write("""
|
72 |
๐ Bread
|
|
|
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('Random Dice Game')
|
|
|
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,
|
|
|
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']:
|
|
|
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
|