title | description |
---|---|
Python SDK and Flask |
Getting Started with the OpenFeature Python SDK on Flask |
import FlagdContent from '@site/src/components/custom/tutorial/flagd-content.mdx'; import FlagdChangeContent from '@site/src/components/custom/tutorial/flagd-change-content.mdx'; import WhyDefaultContent from '@site/src/components/custom/tutorial/why-default-content.mdx';
This walk-through teaches you the basics of using OpenFeature with Python. You'll learn how to:
- Install the Python SDK
- Install and configure a provider
- Perform basic feature flagging
This walk-through assumes that:
- You have a basic understanding of Python and Flask.
- You have Flask 3.x and Python 3.10 or later.
- You have Docker installed and running on the host system. The latest version of Docker can be found here.
NOTE: If you don't have docker installed, check the available options to install Flagd here.
To get started, create a new folder, bootstrap the project, and install the dependencies. This can be done by running the following commands.
mkdir openfeature-python-intro
cd openfeature-python-intro
pip install Flask
Create a new file named app.py
inside openfeature-python-intro directory and include the following code.
from flask import Flask
app = Flask(__name__)
@app.route("/")
def index():
return "Flask Server"
Let's install the OpenFeature SDK using the following commands.
pip install typing-extensions
pip install openfeature-sdk
pip install openfeature-provider-flagd
Update app.py
to import the SDK.
from openfeature import api
from openfeature.contrib.provider.flagd import FlagdProvider
Once you've imported OpenFeature
, a new client can be created using the FlagdProvider
.
api.set_provider(FlagdProvider())
client = api.get_client()
The client can now be used to get a feature flag value.
In this case, we'll get a boolean
value using the welcome-message
flag key
and fallback value, which is returned if there's abnormal behavior using the client.get_boolean_value()
method.
@app.route("/")
def index():
show_welcome_message = client.get_boolean_value("welcome-message", False)
if show_welcome_message:
return "Flask + OpenFeature Server"
return "Flask Server"
Let's start the app and see it in action, use the final code below.
from flask import Flask
from openfeature import api
from openfeature.contrib.provider.flagd import FlagdProvider
app = Flask(__name__)
api.set_provider(FlagdProvider())
client = api.get_client()
@app.route("/")
def index():
show_welcome_message = client.get_boolean_value("welcome-message", False)
if show_welcome_message:
return "Flask + OpenFeature Server"
return "Flask Server"
Run the following command to start the server.
flask run
Open your favorite browser and navigate to http://127.0.0.1:5000. If all goes as planned, you should see "Flask Server".
NOTE: You should stop the app by using the keyboard short
ctrl + c
before moving on to the next step.
Flagd can be run as a standalone binary or Kubernetes Operator as well. If you don't have docker installed, get and install the Flagd binary. With the flagd configuration in place, start flagd service with the following command.
flagd start -f file:flags.flagd.json
Now that everything is in place, let's start the app again.
flask run
Open your browser and navigate to http://127.0.0.1:5000 should show the same value as before. This difference is now the feature flag value can be changed at runtime!
Save the changes to flags.flagd.json
and refresh the browser tab.
You should now be greeted with Flask + OpenFeature Server
.
This walk-through introduced you to the OpenFeature Python SDK. It covered how a provider can be configured to perform the flag evaluation and introduced basic feature flagging concepts. It also showcased how feature flags can be updated at runtime, without requiring a redeployment.