I'm pretty new to Python and am currently exploring Flask. I wanted to subclass one the Field classes to create a percentage field but when I add it to a form, it crashes my interpreter. Here's the code:
I am probably making an obvious mistake somewhere but I have no clue what exactly.
Thanks in advance,
Stef
Some additional info:
The crash only happens when I add
I get the following error message in the terminal window:
Stef
Oversight. So, the bug happens when I add {{ parameter_form.growth_rate }}
So the object is created but things go awry when it is rendered in the browser.
Narrowed it doen further. Apparently the error occurs as soon as I add the process_data() function to the class. Tried just returning the value, returning self and not returning anything (just writing a line of mock code).
Everything leads to a fatal crash but not right away. When I just print the value to the output window I see a couple of values (listed below) before I get a crash. So it has something to do with the WTF package it seems.
The values:
0.0
1.9
4.0
1.0
2.5
0.5
20.0
100.0
0.0
20.0
2.0
2.0
0.0
2.0
from flask_wtf import FlaskForm
from wtforms import Form, FloatField, IntegerField, BooleanField, SelectField, validators
from constants import *
class PercentField(FloatField):
def __init__(self, *args, **kwargs):
super(PercentField, self).__init__(validators=[validators.number_range(0, 100)], *args, **kwargs)
def process_data(self, value):
return value / 100
class ParameterForm(FlaskForm):
num_iterations = IntegerField('Iterations', default=500)
initial_im2 = FloatField('Initial IM2', default=100000)
growth_rate = PercentField('Growth rate', default=0.0)And here's the .html file<!DOCTYPE html>
<title>Parameter Form</title>
{% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %}
<ul>
{% for message in messages %}
<li>{{ message[1] }}</li>
{% endfor %}</ul>
{% endif %}
{% endwith %}
<form action="" method="post">
{{ parameter_form.csrf }}
<div class="input number">
{{ parameter_form.num_iterations.label }} {{ parameter_form.num_iterations }}</div>
<div class="input number">
{{ parameter_form.initial_im2.label }} {{ parameter_form.initial_im2 }}</div>
<div class="input percentage">
{{ parameter_form.growth_rate.label }} {{ parameter_form.growth_rate }}</div>
<div class="input submit">
<input type="submit" value="Run"></div>
</form>I only get the crash when I add the line with {{ parameter_form.growth_rate.label }} {{ parameter_form.growth_rate }}I am probably making an obvious mistake somewhere but I have no clue what exactly.
Thanks in advance,
Stef
Some additional info:
The crash only happens when I add
I get the following error message in the terminal window:
Error:<property object at 0x10276b048>
127.0.0.1 - - [13/Feb/2019 12:49:03] "GET / HTTP/1.1" 200 -
<property object at 0x10276b048>
Process finished with exit code 245Thanks,Stef
Oversight. So, the bug happens when I add {{ parameter_form.growth_rate }}
So the object is created but things go awry when it is rendered in the browser.
Narrowed it doen further. Apparently the error occurs as soon as I add the process_data() function to the class. Tried just returning the value, returning self and not returning anything (just writing a line of mock code).
Everything leads to a fatal crash but not right away. When I just print the value to the output window I see a couple of values (listed below) before I get a crash. So it has something to do with the WTF package it seems.
The values:
0.0
1.9
4.0
1.0
2.5
0.5
20.0
100.0
0.0
20.0
2.0
2.0
0.0
2.0
