import streamlit as st import numpy as np import pandas as pd import plotly.express as px def build_rolls(dice_type, num_rolls): rolls = np.random.randint(1, dice_type['sides'] + 1, num_rolls, dtype=np.int64) roll_counts = pd.Series(rolls).value_counts().sort_index() return rolls, roll_counts def update_dice_roll_history(dice_roll_history, name, rolls, num_rolls, dice_type, bonus_dice_type='', bonus_dice_emoji=''): new_roll_data = pd.DataFrame({ 'Roll': rolls, 'Count': np.ones(num_rolls, dtype=np.int64), 'DiceNumberOfSides': [dice_type['sides']] * num_rolls, 'DiceRollerName': [name] * num_rolls }) if bonus_dice_type: new_roll_data['BonusMatchToDiceName'] = [bonus_dice_type] * num_rolls new_roll_data['BonusMatchToDiceEmoji'] = [bonus_dice_emoji] * num_rolls dice_roll_history = dice_roll_history.append(new_roll_data, ignore_index=True) return dice_roll_history def download_results(dataframe, name): filename = f'dice_roll_history_{name}.csv' st.download_button( label='Download CSV', data=dataframe.to_csv(index=False), file_name=filename, mime='text/csv' ) def main(): 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 'name' not in st.session_state: st.session_state.name = 'Username' if 'dice_roll_history' not in st.session_state: st.session_state.dice_roll_history = pd.DataFrame() 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, value=1000, step=1 ) rolls, roll_counts = build_rolls(dice_type, num_rolls) 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') st.plotly_chart(fig) bonus_match = False bonus_dice_type = '' bonus_dice_emoji = '' for dice in dice_types: if rolls[0] == dice['sides']: bonus_match = True bonus_dice_type = dice['name'] bonus_dice_emoji = dice['emoji'] break dice_roll_history = st.session_state.dice_roll_history dice_roll_history = update_dice_roll_history( dice_roll_history, st.session_state.name, rolls, num_rolls, dice_type, bonus_dice_type, bonus_dice_emoji ) st.session_state.dice_roll_history = dice_roll_history if st.button('Download Results'): download_results(dice_roll_history, st.session_state.name) if dice_roll_history.shape[0] > 0: st.markdown('### Dice Roll History') st.dataframe(dice_roll_history) 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" """) runitforme = main()