awacke1's picture
Update app.py
1cc2da1
raw
history blame
6.04 kB
import os
import streamlit as st
import numpy as np
import pandas as pd
import plotly.express as px
st.set_page_config(layout='wide')
st.title('๐ŸŽฒ Random Dice Game')
dice_types = [{'name': 'Six-sided Dice', 'sides': 6, 'emoji': '๐ŸŽฒ'},
{'name': 'Twenty-sided Dice', 'sides': 20, 'emoji': '๐ŸŽฏ'},
{'name': 'Thirty-sided Dice', 'sides': 30, 'emoji': '๐ŸŽฏ'},
{'name': 'One Hundred-sided Dice', 'sides': 100, 'emoji': '๐Ÿ’ฏ'}]
if 'username' not in st.session_state:
st.session_state.username = ''
if 'dice_roll_history' not in st.session_state:
st.session_state.dice_roll_history = pd.DataFrame()
def username_input():
st.text_input('๐Ÿ‘ค Username', key='username')
def display_dice_roll_distribution():
dice_type = st.selectbox('๐ŸŽฒ Choose a type of dice', dice_types, format_func=lambda d: f"{d['name']} {d['emoji']}")
num_rolls = st.slider('๐Ÿ”„ How many times do you want to roll the dice?', 1, 1000000, 1000)
rolls = np.random.randint(1, dice_type['sides'] + 1, num_rolls, dtype=np.uint64)
roll_counts = pd.Series(rolls).value_counts().sort_index()
fig = px.sunburst(names=[f'Roll {i}' for i in roll_counts.index],
parents=['Dice Rolls'] * dice_type['sides'],
values=roll_counts.values,
color=[f'Roll {i}' for i in roll_counts.index],
color_discrete_sequence=px.colors.qualitative.Dark24,
maxdepth=2)
fig.update_layout(title='๐Ÿ“ˆ Dice Roll Distribution', margin=dict(l=20, r=20, t=40, b=20), width=800, height=600)
show_labels = st.checkbox('๐Ÿท๏ธ Show Labels', value=True)
if not show_labels:
fig.update_traces(textinfo='none')
fig.show()
return dice_type, rolls
def display_bonus_match(bonus_match, bonus_dice_type, bonus_dice_emoji):
if bonus_match:
st.success(f'๐ŸŽ‰ You rolled a {bonus_dice_type} {bonus_dice_emoji} on the first roll!')
def update_dice_roll_history(dice_type, rolls):
history = st.session_state.dice_roll_history
count = st.session_state.count
dice_name = dice_type['name']
dice_emoji = dice_type['emoji']
bonus_match = None
# Check if the count matches the number of sides of the dice
if count > 0 and count % dice_type['sides'] == 0:
bonus_match = dice_type['name']
bonus_emoji = dice_type['emoji']
# Update the dice roll history
if history is None:
history = pd.DataFrame({'Roll': rolls, 'Count': count, 'DiceNumberOfSides': dice_type['sides']})
else:
new_history = pd.DataFrame({'Roll': rolls, 'Count': count, 'DiceNumberOfSides': dice_type['sides']})
history = history.append(new_history, ignore_index=True)
if bonus_match is not None:
history['BonusMatchToDiceName'] = bonus_match
history['BonusMatchToDiceEmoji'] = bonus_emoji
# Add username to history
history['Username'] = st.session_state.username
st.session_state.dice_roll_history = history
return history
def display_dice_roll_results():
download_history = st.checkbox('๐Ÿ“ฅ Download Roll History', value=True)
if download_history:
csv_filename = f'dice_roll_history_{st.session_state.username}.csv'
st.markdown(f'๐Ÿ“ฅ [Download roll history]({csv_filename})')
dice_roll_history = st.session_state.dice_roll_history[['Roll', 'Count', 'DiceNumberOfSides']]
if st.checkbox('๐ŸŽ‰ Show Bonus Matches', value=True):
dice_roll_history = dice_roll_history.join(
st.session_state.dice_roll_history[['BonusMatchToDiceName', 'BonusMatchToDiceEmoji']])
if st.checkbox('๐Ÿ‘ค Show Username', value=True):
dice_roll_history['Username'] = st.session_state.dice_roll_history['Username']
dice_roll_history.to_csv(csv_filename, index=False)
dice_roll_history_files = [f for f in os.listdir() if f.startswith('dice_roll_history_') and f.endswith('.csv')]
if len(dice_roll_history_files) > 0:
st.write('๐Ÿ“š Roll History Files:')
for history_file in dice_roll_history_files:
st.markdown(f'[Download {history_file}]({history_file})')
st.sidebar.title('๐Ÿ”ง Settings')
username_input()
if st.button('๐ŸŽฒ Roll Dice'):
dice_type, rolls = display_dice_roll_distribution()
display_bonus_match(update_dice_roll_history(dice_type, rolls), dice_type['name'], dice_type['emoji'])
display_dice_roll_results()
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")
""")