create an app for rolling dice for dnd
Drop files here
or click to upload
import streamlit as st
import random
st.title("D&D Dice Roller 🎲")
# Add some styling
st.markdown("""
<style>
.dice-result {
font-size: 24px;
font-weight: bold;
padding: 10px;
border-radius: 5px;
background-color: #f0f2f6;
margin: 10px 0;
}
</style>
""", unsafe_allow_html=True)
# Initialize session state for roll history
if 'roll_history' not in st.session_state:
st.session_state.roll_history = []
# Dice options
dice_types = {
"D4": 4,
"D6": 6,
"D8": 8,
"D10": 10,
"D12": 12,
"D20": 20,
"D100": 100
}
# Sidebar for dice selection
with st.sidebar:
st.header("Dice Settings")
selected_die = st.selectbox("Select Dice Type", list(dice_types.keys()))
num_dice = st.number_input("Number of Dice", min_value=1, max_value=10, value=1)
modifier = st.number_input("Modifier (+ or -)", min_value=-20, max_value=20, value=0)
# Advantage/Disadvantage for D20
if selected_die == "D20":
roll_type = st.radio("Roll Type", ["Normal", "Advantage", "Disadvantage"])
clear_history = st.button("Clear History")
if clear_history:
st.session_state.roll_history = []
st.rerun()
# Main roll function
def roll_dice(sides, number=1, mod=0, roll_type="Normal"):
results = [random.randint(1, sides) for _ in range(number)]
if roll_type in ["Advantage", "Disadvantage"] and sides == 20:
second_roll = random.randint(1, sides)
if roll_type == "Advantage":
final_result = max(results[0], second_roll)
details = f"Rolls: {results[0]} and {second_roll} (took higher)"
else:
final_result = min(results[0], second_roll)
details = f"Rolls: {results[0]} and {second_roll} (took lower)"
total = final_result + mod
return total, details
total = sum(results) + mod
details = f"Rolls: {results}" + (f" + {mod}" if mod > 0 else f" - {abs(mod)}" if mod < 0 else "")
return total, details
# Main roll button
if st.button(f"Roll {num_dice}{selected_die} {'+' + str(modifier) if modifier > 0 else str(modifier) if modifier < 0 else ''}"):
roll_type = "Normal" if selected_die != "D20" else roll_type
total, details = roll_dice(dice_types[selected_die], num_dice, modifier, roll_type)
# Create roll description
roll_desc = f"{num_dice}{selected_die}"
if modifier != 0:
roll_desc += f" {'+' + str(modifier) if modifier > 0 else str(modifier)}"
if roll_type in ["Advantage", "Disadvantage"]:
roll_desc += f" with {roll_type}"
# Add to history
st.session_state.roll_history.insert(0, {
"roll": roll_desc,
"result": total,
"details": details
})
# Display current roll and history
st.header("Roll History")
for roll in st.session_state.roll_history:
with st.container():
st.markdown(f"**{roll['roll']}**")
st.markdown(f"<div class='dice-result'>🎲 Result: {roll['result']}</div>", unsafe_allow_html=True)
st.write(roll['details'])
st.divider()
# Add some helpful information at the bottom
with st.expander("How to Use"):
st.write("""
1. Select your dice type from the sidebar
2. Choose how many dice to roll
3. Add any modifier if needed
4. For D20 rolls, you can select Advantage or Disadvantage
5. Click the roll button to see your result
6. Your roll history will be displayed below
""")
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?