Spaces:
Runtime error
Runtime error
import os | |
import pandas as pd | |
import streamlit as st | |
# Set page title and icon | |
st.set_page_config(page_title="Dice Roller", page_icon="๐ฒ") | |
# Set page title | |
st.title("๐ฒ Dice Roller") | |
# Check if username is already set, if not, create a text input to set it | |
if 'username' not in st.session_state: | |
st.session_state.username = st.text_input("๐ค Username", max_chars=50) | |
# Define the dice types | |
dice_types = { | |
"๐ฒ Six-sided dice": {"sides": 6, "name": "d6", "emoji": "๐ฒ"}, | |
"๐ฒ Thirty-sided dice": {"sides": 30, "name": "d30", "emoji": "๐ฒ"}, | |
"๐ฒ Twenty-sided dice": {"sides": 20, "name": "d20", "emoji": "๐ฒ"}, | |
"๐ฒ One hundred-sided dice": {"sides": 100, "name": "d100", "emoji": "๐ฏ"}, | |
} | |
# Create a selectbox to choose the dice type | |
dice_type = st.selectbox("๐ฒ Select a dice type", options=list(dice_types.keys()), format_func=lambda x: x.split(' ')[-2]) | |
# Create a slider to choose the number of rolls | |
num_rolls = st.slider("๐ฒ How many times do you want to roll the dice?", 1, 1000000, 100) | |
# Create a string with the rolls for the selected dice type | |
rolls = [str(pd.Series([dice_types[dice_type]["sides"]] * num_rolls).apply(pd.Series.sample, replace=True))] | |
rolls = rolls[0].strip('[]').split('\n') | |
# Create a button to roll the dice | |
if st.button("๐ฒ Roll Dice"): | |
# Display the rolls | |
display_roll = [int(i) for i in rolls] | |
st.write(f"{dice_types[dice_type]['emoji']} You rolled {len(display_roll)} {dice_types[dice_type]['name']} dice: {', '.join(map(str, display_roll))}.") | |
# Check if the count is a multiple of the dice sides to see if there's a bonus match | |
bonus_match = None | |
count = sum(display_roll) | |
if count % dice_types[dice_type]['sides'] == 0: | |
bonus_match = dice_types[dice_type]['name'] | |
bonus_emoji = dice_types[dice_type]['emoji'] | |
st.write(f"๐ Bonus match! You rolled a multiple of {dice_types[dice_type]['sides']}.") | |
# Create a DataFrame with the rolls, count, and dice number of sides | |
df = pd.DataFrame({'Roll': rolls, 'Count': count, 'DiceNumberOfSides': dice_types[dice_type]['sides']}) | |
# Check if the file exists | |
file_exists = os.path.exists("SharedState.csv") | |
# Append the new rolls to the file | |
if file_exists: | |
df.to_csv("SharedState.csv", mode='a', header=False, index=False) | |
# If the file doesn't exist, create it and write the headers | |
else: | |
df.to_csv("SharedState.csv", mode='w', header=True, index=False) | |
# Create a checkbox to download the roll history | |
download_roll_history = st.checkbox("๐ฅ Download Roll History", value=True) | |
# Check if the file exists and display it as a download link | |
if os.path.exists("SharedState.csv"): | |
# Read the file into a DataFrame | |
data = pd.read_csv("SharedState.csv") | |
# Check if the user wants to include their name in the output | |
if st.checkbox("๐ป Include Username in Output", value=True): | |
# Add the username to the DataFrame | |
data["DiceRollerName"] = st.session_state.username | |
# Check if there is a bonus match and add the bonus match column to the DataFrame | |
if bonus_match: | |
data["BonusMatchToDiceName"] = bonus_match | |
data["BonusMatchToDiceEmoji"] = bonus_emoji | |
# Check if the user wants to download the file | |
if download_roll_history: | |
st.download_button("๐ฅ Download Roll History", data.to_csv(index=False), f"shared_state_{st.session_state.username}.csv", "text/csv") | |
else: | |
# Remove the username from the DataFrame | |
data = data.drop(columns=["DiceRollerName"]) | |
# Check if there is a bonus match and add the bonus match column to the DataFrame | |
if bonus_match: | |
data["BonusMatchToDiceName"] = bonus_match | |
data["BonusMatchToDiceEmoji"] = bonus_emoji | |
# Check if the user wants to download the file | |
if download_roll_history: | |
st.download_button("๐ฅ Download Roll History", data.to_csv(index=False), "shared_state.csv", "text/csv") | |
else: | |
st.write("๐ No roll history available.") | |
st.write(""" | |
๐ Bread, ๐ฅ Croissant, ๐ฅ Baguette Bread, ๐ซ Flatbread, ๐ฅจ Pretzel, ๐ฅฏ Bagel, ๐ฅ Pancakes, ๐ง Waffle, ๐ง Cheese Wedge, ๐ Meat on Bone, ๐ Poultry Leg, ๐ฅฉ Cut of Meat, ๐ฅ Bacon, ๐ Hamburger, ๐ French Fries, ๐ Pizza, ๐ญ Hot Dog, ๐ฅช Sandwich, ๐ฎ Taco, ๐ฏ Burrito, ๐ซ Tamale, ๐ฅ Stuffed Flatbread, ๐ง Falafel, ๐ฅ Egg, ๐ณ Cooking, ๐ฅ Shallow Pan of Food, ๐ฒ Pot of Food, ๐ซ Fondue, ๐ฅฃ Bowl with Spoon, ๐ฅ Green Salad, ๐ฟ Popcorn, ๐ง Butter, ๐ง Salt, ๐ฅซ Canned Food, ๐ฑ Bento Box, ๐ Rice Cracker, ๐ Rice Ball, ๐ Cooked Rice, ๐ Curry Rice, ๐ Steaming Bowl, ๐ Spaghetti, ๐ Roasted Sweet Potato, ๐ข Oden, ๐ฃ Sushi, ๐ค Fried Shrimp, ๐ฅ Fish Cake with Swirl, ๐ฅฎ Moon Cake, ๐ก Dango, ๐ฅ Dumpling, ๐ฅ Fortune Cookie, ๐ฅก Takeout Box, ๐ฆช Oyster, ๐ฆ Soft Ice Cream, ๐ง Shaved Ice, ๐จ Ice Cream, ๐ฉ Doughnut, ๐ช Cookie, ๐ Birthday Cake, ๐ฐ Shortcake, ๐ง Cupcake, ๐ฅง Pie, ๐ซ Chocolate Bar, ๐ฌ Candy, ๐ญ Lollipop, ๐ฎ Custard, ๐ฏ Honey Pot, ๐ผ Baby Bottle, ๐ฅ Glass of Milk, โ Hot Beverage, ๐ซ Teapot, ๐ต Teacup Without Handle, ๐ถ Sake, ๐พ Bottle with Popping Cork, ๐ท Wine Glass, ๐ธ Cocktail Glass, ๐น Tropical Drink, ๐บ Beer Mug, ๐ป Clinking Beer Mugs, ๐ฅ Clinking Glasses, ๐ฅ Tumbler Glass, ๐ซ Pouring Liquid, ๐ฅค Cup with Straw, ๐ง Bubble Tea, ๐ง Beverage Box, ๐ง Mate, ๐ง Ice, ๐ฅข Chopsticks, ๐ฝ๏ธ Fork and Knife with Plate, ๐ด Fork and Knife, ๐ฅ Spoon, ๐ซ Jar") | |
""") | |