Q:

Inserting into Sqlalchemy when number of variables is unknown

I have a html document which dynamically allows a user to create a table. There is an 'Add Row' button, and by clicking on the button they can add a row of inputs to the table. The code that gets added would look like so: 
<tr>
    <td class = 'sct_components_input'> <input class = 'sct_components_input_5'> </input> </td>
    <td class = 'sct_components_input'> <input class = 'sct_components_input_5'> </input> </td>
    <td class = 'sct_components_input'> <input class = 'sct_components_input_10'> </input> </td>
    <td class = 'sct_components_input'> <input class = 'sct_components_input_10'> </input> </td>
    <td class = 'sct_components_input'> <textarea class = 'sct_components_input_15'> </textarea> </td>
    <td class = 'sct_components_input'> <input class = 'sct_components_input_5'> </input> </td>
    <td class = 'sct_components_input'> <input class = 'sct_components_input_5'> </input> </td>
    <td class = 'sct_components_input'> <textarea class = 'sct_components_input_15'> </textarea> </td>
</tr>

So I am left with a table with lots of inputs which a user has created X amount of rows, this value will change with each user. I need to upload all these inputs to a mysql table using FLASK (sqlalchemy). But I have no idea how to upload these inputs. 
My best guess is that you will have to loop the sqlalchemy upload query multiple times, uploading 1 row at a time. Similar to how you would upload a CSV file to sqlalchemy. 
However I do not know how I would give each input a unique 'name' and thus allow the form to be uploaded. 

A:

Assuming your dynamically made table looks something like this:
<form>
  <table>
    <tr>
      <td><input name="first_name"></td>
      <td><input name="age"></td>
    </tr>
    <tr>
      <td><input name="first_name"></td>
      <td><input name="age"></td>
    </tr>
  </table>
</form>

Then, in python you can access all the data as follows:
first_names = request.form.getlist('first_name')
ages = request.form.getlist('age')
for first_name, age in zip(first_names, ages):
    person = Person(first_name, age)
    db.session.add(person)
db.session.commit()

This method allows you to fetch all input elements in the DOM with the same name. You then combine the fetched list of names with the fetched list of ages using zip(). This way you can dynamically add a number of matched to the database. 
Another approach would be to give the DOM elements unique names, but this is a bit easier.

