File size: 1,437 Bytes
d29f19d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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'
    )