File size: 2,583 Bytes
d29f19d
 
 
 
 
982e84c
d29f19d
982e84c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71c2520
 
 
 
982e84c
 
d29f19d
 
 
982e84c
d29f19d
 
 
 
 
 
 
982e84c
d29f19d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
982e84c
d29f19d
71c2520
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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': '๐ŸŽฒ'
    }
]

# Initialize the user's name if it has not been set yet
if 'name' not in st.session_state:
    st.session_state.name = ''

# 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'):
    if st.session_state.name == '':
        st.warning('Please enter a name before downloading the roll data')
    else:
        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'
        )

# Add UI control for the user's name
name = st.text_input('What is your name?', value=st.session_state.name)
st.session_state.name = name