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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import numpy as np
|
| 3 |
+
import math
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
|
| 7 |
+
st.set_page_config(page_title="Advanced Calculator", layout="centered")
|
| 8 |
+
|
| 9 |
+
st.title("🧮 Advanced Calculator")
|
| 10 |
+
st.markdown("Multi-feature calculator for Hugging Face Streamlit deployment")
|
| 11 |
+
|
| 12 |
+
# -------------------------------
|
| 13 |
+
# Safe Expression Evaluation
|
| 14 |
+
# -------------------------------
|
| 15 |
+
allowed_names = {
|
| 16 |
+
k: getattr(math, k) for k in dir(math) if not k.startswith("_")
|
| 17 |
+
}
|
| 18 |
+
allowed_names.update({
|
| 19 |
+
"np": np,
|
| 20 |
+
"abs": abs,
|
| 21 |
+
"round": round
|
| 22 |
+
})
|
| 23 |
+
|
| 24 |
+
def safe_eval(expr):
|
| 25 |
+
try:
|
| 26 |
+
return eval(expr, {"__builtins__": {}}, allowed_names)
|
| 27 |
+
except Exception as e:
|
| 28 |
+
return f"Error: {e}"
|
| 29 |
+
|
| 30 |
+
# -------------------------------
|
| 31 |
+
# Sidebar Navigation
|
| 32 |
+
# -------------------------------
|
| 33 |
+
option = st.sidebar.selectbox(
|
| 34 |
+
"Select Calculator Mode",
|
| 35 |
+
["Basic / Scientific", "Unit Converter", "Function Plotter", "History"]
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
# Session history
|
| 39 |
+
if "history" not in st.session_state:
|
| 40 |
+
st.session_state.history = []
|
| 41 |
+
|
| 42 |
+
# -------------------------------
|
| 43 |
+
# BASIC & SCIENTIFIC CALCULATOR
|
| 44 |
+
# -------------------------------
|
| 45 |
+
if option == "Basic / Scientific":
|
| 46 |
+
st.subheader("Basic & Scientific Calculator")
|
| 47 |
+
|
| 48 |
+
expr = st.text_input(
|
| 49 |
+
"Enter mathematical expression",
|
| 50 |
+
placeholder="Example: sin(pi/2) + log(10)"
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
if st.button("Calculate"):
|
| 54 |
+
result = safe_eval(expr)
|
| 55 |
+
st.write("### Result:", result)
|
| 56 |
+
|
| 57 |
+
st.session_state.history.append({
|
| 58 |
+
"Expression": expr,
|
| 59 |
+
"Result": result
|
| 60 |
+
})
|
| 61 |
+
|
| 62 |
+
# -------------------------------
|
| 63 |
+
# UNIT CONVERTER
|
| 64 |
+
# -------------------------------
|
| 65 |
+
elif option == "Unit Converter":
|
| 66 |
+
st.subheader("Unit Converter")
|
| 67 |
+
|
| 68 |
+
conversion_type = st.selectbox(
|
| 69 |
+
"Select Conversion Type",
|
| 70 |
+
["Length", "Weight", "Temperature"]
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
value = st.number_input("Enter Value")
|
| 74 |
+
|
| 75 |
+
if conversion_type == "Length":
|
| 76 |
+
units = st.selectbox("Convert From", ["Meters", "Kilometers", "Miles"])
|
| 77 |
+
target = st.selectbox("Convert To", ["Meters", "Kilometers", "Miles"])
|
| 78 |
+
|
| 79 |
+
conversion = {
|
| 80 |
+
"Meters": 1,
|
| 81 |
+
"Kilometers": 1000,
|
| 82 |
+
"Miles": 1609.34
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
result = value * conversion[units] / conversion[target]
|
| 86 |
+
st.write("### Result:", result)
|
| 87 |
+
|
| 88 |
+
elif conversion_type == "Weight":
|
| 89 |
+
units = st.selectbox("Convert From", ["Grams", "Kilograms", "Pounds"])
|
| 90 |
+
target = st.selectbox("Convert To", ["Grams", "Kilograms", "Pounds"])
|
| 91 |
+
|
| 92 |
+
conversion = {
|
| 93 |
+
"Grams": 1,
|
| 94 |
+
"Kilograms": 1000,
|
| 95 |
+
"Pounds": 453.592
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
result = value * conversion[units] / conversion[target]
|
| 99 |
+
st.write("### Result:", result)
|
| 100 |
+
|
| 101 |
+
elif conversion_type == "Temperature":
|
| 102 |
+
units = st.selectbox("Convert From", ["Celsius", "Fahrenheit", "Kelvin"])
|
| 103 |
+
target = st.selectbox("Convert To", ["Celsius", "Fahrenheit", "Kelvin"])
|
| 104 |
+
|
| 105 |
+
def temp_convert(val, u, t):
|
| 106 |
+
if u == t:
|
| 107 |
+
return val
|
| 108 |
+
|
| 109 |
+
# Convert to Celsius first
|
| 110 |
+
if u == "Fahrenheit":
|
| 111 |
+
val = (val - 32) * 5/9
|
| 112 |
+
elif u == "Kelvin":
|
| 113 |
+
val = val - 273.15
|
| 114 |
+
|
| 115 |
+
# Convert from Celsius to target
|
| 116 |
+
if t == "Fahrenheit":
|
| 117 |
+
return val * 9/5 + 32
|
| 118 |
+
elif t == "Kelvin":
|
| 119 |
+
return val + 273.15
|
| 120 |
+
return val
|
| 121 |
+
|
| 122 |
+
st.write("### Result:", temp_convert(value, units, target))
|
| 123 |
+
|
| 124 |
+
# -------------------------------
|
| 125 |
+
# FUNCTION PLOTTER
|
| 126 |
+
# -------------------------------
|
| 127 |
+
elif option == "Function Plotter":
|
| 128 |
+
st.subheader("Function Plotter")
|
| 129 |
+
|
| 130 |
+
func_expr = st.text_input(
|
| 131 |
+
"Enter function in terms of x",
|
| 132 |
+
placeholder="Example: sin(x) or x**2 + 3*x"
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
x_min = st.number_input("X min", value=-10.0)
|
| 136 |
+
x_max = st.number_input("X max", value=10.0)
|
| 137 |
+
|
| 138 |
+
if st.button("Plot Function"):
|
| 139 |
+
try:
|
| 140 |
+
x = np.linspace(x_min, x_max, 400)
|
| 141 |
+
y = eval(func_expr, {"__builtins__": {}}, {**allowed_names, "x": x})
|
| 142 |
+
|
| 143 |
+
fig, ax = plt.subplots()
|
| 144 |
+
ax.plot(x, y)
|
| 145 |
+
ax.set_xlabel("x")
|
| 146 |
+
ax.set_ylabel("y")
|
| 147 |
+
ax.set_title(f"Plot of {func_expr}")
|
| 148 |
+
ax.grid(True)
|
| 149 |
+
|
| 150 |
+
st.pyplot(fig)
|
| 151 |
+
|
| 152 |
+
except Exception as e:
|
| 153 |
+
st.error(f"Plot Error: {e}")
|
| 154 |
+
|
| 155 |
+
# -------------------------------
|
| 156 |
+
# HISTORY
|
| 157 |
+
# -------------------------------
|
| 158 |
+
elif option == "History":
|
| 159 |
+
st.subheader("Calculation History")
|
| 160 |
+
|
| 161 |
+
if st.session_state.history:
|
| 162 |
+
df = pd.DataFrame(st.session_state.history)
|
| 163 |
+
st.dataframe(df, use_container_width=True)
|
| 164 |
+
|
| 165 |
+
if st.button("Clear History"):
|
| 166 |
+
st.session_state.history = []
|
| 167 |
+
else:
|
| 168 |
+
st.info("No calculations yet.")
|