How to Create a Stock and Finance App With Python
Part 4: Fixing bugs and adding a search bar for the stocks.
TL;DR We make some bugfixes and add a search bar for the stocks. You should definitely check out pythonanywhere.com* ($ 5 USD/month) for hosting this app.

Layout Bugfixes
(1) When we first start our application and we did not login yet, the form for choosing a period to compare is still there. This should not be there. So when we call the main page or index page, this should be empty.

How can we change that? Fortunately, Jinja has a nice way to solve this problem. You can add to the html template “ifs and else”, see below.

So, we add this to our home.py

So our

We run run.py again, do not forget to reload your browser, that your browser reloads the html and css correctly. In Windows it is ctrl+ F5.

That looks good!
Adding a searchbar for the stocks
The goal is to replace the dropdown on our stocks page with a search bar, see below. The search should autocomplete, when we searching a ticker or ticker name.

Create a form validator
We need to setup a class variable and put it this in run.py.

For that we also need to import wtf forms and wtforms.validator.

Autocomplete feature
We create an autocomplete callback function, also put this in our run.py. We get our stock infos table and put the ticker, name and sector in a list. We then create a sorted json response.

You can test the response in your browser:

Integrate it in stocks.html
(1) Update the route in “/stocks/
form = SearchForm(request.form)
(2) Modify the stocks.html. Code lines are hightlighted. Replace the form and put the javascript for the autocomplete function and the url replacement there.

The first javascript is an ajax function which is searching for a smililar string in your serachbar, which is at least 3 characters long.
The second javascript function observes the form when submitting the button and extrtacting the value from it (see below). Then we have to split it at our separator “|” , take the first string and clean it from whitespace.
"AADI | AADI BIOSCIENCE INC | Health Care"That should work!

Just modify this example to your needs and upload it on pythonanywhere.com*. Here is the repo: https://github.com/AntonioBlago/Webpage_Tutorial_4. Check out the branch “part4” for this version. Here I am showing you how to upload it on pythonanywhere:
Thanks for reading my story. I hope you liked it. Please feel free to clap, share, and comment on it. Follow me on more content about cryptos, stocks, data analysis, and web development.
- Read my other article about creating a financial dashboard in Flask, see below!
- Check out my webpage and get in touch with me on Linkedin: antonioblago.com
- Make a free account on my stock and crypto tracking tool: www.tenxassets.com
- If you like to develop web apps, I recommend you pythonanywhere.com* as a hosting service.
Disclaimer:
- *these are affiliate links
More content at PlainEnglish.io. Sign up for our free weekly newsletter. Follow us on Twitter, LinkedIn, YouTube, and Discord.
