'Python, Flask, Javascript, HTML - Invalid literal for int()

I'm trying to post a number from a HTML input to my Python function, where the number will be used to identify the limit parameter in bing_image_downloader.

Python code looks like this:

@app.route('/indsamle_billede_bing', methods=['GET', 'POST' 'DELETE'])
def indsamle_billede2():
    Keyword = request.args.get('key_word', '')
    Antal = request.args.get('antal_word', '')



    downloader.download(str(Keyword), limit=Antal,  output_dir='hej', adult_filter_off=True, force_replace=False, timeout=60, verbose=True)


return 'something'

HTML code looks like this:

      <input value="" placeholder="Antal" name="Antal" type="number" id="antal_in">
      <input value=""  placeholder="Keyword" name="Keyword" type="text" id="keyword_in">
      <input value="Bing" name="check" type="radio" id="Bing_x">
      <label for="Bing_x">Bing</label>
      <button id="submit">Submit</button>

Javascript code looks like this:

<script> 
$(document).ready(function() {
$('button').click(function() {
      var radio = $("input[type=radio][name=check]").filter(":checked")[0];
      var key = $("input[type=text][name=Keyword]");
      const KEY = document.getElementById("keyword_in").value;
      const ANTAL = document.getElementById("antal_in").value;
      
      let myVar = '';
      let myVar2 = '';
      
      if (radio.value == "Bing") {
          myVar = KEY
          myVar2 = ANTAL
          

      fetch('http://127.0.0.1:5000/indsamle_billede_bing?key_word='+myVar)
          .then(res => res.json())
          .then(data => {
    })
      fetch('http://127.0.0.1:5000/indsamle_billede_bing?antal_word='+myVar2)
          .then(res => res.json())
          .then(data => {
    })})});
</script>

Error code looks like this:

127.0.0.1 - - [03/Mar/2022 18:35:45] "GET /indsamle_billede_bing?key_word=friends HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/flask/app.py", line 2091, in __call__
    return self.wsgi_app(environ, start_response)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/flask/app.py", line 2076, in wsgi_app
    response = self.handle_exception(e)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/flask/app.py", line 2073, in wsgi_app
    response = self.full_dispatch_request()
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/flask/app.py", line 1518, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/flask/app.py", line 1516, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/flask/app.py", line 1502, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "/Users/Jacob/Desktop/billede_indsamling-main/billede_indsamling-main/XCI_software/billede_indsamling.py", line 25, in indsamle_billede2
    downloader.download(str(Keyword), limit=Antal,  output_dir='hej', adult_filter_off=True, force_replace=False, timeout=60, verbose=True).format(Antal)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/bing_image_downloader/downloader.py", line 37, in download
    bing = Bing(query, limit, image_dir, adult, timeout, filter, verbose)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/bing_image_downloader/bing.py", line 24, in __init__
    assert type(limit) == int, "limit must be integer"
AssertionError: limit must be integer
127.0.0.1 - - [03/Mar/2022 18:35:45] "GET /indsamle_billede_bing?antal_word=6 HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/flask/app.py", line 2091, in __call__
    return self.wsgi_app(environ, start_response)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/flask/app.py", line 2076, in wsgi_app
    response = self.handle_exception(e)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/flask/app.py", line 2073, in wsgi_app
    response = self.full_dispatch_request()
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/flask/app.py", line 1518, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/flask/app.py", line 1516, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/flask/app.py", line 1502, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "/Users/Jacob/Desktop/billede_indsamling-main/billede_indsamling-main/XCI_software/billede_indsamling.py", line 25, in indsamle_billede2
    downloader.download(str(Keyword), limit=Antal,  output_dir='hej', adult_filter_off=True, force_replace=False, timeout=60, verbose=True).format(Antal)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/bing_image_downloader/downloader.py", line 37, in download
    bing = Bing(query, limit, image_dir, adult, timeout, filter, verbose)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/bing_image_downloader/bing.py", line 24, in __init__
    assert type(limit) == int, "limit must be integer"
AssertionError: limit must be integer

The problem is that the 'Antal' variable is not recognized as an integer



Solution 1:[1]

Try using Antal = request.args.get('antal_word', 0) instead. Because it seems you are expecting an integer for Antalbut you are setting a default value of'' an empty string.

But really this depends on what you are using the value for. Setting a default value when using request.get simply means that if the value is not in the request then the variable would be set to the default value.

If more information is given there might be a better approach than setting the default value to 0

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1