1

So, I am building a webapp which takes a link from a shopping website then runs it through a python script which interprets the data, stores it in a database and that populates a table for reference.

I am running into a couple issues:

  1. if I put the link into the front end input (html) then submit it just takes me to "page isn't working HTTP error 405". I'm not sure what to do about that one.

  2. the more pressing issue is that even though I believe I routed the input properly through flask I get this issue when I run the python script alongside the frontend "RuntimeError: Working outside of request context."

I tried some of the advice mentioned in these existing posts to no avail:

Sending data from HTML form to a Python script in Flask

Connecting python script with html button and flask

I also tried changing the script itself to use getvalue() instead of getvalue when associating it as an input variable for the python script to work with.

this is my route code from app.py

@app.route("/", methods=['POST'])
 def getvalue():
   HTML_Info = request.form['data_bridge']
   return HTML_Info

code for the HTML input

<form name="passdata" action="{{ url_for('getvalue') }}" method="POST">
        <input type='text' name="data_bridge" placeholder="paste shoe link here">
        <input type="submit">
    </form>

and the python code just imports the app file and the getvalue function and then assigns it to a variable.

if you guys could help me sort this out I would greatly appreciate it.

Thompy02
  • 29
  • 3

1 Answers1

0

I assume you want to take an input (e.g. shoe link) from the user and then do some operations based on the input.

To access the HTML form from / path you need to enable both GET and POST requests in that route. Otherwise, when you try to access the root path / from your browser, you will get the HTTP Method not allowed error.

app.py:

from flask import Flask, render_template, request

app = Flask(__name__)


def get_value_related_info(value):
    return f"You have entered {value}"


@app.route('/', methods=['POST', 'GET'])
def getvalue():
    if request.method == "POST":
        HTML_Info = request.form['data_bridge']
        return get_value_related_info(HTML_Info)
    return render_template('form.html', text="")


if __name__ == "__main__":
    app.run(debug=True)

Output:

  • Before form submission:

before form submission

  • After form submission:

after form submission

templates/form.html:

<html>
<head>
    <title>Form example</title>
</head>
<body>
<form name="passdata" action="{{ url_for('getvalue') }}" method="POST">
    <input type='text' name="data_bridge" placeholder="paste shoe link here">
    <input type="submit">
</form>
</body>
</html>

Explanation:

  • I have mocked the functionality on the user input in get_value_related_info method.

References:

arshovon
  • 13,270
  • 9
  • 51
  • 69
  • thank you for helping out. I put the code in but have to work out something with pytest now I will update on whether or not this helped to solve it whenever I finish up this issue! – Thompy02 Jan 10 '22 at 03:33