awacke1 commited on
Commit
5e13f0c
Β·
1 Parent(s): 4c86fb8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -19
app.py CHANGED
@@ -2,41 +2,79 @@ import os
2
  import pandas as pd
3
  import streamlit as st
4
 
 
5
  st.set_page_config(page_title="Dice Roller", page_icon="🎲")
 
 
6
  st.title("🎲 Dice Roller")
7
 
8
- if 'username' not in st.session_state: st.session_state.username = st.text_input("πŸ‘€ Username", max_chars=50)
 
 
 
 
 
 
 
 
 
 
9
 
10
- dice_types = {"🎲 Six-sided dice": {"sides": 6, "name": "d6", "emoji": "🎲"},
11
- "🎲 Thirty-sided dice": {"sides": 30, "name": "d30", "emoji": "🎲"},
12
- "🎲 Twenty-sided dice": {"sides": 20, "name": "d20", "emoji": "🎲"},
13
- "🎲 One hundred-sided dice": {"sides": 100, "name": "d100", "emoji": "πŸ’―"}}
 
 
 
14
 
 
15
  dice_type = st.selectbox("🎲 Select a dice type", options=list(dice_types.keys()), format_func=lambda x: x.split(' ')[-2])
16
- num_rolls = st.slider("🎲 How many times do you want to roll the dice?", 1, 1000000, 100)
17
- #rolls = [str(pd.Series([dice_types[dice_type]["sides"]] * num_rolls).apply(pd.Series.sample, replace=True))]
18
- rolls = pd.Series([dice_types[dice_type]["sides"]] * num_rolls).apply(pd.Series.sample, replace=True).astype(str).tolist()
19
 
20
- rolls = rolls[0].strip('[]').split('\n')
 
21
 
 
22
  if st.button("🎲 Roll Dice"):
23
- display_roll = [int(i) for i in rolls]
 
 
 
 
24
  st.write(f"{dice_types[dice_type]['emoji']} You rolled {len(display_roll)} {dice_types[dice_type]['name']} dice: {', '.join(map(str, display_roll))}.")
25
- bonus_match, count = (None, sum(display_roll))
 
 
 
26
  if count % dice_types[dice_type]['sides'] == 0:
27
  bonus_match = dice_types[dice_type]['name']
 
28
  st.write(f"πŸŽ‰ Bonus match! You rolled a multiple of {dice_types[dice_type]['sides']}.")
29
- df = pd.DataFrame({'Roll': rolls, 'Count': count, 'DiceNumberOfSides': dice_types[dice_type]['sides']})
 
 
 
 
30
  file_exists = os.path.exists("SharedState.csv")
31
- if file_exists: df.to_csv("SharedState.csv", mode='a', header=False, index=False)
32
- else: df.to_csv("SharedState.csv", mode='w', header=True, index=False)
33
 
 
 
 
 
 
 
 
 
34
  download_roll_history = st.checkbox("πŸ“₯ Download Roll History", value=True)
35
- if os.path.exists("SharedState.csv"):
36
- if download_roll_history:
37
- with open("SharedState.csv", "rb") as f:
38
- st.download_button(label="πŸ“₯ Download Roll History", data=f, file_name="SharedState.csv", mime="text/csv")
39
- else: st.warning("No roll history found.")
 
 
 
 
40
 
41
  st.write("""
42
 
 
2
  import pandas as pd
3
  import streamlit as st
4
 
5
+ # Set page title and icon
6
  st.set_page_config(page_title="Dice Roller", page_icon="🎲")
7
+
8
+ # Set page title
9
  st.title("🎲 Dice Roller")
10
 
11
+ # Check if username is already set, if not, create a text input to set it
12
+ if 'username' not in st.session_state:
13
+ st.session_state.username = st.text_input("πŸ‘€ Username", max_chars=50)
14
+
15
+ # Define the dice types
16
+ dice_types = {
17
+ "🎲 Six-sided dice": {"sides": 6, "name": "d6", "emoji": "🎲"},
18
+ "🎲 Thirty-sided dice": {"sides": 30, "name": "d30", "emoji": "🎲"},
19
+ "🎲 Twenty-sided dice": {"sides": 20, "name": "d20", "emoji": "🎲"},
20
+ "🎲 One hundred-sided dice": {"sides": 100, "name": "d100", "emoji": "πŸ’―"},
21
+ }
22
 
23
+ # Create a function to build the rolls pandas series to be a list of dictionaries
24
+ def build_rolls(dice_type, num_rolls):
25
+ sides = dice_types[dice_type]["sides"]
26
+ rolls = pd.Series([sides] * num_rolls).apply(pd.Series.sample, replace=True)
27
+ rolls = rolls.tolist()
28
+ count = sum(rolls)
29
+ return [{"Roll": roll, "Count": count, "DiceNumberOfSides": sides} for roll in rolls]
30
 
31
+ # Create a selectbox to choose the dice type
32
  dice_type = st.selectbox("🎲 Select a dice type", options=list(dice_types.keys()), format_func=lambda x: x.split(' ')[-2])
 
 
 
33
 
34
+ # Create a slider to choose the number of rolls
35
+ num_rolls = st.slider("🎲 How many times do you want to roll the dice?", 1, 1000000, 100)
36
 
37
+ # Create a button to roll the dice
38
  if st.button("🎲 Roll Dice"):
39
+ # Build the rolls list of dictionaries
40
+ rolls = build_rolls(dice_type, num_rolls)
41
+
42
+ # Display the rolls
43
+ display_roll = [roll["Roll"] for roll in rolls]
44
  st.write(f"{dice_types[dice_type]['emoji']} You rolled {len(display_roll)} {dice_types[dice_type]['name']} dice: {', '.join(map(str, display_roll))}.")
45
+
46
+ # Check if the count is a multiple of the dice sides to see if there's a bonus match
47
+ bonus_match = None
48
+ count = rolls[0]["Count"]
49
  if count % dice_types[dice_type]['sides'] == 0:
50
  bonus_match = dice_types[dice_type]['name']
51
+ bonus_emoji = dice_types[dice_type]['emoji']
52
  st.write(f"πŸŽ‰ Bonus match! You rolled a multiple of {dice_types[dice_type]['sides']}.")
53
+
54
+ # Create a DataFrame with the rolls, count, and dice number of sides
55
+ df = pd.DataFrame(rolls)
56
+
57
+ # Check if the file exists
58
  file_exists = os.path.exists("SharedState.csv")
 
 
59
 
60
+ # Append the new rolls to the file
61
+ if file_exists:
62
+ df.to_csv("SharedState.csv", mode='a', header=False, index=False)
63
+ # If the file doesn't exist, create it and write the headers
64
+ else:
65
+ df.to_csv("SharedState.csv", mode='w', header=True, index=False)
66
+
67
+ # Create a checkbox to download the roll history
68
  download_roll_history = st.checkbox("πŸ“₯ Download Roll History", value=True)
69
+
70
+ # Check if the file exists and display it as a download link
71
+ if os.path.exists("SharedState.csv") and download_roll_history:
72
+ with open("SharedState.csv", "r") as f:
73
+ file_contents = f.read()
74
+ st.markdown(f"<a href='data:file/csv;base64,{file_contents.encode().b64encode()}'>πŸ“₯ Download Roll History</a>", unsafe_allow_html=True)
75
+
76
+
77
+
78
 
79
  st.write("""
80