awacke1 commited on
Commit
71c2520
ยท
1 Parent(s): 982e84c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -12
app.py CHANGED
@@ -29,6 +29,10 @@ dice_types = [
29
  }
30
  ]
31
 
 
 
 
 
32
  # Get user input for the type of dice and number of rolls
33
  dice_type = st.selectbox('Choose a type of dice', dice_types)
34
  num_rolls = st.slider('How many times do you want to roll the dice?', 1, 1000000, 1000)
@@ -65,15 +69,22 @@ fig.show()
65
 
66
  # Allow user to download roll data as CSV file with their name and dice type
67
  if st.button('Download Roll Data as CSV'):
68
- roll_data = pd.DataFrame({
69
- 'Roll': rolls,
70
- 'Count': np.ones(num_rolls),
71
- 'DiceNumberOfSides': [dice_type['sides']] * num_rolls
72
- })
73
- filename = f'roll_data_{dice_type["name"].replace(" ", "_")}_{st.session_state.name}.csv'
74
- st.download_button(
75
- label='Download CSV',
76
- data=roll_data.to_csv(index=False),
77
- file_name=filename,
78
- mime='text/csv'
79
- )
 
 
 
 
 
 
 
 
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)
 
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