AR14020 commited on
Commit
4ef2822
·
verified ·
1 Parent(s): f3e0b15

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -6
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import streamlit as st
2
  from sympy import (
3
- symbols, integrate, diff, exp, log, ln, sin, cos, tan, sec, csc, cot, latex
4
  )
5
 
6
  # Define the Streamlit app
@@ -20,14 +20,13 @@ def main():
20
  # Input section
21
  st.header("Input the Function")
22
  function_input = st.text_input(
23
- "Enter the integrand (e.g., x * exp(x), ln(x), x^2 * cos(x)):",
24
  "x * exp(x)",
25
  )
26
  u_input = st.text_input(
27
  "Choose 'u' (e.g., x for x * exp(x) or ln(x)):", "x"
28
  )
29
 
30
- # Solve button
31
  if st.button("Solve"):
32
  try:
33
  # Define the variable and import common functions for eval()
@@ -36,9 +35,14 @@ def main():
36
  "sin": sin, "cos": cos, "tan": tan,
37
  "sec": sec, "csc": csc, "cot": cot}
38
 
39
- # Parse the integrand and `u`
40
- integrand = eval(function_input, {"__builtins__": None}, safe_globals)
41
- u = eval(u_input, {"__builtins__": None}, safe_globals)
 
 
 
 
 
42
  dv = integrand / u
43
 
44
  # Compute derivatives and integrals
 
1
  import streamlit as st
2
  from sympy import (
3
+ symbols, integrate, diff, exp, log, ln, sin, cos, tan, sec, csc, cot, latex, SympifyError, sympify
4
  )
5
 
6
  # Define the Streamlit app
 
20
  # Input section
21
  st.header("Input the Function")
22
  function_input = st.text_input(
23
+ "Enter the integrand (e.g., x * exp(x), cos(ln(x)), x^2 * sin(x)):",
24
  "x * exp(x)",
25
  )
26
  u_input = st.text_input(
27
  "Choose 'u' (e.g., x for x * exp(x) or ln(x)):", "x"
28
  )
29
 
 
30
  if st.button("Solve"):
31
  try:
32
  # Define the variable and import common functions for eval()
 
35
  "sin": sin, "cos": cos, "tan": tan,
36
  "sec": sec, "csc": csc, "cot": cot}
37
 
38
+ # Validate and parse inputs
39
+ try:
40
+ integrand = sympify(function_input, locals=safe_globals)
41
+ u = sympify(u_input, locals=safe_globals)
42
+ except SympifyError:
43
+ st.error("Invalid input. Please use valid mathematical syntax (e.g., cos(ln(x))).")
44
+ return
45
+
46
  dv = integrand / u
47
 
48
  # Compute derivatives and integrals