我的烧瓶应用程序具有以下形式。我想确保输入值实际上是整数,并且如果在令牌> k中输入的值(此处k可以是某个数字),则会在屏幕上显示一条错误消息。 IntegerField似乎不强制执行整数值,例如,如果用户输入2.3,它将其传递给我的函数,该函数失败,因为期望整数。
这种类型的错误消息会在表格中发生吗,或者一旦将值从表格传递到服务器,我是否需要在flask应用程序中对其进行编程?
class Form(FlaskForm):
token = IntegerField('Token Size', [DataRequired()], default = 2)
submit = SubmitField('Submit')
编辑 根据下面的评论,用我修改后的表格和路线对其进行更新
class Form(FlaskForm):
token = IntegerField('Token Size', validators=[DataRequired(), NumberRange(min=1, max=10, message='Something')], default = 2)
ngram_method = SelectField('Method', [DataRequired()],
choices=[('sliding', 'Sliding Window Method'),
('adjacent', 'Adjacent Text Method')])
rem_stop = BooleanField('Remove Stop Words', render_kw={'checked': True})
rem_punc = BooleanField('Remove Punctuation', default = True)
text2use = SelectField('Text To Use for Word Tree', [DataRequired()],
choices=[('clean', 'Clean/Processed Text'),
('original', 'Original Text String')])
pivot_word = TextField('Pivot Word for Word Tree', [DataRequired()])
submit = SubmitField('Submit')
以及使用表格的路径
@word_analyzer.route('/text', methods=('GET', 'POST'))
def text_analysis():
form = Form()
result = '<table></table>'
ngrams = '<table></table>'
orig_text = '<table></table>'
text = ""
if request.method == 'POST':
tmp_filename = tempfile.gettempdir()+'\\input.txt'
if request.files:
txt_upload = request.files.get('text_file')
if txt_upload:
f = request.files['text_file']
f.save(tmp_filename)
if os.path.exists(tmp_filename):
file = open(tmp_filename, 'r', encoding="utf8")
theText = [line.rstrip('\n') for line in file]
theText = str(theText)
token_size = form.token.data
stops = form.rem_stop.data
punc = form.rem_punc.data
ngram_method = form.ngram_method.data
text_result = text_analyzer(theText, token_size = token_size, remove_stop = stops, remove_punctuation = punc, method = ngram_method)
result = pd.DataFrame.from_dict(text_result, orient='index', columns = ['Results'])[:-3].to_html(classes='table table-striped table-hover', header = "true", justify = "center")
ngrams = pd.DataFrame.from_dict(text_result['ngrams'], orient='index', columns = ['Frequency']).to_html(classes='table table-striped table-hover', header = "true", justify = "center")
if form.pivot_word.data == None:
top_word = json.dumps(text_result['Top Word'])
else:
top_word = json.dumps(form.pivot_word.data)
if form.text2use.data == 'original':
text = json.dumps(text_result['original_text'])
else:
text = json.dumps(text_result['clean_text'])
if form.validate_on_submit():
return render_template('text.html', results = [result], ngrams = [ngrams], form = form, text=text, top_word = top_word)
return render_template('text.html', form = form, results = [result],ngrams = [ngrams], text=text, top_word='')
答案 0 :(得分:1)
使用2020-05-20 10:37:42.681961-0700 ---[61653:10276450] [RemoteControl] userIdentityForMediaRemoteOptions -❗️No user identity data. Using active account.
2020-05-20 10:37:42.774906-0700 ---[61653:10276375] Audio session options array to bitmask: 1
2020-05-20 10:37:43.359056-0700 ---[61653:10276523] [SDKPlayback]
-[MPMusicPlayerController prepareToPlayWithCompletionHandler:] completed id=systemMusicPlayer error:
Error Domain=MPCPlayerRequestErrorDomain Code=1000 "Failed to send command 122"
UserInfo={NSDebugDescription=Failed to send command 122,
NSUnderlyingError=0x281170150 {Error Domain=MPCPlayerRequestErrorDomain Code=1000 "Failed to send command 122 (MRMediaRemoteCommandHandlerStatus = 2)"
UserInfo={NSDebugDescription=Failed to send command 122 (MRMediaRemoteCommandHandlerStatus = 2),
MPCPlayerErrorKeyMediaRemoteCommandHandlerStatus=2}}}
中的NumberRange
验证程序。您可以传递可选的最小值和最大值以及错误消息。更多信息here
更新
wtforms.validators.NumberRange
这是一个应该起作用的示例,请确保您的表单类字段看起来相似,并且在路由中使用# Form Class
class Form(FlaskForm):
token = FloatField('Token Size', validators=[DataRequired(), NumberRange(min=1, max=10, message='Something')])
# Route
if form.validate_on_submit():
print(form.name.data)
。