Create beautiful touch calculator
Drop files here
or click to upload
import streamlit as st
st.title("✨ Beautiful Touch Calculator ✨")
# Initialize memory
if "memory" not in st.session_state:
st.session_state.memory = "0"
if "should_clear" not in st.session_state:
st.session_state.should_clear = False
# Calculator display
display = st.text_input("", value=st.session_state.memory, key="display",
label_visibility="collapsed")
# Custom CSS for button styling
st.markdown("""
<style>
div.stButton > button {
width: 100%;
height: 50px;
font-size: 20px;
font-weight: bold;
margin: 2px;
border-radius: 10px;
}
div.stButton > button:hover {
background-color: #f0f0f0;
border-color: #808080;
}
</style>
""", unsafe_allow_html=True)
def update_display(value):
if st.session_state.should_clear:
st.session_state.memory = str(value)
st.session_state.should_clear = False
else:
if st.session_state.memory == "0":
st.session_state.memory = str(value)
else:
st.session_state.memory += str(value)
def clear():
st.session_state.memory = "0"
st.session_state.should_clear = False
def calculate():
try:
st.session_state.memory = str(eval(st.session_state.memory))
st.session_state.should_clear = True
except:
st.session_state.memory = "Error"
st.session_state.should_clear = True
# Calculator buttons
col1, col2, col3, col4 = st.columns(4)
with col1:
if st.button("7"): update_display(7)
with col2:
if st.button("8"): update_display(8)
with col3:
if st.button("9"): update_display(9)
with col4:
if st.button("÷"): update_display("/")
col1, col2, col3, col4 = st.columns(4)
with col1:
if st.button("4"): update_display(4)
with col2:
if st.button("5"): update_display(5)
with col3:
if st.button("6"): update_display(6)
with col4:
if st.button("×"): update_display("*")
col1, col2, col3, col4 = st.columns(4)
with col1:
if st.button("1"): update_display(1)
with col2:
if st.button("2"): update_display(2)
with col3:
if st.button("3"): update_display(3)
with col4:
if st.button("-"): update_display("-")
col1, col2, col3, col4 = st.columns(4)
with col1:
if st.button("0"): update_display(0)
with col2:
if st.button("."): update_display(".")
with col3:
if st.button("="): calculate()
with col4:
if st.button("+"): update_display("+")
if st.button("Clear"): clear()
# Footer
st.markdown("---")
st.markdown("Made with ❤️ using Streamlit")
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?