import streamlit as st import numpy as np import pandas as pd import plotly.express as px st.title('Random Dice Game') # Define the available types of dice 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': '🎲' } ] # Get user input for the type of dice and number of rolls dice_type = st.selectbox('Choose a type of dice', dice_types) num_rolls = st.slider('How many times do you want to roll the dice?', 1, 1000000, 1000) # Generate dice rolls rolls = np.random.randint(1, dice_type['sides']+1, num_rolls) # Count the number of occurrences of each roll roll_counts = pd.Series(rolls).value_counts().sort_index() # Create a sunburst chart of the roll counts 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 ) # Customize the chart layout fig.update_layout( title='Dice Roll Distribution', margin=dict(l=20, r=20, t=40, b=20), width=800, height=600 ) # Add UI controls to modify the chart show_labels = st.checkbox('Show Labels', value=True) if not show_labels: fig.update_traces(textinfo='none') fig.show() # Allow user to download roll data as CSV file with their name and dice type if st.button('Download Roll Data as CSV'): roll_data = pd.DataFrame({ 'Roll': rolls, 'Count': np.ones(num_rolls), 'DiceNumberOfSides': [dice_type['sides']] * num_rolls }) filename = f'roll_data_{dice_type["name"].replace(" ", "_")}_{st.session_state.name}.csv' st.download_button( label='Download CSV', data=roll_data.to_csv(index=False), file_name=filename, mime='text/csv' )