Saltear al contenido principal

Ejemplos flask.templating render_template Código de ejemplo Python

render_template es una función Flask del flask.templating paquete.
render_template se utiliza para generar resultados a partir de un archivo de plantilla basado en el motor Jinja2 que se encuentra en la carpeta de plantillas de la aplicación.

Contrata a un experto en Python para iniciar tu proyecto hoy mismo:

Tenga en cuenta que render_template normalmente se importa directamente del flask
paquete en lugar de desde flask.templating. Es la misma función que se importa, pero hay menos caracteres para escribir cuando deja fuera de la .templating parte.

render_template_string es otro invocable desde el flask.templating paquete con ejemplos de código.

Debería leer sobre estos temas junto con estos render_template ejemplos:

Ejemplo 1 de la aplicación Braintree Flask

Ejemplo de aplicación de pagos de Braintree’s Flask demuestra cómo incorporar la API de este proveedor de pagos en su aplicación web Flask. El código es de código abierto bajo el Licencia MIT.

Aplicación Braintree Flask / app.py

# app.py
from flask import Flask, redirect, url_for, render_template, request, flash

import os
from os.path import join, dirname
from dotenv import load_dotenv
import braintree
from gateway import generate_client_token, transact, find_transaction

load_dotenv()

app = Flask(__name__)
app.secret_key = os.environ.get('APP_SECRET_KEY')

PORT = int(os.environ.get('PORT', 4567))

TRANSACTION_SUCCESS_STATUSES = [
    braintree.Transaction.Status.Authorized,
    braintree.Transaction.Status.Authorizing,
    braintree.Transaction.Status.Settled,
    braintree.Transaction.Status.SettlementConfirmed,
    braintree.Transaction.Status.SettlementPending,
    braintree.Transaction.Status.Settling,
    braintree.Transaction.Status.SubmittedForSettlement
]

@app.route('/', methods=['GET'])
def index():
    return redirect(url_for('new_checkout'))

@app.route('/checkouts/new', methods=['GET'])
def new_checkout():
    client_token = generate_client_token()
    return render_template('checkouts/new.html', client_token=client_token)

@app.route('/checkouts/<transaction_id>', methods=['GET'])
def show_checkout(transaction_id):
    transaction = find_transaction(transaction_id)
    result = {}
    if transaction.status in TRANSACTION_SUCCESS_STATUSES:
        result = {
            'header': 'Sweet Success!',
            'icon': 'success',
            'message': 'Your test transaction has been successfully processed. See the Braintree API response and try again.'
        }
    else:
        result = {
            'header': 'Transaction Failed',
            'icon': 'fail',
            'message': 'Your test transaction has a status of ' + transaction.status + '. See the Braintree API response and try again.'
        }

    return render_template('checkouts/show.html', transaction=transaction, result=result)

@app.route('/checkouts', methods=['POST'])
def create_checkout():
    result = transact({
        'amount': request.form['amount'],
        'payment_method_nonce': request.form['payment_method_nonce'],
        'options': {
            "submit_for_settlement": True
        }
    })

    if result.is_success or result.transaction:
        return redirect(url_for('show_checkout',transaction_id=result.transaction.id))
    else:
        for x in result.errors.deep_errors: flash('Error: %s: %s' % (x.code, x.message))
        return redirect(url_for('new_checkout'))

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=PORT, debug=True)



## ... source file continues with no further render_template examples...

Ejemplo 2 de CTFd

CTFd (página principal) es un Capturar la aplicación web de piratería de bandera (CTF) construido con Matraz. La aplicación se puede utilizar tal cual para ejecutar eventos CTF o modificarse para reglas personalizadas para escenarios relacionados. CTFd es de código abierto bajo el Licencia Apache 2.0.

CTFd / CTFd / auth.py

# auth.py
import base64

import requests
from flask import Blueprint
from flask import current_app as app
from flask import redirect, render_template, request, session, url_for
from itsdangerous.exc import BadSignature, BadTimeSignature, SignatureExpired

from CTFd.cache import clear_team_session, clear_user_session
from CTFd.models import Teams, UserFieldEntries, UserFields, Users, db
from CTFd.utils import config, email, get_app_config, get_config
from CTFd.utils import user as current_user
from CTFd.utils import validators
from CTFd.utils.config import is_teams_mode
from CTFd.utils.config.integrations import mlc_registration
from CTFd.utils.config.visibility import registration_visible
from CTFd.utils.crypto import verify_password
from CTFd.utils.decorators import ratelimit
from CTFd.utils.decorators.visibility import check_registration_visibility
from CTFd.utils.helpers import error_for, get_errors, markup
from CTFd.utils.logging import log
from CTFd.utils.modes import TEAMS_MODE
from CTFd.utils.security.auth import login_user, logout_user
from CTFd.utils.security.signing import unserialize
from CTFd.utils.validators import ValidationError

auth = Blueprint("auth", __name__)


@auth.route("/confirm", methods=["POST", "GET"])
@auth.route("/confirm/<data>", methods=["POST", "GET"])
@ratelimit(method="POST", limit=10, interval=60)
def confirm(data=None):
    if not get_config("verify_emails"):
        return redirect(url_for("challenges.listing"))

    if data and request.method == "GET":
        try:
            user_email = unserialize(data, max_age=1800)
        except (BadTimeSignature, SignatureExpired):
            return render_template(
                "confirm.html", errors=["Your confirmation link has expired"]
            )
        except (BadSignature, TypeError, base64.binascii.Error):
            return render_template(
                "confirm.html", errors=["Your confirmation token is invalid"]
            )

        user = Users.query.filter_by(email=user_email).first_or_404()
        if user.verified:
            return redirect(url_for("views.settings"))

        user.verified = True
        log(
            "registrations",
            format="[{date}] {ip} - successful confirmation for {name}",
            name=user.name,
        )
        db.session.commit()
        clear_user_session(user_id=user.id)
        email.successful_registration_notification(user.email)
        db.session.close()
        if current_user.authed():
            return redirect(url_for("challenges.listing"))
        return redirect(url_for("auth.login"))

    if current_user.authed() is False:
        return redirect(url_for("auth.login"))

    user = Users.query.filter_by(id=session["id"]).first_or_404()
    if user.verified:
        return redirect(url_for("views.settings"))

    if data is None:
        if request.method == "POST":
            email.verify_email_address(user.email)
            log(
                "registrations",
                format="[{date}] {ip} - {name} initiated a confirmation email resend",
            )
            return render_template(
                "confirm.html", infos=[f"Confirmation email sent to {user.email}!"]
            )
        elif request.method == "GET":
            return render_template("confirm.html")


@auth.route("/reset_password", methods=["POST", "GET"])
@auth.route("/reset_password/<data>", methods=["POST", "GET"])
@ratelimit(method="POST", limit=10, interval=60)
def reset_password(data=None):
    if config.can_send_mail() is False:
        return render_template(
            "reset_password.html",
            errors=[
                markup(
                    "This CTF is not configured to send email.<br> Please contact an organizer to have your password reset."
                )
            ],
        )

    if data is not None:
        try:
            email_address = unserialize(data, max_age=1800)
        except (BadTimeSignature, SignatureExpired):
            return render_template(
                "reset_password.html", errors=["Your link has expired"]
            )
        except (BadSignature, TypeError, base64.binascii.Error):
            return render_template(
                "reset_password.html", errors=["Your reset token is invalid"]
            )

        if request.method == "GET":
            return render_template("reset_password.html", mode="set")
        if request.method == "POST":
            password = request.form.get("password", "").strip()
            user = Users.query.filter_by(email=email_address).first_or_404()
            if user.oauth_id:
                return render_template(
                    "reset_password.html",
                    infos=[
                        "Your account was registered via an authentication provider and does not have an associated password. Please login via your authentication provider."
                    ],
                )

            pass_short = len(password) == 0
            if pass_short:
                return render_template(
                    "reset_password.html", errors=["Please pick a longer password"]
                )

            user.password = password
            db.session.commit()
            clear_user_session(user_id=user.id)
            log(
                "logins",
                format="[{date}] {ip} -  successful password reset for {name}",
                name=user.name,
            )
            db.session.close()
            email.password_change_alert(user.email)
            return redirect(url_for("auth.login"))

    if request.method == "POST":
        email_address = request.form["email"].strip()
        user = Users.query.filter_by(email=email_address).first()

        get_errors()

        if not user:
            return render_template(
                "reset_password.html",
                infos=[
                    "If that account exists you will receive an email, please check your inbox"
                ],
            )

        if user.oauth_id:
            return render_template(
                "reset_password.html",
                infos=[
                    "The email address associated with this account was registered via an authentication provider and does not have an associated password. Please login via your authentication provider."
                ],
            )

        email.forgot_password(email_address)

        return render_template(
            "reset_password.html",
            infos=[
                "If that account exists you will receive an email, please check your inbox"
            ],
        )
    return render_template("reset_password.html")


