Created
February 15, 2022 16:54
-
-
Save Whatapalaver/24914e03e6452bf599c32f4a22685b42 to your computer and use it in GitHub Desktop.
RadioFields using WTForms in Flask
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask_wtf import FlaskForm | |
from wtforms import TextAreaField, SubmitField, RadioField | |
from wtforms.validators import DataRequired | |
class HtmlForm(FlaskForm): | |
html = TextAreaField( | |
"Paste exported html", validators=[DataRequired()] | |
) | |
hosts = [("s1", "S1"), ("b2", "B2")] | |
host_option = RadioField("Label", choices=hosts, default="b2") | |
submit = SubmitField("Process") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends "base.html" %} | |
{% block content %} | |
<h1>Process HTML</h1> | |
<form action="" method="post" novalidate> | |
{{ form.hidden_tag() }} | |
<p> | |
{{ form.html.label }}<br> | |
{{ form.html(rows='30',cols='100') }} | |
{% for error in form.html.errors %} | |
<p style="color: red;">[{{ error }}]</p> | |
{% endfor %} | |
</p> | |
<p>Select host destination</p> | |
{% for subfield in form.host_option %} | |
<tr> | |
<td>{{ subfield }}</td> | |
<td>{{ subfield.label }}</td> | |
</tr> | |
{% endfor %} | |
<p>{{ form.submit() }}</p> | |
</form> | |
{% endblock %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, flash, render_template, redirect, url_for, request | |
from config import Config | |
from forms import HtmlForm | |
app = Flask(__name__) | |
app.config.from_object(Config) | |
@app.route("/", methods=["GET", "POST"]) | |
def index(): | |
form = HtmlForm() | |
if form.validate_on_submit(): | |
# DO SOMETHING | |
pass | |
return redirect(url_for("processed", processed_html=processed_html)) | |
return render_template("index.html", title="Export", form=form) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment