awacke1 commited on
Commit
1f9a1e6
ยท
1 Parent(s): ce7029c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -15
app.py CHANGED
@@ -8,31 +8,39 @@ 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']:
@@ -40,6 +48,7 @@ for dice in dice_types:
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),
@@ -48,27 +57,20 @@ new_roll_data = pd.DataFrame({'Roll': 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
 
8
  import numpy as np
9
  import pandas as pd
10
  import plotly.express as px
11
+
12
  st.set_page_config(layout='wide')
13
  st.title('Any ๐ŸŽฒDice๐ŸŽฒ Way - STEM Math and Data Science Numerical Potluck')
14
+
15
  dice_types = [{'name': 'Six-sided Dice', 'sides': 6, 'emoji': '๐ŸŽฒ'},
16
  {'name': 'Twenty-sided Dice', 'sides': 20, 'emoji': '๐Ÿฟ'},
17
  {'name': 'Thirty-sided Dice', 'sides': 30, 'emoji': '๐Ÿฅ—'},
18
  {'name': 'One Hundred-sided Dice', 'sides': 100, 'emoji': '๐ŸŽ‚'}]
19
+
20
  if 'username' not in st.session_state:
21
  st.session_state.username = ''
22
  if 'dice_roll_history' not in st.session_state:
23
  st.session_state.dice_roll_history = pd.DataFrame()
24
+
25
  dice_type = st.selectbox('Choose a type of dice', dice_types, format_func=lambda d: f"{d['name']} {d['emoji']}")
26
  num_rolls = st.slider('How many times do you want to roll the dice?', 1, 1000000, 1000)
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,
33
  color=[f'Roll {i}' for i in roll_counts.index],
34
  color_discrete_sequence=px.colors.qualitative.Dark24,
35
  maxdepth=2)
36
+
37
  fig.update_layout(title='Dice Roll Distribution', margin=dict(l=20, r=20, t=40, b=20), width=800, height=600)
38
+
39
  show_labels = st.checkbox('Show Labels', value=True)
40
  if not show_labels:
41
  fig.update_traces(textinfo='none')
42
  fig.show()
43
+
44
  bonus_match = False
45
  for dice in dice_types:
46
  if rolls[0] == dice['sides']:
 
48
  bonus_dice_type = dice['name']
49
  bonus_dice_emoji = dice['emoji']
50
  break
51
+
52
  dice_roll_history = st.session_state.dice_roll_history
53
  new_roll_data = pd.DataFrame({'Roll': rolls,
54
  'Count': np.ones(num_rolls, dtype=np.uint64),
 
57
  if bonus_match:
58
  new_roll_data['BonusMatchToDiceName'] = [bonus_dice_type] * num_rolls
59
  new_roll_data['BonusMatchToDiceEmoji'] = [bonus_dice_emoji] * num_rolls
60
+
61
  dice_roll_history = dice_roll_history.append(new_roll_data, ignore_index=True)
62
  st.session_state.dice_roll_history = dice_roll_history
63
+
64
  include_name_column = st.checkbox('Include Username Column in Downloaded CSV', value=True)
65
+
66
  if st.button('Download Results'):
67
+ if include_name_column:
68
+ filename = f'dice_roll_history_{st.session_state.username}{dice_type["emoji"]}.csv'
69
+ else:
70
  dice_roll_history = dice_roll_history.drop(columns=['Username'])
71
  filename = f'dice_roll_history{dice_type["emoji"]}.csv'
72
+ st.download_button(label='Download CSV', data=dice_roll_history.to_csv(index=False), file_name=filename, mime
73
+
 
 
 
 
 
 
 
 
 
 
 
74
 
75
  st.write("""
76
  ๐Ÿž Bread