【发布时间】:2021-05-29 16:45:18
【问题描述】:
我是 ReactJS 和 web 开发的新手。
我正在创建一个网络应用程序,我在其中输入来自用户的字符串(数学表达式)。按下提交按钮后,我想在 python 脚本中处理这个字符串,并在输出文本框中返回所需的输出。如何使这个 python 脚本成为我的网络应用程序的永久部分。
输入文本框和提交按钮是文件“Submit.js”中提交组件的一部分。 输出文本框是“Solution.js”文件中解决方案组件的一部分。
这两个组件都是 LeftWindowComponent 的子组件。 我将两个文本框的值存储在父组件的状态中,即 LeftWindowComponent。 状态“fx”定义输入文本框中的文本。 状态“fprimex”定义解决方案(输出)文本框中的文本。
提交组件代码-->
class Submit extends Component{
constructor(props){
super(props);
}
render(){
return(
<div className="center">
<div>
<Form onSubmit={this.handleSubmit}>
<Col md={{ size: 6, offset: 3 }} >
<FormGroup row>
<Input type="text" id="fx" name="fx" placeholder="Type Here" value = {this.props.fx} onChange={this.props.handleInputChangeFromKeyboard} />
</FormGroup>
</Col>
</Form>
<Button type="submit" color="primary">Differentiate</Button>
</div></div>
)
}
}
解决方案组件代码 -->
class Solution extends Component{
constructor(props){
super(props);
}
render(){
return(
<div class = "center">
<Form>
<FormGroup>
<Col md={{size:8,offset:2}}>
<Input type="textarea" id="fprimex" name="fprimex" placeholder="Solution" value = {this.props.fprimex} />
</Col>
</FormGroup>
</Form>
</div>
)
LeftWindow 组件代码 -->
class LeftWindow extends Component{
constructor(props){
super(props);
this.state = {
fx: "",
fprimex: "",
};
}
handleInputChange = (newInput) => {
this.setState({
fx: this.state.fx.concat(newInput),
});
} ;
handleInputChangeFromKeyboard = (event) => {
const target = event.target;
const value = target.value;
const name = target.name;
this.setState({
[name]: value
});
} ;
render(){
return(
<div className="col-12 bg-dark fullheight">
<h3 className="center">Enter the function to be differentiated</h3>
<Buttons handleInputChange={this.handleInputChange} />
<Submit fx = {this.state.fx} handleInputChange={this.handleInputChange} handleInputChangeFromKeyboard = {this.handleInputChangeFromKeyboard}/>
<Solution fprimex = {this.state.fprimex}/>
</div>
);
}
}
提前致谢!
【问题讨论】:
-
我相信你必须为你的应用程序设置一个后端。如果你想使用 python,你可以使用 Flask。这是一个使用烧瓶反应的教程:blog.miguelgrinberg.com/post/…
-
你添加的代码完全不相关。您只需使用适当的选项调用
fetch函数将值发布到服务器,处理值并返回您想要的任何内容。
标签: javascript python node.js reactjs