-1

I want to generate this JSON object containing an array of objects from form inputs:

{
"network":[
    {"layer_type": "conv2d", "num_filters": 16, "kernel_size": 2, "padding": "valid", "stride": 2},
    {"layer_type": "max_pool2d", "num_filters": 16, "kernel_size": 2, "padding": "valid", "stride": 2},
    {"layer_type": "conv2d", "num_filters": 32, "kernel_size": 3, "padding": "valid", "stride": 2}       
  ]
}

Is there a way I can do this using Flask?

Update

Here's what the form looks like:

form with array of objects

As for the snippet of html code dynamically generated:

<li>
    <select name="network[][layer_type]"><!-- options here --></select>
    <input type="number" name="network[][num_filters]">
    <!-- other parameters here -->
</li>
<li>
    <select name="network[][layer_type]"><!-- options here --></select>
    <input type="number" name="network[][num_filters]">
    <!-- other parameters here -->
</li>

Edit: Since this question is being marked as duplicate I'm going to add more info. I want to achieve something like this in this question but using Flask:

{"students" => [
  {
    "first" => "foo",
     "last" => "bar",
      "age" => "21"
  },
  {
    "first" => "baz",
     "last" => "qux",
      "age" => "19"
  }
]}

It does work with Ruby according to the accepted answer there by having this kind of form:

<!-- first student -->
<input type="text" name="students[][first]">
<input type="text" name="students[][last]">
<input type="text" name="students[][age]">

<!-- second student -->
<input type="text" name="students[][first]">
<input type="text" name="students[][last]">
<input type="text" name="students[][age]">

But I want to know how to do it using Flask.

Rocket Pingu
  • 581
  • 6
  • 24

1 Answers1

1
  1. You can access the form data as json using form.data

For e.g.

Consider the form defined as below,

class GeneralForm(FlaskForm):
    boolean_val = BooleanField('Boolean')
    a_float = FloatField('Severity')
    submit = SubmitField('Submit')

In the app route,

@app.route('/wtforms', methods=['GET', 'POST'])
def debug_wtforms():
    form = GeneralForm()
    if request.method == 'POST' and form.validate_on_submit():
        print(form.data) # Form data as a dict
    return render_template('index1.html', form=form)
  1. If you have simply defined the form directly in the html template, you can access the form data using, request.form.to_dict(flat=False)

I hope this helps.

  • I defined the form form directly in the html template. I followed the second option you gave and I didn't get the output I want. Here's the output I got: `{'network[][layer_type]': 'conv2d', 'network[][padding]': 'valid', 'output_layer': 'ctc_decoder', 'network[][kernel_size]': '2', 'loss': 'ctc', 'network[][num_filters]': '16', 'optimizer': 'adam', 'learning_rate': '0.0001', 'network[][stride]': '2', 'architecture_name': 'dummy_model', 'action': '', 'network[][pool_size]': '2', 'metrics': 'label_error_rate'}` – Rocket Pingu Mar 19 '18 at 03:56
  • Could you support your question with the html template containing the form ? – George J Padayatti Mar 19 '18 at 03:57
  • Updating the post. – Rocket Pingu Mar 19 '18 at 03:57
  • Don't mind the rest of the the parameters that don't contain "network" in my comment. – Rocket Pingu Mar 19 '18 at 04:09
  • Here is a related question: https://stackoverflow.com/questions/38126371/handling-array-as-form-name-in-flask – Rocket Pingu Mar 19 '18 at 07:27