@auth.route("/register", methods=["POST", "GET"])
@check_registration_visibility
@ratelimit(method="POST", limit=10, interval=5)
def register():
    errors = get_errors()
    if current_user.authed():
        return redirect(url_for("challenges.listing"))

    if request.method == "POST":
        name = request.form.get("name", "").strip()
        email_address = request.form.get("email", "").strip().lower()
        password = request.form.get("password", "").strip()

        website = request.form.get("website")
        affiliation = request.form.get("affiliation")
        country = request.form.get("country")

        name_len = len(name) == 0
        names = Users.query.add_columns("name", "id").filter_by(name=name).first()
        emails = (
            Users.query.add_columns("email", "id")
            .filter_by(email=email_address)


## ... source file abbreviated to get to render_template examples ...


            errors.append(
                "Only email addresses under {domains} may register".format(
                    domains=get_config("domain_whitelist")
                )
            )
        if names:
            errors.append("That user name is already taken")
        if team_name_email_check is True:
            errors.append("Your user name cannot be an email address")
        if emails:
            errors.append("That email has already been used")
        if pass_short:
            errors.append("Pick a longer password")
        if pass_long:
            errors.append("Pick a shorter password")
        if name_len:
            errors.append("Pick a longer user name")
        if valid_website is False:
            errors.append("Websites must be a proper URL starting with http or https")
        if valid_country is False:
            errors.append("Invalid country")
        if valid_affiliation is False:
            errors.append("Please provide a shorter affiliation")

        if len(errors) > 0:
            return render_template(
                "register.html",
                errors=errors,
                name=request.form["name"],
                email=request.form["email"],
                password=request.form["password"],
            )
        else:
            with app.app_context():
                user = Users(name=name, email=email_address, password=password)

                if website:
                    user.website = website
                if affiliation:
                    user.affiliation = affiliation
                if country:
                    user.country = country

                db.session.add(user)
                db.session.commit()
                db.session.flush()

                for field_id, value in entries.items():
                    entry = UserFieldEntries(
                        field_id=field_id, value=value, user_id=user.id


## ... source file abbreviated to get to render_template examples ...



                if config.can_send_mail() and get_config(
                    "verify_emails"
                ):  # Confirming users is enabled and we can send email.
                    log(
                        "registrations",
                        format="[{date}] {ip} - {name} registered (UNCONFIRMED) with {email}",
                    )
                    email.verify_email_address(user.email)
                    db.session.close()
                    return redirect(url_for("auth.confirm"))
                else:  # Don't care about confirming users
                    if (
                        config.can_send_mail()
                    ):  # We want to notify the user that they have registered.
                        email.successful_registration_notification(user.email)

        log("registrations", "[{date}] {ip} - {name} registered with {email}")
        db.session.close()

        if is_teams_mode():
            return redirect(url_for("teams.private"))

        return redirect(url_for("challenges.listing"))
    else:
        return render_template("register.html", errors=errors)


@auth.route("/login", methods=["POST", "GET"])
@ratelimit(method="POST", limit=10, interval=5)
def login():
    errors = get_errors()
    if request.method == "POST":
        name = request.form["name"]

        if validators.validate_email(name) is True:
            user = Users.query.filter_by(email=name).first()
        else:
            user = Users.query.filter_by(name=name).first()

        if user:
            if user and verify_password(request.form["password"], user.password):
                session.regenerate()

                login_user(user)
                log("logins", "[{date}] {ip} - {name} logged in")

                db.session.close()
                if request.args.get("next") and validators.is_safe_url(
                    request.args.get("next")
                ):
                    return redirect(request.args.get("next"))
                return redirect(url_for("challenges.listing"))

            else:
                log("logins", "[{date}] {ip} - submitted invalid password for {name}")
                errors.append("Your username or password is incorrect")
                db.session.close()
                return render_template("login.html", errors=errors)
        else:
            log("logins", "[{date}] {ip} - submitted invalid account information")
            errors.append("Your username or password is incorrect")
            db.session.close()
            return render_template("login.html", errors=errors)
    else:
        db.session.close()
        return render_template("login.html", errors=errors)


@auth.route("/oauth")
def oauth_login():
    endpoint = (
        get_app_config("OAUTH_AUTHORIZATION_ENDPOINT")
        or get_config("oauth_authorization_endpoint")
        or "https://auth.majorleaguecyber.org/oauth/authorize"
    )

    if get_config("user_mode") == "teams":
        scope = "profile team"
    else:
        scope = "profile"

    client_id = get_app_config("OAUTH_CLIENT_ID") or get_config("oauth_client_id")

    if client_id is None:
        error_for(
            endpoint="auth.login",
            message="OAuth Settings not configured. "
            "Ask your CTF administrator to configure MajorLeagueCyber integration.",
        )
        return redirect(url_for("auth.login"))


## ... source file continues with no further render_template examples...

Ejemplo 3 de FlaskBB

Matraz BB (sitio web del proyecto) es una aplicación web de foro basada en Flask. La aplicación web permite a los usuarios chatear en un tablero de mensajes abierto o enviar mensajes privados en texto sin formato o Markdown.

FlaskBB se proporciona como código abierto bajo esta licencia.

FlaskBB / flaskbb / email.py

# email.py
import logging
from flask import render_template
from flask_mail import Message
from flask_babelplus import lazy_gettext as _

from flaskbb.extensions import mail, celery


logger = logging.getLogger(__name__)


@celery.task
def send_reset_token(token, username, email):
    send_email(
        subject=_("Password Recovery Confirmation"),
        recipients=[email],
        text_body=render_template(
            "email/reset_password.txt",
            username=username,
            token=token
        ),
        html_body=render_template(
            "email/reset_password.html",
            username=username,
            token=token
        )
    )


@celery.task
def send_activation_token(token, username, email):
    send_email(
        subject=_("Account Activation"),
        recipients=[email],
        text_body=render_template(
            "email/activate_account.txt",
            username=username,
            token=token
        ),
        html_body=render_template(
            "email/activate_account.html",
            username=username,
            token=token
        )
    )


@celery.task
def send_async_email(*args, **kwargs):
    send_email(*args, **kwargs)


def send_email(subject, recipients, text_body, html_body, sender=None):
    msg = Message(subject, recipients=recipients, sender=sender)
    msg.body = text_body
    msg.html = html_body
    mail.send(msg)



## ... source file continues with no further render_template examples...

Ejemplo 4 de la base del matraz

base de matraz (documentación del proyecto) proporciona un código repetitivo para las nuevas aplicaciones web de Flask. El propósito de la plantilla es unir bibliotecas dispares que se usan comúnmente en proyectos de Flask, como Redis para almacenamiento en caché rápido y almacenamiento de datos transitorios.
SendGrid para correo electrónico transaccional, SQLAlchemy para almacenamiento de datos persistentes a través de un backend de base de datos relacional, Matraz-WTF para el manejo de formularios y muchos otros.

La base del matraz se proporciona como código abierto bajo el Licencia MIT.

flask-base / app / email.py

# email.py
import os

from flask import render_template
from flask_mail import Message

from app import create_app
from app import mail


def send_email(recipient, subject, template, **kwargs):
    app = create_app(os.getenv('FLASK_CONFIG') or 'default')
    with app.app_context():
        msg = Message(
            app.config['EMAIL_SUBJECT_PREFIX'] + ' ' + subject,
            sender=app.config['EMAIL_SENDER'],
            recipients=[recipient])
        msg.body = render_template(template + '.txt', **kwargs)
        msg.html = render_template(template + '.html', **kwargs)
        mail.send(msg)



## ... source file continues with no further render_template examples...

Ejemplo 5 de matraces-huesos

huesos de matraz (manifestación) es una aplicación de ejemplo de Flask a gran escala construida con Planos (ejemplo de código Blueprint). Este proyecto se proporciona como código abierto bajo la Licencia MIT.

frasco-huesos / app / en eso.py

# __init__.py
import time

from flask import Flask, g, render_template, request
import arrow
import requests

from app import config
from app.assets import assets
from app.auth import auth
from app.commands import create_db, drop_db, populate_db, recreate_db
from app.database import db
from app.extensions import lm, travis, mail, migrate, bcrypt, babel, rq, limiter
from app.user import user
from app.utils import url_for_other_page


def create_app(config=config.base_config):
    app = Flask(__name__)
    app.config.from_object(config)

    register_extensions(app)
    register_blueprints(app)
    register_errorhandlers(app)
    register_jinja_env(app)
    register_commands(app)

    def get_locale():
        return request.accept_languages.best_match(config.SUPPORTED_LOCALES)

    if babel.locale_selector_func is None:
        babel.locale_selector_func = get_locale

    @app.before_request
    def before_request():
        g.request_start_time = time.time()
        g.request_time = lambda: '%.5fs' % (time.time() - g.request_start_time)
        g.pjax = 'X-PJAX' in request.headers

    @app.route('/', methods=['GET'])
    def index():
        return render_template('index.html')

    return app


def register_commands(app):
    for command in [create_db, drop_db, populate_db, recreate_db]:
        app.cli.command()(command)


def register_extensions(app):
    travis.init_app(app)
    db.init_app(app)
    lm.init_app(app)
    mail.init_app(app)
    bcrypt.init_app(app)
    assets.init_app(app)
    babel.init_app(app)
    rq.init_app(app)
    migrate.init_app(app, db)
    limiter.init_app(app)


def register_blueprints(app):
    app.register_blueprint(user, url_prefix='/user')
    app.register_blueprint(auth)


def register_errorhandlers(app):

    def render_error(e):
        return render_template('errors/%s.html' % e.code), e.code

    for e in [
        requests.codes.INTERNAL_SERVER_ERROR,
        requests.codes.NOT_FOUND,
        requests.codes.UNAUTHORIZED,
    ]:
        app.errorhandler(e)(render_error)


def register_jinja_env(app):
    app.jinja_env.globals.update({
        'timeago': lambda x: arrow.get(x).humanize(),
        'url_for_other_page': url_for_other_page,
    })



## ... source file continues with no further render_template examples...

Ejemplo 6 de matraz-estantería

matraz-estantería es la aplicación Flask de ejemplo que los desarrolladores crean al pasar por esta serie de publicaciones de blog de Flask.

matraz-estantería / estantería / en eso.py

# __init__.py
from flask import abort, Flask, g, render_template, request, current_app
from flask_babel import Babel
from flask_security import current_user
from bookshelf.utils import get_instance_folder_path
from bookshelf.main.controllers import main
from bookshelf.admin.controllers import admin
from bookshelf.cache import cache
from bookshelf.config import configure_app
from bookshelf.data.models import db

app = Flask(
    __name__,
    instance_path=get_instance_folder_path(),
    instance_relative_config=True,
    template_folder="templates",
)

babel = Babel(app)
configure_app(app)
cache.init_app(app)
db.init_app(app)
app.jinja_env.add_extension("jinja2.ext.loopcontrols")


@app.url_defaults


## ... source file abbreviated to get to render_template examples ...




@app.before_request
def ensure_lang_support():
    lang_code = g.get("lang_code", None)
    if lang_code and lang_code not in app.config["SUPPORTED_LANGUAGES"].keys():
        abort(404)


@babel.localeselector
def get_locale():
    return g.get("lang_code", app.config["BABEL_DEFAULT_LOCALE"])


@babel.timezoneselector
def get_timezone():
    user = g.get("user", None)
    if user is not None:
        return user.timezone
    return "UTC"


@app.errorhandler(404)
def page_not_found(error):
    current_app.logger.error("Page not found: %s", (request.path, error))
    return render_template("404.htm"), 404


@app.errorhandler(500)
def internal_server_error(error):
    current_app.logger.error("Server Error: %s", (error))
    return render_template("500.htm"), 500


@app.errorhandler(Exception)
def unhandled_exception(error):
    current_app.logger.error("Unhandled Exception: %s", (error))
    return render_template("500.htm"), 500


@app.context_processor
def inject_data():
    return dict(user=current_user, lang_code=g.get("lang_code", None))


@app.route("/")
@app.route("/<lang_code>/")
@cache.cached(300)
def home(lang_code=None):
    return render_template("index.htm")


app.register_blueprint(main, url_prefix="/main")
app.register_blueprint(main, url_prefix="/<lang_code>/main")
app.register_blueprint(admin, url_prefix="/admin")
app.register_blueprint(admin, url_prefix="/<lang_code>/admin")



## ... source file continues with no further render_template examples...

Ejemplo 7 de flaskex

Flaskex es un ejemplo funcional de la aplicación web de Flask, pensada como base para construir sus propias aplicaciones. La aplicación viene con registro preconfigurado, inicio de sesión y pantallas relacionadas, así como un backend de base de datos. Flaskex se proporciona como código abierto bajo la Licencia MIT.

flaskex / app.py

# app.py

from scripts import tabledef
from scripts import forms
from scripts import helpers
from flask import Flask, redirect, url_for, render_template, request, session
import json
import sys
import os

app = Flask(__name__)
app.secret_key = os.urandom(12)  # Generic key for dev purposes only


@app.route('/', methods=['GET', 'POST'])
def login():
    if not session.get('logged_in'):
        form = forms.LoginForm(request.form)
        if request.method == 'POST':
            username = request.form['username'].lower()
            password = request.form['password']
            if form.validate():
                if helpers.credentials_valid(username, password):
                    session['logged_in'] = True
                    session['username'] = username
                    return json.dumps({'status': 'Login successful'})
                return json.dumps({'status': 'Invalid user/pass'})
            return json.dumps({'status': 'Both fields required'})
        return render_template('login.html', form=form)
    user = helpers.get_user()
    return render_template('home.html', user=user)


@app.route("/logout")
def logout():
    session['logged_in'] = False
    return redirect(url_for('login'))


@app.route('/signup', methods=['GET', 'POST'])
def signup():
    if not session.get('logged_in'):
        form = forms.LoginForm(request.form)
        if request.method == 'POST':
            username = request.form['username'].lower()
            password = helpers.hash_password(request.form['password'])
            email = request.form['email']
            if form.validate():
                if not helpers.username_taken(username):
                    helpers.add_user(username, password, email)
                    session['logged_in'] = True
                    session['username'] = username
                    return json.dumps({'status': 'Signup successful'})
                return json.dumps({'status': 'Username taken'})
            return json.dumps({'status': 'User/Pass required'})
        return render_template('login.html', form=form)
    return redirect(url_for('login'))


@app.route('/settings', methods=['GET', 'POST'])
def settings():
    if session.get('logged_in'):
        if request.method == 'POST':
            password = request.form['password']
            if password != "":
                password = helpers.hash_password(password)
            email = request.form['email']
            helpers.change_user(password=password, email=email)
            return json.dumps({'status': 'Saved'})
        user = helpers.get_user()
        return render_template('settings.html', user=user)
    return redirect(url_for('login'))


if __name__ == "__main__":
    app.run(debug=True, use_reloader=True, host="0.0.0.0")



## ... source file continues with no further render_template examples...

Ejemplo 8 de flask_jsondash

Matraz JSONDash es una aplicación web configurable integrada en Flask que crea gráficos y cuadros de mando a partir de puntos finales de API arbitrarios. Todo para la aplicación web está configurado en JSON. El código se proporciona como fuente abierta bajo la Licencia MIT.

flask_jsondash / flask_jsondash / charts_builder.py

# charts_builder.py


import json
import os
import uuid
from datetime import datetime as dt

import jinja2
from flask import (Blueprint, current_app, flash, redirect, render_template,
                   request, send_from_directory, url_for)

from flask_jsondash import static, templates

from flask_jsondash import db
from flask_jsondash import settings
from flask_jsondash.utils import setting
from flask_jsondash.utils import adapter
from flask_jsondash import utils
from flask_jsondash.schema import (
    validate_raw_json, InvalidSchemaError,
)

TEMPLATE_DIR = os.path.dirname(templates.__file__)
STATIC_DIR = os.path.dirname(static.__file__)

REQUIRED_STATIC_FAMILES = ['D3']

charts = Blueprint(
    'jsondash',
    __name__,
    template_folder=TEMPLATE_DIR,
    static_url_path=STATIC_DIR,
    static_folder=STATIC_DIR,


## ... source file abbreviated to get to render_template examples ...


            opts.update(
                filter=dict(created_by=setting('JSONDASH_GLOBAL_USER')))
            views += list(adapter.read(**opts))
    else:
        views = list(adapter.read(**opts))
    if views:
        pagination = utils.paginator(count=len(views),
                                     page=page, per_page=per_page)
        opts.update(limit=pagination.limit, skip=pagination.skip)
        views = views[pagination.skip:pagination.next]
    else:
        pagination = None
    categorized = utils.categorize_views(views)
    kwargs = dict(
        total=len(views),
        views=categorized,
        view=None,
        paginator=pagination,
        creating=True,
        can_edit_global=auth(authtype='edit_global'),
        total_modules=sum([
            len(view.get('modules', [])) for view in views
            if isinstance(view, dict)
        ]),
    )
    return render_template('pages/charts_index.html', **kwargs)


@charts.route('/charts/<c_id>', methods=['GET'])
def view(c_id):
    if not auth(authtype='view', view_id=c_id):
        flash('You do not have access to view this dashboard.', 'error')
        return redirect(url_for('jsondash.dashboard'))
    viewjson = adapter.read(c_id=c_id)
    if not viewjson:
        flash('Could not find view: {}'.format(c_id), 'error')
        return redirect(url_for('jsondash.dashboard'))
    if '_id' in viewjson:
        viewjson.pop('_id')
    if 'modules' not in viewjson:
        flash('Invalid configuration - missing modules.', 'error')
        return redirect(url_for('jsondash.dashboard'))
    active_charts = [v.get('family') for v in viewjson['modules']
                     if v.get('family') is not None]
    if metadata(key='username') == viewjson.get('created_by'):
        can_edit = True
    else:
        can_edit = auth(authtype='edit_others', view_id=c_id)
    layout_type = viewjson.get('layout', 'freeform')
    kwargs = dict(
        id=c_id,
        view=viewjson,
        categories=get_categories(),
        num_rows=(
            None if layout_type == 'freeform' else utils.get_num_rows(viewjson)
        ),
        modules=utils.sort_modules(viewjson),
        assets=get_active_assets(active_charts),
        can_edit=can_edit,
        can_edit_global=auth(authtype='edit_global'),
        is_global=utils.is_global_dashboard(viewjson),
    )
    return render_template('pages/chart_detail.html', **kwargs)


@charts.route('/charts/<c_id>/delete', methods=['POST'])
def delete(c_id):
    dash_url = url_for('jsondash.dashboard')
    if not auth(authtype='delete'):
        flash('You do not have access to delete dashboards.', 'error')
        return redirect(dash_url)
    adapter.delete(c_id)
    flash('Deleted dashboard "{}"'.format(c_id))
    return redirect(dash_url)


@charts.route('/charts/<c_id>/update', methods=['POST'])
def update(c_id):
    if not auth(authtype='update'):
        flash('You do not have access to update dashboards.', 'error')
        return redirect(url_for('jsondash.dashboard'))
    viewjson = adapter.read(c_id=c_id)
    if not viewjson:
        flash('Could not find view: {}'.format(c_id), 'error')
        return redirect(url_for('jsondash.dashboard'))
    form_data = request.form
    view_url = url_for('jsondash.view', c_id=c_id)


## ... source file continues with no further render_template examples...

Ejemplo 9 de flask-phone-input

Es una aplicación de ejemplo que une el intTellInput.js Complemento de JavaScript con el Matraz-WTF biblioteca de manejo de formularios. flask-phone-input se proporciona como código abierto bajo el Licencia MIT.

flask-phone-input / app.py

# app.py
from flask import Flask, render_template, session, redirect, url_for
from flask_bootstrap import Bootstrap
from flask_wtf import FlaskForm
import phonenumbers
from wtforms import StringField, SubmitField
from wtforms.validators import DataRequired, ValidationError

app = Flask(__name__)
app.config['SECRET_KEY'] = 'top-secret!'
Bootstrap(app)


class PhoneForm(FlaskForm):
    phone = StringField('Phone', validators=[DataRequired()])
    submit = SubmitField('Submit')

    def validate_phone(self, phone):
        try:
            p = phonenumbers.parse(phone.data)
            if not phonenumbers.is_valid_number(p):
                raise ValueError()
        except (phonenumbers.phonenumberutil.NumberParseException, ValueError):
            raise ValidationError('Invalid phone number')


@app.route('/', methods=['GET', 'POST'])
def index():
    form = PhoneForm()
    if form.validate_on_submit():
        session['phone'] = form.phone.data
        return redirect(url_for('show_phone'))
    return render_template('index.html', form=form)


@app.route('/showphone')
def show_phone():
    return render_template('show_phone.html', phone=session['phone'])



## ... source file continues with no further render_template examples...

Ejemplo 10 de Flask-Meld

Matraz-Meld (Información del paquete PyPI) le permite escribir su código web front-end en su código Python back-end. Hace esto agregando un {% meld_scripts %} etiqueta al motor de plantilla Flask y luego insertando componentes escritos en scripts de Python creados por un desarrollador.

Frasco-Meld / flask_meld / component.py

# component.py
import uuid
import os
from importlib.util import module_from_spec, spec_from_file_location

import orjson
from flask import render_template, current_app, url_for
from bs4 import BeautifulSoup
from bs4.formatter import HTMLFormatter


def convert_to_snake_case(s):
    s.replace("-", "_")
    return s


def convert_to_camel_case(s):
    s = convert_to_snake_case(s)
    return "".join(word.title() for word in s.split("_"))


def get_component_class(component_name):
    module_name = convert_to_snake_case(component_name)
    class_name = convert_to_camel_case(module_name)
    module = get_component_module(module_name)
    component_class = getattr(module, class_name)

    return component_class


def get_component_module(module_name):


## ... source file abbreviated to get to render_template examples ...


    def __context__(self):
        return {
            "attributes": self._attributes(),
            "methods": self._functions(),
        }

    def updated(self, name):
        pass

    def render(self, component_name):
        return self._view(component_name, self._attributes())

    def _view(self, component_name, data):
        context = self.__context__()
        context_variables = {}
        context_variables.update(context["attributes"])
        context_variables.update(context["methods"])
        context_variables.update(data)
        context_variables.update({"form": self._form})

        frontend_context_variables = {}
        frontend_context_variables.update(context["attributes"])
        frontend_context_variables = orjson.dumps(frontend_context_variables).decode(
            "utf-8"
        )
        rendered_template = render_template(
            f"meld/{component_name}.html", **context_variables
        )

        soup = BeautifulSoup(rendered_template, features="html.parser")
        root_element = Component._get_root_element(soup)
        root_element["meld:id"] = str(self.id)
        root_element["meld:data"] = frontend_context_variables
        self._set_values(root_element, context_variables)

        script = soup.new_tag("script", type="module")
        init = {"id": str(self.id), "name": component_name, "data": data}
        init = orjson.dumps(init).decode("utf-8")

        meld_url = url_for("static", filename="meld/meld.js")
        meld_import = f'import {{Meld}} from ".{meld_url}";'
        script.string = f"{meld_import} Meld.componentInit({init});"
        root_element.append(script)

        rendered_template = Component._desoupify(soup)

        return rendered_template

    def _set_values(self, soup, context_variables):
        for element in soup.find_all(attrs={'meld:model': True}):


## ... source file continues with no further render_template examples...

Ejemplo 11 de flask-restx

Matraz RESTX es una extensión que facilita la creación de API RESTful en sus aplicaciones. Flask RESTX tiene como objetivo una configuración mínima para que las API básicas se ejecuten para aplicaciones existentes y expone la documentación de punto final utilizando Pavonearse.

Flask RESTX se proporciona como código abierto bajo la Licencia BSD de 3 cláusulas.

flask-restx / flask_restx / apidoc.py

# apidoc.py
from __future__ import unicode_literals

from flask import url_for, Blueprint, render_template


class Apidoc(Blueprint):

    def __init__(self, *args, **kwargs):
        self.registered = False
        super(Apidoc, self).__init__(*args, **kwargs)

    def register(self, *args, **kwargs):
        super(Apidoc, self).register(*args, **kwargs)
        self.registered = True


apidoc = Apidoc(
    "restx_doc",
    __name__,
    template_folder="templates",
    static_folder="static",
    static_url_path="/swaggerui",
)


@apidoc.add_app_template_global
def swagger_static(filename):
    return url_for("restx_doc.static", filename=filename)


def ui_for(api):
    return render_template("swagger-ui.html", title=api.title, specs_url=api.specs_url)



## ... source file continues with no further render_template examples...

Ejemplo 12 de flaskSaaS

matraz Saas es un proyecto inicial estándar para crear una aplicación web de software como servicio (SaaS) en Flask, con Stripe para la facturación. El texto estándar se basa en muchas extensiones de matraces comunes, como Matraz-WTF, Frasco-Iniciar sesión, Frasco-Admin, y muchos otros. El proyecto se proporciona como código abierto bajo la Licencia MIT.

flaskSaaS / app / views / user.py

# user.py
from flask import (Blueprint, render_template, redirect, request, url_for,
                   abort, flash)
from flask.ext.login import login_user, logout_user, login_required, current_user
from itsdangerous import URLSafeTimedSerializer
from app import app, models, db
from app.forms import user as user_forms
from app.toolbox import email
import stripe
import json
from json import dumps

stripe_keys = {
    'secret_key': "sk_test_GvpPOs0XFxeP0fQiWMmk6HYe",
    'publishable_key': "pk_test_UU62FhsIB6457uPiUX6mJS5x"
}

stripe.api_key = stripe_keys['secret_key']

ts = URLSafeTimedSerializer(app.config['SECRET_KEY'])

userbp = Blueprint('userbp', __name__, url_prefix='/user')


@userbp.route('/signup', methods=['GET', 'POST'])
def signup():
    form = user_forms.SignUp()
    if form.validate_on_submit():
        user = models.User(
            first_name=form.first_name.data,
            last_name=form.last_name.data,
            phone=form.phone.data,
            email=form.email.data,
            confirmation=False,
            password=form.password.data,
        )
        db.session.add(user)
        db.session.commit()
        subject = 'Please confirm your email address.'
        token = ts.dumps(user.email, salt='email-confirm-key')
        confirmUrl = url_for('userbp.confirm', token=token, _external=True)
        html = render_template('email/confirm.html',
                               confirm_url=confirmUrl)
        email.send(user.email, subject, html)
        flash('Check your emails to confirm your email address.', 'positive')
        return redirect(url_for('index'))
    return render_template('user/signup.html', form=form, title='Sign up')


@userbp.route('/confirm/<token>', methods=['GET', 'POST'])
def confirm(token):
    try:
        email = ts.loads(token, salt='email-confirm-key', max_age=86400)
    except:
        abort(404)
    user = models.User.query.filter_by(email=email).first()
    user.confirmation = True
    db.session.commit()
    flash(
        'Your email address has been confirmed, you can sign in.', 'positive')
    return redirect(url_for('userbp.signin'))


@userbp.route('/signin', methods=['GET', 'POST'])
def signin():
    form = user_forms.Login()
    if form.validate_on_submit():
        user = models.User.query.filter_by(email=form.email.data).first()
        if user is not None:
            if user.check_password(form.password.data):
        if user.confirmation == True:
            login_user(user)            
            flash('Succesfully signed in.', 'positive')
            return redirect(url_for('userbp.account'))
        else:
                    flash('Confirm your email address first.', 'negative')
                    return redirect(url_for('userbp.signin'))
            else:
                flash('The password you have entered is wrong.', 'negative')
                return redirect(url_for('userbp.signin'))
        else:
            flash('Unknown email address.', 'negative')
            return redirect(url_for('userbp.signin'))
    return render_template('user/signin.html', form=form, title='Sign in')


@userbp.route('/signout')
def signout():
    logout_user()
    flash('Succesfully signed out.', 'positive')
    return redirect(url_for('index'))


@userbp.route('/account')
@login_required
def account():
    return render_template('user/account.html', title='Account')


@userbp.route('/forgot', methods=['GET', 'POST'])
def forgot():
    form = user_forms.Forgot()
    if form.validate_on_submit():
        user = models.User.query.filter_by(email=form.email.data).first()
        if user is not None:
            subject = 'Reset your password.'
            token = ts.dumps(user.email, salt='password-reset-key')
            resetUrl = url_for('userbp.reset', token=token, _external=True)
            html = render_template('email/reset.html', reset_url=resetUrl)
            email.send(user.email, subject, html)
            flash('Check your emails to reset your password.', 'positive')
            return redirect(url_for('index'))
        else:
            flash('Unknown email address.', 'negative')
            return redirect(url_for('userbp.forgot'))
    return render_template('user/forgot.html', form=form)


@userbp.route('/reset/<token>', methods=['GET', 'POST'])
def reset(token):
    try:
        email = ts.loads(token, salt='password-reset-key', max_age=86400)
    except:
        abort(404)
    form = user_forms.Reset()
    if form.validate_on_submit():
        user = models.User.query.filter_by(email=email).first()
        if user is not None:
            user.password = form.password.data
            db.session.commit()
            flash('Your password has been reset, you can sign in.', 'positive')
            return redirect(url_for('userbp.signin'))
        else:
            flash('Unknown email address.', 'negative')
            return redirect(url_for('userbp.forgot'))
    return render_template('user/reset.html', form=form, token=token)

@app.route('/user/pay')
@login_required
def pay():
    user = models.User.query.filter_by(email=current_user.email).first()
    if user.paid == 0:
      return render_template('user/buy.html', key=stripe_keys['publishable_key'], email=current_user.email)
    return "You already paid."

@app.route('/user/charge', methods=['POST'])
@login_required
def charge():
    amount = 500
    customer = stripe.Customer.create(email=current_user.email, source=request.form['stripeToken'])
    charge = stripe.Charge.create(
        customer=customer.id,
        amount=amount,
        currency='usd',
        description='Service Plan'
    )
    user = models.User.query.filter_by(email=current_user.email).first()
    user.paid = 1
    db.session.commit()
    return render_template('user/charge.html', amount=amount)

@app.route('/api/payFail', methods=['POST', 'GET'])
def payFail():
    content = request.json
    stripe_email = content['data']['object']['email']
    user = models.User.query.filter_by(email=stripe_email).first()
    if user is not None: 
        user.paid = 0
        db.session.commit()
    return "Response: User with associated email " + str(stripe_email) + " updated on our end (payment failure)."

@app.route('/api/paySuccess', methods=['POST', 'GET'])
def paySuccess():
    content = request.json
    stripe_email = content['data']['object']['email']
    user = models.User.query.filter_by(email=stripe_email).first()
    if user is not None: 
        user.paid = 1
        db.session.commit()
    return "Response: User with associated email " + str(stripe_email) + " updated on our end (paid)."




## ... source file continues with no further render_template examples...

Ejemplo 13 de Flask-Security-Too

(Página PyPi y documentación del proyecto) es una bifurcación mantenida del original Frasco de seguridad proyecto que facilita la adición de funciones de seguridad comunes a las aplicaciones web de Flask. Algunos de los objetivos críticos del proyecto Flask-Security-Too son garantizar que las aplicaciones de una sola página (SPA) basadas en el cliente de JavaScript puedan funcionar de forma segura con los backends basados ​​en Flask y que la orientación del OWASP La organización se sigue de forma predeterminada.

El proyecto Flask-Security-Too se proporciona como código abierto bajo la Licencia MIT.

Flask-Security-Too / flask_security / core.py

# core.py

from datetime import datetime, timedelta
import warnings

import pkg_resources
from flask import _request_ctx_stack, current_app, render_template
from flask_login import AnonymousUserMixin, LoginManager
from flask_login import UserMixin as BaseUserMixin
from flask_login import current_user
from flask_principal import Identity, Principal, RoleNeed, UserNeed, identity_loaded
from itsdangerous import URLSafeTimedSerializer
from passlib.context import CryptContext
from werkzeug.datastructures import ImmutableList
from werkzeug.local import LocalProxy

from .babel import get_i18n_domain, have_babel
from .decorators import (
    default_reauthn_handler,
    default_unauthn_handler,
    default_unauthz_handler,
)
from .forms import (
    ChangePasswordForm,
    ConfirmRegisterForm,
    ForgotPasswordForm,
    LoginForm,
    PasswordlessLoginForm,
    RegisterForm,
    ResetPasswordForm,
    SendConfirmationForm,


## ... source file abbreviated to get to render_template examples ...


                sms_service = cv("SMS_SERVICE", app=app)
                if sms_service == "Twilio":  # pragma: no cover
                    self._check_modules("twilio", "SMS")
                if state.phone_util_cls == PhoneUtil:
                    self._check_modules("phonenumbers", "SMS")

            secrets = cv("TOTP_SECRETS", app=app)
            issuer = cv("TOTP_ISSUER", app=app)
            if not secrets or not issuer:
                raise ValueError("Both TOTP_SECRETS and TOTP_ISSUER must be set")
            state.totp_factory(state.totp_cls(secrets, issuer))

        if cv("PASSWORD_COMPLEXITY_CHECKER", app=app) == "zxcvbn":
            self._check_modules("zxcvbn", "PASSWORD_COMPLEXITY_CHECKER")
        return state

    def _check_modules(self, module, config_name):  # pragma: no cover
        from importlib.util import find_spec

        module_exists = find_spec(module)
        if not module_exists:
            raise ValueError(f"{module} is required for {config_name}")

        return module_exists

    def render_template(self, *args, **kwargs):
        return render_template(*args, **kwargs)

    def render_json(self, cb):
        self._state._render_json = cb

    def want_json(self, fn):
        self._state._want_json = fn

    def unauthz_handler(self, cb):
        self._state._unauthz_handler = cb

    def unauthn_handler(self, cb):
        self._state._unauthn_handler = cb

    def reauthn_handler(self, cb):
        self._state._reauthn_handler = cb

    def __getattr__(self, name):
        return getattr(self._state, name, None)



## ... source file continues with no further render_template examples...

Ejemplo 14 de Flask-SocketIO

(Información del paquete PyPI, tutorial oficial y documentación del proyecto) es una biblioteca de códigos de Miguel Grinberg que proporciona integración Socket.IO para aplicaciones Flask. Esta extensión facilita la adición de comunicaciones bidireccionales en la web a través del protocolo WebSockets.

El proyecto Flask-SocketIO es de código abierto bajo Licencia MIT.

Flask-SocketIO / ejemplo / sesiones.py

# sessions.py
from flask import Flask, render_template, session, request, jsonify
from flask_login import LoginManager, UserMixin, current_user, login_user, 
    logout_user
from flask_session import Session
from flask_socketio import SocketIO, emit

app = Flask(__name__)
app.config['SECRET_KEY'] = 'top-secret!'
app.config['SESSION_TYPE'] = 'filesystem'
login = LoginManager(app)
Session(app)
socketio = SocketIO(app, manage_session=False)


class User(UserMixin, object):
    def __init__(self, id=None):
        self.id = id


@login.user_loader
def load_user(id):
    return User(id)


@app.route('/')
def index():
    return render_template('sessions.html')


@app.route('/session', methods=['GET', 'POST'])
def session_access():
    if request.method == 'GET':
        return jsonify({
            'session': session.get('value', ''),
            'user': current_user.id
                if current_user.is_authenticated else 'anonymous'
        })
    data = request.get_json()
    if 'session' in data:
        session['value'] = data['session']
    elif 'user' in data:
        if data['user']:
            login_user(User(data['user']))
        else:
            logout_user()
    return '', 204


@socketio.on('get-session')
def get_session():
    emit('refresh-session', {


## ... source file continues with no further render_template examples...

Ejemplo 15 de Flask-User

(Información de PyPI y documentación del proyecto) es una extensión de Flask que facilita la adición de autenticación y administración de cuentas de usuario personalizadas a los proyectos que está creando. La extensión admite el almacenamiento de datos persistentes a través de bases de datos relacionales y MongoDB. El proyecto se proporciona como código abierto bajo la Licencia MIT.

Flask-User / flask_user / email_manager.py

# email_manager.py


from flask import render_template, url_for

from flask_user import ConfigError

class EmailManager(object):

    def __init__(self, app):
        self.app = app
        self.user_manager = app.user_manager
        self.sender_name = self.user_manager.USER_EMAIL_SENDER_NAME
        self.sender_email = self.user_manager.USER_EMAIL_SENDER_EMAIL

        if not self.sender_email:
            raise ConfigError('Config setting USER_EMAIL_SENDER_EMAIL is missing.')

        if '@' not in self.sender_email:
            raise ConfigError('Config setting USER_EMAIL_SENDER_EMAIL is not a valid email address.')


    def send_confirm_email_email(self, user, user_email):

        if not self.user_manager.USER_ENABLE_EMAIL: return
        if not self.user_manager.USER_ENABLE_CONFIRM_EMAIL: return

        email = user_email.email if user_email else user.email


## ... source file abbreviated to get to render_template examples ...


            user,
            self.user_manager.USER_REGISTERED_EMAIL_TEMPLATE,
            confirm_email_link=confirm_email_link,
        )

    def send_username_changed_email(self, user):

        if not self.user_manager.USER_ENABLE_EMAIL: return
        if not self.user_manager.USER_SEND_USERNAME_CHANGED_EMAIL: return

        user_or_user_email_object = self.user_manager.db_manager.get_primary_user_email_object(user)
        email = user_or_user_email_object.email

        self._render_and_send_email(
            email,
            user,
            self.user_manager.USER_USERNAME_CHANGED_EMAIL_TEMPLATE,
        )

    def _render_and_send_email(self, email, user, template_filename, **kwargs):
        kwargs['app_name'] = self.user_manager.USER_APP_NAME
        kwargs['email'] = email
        kwargs['user'] = user
        kwargs['user_manager'] = self.user_manager

        subject = render_template(template_filename+'_subject.txt', **kwargs)
        subject = subject.replace('n', ' ')
        subject = subject.replace('r', ' ')
        html_message = render_template(template_filename+'_message.html', **kwargs)
        text_message = render_template(template_filename+'_message.txt', **kwargs)

        self.user_manager.email_adapter.send_email_message(
            email, subject, html_message, text_message,
            self.sender_email, self.sender_name)





## ... source file continues with no further render_template examples...

Ejemplo 16 de Flasky

Flasky es una aplicación de ejemplo maravillosa de Miguel Grinberg que construye mientras enseña a los desarrolladores cómo usar Flask en sus libros y videos. Flasky es de código abierto bajo la licencia MIT.

Flasky / app / email.py

# email.py
from threading import Thread
from flask import current_app, render_template
from flask_mail import Message
from . import mail


def send_async_email(app, msg):
    with app.app_context():
        mail.send(msg)


def send_email(to, subject, template, **kwargs):
    app = current_app._get_current_object()
    msg = Message(app.config['FLASKY_MAIL_SUBJECT_PREFIX'] + ' ' + subject,
                  sender=app.config['FLASKY_MAIL_SENDER'], recipients=[to])
    msg.body = render_template(template + '.txt', **kwargs)
    msg.html = render_template(template + '.html', **kwargs)
    thr = Thread(target=send_async_email, args=[app, msg])
    thr.start()
    return thr



## ... source file continues with no further render_template examples...

Ejemplo 17 de la aplicación de ejemplo Datadog Flask

los Aplicación de ejemplo Datadog Flask contiene muchos ejemplos de las funciones principales de Flask disponibles para un desarrollador que usa el marco web.

Aplicación de ejemplo de Datadog Flask / python / flask / app / app.py

# app.py
from ddtrace import patch_all; patch_all(flask=True, requests=True)  # noqa

from ddtrace import tracer

from flask import Flask, Response
from flask import after_this_request
from flask import abort, jsonify, render_template, url_for
from flask.views import View
from werkzeug.routing import Rule

from flask_caching import Cache
from flask_cors import CORS

import requests

from .blueprint import bp
from .exceptions import AppException
from .limiter import limiter
from .signals import connect_signals

app = Flask(__name__)

app.register_blueprint(bp)

connect_signals(app)

CORS(app)
Cache(app, config=dict(CACHE_TYPE='simple'))
limiter.init_app(app)




## ... source file abbreviated to get to render_template examples ...


                'Blueprint endpoint that uses <code>render_template_string()</code>',
            ],
            links=[
                dict(label='GET /bp/', url=url_for('bp.index')),
            ],
        ),
        dict(
            rule='GET /bp/unknown',
            description=[
                'Blueprint endpoint that calls <code>abort(404)</code>',
            ],
            links=[
                dict(label='GET /bp/unkown', url=url_for('bp.unknown')),
            ],
        ),
        dict(
            rule='GET /static/test.txt',
            description=[
                'Endpoint to fetch a simple .txt static file.',
            ],
            links=[
                dict(label='GET /static/test.txt', url=url_for('static', filename='test.txt')),
            ],
        ),
    ]
    return render_template('index.jinja2', routes=routes)


@app.route('/joke')
def joke():
    res = requests.get('https://icanhazdadjoke.com/', headers=dict(Accept='text/plain'))
    res.raise_for_status()

    @after_this_request
    def after_joke(response):
        print('Hook: after_this_request')
        return response

    return res.content


@app.route('/json')
def json():
    return jsonify(hello='world')


app.url_map.add(Rule('/custom-endpoint/', endpoint='custom-endpoint', defaults=dict(msg='Hello')))
app.url_map.add(Rule('/custom-endpoint/<msg>', endpoint='custom-endpoint'))

@app.endpoint('custom-endpoint')


## ... source file continues with no further render_template examples...

Ejemplo 18 de indico

indico (sitio web del proyecto, documentación y demostración de sandbox) es una aplicación web basada en Flask para la gestión de eventos. El código es de código abierto bajo Licencia MIT.

indico / indico / util / mathjax.py

# mathjax.py

from flask import current_app, render_template


class MathjaxMixin:
    def _get_head_content(self):
        return render_template('mathjax_config.html') + str(current_app.manifest['mathjax.js'])



## ... source file continues with no further render_template examples...

Ejemplo 19 de keras-flask-deploy-webapp

los
keras-flask-deploy-webapp
proyecto combina el marco web Flask con el Biblioteca de aprendizaje profundo de Keras para proporcionar un clasificador de imágenes de ejemplo que sea fácil de implementar. La aplicación se puede ejecutar rápidamente en un contenedor Docker en su entorno de desarrollo local. El proyecto tiene la licencia
Licencia pública general GNU v3.0.

keras-flask-deploy-webapp / app.py

# app.py
import os
import sys

from flask import Flask, redirect, url_for, request, render_template, Response, jsonify, redirect
from werkzeug.utils import secure_filename
from gevent.pywsgi import WSGIServer

import tensorflow as tf
from tensorflow import keras

from tensorflow.keras.applications.imagenet_utils import preprocess_input, decode_predictions
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import image

import numpy as np
from util import base64_to_pil


app = Flask(__name__)



from tensorflow.keras.applications.mobilenet_v2 import MobileNetV2
model = MobileNetV2(weights='imagenet')

print('Model loaded. Check http://127.0.0.1:5000/')


MODEL_PATH = 'models/your_model.h5'



def model_predict(img, model):
    img = img.resize((224, 224))

    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)

    x = preprocess_input(x, mode='tf')

    preds = model.predict(x)
    return preds


@app.route('/', methods=['GET'])
def index():
    return render_template('index.html')


@app.route('/predict', methods=['GET', 'POST'])
def predict():
    if request.method == 'POST':
        img = base64_to_pil(request.json)


        preds = model_predict(img, model)

        pred_proba = "{:.3f}".format(np.amax(preds))    # Max probability
        pred_class = decode_predictions(preds, top=1)   # ImageNet Decode

        result = str(pred_class[0][0][1])               # Convert to string
        result = result.replace('_', ' ').capitalize()

        return jsonify(result=result, probability=pred_proba)

    return None


if __name__ == '__main__':

    http_server = WSGIServer(('0.0.0.0', 5000), app)


## ... source file continues with no further render_template examples...

Ejemplo 20 de newspie

NewsPie es un agregador de noticias minimalista creado con Flask y el API de noticias.

NewsPie se proporciona como código abierto bajo la Licencia MIT.

newspie / news.py

# news.py
import configparser
import json
import logging
import os

import requests
import requests_cache
from dateutil import parser
from flask import (Flask, make_response, redirect, render_template, request,
                   url_for)

CONFIG = configparser.ConfigParser()
CONFIG.read('config.ini')
API_KEY = os.environ.get('NEWS_API_KEY')
TOP_HEADLINES = CONFIG['ENDPOINTS']['TOP_HEADLINES']
EVERYTHING = CONFIG['ENDPOINTS']['EVERYTHING']
PAGE_SIZE = int(CONFIG['VARIOUS']['PAGE_SIZE'])

CATEGORIES = ('general', 'sports', 'business', 'entertainment', 'health',
              'science', 'technology')
with open('data/countries.json') as json_file:
    COUNTRIES = json.load(json_file)

logging.basicConfig(level=logging.DEBUG)
requests_cache.install_cache(cache_name='news_cache',
                             backend='sqlite',
                             expire_after=300)

APP = Flask(__name__)


@APP.route('/', methods=['GET', 'POST'])
def root():


## ... source file abbreviated to get to render_template examples ...




@APP.route('/category/<string:category>', methods=['GET', 'POST'])
def category(category):
    page = request.args.get('page', default=1, type=int)
    if page < 1:
        return redirect(url_for('category', category=category, page=1))
    if request.method == 'POST' and category in CATEGORIES:
        return do_post(page, category)
    if category in CATEGORIES:
        params = {'page': page, 'category': category, 'pageSize': PAGE_SIZE}
        country = get_cookie('country')
        if country is not None:
            params.update({'country': country})
        response = requests.get(TOP_HEADLINES,
                                params=params,
                                headers={'Authorization': API_KEY})
        if response.status_code == 200:
            pages = count_pages(response.json())
            if page > pages:
                page = pages
                return redirect(url_for('category', category=category, page=page))
            articles = parse_articles(response.json())
            return render(articles, page, pages, country, category)
        elif response.status_code == 401:
            return render_template(CONFIG['VARIOUS']['401_TEMPLATE'])
    return redirect(url_for('category', category='general', page=page))


@APP.route('/search/<string:query>', methods=['GET', 'POST'])
def search(query):
    page = request.args.get('page', default=1, type=int)
    if page < 1:
        return redirect(url_for('search', query=query, page=1))
    params = {
        'qInTitle': query,
        'sortBy': 'relevancy',
        'page': page,
        'pageSize': PAGE_SIZE
    }
    if request.method == 'POST':
        return do_post(page, category='search', current_query=query)
    response = requests.get(EVERYTHING,
                            params=params,
                            headers={'Authorization': API_KEY})
    pages = count_pages(response.json())
    if page > pages:
        page = pages
        return redirect(url_for('search', query=query, page=page))
    articles = parse_articles(response.json())


## ... source file abbreviated to get to render_template examples ...


    if response.get('status') == 'ok':
        for article in response.get('articles'):
            parsed_articles.append({
                'published_at':
                    parser.isoparse(article['publishedAt']
                                   ).strftime('%Y-%m-%d %H:%M'),
                'title':
                    article['title'],
                'url':
                    article['url'],
                'source':
                    article['source']['name']
            })
    return parsed_articles


def count_pages(response):
    pages = 0
    if response.get('status') == 'ok':
        pages = (-(-response.get('totalResults', 0) // PAGE_SIZE))
    return pages


def render(articles, page, pages, country, category):
    pages = pages if pages <= 12 else 12
    return render_template(CONFIG['VARIOUS']['TEMPLATE'],
                           articles=articles,
                           categories=CATEGORIES,
                           category=category,
                           countries=COUNTRIES,
                           country=country,
                           page=page,
                           pages=pages)


def get_cookie(key):
    cookie_value = request.cookies.get(key)
    return cookie_value


if __name__ == '__main__':
    APP.run()



## ... source file continues with no further render_template examples...

Ejemplo 21 de Science Flask

Matraz de ciencia es una aplicación web impulsada por Flask para herramientas de investigación científica en línea. El proyecto se creó como una plantilla para que cualquier científico o grupo de científicos lo use cuando trabaja en conjunto sin tener que comprender realmente cómo se construye la aplicación. La aplicación incluye un proceso de registro académico (solo se pueden usar direcciones de correo electrónico académicas válidas), un panel de administración, registro y formularios de análisis.

@danielhomola es el creador principal de Science Flask y el proyecto es de código abierto bajo Licencia pública general GNU.

Science Flask / frontend / views.py

# views.py
import datetime
import json
import os
import shutil
from flask import render_template, redirect, request, g, url_for, flash, abort,
                  send_from_directory, session
from flask_login import login_required
from flask_security import current_user
from werkzeug.utils import secure_filename

from .analysis import run_analysis, terminate_analysis
from .view_functions import save_study, get_form, save_analysis, 
                           get_studies_array, get_analyses_array, 
                           get_user_folder, security_check
from backend.utils.check_uploaded_files import clear_up_study
from . import app, db, models
from .forms import UploadForm, AnalysisForm


@app.route('/')
def index():
    return render_template('index.html')

@app.route('/about')
def about():
    return render_template('about.html')

@app.route('/help')
def help():
    return render_template('help.html')

@app.route('/tc')
def tc():
    return render_template('tc.html')


@app.route('/upload', methods=['GET', 'POST'])
@login_required
def upload():

    if request.method == 'POST':
        form = UploadForm(data=get_form(request.values, request.files))
        try:
            check = request.form['check']
            if check == 'true':
                check = True
            else:
                check = False
        except:
            return json.dumps(dict(status='invalid'))

        if check:
            if form.validate_on_submit():
                return json.dumps(dict(status='OK'))
            else:
                return json.dumps(dict(status='errors', errors=form.errors))

        else:
            try:
                return save_study(form, request.files)
            except:
                user_folder = get_user_folder()
                study_folder = secure_filename(form.study_name.data)
                user_data_folder = os.path.join(user_folder, study_folder)
                clear_up_study(user_data_folder)
                return json.dumps(dict(status='invalid'))

    else:
        form = UploadForm()
        too_many_studies = 0
        if len(current_user.studies.all()) >= app.config['ACTIVE_STUDY_PER_USER']:
            too_many_studies = 1
        if current_user.num_studies >= app.config['STUDY_PER_USER']:
            too_many_studies = 2
        return render_template('upload.html', form=form,
                               too_many_studies=too_many_studies)


@app.route('/too_large_file')
@login_required
def too_large_file():
    return render_template('utils/max_file_size.html')


@app.route('/something_wrong/<page>')
@login_required
def something_wrong(page):
    return render_template('utils/something_wrong.html', page=page)


@app.route('/analysis/<int:user_id>_<int:study_id>', methods=['GET', 'POST'])
@login_required
def analysis(user_id, study_id):
    if not security_check(user_id, study_id):
        abort(403)

    if len(current_user.analyses.filter_by(status=1).all()) > 0:
        return render_template('utils/analysis_in_progress.html')

    if request.method == 'POST':
        form = AnalysisForm(data=get_form(request.values, request.files))
        try:
            check = request.form['check']
            if check == 'true':
                check = True
            else:
                check = False
        except:
            return json.dumps(dict(status='invalid'))

        if check:
            session['study_id'] = study_id
            if form.validate_on_submit():
                return json.dumps(dict(status='OK'))
            else:
                return json.dumps(dict(status='errors', errors=form.errors))

        else:
            try:
                save_analysis(form, study_id)
                task = run_analysis.apply_async(args=[current_user.id], countdown=1)
                session['task_id'] = task.id
                return json.dumps(dict(status='OK'))
            except:
                return json.dumps(dict(status='invalid'))

    else:
        form = AnalysisForm()
        too_many_analyses = 0
        if len(current_user.analyses.all()) >= app.config['ACTIVE_ANALYSIS_PER_USER']:
            too_many_analyses = 1
        if current_user.num_analyses >= app.config['ANALYSIS_PER_USER']:
            too_many_analyses = 2

        study = models.Studies.query.get(study_id)
        study_name = study.study_name
        return render_template('analysis.html', form=form, user_id=user_id,
                               study_id=study_id, study_name=study_name,
                               too_many_analyses=too_many_analyses)


@app.route('/profile')
@login_required
def profile():
    studies_array = get_studies_array()
    analyses_array = get_analyses_array()
    user_id = current_user.id
    if len(studies_array) > 0:
        study_id = studies_array[0]['id']
    else:
        study_id = 0

    if current_user.profile_intro == 0:
        profile_intro = True
        current_user.profile_intro = 1
        db.session.add(current_user)
        db.session.commit()
    else:
        profile_intro = False

    stats = {
        'active_studies': len(current_user.studies.all()),
        'all_studies': current_user.num_studies,
        'active_analyses': len(current_user.analyses.all()),
        'all_analyses': current_user.num_analyses
    }
    return render_template('profile.html', studies=studies_array, stats=stats,
                           analyses=analyses_array, profile_intro=profile_intro,
                           user_id=user_id, study_id=study_id)


@app.route('/delete_study/<int:user_id>_<int:study_id>/', methods=['POST'])
@login_required
def delete_study(user_id, study_id):
    if not security_check(user_id, study_id):
        abort(403)

    study = models.Studies.query.get(study_id)
    study_name = study.study_name
    for analysis in study.analyses.all():
        db.session.delete(analysis)
    db.session.delete(study)
    db.session.commit()

    user_folder = get_user_folder()
    study_folder = secure_filename(study_name)
    folder_to_delete = os.path.join(user_folder, study_folder)
    if os.path.exists(folder_to_delete):
        shutil.rmtree(folder_to_delete)
    return redirect(url_for('profile'))



## ... source file abbreviated to get to render_template examples ...




@app.route('/vis/<int:user_id>_<int:analysis_id>_<path:data_file>')
@login_required
def vis(user_id, analysis_id, data_file):
    if not security_check(user_id, analysis_id, True):
        abort(403)

    if data_file not in ['dataset1_2', 'dataset1', 'dataset2']:
        abort(403)

    analysis = models.Analyses.query.get(analysis_id)
    analysis_name = analysis.analysis_name
    study = models.Studies.query.get(analysis.study_id)
    study_name = study.study_name
    username = app.config['USER_PREFIX'] + str(current_user.id)
    analysis_folder = os.path.join(username, secure_filename(study_name),
                                   secure_filename(analysis_name))
    autocorr = bool(study.autocorr)
    dataset_names = [study.dataset1_type]
    if autocorr:
        dataset_names += study.dataset1_type
    else:
        dataset_names += [study.dataset2_type]

    return render_template('vis.html', analysis_folder=analysis_folder,
                           analysis_name=analysis_name, autocorr=autocorr,
                           user_id=user_id, analysis_id=analysis_id,
                           data_file=data_file,
                           dataset_names=dataset_names)


@app.errorhandler(403)
def forbidden_error(error):
    app.logger.error('403 - Forbidden request: %s', request.path)
    return render_template('utils/403.html'), 403


@app.errorhandler(404)
def not_found_error(error):
    if "/get_file/" not in request.path:
        app.logger.error('404 - Page not found: %s', request.path)
    return render_template('utils/404.html'), 404


@app.errorhandler(500)
def internal_error(error):
    db.session.rollback()
    app.logger.error('500 - Internal server error: %s', request.path)
    return render_template('utils/500.html'), 500


@app.errorhandler(500)
def all_exception_error(exception):
    db.session.rollback()
    app.logger.error('All other exception error: %s', request.path)
    return render_template('utils/500.html'), 500



@app.route('/robots.txt')
@app.route('/sitemap.xml')
@app.route('/favicon.ico')
def static_from_root():
    return send_from_directory(app.static_folder, request.path[1:])



## ... source file continues with no further render_template examples...

Ejemplo 22 de tedivms-flask

Aplicación de inicio de matraces de tedivm es una base de código de Flask y proyectos relacionados, como Celery, que proporciona una plantilla para iniciar su propia aplicación web Flask. El proyecto viene con un panel de administración, autenticación y autorización de API, SQLAlchemy y muchas otras bibliotecas comunes que se usan a menudo con Flask.

El código del proyecto se proporciona como fuente abierta bajo la Licencia «simplificada» de 2 cláusulas BSD.

tedivms-flask / app / en eso.py

# __init__.py
import boto3
from celery import Celery
from datetime import datetime
import os
import requests
import yaml

from flask import Flask, session, render_template
from flask_mail import Mail
from flask_migrate import Migrate, MigrateCommand
from flask.sessions import SessionInterface
from flask_sqlalchemy import SQLAlchemy
from flask_user import user_logged_out
from flask_wtf.csrf import CSRFProtect

from beaker.cache import CacheManager
from beaker.util import parse_cache_config_options
from beaker.middleware import SessionMiddleware

db = SQLAlchemy()
csrf_protect = CSRFProtect()
mail = Mail()
migrate = Migrate()


def get_config():
    app = Flask(__name__)

    app.config.from_object('app.settings')
    if 'APPLICATION_SETTINGS' in os.environ:
        app.config.from_envvar(os.environ['APPLICATION_SETTINGS'])
    if 'AWS_SECRETS_MANAGER_CONFIG' in os.environ:


## ... source file abbreviated to get to render_template examples ...


    session_opts['session.secret'] = app.secret_key

    class BeakerSessionInterface(SessionInterface):
        def open_session(self, app, request):
            session = request.environ['beaker.session']
            return session

        def save_session(self, app, session, response):
            session.save()

    app.wsgi_app = SessionMiddleware(app.wsgi_app, session_opts)
    app.session_interface = BeakerSessionInterface()

    @user_logged_out.connect_via(app)
    def clear_session(sender, user, **extra):
        session.clear()


def init_celery_service(app):
    celery.conf.update(app.config)


def init_error_handlers(app):

    def show_error(status, message='An unknown error has occured.'):
        return render_template('pages/errors.html', error_code=status, message=message), status

    @app.errorhandler(401)
    def error_unauthorized(e):
        return show_error(401, 'Unauthorized')

    @app.errorhandler(403)
    def error_forbidden(e):
        return show_error(403, 'Forbidden')

    @app.errorhandler(404)
    def error_pagenotfound(e):
        return show_error(404, 'Page not found.')

    @app.errorhandler(500)
    def error_servererror(e):
        return show_error(500, 'An unknown error has occurred on the server.')



## ... source file continues with no further render_template examples...

Ejemplo 23 de trape

trape es una herramienta de investigación para rastrear las actividades de las personas que se registran digitalmente. La herramienta usa Flask para crear una interfaz web para ver datos agregados sobre una persona que la aplicación está configurada para rastrear. El código fuente se proporciona como fuente abierta bajo la licencia MIT, según el LÉAME.

trape / core / stats.py

# stats.py
from core.dependence import urllib2
import sys
import os
from flask import Flask, render_template, session, request, json, redirect, url_for, send_from_directory
from flask_cors import CORS
from trape import Trape
from core.db import Database

trape = Trape(1)

if getattr(sys, 'frozen', False):
    template_folder = os.path.join(sys._MEIPASS, 'templates')
    static_folder = os.path.join(sys._MEIPASS, 'static')
    app = Flask(__name__, template_folder=template_folder, static_folder=static_folder)
else:
    app = Flask(__name__, template_folder='../templates', static_folder='../static')

cors = CORS(app)

db = Database()

trape.header()


@app.route("/" + trape.stats_path)
def index():
    return trape.injectCSS_Paths(render_template("/login.html").replace('[LOGIN_SRC]', trape.JSFiles[2]['src']).replace('[LIBS_SRC]', trape.JSFiles[1]['src']))



## ... source file abbreviated to get to render_template examples ...


    server_code = ''
    if trape.nGrokUrl != '':
        server_code = str(trape.nGrokUrl) 
    else:
        server_code = str(trape.localIp) + ':' + str(trape.app_port) 

    codeToInject = codeToInject.replace('[HOST_ADDRESS]', server_code)
    codeToInject = codeToInject.replace('[YOUR_GMAPS_API_KEY]', trape.gmaps)
    return codeToInject

@app.route("/static/js/<JSFile>")
def busted(JSFile):
    code = ''
    mPath = ''
    if getattr(sys, 'frozen', False):
        mPath = sys._MEIPASS + '/'
    for obj in trape.JSFiles:
        if str(obj['src']) == str(JSFile):
            s_code = open(mPath + "static/js/" + obj['path'],"r") 
            code = s_code.read()
            s_code.close()
            break
    if code != '':
        return code
    else:
        return render_template('404.html') 

@app.route("/styles/<CSSFile>")
def style_redirect(CSSFile):
    code = ''
    for obj in trape.CSSFiles:
        if str(obj['src']) == str(CSSFile):
            code = obj['path']
            break
    return redirect(code)

@app.route("/static/files/<File>")
def file_redirect(File):
    uploads = os.path.join(os.getcwd(), './')
    return send_from_directory(directory=uploads, filename=File)



## ... source file continues with no further render_template examples...

¿Necesitas ayuda para revisar el código de tu proyecto?



Esta entrada tiene 0 comentarios

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *

Volver arriba