0

So I am running Python Flask with Bootstrap. And when both of my form selects are filled in I would like to submit the form, which would then run my script. My script is already made and works fine. It outputs data for every new line like so:

1.25:7000
2.75:13000
6.40:50400

(Sometimes the output is more or less than 3 lines as it varies per product) So after somebody fills out the two select options, I will pass the two select options onto my script in order to fetch the prices and stocks for each seller, which the output is displayed like so above.

Then I would like to update and display this data onto my website. In the example above, the script should make the Price update to 1.25 and stock 70,400 (since 7000+13000+50400) Then I would like to display a table, which would only appear after the two options are selected and it would look like this:

Price|Stock
1.25 |7000
2.75 |13000
6.40 |50400

I would like this to appear under everything on the form.

My template code:

{% extends "_base.html" %}
{% block content %}
{% if current_user.is_authenticated %}
<h3 class="text-center">Welcome {{current_user.email}}!</h3>
{% else %}
<h3 class="text-center">Price List</h3>
{% endif %}
<br>
<div class="form-group">
<form action="" method="post">
<div class="input-group mb-3-center" style="display: flex;justify-content: center;">
    <div class="input-group-prepend">
    <label for="inputsm">Country</label>
    <br>
    <select class="selectpicker" title="Choose a country..." data-live-search="true" id="country">
      <option value="uk">United Kingdom</option>
      <option value="2">Two</option>
      <option value="3">Three</option>
    </select>
    <br>
    <br>
    <label for="inputsm">Service</label>
    <br>
    <select class="selectpicker" title="Choose a service..." data-live-search="true" id="service">
      <option value="1">One</option>
      <option value="2">Two</option>
      <option value="3">Three</option>
    </select>
    <br>
    <br>
    <label for="inputsm">Price</label>
    <span class="input-group-text" style="font-size: medium;">$</span>
    <br>
    <label for="inputsm">Stock</label>
    <span class="input-group-text" style="font-size: medium;">-</span>
    <br>
  </div>
</div>
</form>
{% endblock %}

Python file:

from flask import Blueprint, render_template
from flask_login import login_required

core_bp = Blueprint("core", __name__)


@core_bp.route("/")
#@login_required
def home():
    return render_template("core/index.html")

def getPrices(service,country):
    if service=="1" and country=="uk":
        return("1.25:7000\n2.75:13000\n6.40:50400")
big boss
  • 11
  • 4

1 Answers1

0

You can add an onchange listener to the selects in your HTML. It would be easiest to add an additional value to the selects, e.g. "Choose a value" so that you could test whether the user has selected it or not.

Updated template:

<div class="form-group">
<form action="" method="post">
<div class="input-group mb-3-center" style="display: flex;justify-content: center;">
    <div class="input-group-prepend">
    <label for="inputsm">Country</label>
    <br>
    <select class="selectpicker" title="Choose a country..." data-live-search="true" id="country" onchange="update()">
      <option value="select">Select a country</option>
      <option value="uk">United Kingdom</option>
      <option value="2">Two</option>
      <option value="3">Three</option>
    </select>
    <br>
    <br>
    <label for="inputsm">Service</label>
    <br>
    <select class="selectpicker" title="Choose a service..." data-live-search="true" id="service" onchange="update()">
      <option value="select">Select a service</option>
      <option value="1">One</option>
      <option value="2">Two</option>
      <option value="3">Three</option>
    </select>
    <br>
    <br>
    <label for="inputsm">Price</label>
    <span class="input-group-text" style="font-size: medium;">$</span>
    <br>
    <label for="inputsm">Stock</label>
    <span class="input-group-text" style="font-size: medium;">-</span>
    <br>
  </div>
</div>
</form>
</div>
<script>
function update() {
  const countrySelect = document.getElementById('country').value;
  const serviceSelect = document.getElementById('service').value;

  if (countrySelect !== 'select' && serviceSelect !== 'select') {
    // POST req to your server with the country and service
    // use the return val to populate the table 
  }
}
</script>
{% endblock %}

And the server would have to be updated with something like:

@core_bp.route("/")
#@login_required
def home():
    return render_template("core/index.html")

@core_bp.post('/prices')
def getPrices():
    country = request.values.get('country')
    service = request.values.get('service')
    if service=="1" and country=="uk":
        return("1.25:7000\n2.75:13000\n6.40:50400")

It'd probably be better to return a JSON object so you can get the values more easily back in your JS.

Rob
  • 518
  • 1
  • 3
  • 18
  • Thank you. I've managed to roughly get this working. However, if I select one of the selects then I will get an error in the console. So it doesn't seem to be executing when both have values like it should be, but instead just running when one is changed. – big boss Jun 12 '23 at 22:17
  • What's the error you're getting? – Rob Jun 12 '23 at 22:23
  • It's okay I fixed it. Main thing I'm struggling with is outputting this information into a table. I converted my script output to json so it's now looking like: [{'0.1': '4134'}, {'0.1': '842'}, {'0.1': '6405'}] to make things easier. I'm not very good with JS, I mainly just do Python. But, I'm trying to put this into a table, that only gets enabled after we get this output to put into a table. – big boss Jun 12 '23 at 22:37
  • Yeah, for updating content in the page you'll have to do a little fiddling with the JS, shouldn't be too bad though. If I'm understand correctly, the table should not appear until the selects are selected? [Check out this post](https://stackoverflow.com/questions/18333427/how-to-insert-a-row-in-an-html-table-body-in-javascript) for info on how to modify tables with JS. Probably easiest to do with jQuery (see second answer). – Rob Jun 12 '23 at 22:50
  • yeah exactly, but I'm clueless how to convert json into the table with JS. I don't really know much JS at all. – big boss Jun 12 '23 at 22:54
  • I think the best way would be to iterate through the json object and add each key val pair to an array. Then you can iterate through the array and add the pairs to the table. There are probably some decent posts or videos out there that show how to add rows to your table. If you have any specific questions feel free to ask – Rob Jun 12 '23 at 23:11