Skip to content

Commit 402bb78

Browse files
Jose Bovet Derpichrenovate[bot]beeme1mr
authored
chore: add Python SDK tutorial with Flask integration (#560)
Signed-off-by: Jose Bovet Derpich <[email protected]> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Michael Beemer <[email protected]>
1 parent 80ec6e2 commit 402bb78

File tree

1 file changed

+166
-0
lines changed

1 file changed

+166
-0
lines changed
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
---
2+
title: Python SDK and Flask
3+
description: Getting Started with the OpenFeature Python SDK on Flask
4+
---
5+
6+
import FlagdContent from '@site/src/components/custom/tutorial/flagd-content.mdx';
7+
import FlagdChangeContent from '@site/src/components/custom/tutorial/flagd-change-content.mdx';
8+
import WhyDefaultContent from '@site/src/components/custom/tutorial/why-default-content.mdx';
9+
10+
11+
# Getting Started with the OpenFeature Python SDK
12+
13+
## Introduction
14+
15+
This walk-through teaches you the basics of using OpenFeature with Python.
16+
You'll learn how to:
17+
18+
- Install the Python SDK
19+
- Install and configure a provider
20+
- Perform basic feature flagging
21+
22+
## Requirements
23+
24+
This walk-through assumes that:
25+
26+
- You have a basic understanding of Python and Flask.
27+
- You have Flask 3.x and Python 3.10 or later.
28+
- You have Docker installed and running on the host system.
29+
The latest version of Docker can be found [here][docker-download].
30+
31+
> **NOTE:** If you don't have docker installed, check the available options to install Flagd [here](https://flagd.dev/installation/).
32+
33+
## Walk-through
34+
35+
### Step 1: Create a minimal flask application
36+
37+
To get started, create a new folder, bootstrap the project, and install the dependencies.
38+
This can be done by running the following commands.
39+
40+
```sh
41+
mkdir openfeature-python-intro
42+
cd openfeature-python-intro
43+
pip install Flask
44+
```
45+
46+
### Step 2: Create a Flask app
47+
48+
Create a new file named `app.py` inside openfeature-python-intro directory and include the following code.
49+
50+
```python
51+
from flask import Flask
52+
app = Flask(__name__)
53+
54+
@app.route("/")
55+
def index():
56+
return "Flask Server"
57+
```
58+
59+
### Step 3: Add the OpenFeature SDK
60+
61+
Let's install the OpenFeature SDK using the following commands.
62+
63+
```sh
64+
pip install typing-extensions
65+
pip install openfeature-provider-flagd
66+
```
67+
68+
Update `app.py` to import the SDK.
69+
```python
70+
from openfeature import api
71+
from openfeature.contrib.provider.flagd import FlagdProvider
72+
```
73+
74+
Once you've imported `OpenFeature`, a new client can be created using the `FlagdProvider`.
75+
76+
```python
77+
api.set_provider(FlagdProvider())
78+
client = api.get_client()
79+
```
80+
81+
The client can now be used to get a feature flag value.
82+
In this case, we'll get a `boolean` value using the `welcome-message` [flag key][flag-key]
83+
and fallback value, which is returned if there's abnormal behavior using the `client.get_boolean_value()` method.
84+
85+
```python
86+
@app.route("/")
87+
def index():
88+
show_welcome_message = client.get_boolean_value("welcome-message", False)
89+
if show_welcome_message:
90+
return "Flask + OpenFeature Server"
91+
92+
return "Flask Server"
93+
```
94+
95+
### Step 4: Run the application
96+
Let's start the app and see it in action, use the final code below.
97+
98+
```python
99+
from flask import Flask
100+
from openfeature import api
101+
from openfeature.contrib.provider.flagd import FlagdProvider
102+
103+
app = Flask(__name__)
104+
api.set_provider(FlagdProvider())
105+
client = api.get_client()
106+
107+
108+
@app.route("/")
109+
def index():
110+
show_welcome_message = client.get_boolean_value("welcome-message", False)
111+
if show_welcome_message:
112+
return "Flask + OpenFeature Server"
113+
114+
return "Flask Server"
115+
```
116+
Run the following command to start the server.
117+
118+
```sh
119+
flask run
120+
```
121+
122+
Open your favorite browser and navigate to [ http://127.0.0.1:5000]( http://127.0.0.1:5000).
123+
If all goes as planned, you should see "Flask Server".
124+
125+
<WhyDefaultContent />
126+
127+
> NOTE: You should stop the app by using the keyboard short `ctrl + c` before moving on to the next step.
128+
129+
### Step 5: Configure a provider (flagd)
130+
131+
<FlagdContent />
132+
133+
Flagd can be run as a [standalone binary](https://flagd.dev/reference/flagd-cli/flagd/) or [Kubernetes Operator](https://openfeature.dev/docs/tutorials/ofo/)
134+
as well. If you don't have docker installed, get and install the [Flagd binary](https://github.com/open-feature/flagd/releases).
135+
With the flagd configuration in place, start flagd service with the following command.
136+
137+
```sh
138+
flagd start -f file:flags.flagd.json
139+
```
140+
141+
### Step 6: Rerun the application
142+
143+
Now that everything is in place, let's start the app again.
144+
145+
146+
```sh
147+
flask run
148+
```
149+
150+
Open your browser and navigate to [ http://127.0.0.1:5000]( http://127.0.0.1:5000) should show the same value as before.
151+
This difference is now the feature flag value can be changed at runtime!
152+
153+
<FlagdChangeContent />
154+
155+
Save the changes to `flags.flagd.json` and refresh the browser tab.
156+
You should now be greeted with `Flask + OpenFeature Server`.
157+
158+
## Conclusion
159+
160+
This walk-through introduced you to the OpenFeature Python SDK.
161+
It covered how a provider can be configured to perform the flag evaluation and introduced basic feature flagging concepts.
162+
It also showcased how feature flags can be updated at runtime, without requiring a redeployment.
163+
164+
[learn-flask]: https://flask.palletsprojects.com/en/3.0.x/
165+
[docker-download]: https://docs.docker.com/engine/install/
166+
[flag-key]: /specification/glossary#flag-key

0 commit comments

Comments
 (0)