Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for How to use Feature Flags with Flask
Chavez Harris
Chavez Harris

Posted on • Originally published atconfigcat.com

How to use Feature Flags with Flask

How to use Feature Flags with Flask

Feature flags benefit our continuous integration and continuous deployment (CI/CD) workflows. As developers, we can quickly roll out and test new features without re-deploying or changing the application's code. Due to its simplicity, feature flagging can be incorporated into both new and existing applications. Let me walk you through how to use feature flags in a Flask application to help you better understand this concept.

How do feature flags work?

Feature flags are also called feature switches or feature toggles. They are boolean values that can be added to conditional statements in our code to control what features are rendered. In brief, feature flags are used to control which features users are allowed to view or interact with.

Flask integration

Following our discussion about feature flags, how can we put them to good use in a Flask application?
This will be accomplished by installing and configuring our Flask application to useConfigCat's feature flag services.

ConfigCat allows us to manage feature flags from a central dashboard. Additionally, users can be segmented and targeted based on their characteristics. This will allow you to release features only to specific segments. For instance, we might enable certain features or a set of features for users who are between 30 and 40 years of age.

Sample application

Let's develop a newsletter subscription feature for a fictional magazine website using Flask. We'll control the visibility of the subscription form using a feature flag. When the feature flag is off, an error message will be displayed instead. Now let's put this all together.

You can find the code for this sample apphere.

Visit theConfigCat labs organization on GitHub for other code samples for a variety of languages, frameworks, and topics.

Prerequisites

  • A code editor (eg. Visual Studio Code)
  • virtualenv - a tool for creating a virtual environment for Python
  • Python 3.7 or higher
  • Basic Python and Flask knowledge

Creating the Flask website

Launch your favorite code editor and a terminal window in the root of the application's folder. If youdo not have an existing app, we'll create one together. The first thing you need to do is create anew folder on your computer. Name the folder whatever you like, for example, flask_app.

You'll need to create a Python virtual environment. This creates an isolated space for the Flask app and prevents packages that it depends on from conflicting with those installed at the system level. To create a new virtual environment, run the following command:

virtualenv venv
Enter fullscreen modeExit fullscreen mode

Create a new file calledrequirements.txt in the app root folder with content fromhere.

Activate the virtual environment and install the Python packages from therequirements.txt file into the virtual environment with these commands:

sourcevenv/bin/activate
Enter fullscreen modeExit fullscreen mode
pipinstall-r requirements.txt
Enter fullscreen modeExit fullscreen mode

Create a folder calledstatic, then create a file inside calledstyles.css withthis content, or feel free to use whatever styling you like.

Create a folder calledtemplates in the root folder of the app. Add the following files to the folder.

  • base.html - This is the base HTML template for the Flask app. Other files can extend this file.

  • home.html - This template extends the base template and appears as the home or index page.

  • not-available.html - This extends the base template, and it would only be shown if the feature flag is turned off. It indicates to the
    user that the feature is not available.

  • subscribe.html - This also extends the base template and appears as the feature page. It will only be shown if the feature flag is turned on.

Creating a feature flag

Have you noticed that I didn't create anapp.py file just yet? I'll get to this after creating a feature flag.

To continue, you'll need to sign up for a free account onConfigCat if you don't have one.

Navigate to the dashboard and create a feature flag with the following data:

FieldValue
namecanShowSubscriptionPage
keycanshowsubscriptionpage

Snapshot of creating a feature flag

Click the switcher on the right to enable this feature:

Snapshot of turning on the feature flag

Now that the feature flag is created and switched on, let's integrate the feature flag with the Flask website.

Integrating with ConfigCat

It is strongly recommended using the ConfigCat Client as aSingleton object in production.

Activate your Python virtual environment with:

sourcevenv/bin/activate
Enter fullscreen modeExit fullscreen mode

Install the ConfigCat client Python SDK by running:

pipinstallconfigcat-client
Enter fullscreen modeExit fullscreen mode

In the root folder, create anapp.py file with the following contents. I've added comments in the code to explain what some logic does.

# ...# Imports the ConfigCat client SDK Python package we've installedimportconfigcatclient# Initialize the client using your SDK key:configcat_client=configcatclient.create_client('YOUR_CONFIGCAT_SDK_KEY')
Enter fullscreen modeExit fullscreen mode
# Create a variable to store the state of the feature flag from ConfigCat.# This variable will be automatically updated every 60 seconds by default.isCanShowSubscriptionPageEnabled=configcat_client.get_value('canshowsubscriptionpage',False)
Enter fullscreen modeExit fullscreen mode
# ...# When the user navigates to the /subscribe route@app.route('/subscribe')defsubscribe():ifisCanShowSubscriptionPageEnabled:# If the feature flag is on, show the subscribe pagereturnrender_template('subscribe.html')else:# If the feature flag is off, show the not-available pagereturnrender_template('not-available.html')
Enter fullscreen modeExit fullscreen mode

Let's see if it works

Head over to the ConfigCat dashboard and turn the feature flag off:

Snapshot of turning off the feature flag

Restart the flask app. You should now see this alert:

Snapshot of the app - feature flag turned off

Keep in mind that you don't have to restart the app's server when you toggle a feature flag on or off. The app would pull the updated feature flag value automatically every 60 seconds by default and just reloading the page would be enough to see the changes. ConfigCat also gives you the ability to increase or decrease the polling interval period. You can read more about thathere.

Conclusion

As you can see, integrating feature flags into your apps is simple, and it doesn't take much effort to start using them. Feature flagging is ideal for development workflows with steady feature rollouts.

ConfigCat's documentation is well explained, making it super simple for you or your team to get up and running fast. Other frameworks and languages are also supported. Check out the full list of supported SDKshere.

Stay tuned to ConfigCat for more awesome content onTwitter,Facebook,LinkedIn, andGitHub.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Inspiration does exist, but it must find you writing code.
  • Location
    Georgetown, Guyana
  • Work
    Software Engineer
  • Joined

More fromChavez Harris

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp