Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有没有一种简单的方法让html按钮执行python函数_Python_Html_Button - Fatal编程技术网

有没有一种简单的方法让html按钮执行python函数

有没有一种简单的方法让html按钮执行python函数,python,html,button,Python,Html,Button,我有一个python函数,它创建一个带有输出/结果的.txt文件,并将其保存在我的项目目录中。现在我有了一个html js文件,我从.txt文件中获取数据,并使用它显示数据 现在我的问题是,我想要一个按钮,它执行python脚本并根据变量创建新数据 有没有一个简单的方法可以做到这一点 我对编写web应用程序相当陌生。您可以使用类似flask的框架。使用flask,您只需将函数粘贴到一个根上,当您转到该根上时,将执行该根。 只需将Form action设置为您的根,然后使您的方法与之类似: <

我有一个python函数,它创建一个带有输出/结果的.txt文件,并将其保存在我的项目目录中。现在我有了一个html js文件,我从.txt文件中获取数据,并使用它显示数据

现在我的问题是,我想要一个按钮,它执行python脚本并根据变量创建新数据

有没有一个简单的方法可以做到这一点


我对编写web应用程序相当陌生。

您可以使用类似flask的框架。使用flask,您只需将函数粘贴到一个根上,当您转到该根上时,将执行该根。 只需将Form action设置为您的根,然后使您的方法与之类似:

<form method="POST" action={{ url_for('your_function') }}>
<div style="text-align: center;">
<H1>Admin panel login </H1> <br>
Username <input type = "text" name= "username" /> <br>
Password <input type = "password" name = "password" /> <br>
<input type = "submit">
</div>
</form>

浏览器没有读取本地文件系统的权限。因此,您必须首先提供.txt文件

foo.txt

Foo
Bar
Baz
foo.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
</body>
<script>
    fetch("foo.txt").then(x => x.text()).then(text => document.getElementsByTagName("body")[0].innerText = text)
</script>
</html>
然后打开
http://localhost:8888/foo.html
查看结果

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
</body>
<script>
    fetch("foo.txt").then(x => x.text()).then(text => document.getElementsByTagName("body")[0].innerText = text)
</script>
</html>
python3 -mhttp.server 8888