Skip to main content

a light and simple Flask extension for integrating google captcha with Flask Apps

Project description

a light and simple Flask extension for integrating google recaptcha with Flask Apps

0.0 how to install:

pip install -U flask_captcha2

0.1 how to use:

from flask import Flask
from flask_captcha2.GoogleCaptcha import FlaskCaptcha2, FlaskCaptcha3

# `FlaskCaptcha3` is for Google Captcha version 3
# `FlaskCaptcha2` is for Google Captcha version 2

app = Flask(__name__)

# Captcha version 2 Configuration (I'm not a robot)
RECAPTCHA_PRIVATE_KEY = "Put Your private<secret> key here"
RECAPTCHA_PUBLIC_KEY = "Put your public<site> key here"
RECAPTCHA_TABINDEX = "Tab index for Captcha Widget"
RECAPTCHA_LANGUAGE = "Captcha Language <default en>"
RECAPTCHA_SIZE = "Captcha Widget Size default normal <compact،, normal, invisible>"
RECAPTCHA_TYPE = "Captcha type default image"
RECAPTCHA_THEME = "Captcha theme default light <dark, light>"
RECAPTCHA_ENABLED = "Captcha status default True <True, False>"
RECAPTCHA_LOG = "Show captcha requests in stdout <True, False>"

# Captcha version 3 Configuration (invisible captcha)
RECAPTCHA_PRIVATE_KEY = "Put Your private<secret> key here"
RECAPTCHA_PUBLIC_KEY = "Put your public<site> key here"
RECAPTCHA_ENABLED = "Captcha status default True <True, False>"
RECAPTCHA_LOG = "Show captcha requests in stdout <True, False>"
RECAPTCHA_SCORE = "Score for captcha <Float, between 0.5 to 1>"

# Create a captcha instance
captcha2 = FlaskCaptcha2(app=app)
captcha3 = FlaskCaptcha3(app=app)

# or
captcha2 = FlaskCaptcha2()
captcha3 = FlaskCaptcha3()

captcha2.init_app(app=app)
captcha3.init_app(app=app)

0.2 how use in templates for rendering Captcha Widget:

Use < captchaField > Filter to render captcha in html

Version 2 Captcha rendering:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Captcha version 2</title>
</head>
<body>

    <form method="POST">
        <input type="text" name="username">
        <input type="submit" value="submit">
        {# With captchaField filter you can render captcha widget in your html code #}
        {{ captchaField }}
    </form>
</body>
</html>

Version 3 Captcha rendering:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Captcha version 3</title>
</head>
<body>

    <form method="POST" id="ParentForm">
        <input type="text" name="username">
        <input type="submit" value="submit">
        {# With captchaField filter you can render captcha widget in your html code #}
        {{
            captchaField
            ( {
                 'btnText': "Submit", # required
                 'ParentFormID': 'ParentForm', # required
            } )
        }}

<!--
            full arguments in captcha version 3
            captchaField(
            {
                'btnText': "submit btn text", # required
                'ParentFormID': 'put prent form id here', # required
                'id':'if you want to set id for btn set id in here', # optional
                'style': 'css style', # optional
                'dataset': optional for giving dataset attribute to submit btn
                'hidden-badge':True or False, this value can hide or show captcha badge
            })
-->

    </form>
</body>
</html>

0.3 How verify Captcha:

Use is_verify method

captcha = FlaskCaptcha2(app)
captcha = FlaskCaptcha3(app)

@app.route("/", methods=["POST"])
def index():
    # with is_verify method verify the captcha
    if captcha.is_verify():
        return "Captcha is ok."
    else:
        return "Try again!"

Version History:

  • version 2.0.0 Released: May 18, 2023

  • Changes:

    None
  • version 2.0.1 Released: June 9, 2023

  • Changes:

  • version 3.0.0 Released: September 9, 2023

  • Changes:

  • version 3.0.2 Released: October 27, 2023

  • Changes:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Flask-captcha2-3.0.3.tar.gz (6.7 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page