How to use the quart.flash function in Quart

To help you get started, we’ve selected a few Quart examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github synesthesiam / voice2json / mqtt / app_noprofile.py View on Github external
async def index():
    if request.method == "POST":
        form = await request.form
        for key in form:
            if key.startswith("download-"):
                profile_name = key.split("-", maxsplit=1)[1]
                await flash(f"Downloading {profile_name}", "info")
                break
            elif key.startswith("install-"):
                profile_name = key.split("-", maxsplit=1)[1]
                await install_profile(profile_name)
                await flash(f"Installed {profile_name}", "success")
                break

    return await render_template(
        "index_noprofile.html",
        profile_path=profile_path,
        download_dir=download_dir,
        profiles=PROFILES,
        sorted=sorted,
        range=range,
    )
github pgjones / quart / examples / blog / blog.py View on Github external
async def create():
    if not session.get('logged_in'):
        abort(401)
    db = get_db()
    form = await request.form
    db.execute(
        "INSERT INTO post (title, text) VALUES (?, ?)",
        [form['title'], form['text']],
    )
    db.commit()
    await flash('New entry was successfully posted')
    return redirect(url_for('posts'))
github synesthesiam / voice2json / mqtt / app_noprofile.py View on Github external
async def index():
    if request.method == "POST":
        form = await request.form
        for key in form:
            if key.startswith("download-"):
                profile_name = key.split("-", maxsplit=1)[1]
                await flash(f"Downloading {profile_name}", "info")
                break
            elif key.startswith("install-"):
                profile_name = key.split("-", maxsplit=1)[1]
                await install_profile(profile_name)
                await flash(f"Installed {profile_name}", "success")
                break

    return await render_template(
        "index_noprofile.html",
        profile_path=profile_path,
        download_dir=download_dir,
        profiles=PROFILES,
        sorted=sorted,
        range=range,
    )
github synesthesiam / voice2json / web / app.py View on Github external
is_speak = "speak" in form
            if len(word) > 0:
                if is_speak:
                    # Speak pronunciation
                    result = voice2json("pronounce-word", "--nbest", "1", word)
                else:
                    # Get multiple guesses
                    result = voice2json(
                        "pronounce-word", "--quiet", "--nbest", "3", word
                    )

                # Display guess(s)
                for line in result:
                    await flash(line.strip(), "info")
            else:
                await flash("No word given", "danger")

    # Load custom words
    custom_words_text = custom_words_path.read_text()

    return await render_template(
        "words.html",
        profile=profile,
        pydash=pydash,
        custom_words=custom_words_text,
        word=word,
    )
github synesthesiam / voice2json / web / app.py View on Github external
warn_lines = None
    for line in result.splitlines():
        line = line.strip()
        if line.startswith("-") or line.startswith("."):
            continue

        if "unknown" in line:
            warn_lines = []
            line = line + ":"

        if warn_lines is not None:
            warn_lines.append(line)

    if warn_lines is not None:
        await flash("\n".join(warn_lines), "warning")
github synesthesiam / voice2json / web / app.py View on Github external
wav_file.setnchannels(1)
                        wav_file.writeframesraw(raw_audio_data)

                    wav_data = wav_buffer.getvalue()

                # Clean up
                del record_file
                record_file = None
        elif "upload" in form:
            files = await request.files
            if "wavfile" in files:
                # Get WAV data from file upload
                wav_file = files["wavfile"]
                wav_data = wav_file.read()
            else:
                await flash("No WAV file given", "danger")
        elif "recognize" in form:
            # Get sentence to recognize from form
            sentence = form["sentence"]
            if len(sentence) == 0:
                await flash("No sentence to recognize", "danger")

            transcribe_result = {
                "text": sentence.strip()
            }

        # ---------------------------------------------------------------------

        if wav_data is not None:
            # Transcribe WAV
            logger.debug("Transcribing %s byte(s)", len(wav_data))
            transcribe_result = json.load(
github pgjones / quart / examples / blog / blog.py View on Github external
async def login():
    error = None
    if request.method == 'POST':
        form = await request.form
        if form['username'] != app.config['USERNAME']:
            error = 'Invalid username'
        elif form['password'] != app.config['PASSWORD']:
            error = 'Invalid password'
        else:
            session['logged_in'] = True
            await flash('You were logged in')
            return redirect(url_for('posts'))
    return await render_template('login.html', error=error)
github pgjones / quart / examples / blog / blog.py View on Github external
async def logout():
    session.pop('logged_in', None)
    await flash('You were logged out')
    return redirect(url_for('posts'))
github synesthesiam / voice2json / web / app.py View on Github external
async def do_retrain():
    """Re-trains voice2json profile and flashes warnings for unknown words."""
    # Re-train
    start_time = time.time()
    result = voice2json("train-profile", stderr=subprocess.STDOUT).read()
    train_seconds = time.time() - start_time
    await flash(f"Re-trained in {train_seconds:0.2f} second(s)", "success")

    logger.debug(result)

    warn_lines = None
    for line in result.splitlines():
        line = line.strip()
        if line.startswith("-") or line.startswith("."):
            continue

        if "unknown" in line:
            warn_lines = []
            line = line + ":"

        if warn_lines is not None:
            warn_lines.append(line)
github synesthesiam / voice2json / mqtt / app.py View on Github external
async def do_retrain(do_flash=True) -> str:
    """Re-trains voice2json profile and flashes warnings for unknown words."""
    start_time = time.time()
    client.publish(TOPIC_TRAIN, "{}")

    # Wait for result
    success, result = await mqtt_train_queue.get()
    train_seconds = time.time() - start_time

    if do_flash:
        if success:
            await flash(f"Re-trained in {train_seconds:0.2f} second(s)", "success")
        else:
            await flash(f"Training failed: {result}", "danger")

    logger.debug(result)

    warn_lines = None
    for line in result.splitlines():
        line = line.strip()
        if line.startswith("-") or line.startswith("."):
            continue

        if "unknown" in line:
            warn_lines = [line]
            line = line + ":"
            break