简体   繁体   English

如何将 HTML 输入文本值作为参数传递给 python function?

[英]How to pass HTML input text value as a parameter to python function?

So I have a python code, that I have implemented in HTML using pyscript.所以我有一个 python 代码,我使用 pyscript 在 HTML 中实现了它。 My python code has a function which takes in a word as a parameter and does something with it.我的 python 代码有一个 function,它接受一个词作为参数并用它做一些事情。 How can I make it so whatever I put in the text field in HTML, gets passed as a parameter and calls the python function?我怎样才能做到无论我在 HTML 的文本字段中输入什么,都会作为参数传递并调用 python function?

The alternative, if anyone knows how to do, how can I get a value of HTML element with python?或者,如果有人知道该怎么做,我怎样才能用 python 获得 HTML 元素的值?

If anyone has any methods including javascript that can make this work, I'd appreciate that too.如果有人有任何方法,包括 javascript 可以使这项工作,我也很感激。 I'm just looking for any way possible to call my python function with the content or value of element.我只是在寻找任何可能的方式来调用我的 python function 元素的内容或值。

I have tried multiple methods (ALTHOUGH I'm not sure if I did them right) and here they are:我尝试了多种方法(尽管我不确定我是否做对了),它们是:

I tried using cgi like this,我试过像这样使用cgi,

In HTML:在 HTML:

<form action = "/submit" method = "post">
      <input name="word" type="text" value="TESTING">
</form>

In Python:在 Python:

import cgi, cgitb
form = cgi.FieldStorage()
text = form.getvalue('word')
print(text)

and text returns none, so no value taken.并且 text 不返回任何值,因此没有值。

I tried the form and @app.route method, but everytime I submit the form, it opens a new page and finds nothing, saying file not found.我尝试了表单和@app.route 方法,但每次提交表单时,它都会打开一个新页面,但什么也没找到,说找不到文件。

In HTML:在 HTML:

<form action = "/submit" method = "post">
      <input name="word" type="text" value="TESTING">
      <input type="submit" class = "file_submit">
</form>

In Python:在 Python:

from flask import Flask, render_template, request, redirect, url_for,request, make_response
app = Flask(__name__)

@app.route("/")
def home():
  return "hi"
@app.route("/index")

@app.route("/submit", methods = ['POST', "GET"])
def submit():
   message = None
   if request.method == 'POST':
    output = request.form.to_dict()
    word = output["submit"]
    anagram(word) #this is the function I want to call, I want word to be what I type in HTML input
    return render_template("Pyth/py.html")

I'm new to web developm.net, but I have looked everywhere on the inte.net and I cannot find the asnwer to this.我是 web developm.net 的新手,但我在 inte.net 上到处都看过,但找不到对此的答案。

Here's an example that does a very basic anagram service.这是一个执行非常基本的 Anagram 服务的示例。 Put this in a file called something.py , then bring up http://yourwebsite/something.py in your browser.将其放入名为something.py的文件中,然后在浏览器中打开http://yourwebsite/something.py

#! /usr/bin/env python3

import cgi
import random
form = cgi.FieldStorage()
if 'submit' not in form:
    print('''Content-Type: text/html

<form method="post">
<input name="word" type="text" value="TESTING">
<input type='submit' name='submit'>
</form>
''')
else:
    word = form.getvalue('word')
    anagram = list(word)
    random.shuffle(anagram)
    anagram = ''.join(anagram)
    print(f'''Content-Type: text/html

Your word is {anagram}.
''')
~

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM