awacke1 commited on
Commit
27798b9
ยท
1 Parent(s): 71c2520

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -68
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
- # Define the available types of dice
9
- dice_types = [
10
- {
11
- 'name': 'Six-sided Dice ๐ŸŽฒ',
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
- # Get user input for the type of dice and number of rolls
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
- # Generate dice rolls
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
- # Create a sunburst chart of the roll counts
47
- fig = px.sunburst(
48
- names=[f'Roll {i}' for i in roll_counts.index],
49
- parents=['Dice Rolls'] * dice_type['sides'],
50
- values=roll_counts.values,
51
- color=[f'Roll {i}' for i in roll_counts.index],
52
- color_discrete_sequence=px.colors.qualitative.Dark24,
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
- # Allow user to download roll data as CSV file with their name and dice type
71
- if st.button('Download Roll Data as CSV'):
72
- if st.session_state.name == '':
73
- st.warning('Please enter a name before downloading the roll data')
74
- else:
75
- roll_data = pd.DataFrame({
76
- 'Roll': rolls,
77
- 'Count': np.ones(num_rolls),
78
- 'DiceNumberOfSides': [dice_type['sides']] * num_rolls
79
- })
80
- filename = f'roll_data_{dice_type["name"].replace(" ", "_")}_{st.session_state.name}.csv'
81
- st.download_button(
82
- label='Download CSV',
83
- data=roll_data.to_csv(index=False),
84
- file_name=filename,
85
- mime='text/csv'
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