'how do I make a math Django and react web application?
Hope all is well.
I want to have an input field in my react frontend that takes in a math expression (e.g 1 + 1) and I want my backend to respond with the answer 2.
How do you do this using reactjs and Django?
So far I have some of the frontend finished:
it will send the input data to the backend using the API url.
import React, {useState, UseEffect } from 'react';
function Home() {
const [query, setQuery] = useState("");
return (
<div className='Home'>
<form action={APIL_URL} method="POST">
<input name="linear-algebra-expression"
id="linear-algebra-expression"
type="text"
onChange={e => setQuery(e.target.value)}
value={query}>
</input>
</form>
</div>
)
}
export default Home;
I am aware that in my Django backend I am going to have something like:
class LinearAlgebraView(APIView):
def post(self, request):
expression = request.POST['linear-algebra-expression']
return (... ?)
I want to be able to return the computed data from Django to react at the samne web page where the user entered the math expression in the form/input. How do I do this?
Thanks
Solution 1:[1]
you could just use eval() method witch taking a string as parameter and return the computed value like for exemple :
linear-algebra-expression = "3-1 + 12/10"
result = eval(linear-algebra-expression)
print(" 3-1 + 12/10 = " + str(result))
output: 3-1 + 12/10 = 3.2
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Oussama Tachi |
