Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

112
Views
How to filter a df based on user input

I am trying to implement an auto complete feature on a flask app where I want that when the user input characters, a windows will appear with options with stock symbols.

My current /search html is basic intentionally becaue I want to debug as I go along

def search():
    from ftplib import FTP

    directory = 'symboldirectory'
    filenames = ('otherlisted.txt', 'nasdaqlisted.txt')

    ftp = FTP('ftp.nasdaqtrader.com')
    ftp.login()
    ftp.cwd(directory)

    for item in filenames:
        ftp.retrbinary('RETR {0}'.format(item), open(item, 'wb').write)

    ftp.quit()

    # Create pandas dataframes from the nasdaqlisted and otherlisted files.
    nasdaq_exchange_info = pd.read_csv('nasdaqlisted.txt', '|')
    other_exchange_info = pd.read_csv('otherlisted.txt', '|')
    nasdaq_exchange_info=nasdaq_exchange_info.query('ETF == ETF')
    nasdaq_exchange_info = nasdaq_exchange_info.rename(columns={'Security Name': 'Security_Name'})
    nasdaq_exchange_info=nasdaq_exchange_info.query("Security_Name.str.contains(request.args.get('q'), case=False)")
    print(nasdaq_exchange_info)
    return render_template("search.html", nasdaq_exchange_info=nasdaq_exchange_info)

My issue is in

    nasdaq_exchange_info=nasdaq_exchange_info.query("Security_Name.str.contains(request.args.get('q'), case=False)")
print(nasdaq_exchange_info)

I get name 'request' is not defined error (if I'll input Security_Name.str.contains('apple', case=False) for example it will get printed.) Also I tried every variation I could think of but I get syntax error.

My html look like this:

<body>

        <input autocomplete="off" autofocus placeholder="Query" type="text">

        <ul></ul>

        <script>

            let input = document.querySelector('input');
            input.addEventListener('input', async function() {
                let response = await fetch('/search?q=' + input.value);
                let nasdaq_exchange_info = await response.json();
                let html = '';
                for (let symbol in nasdaq_exchange_info) {
                    let ticker = nasdaq_exchange_info[Security_Name].symbol;
                    html += '<li>' + ticker + '</li>';
                }
                document.querySelector('ul').innerHTML = html;
            });

        </script>

    </body>

Is there a knwon way of implementing the user typing in q and serach the db for it? I know how to do it in a db using sql but I am trying to avoid that.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

try applying fstrings like that:

nasdaq_exchange_info=nasdaq_exchange_info.query(f"Security_Name.str.contains({request.args.get('q')}, case=False)")
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!