Feature flags allow you to configure your app without writing additional code in your codebase. With dynamic configurations, you can easily specify the experience desired for each segment of users without ever deploying new code. Feature flags are the foundation for progressive delivery, making it easier to test in production, release faster, and deploy code more safely. In this step-by-step tutorial, you will build a
Python web app with Flask and then implement feature flags. For this example, you will build a menu application that will store the two menus for my restaurant. You will need to install the Python extension for this project. From Visual Studio, click on the extensions tab and search for
Python. Look for the extension that looks like this: Then, from your terminal, install python3. You should also be using a virtual environment to download all of the Flask extensions you need. In your root folder, enter the following. Next, we
will select our Python interpreter. In VSCode, from the command palette, search for Python: Select Interpreter. Select the path that starts with Now, from the Command Palette, search for Terminal: Create New Integrated Terminal. This
will automatically activate your virtual environment. Now, install Flask. You will also need a free developer account from Split, so be sure to sign up if you don’t have one yet! Create Your Flask AppIn your root directory, create a file named
Next, in the
In your terminal, run Now that we got the app up and running, let’s add some functionality and styles to it. Update your
In your root directory, create a new folder called templates, and in that folder, create a new file called I am going to call my restaurant The Queen’s Gambit, so let’s add that in a new Header.
Now, we need to build our menus and add them to the home page. In the templates folder, add a new HTML file called Let’s add those routes to our
Now we can add the corresponding links to our index.html file.
When you run Clicking on each link will take you to the corresponding menu. Now let’s add feature flags. Create a Feature Flag with SplitThe first thing we need to do is create a feature flag from the Split UI. Once you log in, click on Create Split. Next, Click on Add Rules. Here, you will define the different treatments of your feature flag. I am going to define treatment on and treatment off. If the treatment is on for the current user, they will have access to the menu app. If the treatment is off, they will not have access. Next, we need to target the specific users who should have access to the app, at least initially, me. For everyone else who is not in that list, they will get the default treatment of off because the default rule is off. Instantiate and Use the Python SDKIn your root folder, import the Python SDK using pip.
Then, in your
Now we will instantiate the SDK and create a new
Split client (still in
To find your API key, go to the Syntax tab of your split, and then select Python as the language. In our
Now, in our index function, we can add this template.
Here, we are telling Split that I am the current user, and treatment should be on for me. Let’s run the app and see what happens. I can see the app! Now, let’s change the targeted user to someone who should not have access (i.e., anyone else).
The test user does not see our app because they are not targeted. For the full repo and more examples with feature flags, check out our examples repo. Improve Your Flask Apps with Feature FlagsNow that you have feature flags implemented, the possibilities are endless! You can test your code in production, make sure it works, and then turn on the flag for your end-users. You can also release your app through a canary release, or percentage rollout, where only a small percentage of your user base has access to the app to mitigate risk in case something goes wrong. With feature flags, you have full control over your app, and Split makes it easy to implement! Build More Apps with Feature FlagsFeature flags are foundational to engineering. We at Split love building apps in all different languages and frameworks!
To stay up to date with all of our content, be sure to follow us on Twitter and subscribe to our Youtube Channel! |