Spaces:
Runtime error
Runtime error
import streamlit as st | |
import numpy as np | |
import pandas as pd | |
import plotly.express as px | |
st.title('Random Dice Games for STEM Math and Data Science') | |
# Get user input for the number of dice rolls | |
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, 7, 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'] * 6, | |
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 | |
if st.button('Download Roll Data as CSV'): | |
roll_data = pd.DataFrame({ | |
'Roll': rolls, | |
'Count': np.ones(num_rolls) | |
}) | |
st.download_button( | |
label='Download CSV', | |
data=roll_data.to_csv(index=False), | |
file_name='roll_data.csv', | |
mime='text/csv' | |
) |