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.
try applying fstrings like that:
nasdaq_exchange_info=nasdaq_exchange_info.query(f"Security_Name.str.contains({request.args.get('q')}, case=False)")