Skip to content

Instantly share code, notes, and snippets.

@nsfyn55
Last active August 29, 2015 14:02
Show Gist options
  • Save nsfyn55/039288a4c1a6dd6ca8ee to your computer and use it in GitHub Desktop.
Save nsfyn55/039288a4c1a6dd6ca8ee to your computer and use it in GitHub Desktop.
Stack Predictions Sample
from collections import namedtuple
from wtforms import Form, IntegerField, FieldList, FormField, TextField
from werkzeug.datastructures import MultiDict
Prediction = namedtuple('Prediction', ['id'])
predictions = [Prediction(id=1), Prediction(id=2)]
class PredictionForm(Form):
id = IntegerField()
home = TextField()
away = TextField()
class PredictionListForm(Form):
predictions = FieldList(FormField(PredictionForm))
form = PredictionListForm(data=MultiDict({'predictions': predictions}))
print form.predictions()
@nsfyn55
Copy link
Author

nsfyn55 commented Jun 5, 2014

This is the result

<ul id="predictions">
   <li>
      <label for="predictions-0">Predictions-0</label> 
      <table id="predictions-0">
         <tr>
            <th><label for="predictions-0-id">Id</label></th>
            <td><input id="predictions-0-id" name="predictions-0-id" type="text" value="1"></td>
         </tr>
         <tr>
            <th><label for="predictions-0-home">Home</label></th>
            <td><input id="predictions-0-home" name="predictions-0-home" type="text" value=""></td>
         </tr>
         <tr>
            <th><label for="predictions-0-away">Away</label></th>
            <td><input id="predictions-0-away" name="predictions-0-away" type="text" value=""></td>
         </tr>
      </table>
   </li>
   <li>
      <label for="predictions-1">Predictions-1</label> 
      <table id="predictions-1">
         <tr>
            <th><label for="predictions-1-id">Id</label></th>
            <td><input id="predictions-1-id" name="predictions-1-id" type="text" value="2"></td>
         </tr>
         <tr>
            <th><label for="predictions-1-home">Home</label></th>
            <td><input id="predictions-1-home" name="predictions-1-home" type="text" value=""></td>
         </tr>
         <tr>
            <th><label for="predictions-1-away">Away</label></th>
            <td><input id="predictions-1-away" name="predictions-1-away" type="text" value=""></td>
         </tr>
      </table>
   </li>
</ul>

@nsfyn55
Copy link
Author

nsfyn55 commented Jun 5, 2014

Python 2.7.6

Flask==0.10.1
Jinja2==2.7.2
MarkupSafe==0.23
WTForms==2.0
WebOb==1.4
Werkzeug==0.9.4
itsdangerous==0.24
wsgiref==0.1.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment