0

I am using the following HTML code for form options:

<form action="/submit" method="POST">

        <div class="form-group">
          <h3>Section</h3>
          <select name="articles">
            <option value="">Select Section</option>
            <option value="Business">Business</option>
            <option value="Sport">Sports</option>
            <option value="Culture">Culture</option>
            <option value="Technology">Technology</option>

          </select>
        </div>

And the following python code:

url = 'https://api.nytimes.com/svc/topstories/v2/{section}.json?api-XXXX'

response1 = urllib.request.urlopen(url).read()
data = json.loads(response1.decode('utf-8').replace('\n', ''))

I am using Jinja2 and Flask and would like the {section} to be filled according to the option chosen by the user before submitting.

Thanks!

GOONhoon
  • 55
  • 6

1 Answers1

0

Looks similar to this question: Getting value from select tag using flask

Grab the results of the select element using flask's built-in request.form.get method. From there, pass the value to an f-string using the exact URL string as you posted above except with the f in front.

See https://docs.python.org/3/reference/lexical_analysis.html#formatted-string-literals

section = flask.request.form.get('articles')
url = f'https://api.nytimes.com/svc/topstories/v2/{section}.json?api-XXXX'
ntbloom
  • 111
  • 1
  • 9
  • I have struggles implementing this. I think the issue might be that I am working with the URLs as soon as the code runs, and passing the value to {section} happens only after the user submits his choice on the app's frontpage. – GOONhoon May 26 '20 at 11:31
  • Sounds like it's better to do it all on the client side using javascript. Something like `const value = document.getElementById('id-you-created-for-the-select-tag').value; const url = \`http://blah.blah/api/${value}/\`` and then use the `fetch` API to make the call. All of the code would run in the browser and you don't involve Flask at all. – ntbloom May 26 '20 at 11:54
  • I, unfortunately, have absolutely no idea about JS to implement that. I simply wanted to learn more about Flask and made a simple web app that allows the user to choose what sections of a given news site to scrape (i.e. business, sports etc.) and then returns the summaries of the top articles. My issue is that I wanted to extend the hardcoded section within the URL with a form field, where the user can choose. Perhaps I might be forced to create 'if-statements' on the route to then redirect to a different template with every section, although that sounds troublesome :D – GOONhoon May 26 '20 at 13:11
  • If you don't want to use javascript then you need to send the user's form selection to the server for Flask to parse. That's what the `flask.request.get` method does for you. You'll need a submit button to make that happen -- when the user presses submit, Flask reads the results of the select form, makes the appropriate nyt API call, and then renders the headlines on the page accordingly. FWIW this is kind of task is done more easily in just javascript since you're using someone else's API. Generally speaking, Flask is for standing up your own API where you have control over the content. – ntbloom May 26 '20 at 13:40
  • I see. Thanks for the reply anyway. I have a button that already links with a different form, and wanted to include more options by creating additional forms. It, however, gives me a traceback error when attempting the method you posted above, since there is no URL (before submit button is pressed) that the code can run. I hardcoded three URLs to be scraped through an API and then summarized the content in them and it feels like all of this happens before the submit button is pressed (since the button only redirects to either of the two present HTML templates). – GOONhoon May 26 '20 at 14:35
  • No problem. Hope you find what you're looking for. – ntbloom May 26 '20 at 16:04