Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,66 +1,68 @@
|
|
1 |
-
import
|
2 |
-
import numpy as np
|
3 |
import pandas as pd
|
4 |
-
import
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
if '
|
14 |
-
st.session_state.
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
|
|
|
|
|
|
|
64 |
|
65 |
st.write("""
|
66 |
|
|
|
1 |
+
import os
|
|
|
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 selectbox to choose the dice type
|
24 |
+
dice_type = st.selectbox("π² Select a dice type", options=list(dice_types.keys()), format_func=lambda x: x.split(' ')[-2])
|
25 |
+
|
26 |
+
# Create a slider to choose the number of rolls
|
27 |
+
num_rolls = st.slider("π² How many times do you want to roll the dice?", 1, 1000000, 100)
|
28 |
+
|
29 |
+
# Create a string with the rolls for the selected dice type
|
30 |
+
rolls = [str(pd.Series([dice_types[dice_type]["sides"]] * num_rolls).apply(pd.Series.sample, replace=True))]
|
31 |
+
rolls = rolls[0].strip('[]').split('\n')
|
32 |
+
|
33 |
+
# Create a button to roll the dice
|
34 |
+
if st.button("π² Roll Dice"):
|
35 |
+
# Display the rolls
|
36 |
+
display_roll = [int(i) for i in rolls]
|
37 |
+
st.write(f"{dice_types[dice_type]['emoji']} You rolled {len(display_roll)} {dice_types[dice_type]['name']} dice: {', '.join(map(str, display_roll))}.")
|
38 |
+
|
39 |
+
# Check if the count is a multiple of the dice sides to see if there's a bonus match
|
40 |
+
bonus_match = None
|
41 |
+
count = sum(display_roll)
|
42 |
+
if count % dice_types[dice_type]['sides'] == 0:
|
43 |
+
bonus_match = dice_types[dice_type]['name']
|
44 |
+
bonus_emoji = dice_types[dice_type]['emoji']
|
45 |
+
st.write(f"π Bonus match! You rolled a multiple of {dice_types[dice_type]['sides']}.")
|
46 |
+
|
47 |
+
# Create a DataFrame with the rolls, count, and dice number of sides
|
48 |
+
df = pd.DataFrame({'Roll': rolls, 'Count': count, 'DiceNumberOfSides': dice_types[dice_type]['sides']})
|
49 |
+
|
50 |
+
# Check if the file exists
|
51 |
+
file_exists = os.path.exists("SharedState.csv")
|
52 |
+
|
53 |
+
# Append the new rolls to the file
|
54 |
+
if file_exists:
|
55 |
+
df.to_csv("SharedState.csv", mode='a', header=False, index=False)
|
56 |
+
# If the file doesn't exist, create it and write the headers
|
57 |
+
else:
|
58 |
+
df.to_csv("SharedState.csv", mode='w', header=True, index=False)
|
59 |
+
|
60 |
+
# Create a checkbox to download the roll history
|
61 |
+
download_roll_history = st.checkbox("π₯ Download Roll History", value=True)
|
62 |
|
63 |
+
# Check if the file exists and display it as a download link
|
64 |
+
if os.path.exists("SharedState.csv"):
|
65 |
+
data
|
66 |
|
67 |
st.write("""
|
68 |
|