-1

I'm learning flask and I'm building a project with ebay api.

Python:

from flask import Flask, render_template, redirect, request
from ebaysdk.finding import Connection

app = Flask(__name__)

    @app.route('/')
    def index():
        return render_template('search_on_ebay.html')
    
    def get_data():    
        api = Connection(config_file='ebay.yml', siteid="EBAY-US")
        request_ = {
            'keywords': 'vasco rossi',
            'itemFilter': [
                {'name': 'condition', 'value': 'new'}
            ],
            'paginationInput': {
                'entriesPerPage': 10,
                'pageNumber': 1
            },
            'sortOrder': 'PricePlusShippingLowest'
        }
        response = api.execute('findItemsByKeywords', request_)
        array_data = []
        for item in response.reply.searchResult.item:
            print(f"Title: {item.title}, Price: {item.sellingStatus.currentPrice.value}")
            array_data.append(item.title)
            array_data.append(item.sellingStatus.currentPrice.value)
        return f'{array_data}'

in html I have two forms with input and button:

<div id="search-div" style="display: flex; align-items: center; justify-content: center;">
     <form>
            <input type="text" name="searchinput" style="margin-top: -20px; margin-left: 2px; width: 20%;" id="search-input">
            </form>
            <label style="color:#eeeeee">a</label>
            <form action="**HERE I WANT TO RUN THE FUNCTION**" method="POST">
                <button text="cerca" id="search-button" class="button-primary">search</button>
            </form>
        </div>

I wanted to run first function, named index, at the begin of the site (that works) and get_data when I click the search button. I can't do it opening another url, because my site needs to get keywords from the input, and if I open a new url the input disappers. How can I do? Thanks for help

JacopoBiondi
  • 57
  • 1
  • 7

1 Answers1

0

did you have a look at these Flask docs?

if someone makes a get request, you can render your HTML form, and if someone makes a post request (via your form), you can run your function.

you can surround your function with:

if request.method == 'POST':

(do stuff here)
return

also don't forget to set the methods for your route (I believe routes only accept get by default) like this:

@app.route('/', methods=['GET', 'POST'])

this answer has some relevant info as well.

AudioBaton
  • 361
  • 2
  • 6