CoolFace
Apppublic

syedanemra/math-solver-app

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py52 linesDownload Raw Back to root
1from flask import Flask, render_template, request, jsonify2from flask_sqlalchemy import SQLAlchemy3from sympy import sympify, Eq, solve4import json5 6# Initialize Flask app and database7app = Flask(__name__)8app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///history.db'9app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False10db = SQLAlchemy(app)11 12# History database model13class History(db.Model):14    id = db.Column(db.Integer, primary_key=True)15    question = db.Column(db.String(200), nullable=False)16    answer = db.Column(db.String(200), nullable=False)17 18# Home route to render the main page19@app.route('/')20def index():21    history = History.query.all()22    return render_template('index.html', history=history)23 24# Route for processing the mathematical question25@app.route('/solve', methods=['POST'])26def solve_math():27    data = request.json28    question = data.get('question')29 30    try:31        # Use sympy to parse and solve mathematical expressions32        expression = sympify(question)33        result = str(expression)34        history_item = History(question=question, answer=result)35        db.session.add(history_item)36        db.session.commit()37 38        return jsonify({39            'status': 'success',40            'solution': result41        })42    except Exception as e:43        return jsonify({44            'status': 'error',45            'message': str(e)46        })47 48# Run the app49if __name__ == '__main__':50    db.create_all()  # Create database tables51    app.run(debug=True)52