Package: fb-python-sdk
FeatBit Server-Side SDK for PythonIntroductionThis is the Python Server-Side SDK for the 100% open-source feature flags management platformFeatBit.The FeatBit Server-Side SDK for Python is designed primarily for use in multi-user systems such as web servers and applications.Data synchonizationWe use websocket to make the local data synchronized with the FeatBit server, and then store them in memory by
default. Whenever there is any change to a feature flag or its related data, this change will be pushed to the SDK and
the average synchronization time is less than 100 ms. Be aware the websocket connection may be interrupted due to
internet outage, but it will be resumed automatically once the problem is gone.If you want to use your own data source, seeOffline Mode.Get StartedInstallationinstall the sdk in using pip, this version of the SDK is compatible with Python 3.6 through 3.11.pipinstallfb-python-sdkPrerequisiteBefore using the SDK, you need to obtain the environment secret and SDK URLs.Follow the documentation below to retrieve these valuesHow to get the environment secretHow to get the SDK URLsQuick StartNote that theenv_secret,streaming_urlandevent_urlare required to initialize the SDK.The following code demonstrates basic usage of the SDK.fromfbclientimportget,set_configfromfbclient.configimportConfigenv_secret='<replace-with-your-env-secret>'event_url='http://localhost:5100'streaming_url='ws://localhost:5100'set_config(Config(env_secret,event_url,streaming_url))client=get()ifclient.initialize:flag_key='<replace-with-your-flag-key>'user_key='bot-id'user_name='bot'user={'key':user_key,'name':user_name}detail=client.variation_detail(flag_key,user,default=None)print(f'flag{flag_key}returns{detail.variation}for user{user_key}, reason:{detail.reason}')# ensure that the SDK shuts down cleanly and has a chance to deliver events to FeatBit before the program exitsclient.stop()ExamplesPython DemoFBClientApplicationsSHOULD instantiate a single FBClient instancefor the lifetime of the application. In the case where an application
needs to evaluate feature flags from different environments, you may create multiple clients, but they should still be
retained for the lifetime of the application rather than created per request or per thread.BootstrappingThe bootstrapping is in fact the call of constructor ofFBClient, in which the SDK will be initialized and connect to FeatBit.The constructor will return when it successfully connects, or when the timeout(default: 15 seconds) expires, whichever comes first. If it has not succeeded in connecting when the timeout elapses, you will receive the client in an uninitialized state where feature flags will return default values; it will still continue trying to connect in the background unless there has been a network error or you close the client(usingstop()). You can detect whether initialization has succeeded by callinginitialize().The best way to use the SDK as a singleton, first make sure you have calledfbclient.set_config()at startup time. Thenfbclient.get()will return the same sharedfbclient.client.FBClientinstance each time. The client will be initialized if it runs first time.fromfbclient.configimportConfigfromfbclientimportget,set_configset_config(Config(env_secret,event_url,streaming_url))client=get()ifclient.initialize:# the client is readyYou can also manage yourfbclient.client.FBClient, the SDK will be initialized if you callfbclient.client.FBClientconstructor. With constructor, you can set the timeout for initialization, the default value is 15 seconds.fromfbclient.configimportConfigfromfbclient.clientimportFBClientclient=FBClient(Config(env_secret,event_url,streaming_url),start_wait=15)ifclient.initialize:# the client is readyIf you prefer to have the constructor return immediately, and then wait for initialization to finish at some other point, you can usefbclient.client.fbclient.update_status_providerobject, which provides an asynchronous way, as follows:fromfbclient.configimportConfigfromfbclient.clientimportFBClientclient=FBClient(Config(env_secret),start_wait=0)ifclient.update_status_provider.wait_for_OKState():# the client is readyIt's possible to set a timeout in seconds for thewait_for_OKStatemethod. If the timeout is reached, the method will returnFalseand the client will still be in an uninitialized state. If you do not specify a timeout, the method will wait indefinitely.To check if the client is ready is optional. Even if the client is not ready, you can still evaluate feature flags, but the default value will be returned if SDK is not yet initialized.FBUserA dictionary of attributes that can affect flag evaluation, usually corresponding to a user of your application.
This object contains built-in properties(key,name). Thekeyandnameare required. Thekeymust uniquely identify each user; this could be a username or email address for authenticated users, or a ID for anonymous users. Thenameis used to search your user quickly. You may also define custom properties with arbitrary names and values.
For instance, the custom key should be a string; the custom value should be a string, number or boolean valueuser={'key':user_key,'name':user_name,'age':age}EvaluationSDK calculates the value of a feature flag for a given user, and returns a flag vlaue/an object that describes the way that the value was determined.ifclient.initialize:user={'key':user_key,'name':user_name,'age':age}# evaluate the flag valueflag_value=client.variation(flag_key,user,default_value)# evaluate the flag value and get the detaildetail=client.variation_detail(flag_key,user,default=None)If you would like to get variations of all feature flags in a special environment, you can usefbclient.client.FBClient.get_all_latest_flag_variations, SDK will returnfbclient.common_types.AllFlagStates, that explain the details of all feature flags.fbclient.common_types.AllFlagStates.get()returns the detail of a given feature flag key.ifclient.initialize:user={'key':user_key,'name':user_name}all_flag_values=client.get_all_latest_flag_variations(user)# get all feature flag keyskeys=all_flag_values.keys()forflag_keyinkeys:# get viariation detaildetail=all_flag_values.get(flag_key,default=None)# get viariationvalue=all_flag_values.get_variation(flag_key,default=None)NoteIf evaluation happened before the client is initialized, or you provide the wrong flag key/user for evaluation, thevariationcalls will return the default value. Thefbclient.common_types.EvalDetailwill explain the details of the latest evaluation including error reason.Flag Trackingfbclient.client.FBClient.flag_trackerregisters a listener to be notified of feature flag changes in general.Note that a flag value change listener is bound to a specific user and flag key.The flag value change listener will be notified whenever the SDK receives any change to any feature flag's configuration,
or to a user segment that is referenced by a feature flag. To register a flag value change listener, useadd_flag_value_may_changed_listeneroradd_flag_value_changed_listenerWhen you track a flag change usingadd_flag_value_may_changed_listener, this does not necessarily mean the flag's value has changed for any particular flag, only that some part of the flag configuration was changed so that itMAYreturn a different value than it previously returned for some user.If you want to track a flag whose valueMUSTbe changed,add_flag_value_changed_listenerwill register a listener that will be notified if and only if the flag value changes.Change notices only work if the SDK is actually connecting to FeatBit feature flag center.
If the SDK is in offline mode, then it cannot know when there is a change, because flags are read on an as-needed basis.ifclient.initialize:#  flag value may be changedclient.flag_tracker.add_flag_value_may_changed_listener(flag_key,user,callback_fn)#  flag value must be changedclient.flag_tracker.add_flag_value_changed_listener(flag_key,user,callback_fn)flag_key: the key of the feature flag to trackuser: the user to evaluate the flag valuecallback_fn: the function to be called for the flag value changethe first argument is the flag keythe second argument is the latest flag valueOffline ModeIn some situations, you might want to stop making remote calls to FeatBit. Here is how:config=Config(env_secret,event_url,streaming_url,offline=True)When you put the SDK in offline mode, no insight message is sent to the server and all feature flag evaluations return
fallback values because there are no feature flags or segments available. If you want to use your own data source,
SDK allows users to populate feature flags and segments data from a JSON string. Here is an example:fbclient_test_data.json.The format of the data in flags and segments is defined by FeatBit and is subject to change. Rather than trying to construct these objects yourself, it's simpler to request existing flags directly from the FeatBit server in JSON format and use this output as the starting point for your file. Here's how:# replace http://localhost:5100 with your evaluation server urlcurl-H"Authorization: <your-env-secret>"http://localhost:5100/api/public/sdk/server/latest-all>featbit-bootstrap.jsonThen you can use this file to initialize the SDK in offline mode://firstloaddatafromfileandthenclient.initialize_from_external_json(json)Experiments (A/B/n Testing)We support automatic experiments for pageviews and clicks, you just need to set your experiment on our SaaS platform, then you should be able to see the result in near real time after the experiment is started.In case you need more control over the experiment data sent to our server, we offer a method to send custom event.client.track_metric(user,event_name,numeric_value);numeric_valueis not mandatory, the default value is1.Make suretrack_metricis called after the related feature flag is evaluated by simply callingvariationorvariation_detailotherwise, the custom event may not be included into the experiment result.Getting supportIf you have a specific question about using this sdk, we encourage you
toask it in our slack.If you encounter a bug or would like to request a
feature,submit an issue.See AlsoConnect To Python Sdk