Spaces:
Runtime error
Runtime error
# Code Destiny and Density is equal to one hundred. | |
st.Markdown("TODO: A code density slider bar shows and adjusts the size of lines. ') | |
st.Markdown('Any ๐ฒDice๐ฒ way to compress the lines yet make it readable as optimal list of sets will do. Match language from good math books.') | |
import streamlit as st | |
import numpy as np | |
import pandas as pd | |
import plotly.express as px | |
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 = '' | |
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) | |
rolls = np.random.randint(1, dice_type['sides'] + 1, num_rolls, dtype=np.uint64) | |
roll_counts = pd.Series(rolls).value_counts().sort_index() | |
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') | |
fig.show() | |
bonus_match = False | |
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 | |
new_roll_data = pd.DataFrame({'Roll': rolls, | |
'Count': np.ones(num_rolls, dtype=np.uint64), | |
'DiceNumberOfSides': [dice_type['sides']] * num_rolls, | |
'DiceRollerName': [st.session_state.name] * num_rolls}) | |
if bonus_match: | |
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) | |
st.session_state.dice_roll_history = dice_roll_history | |
if st.button('Download Results'): | |
filename = f'dice_roll_history_{st.session_state.name}.csv' | |
st.download_button(label='Download CSV', data=dice_roll_history.to_csv(index=False), file_name=filename, mime='text/csv') | |
if st.session_state.dice_roll_history.shape[0] > 0: | |
st.markdown('### Dice Roll History') | |
st.dataframe(st.session_state.dice_roll_history) | |