Package: composapy
ComposapyDataLabs comes pre-installed with Composapy, a python package that makes it easy to
programmatically interface with Composable. Composapy allows you to interactively author, execute
and retrieve results from DataFlows (Directed Acyclic Graphs, DAGs).Visit ourReadTheDocspage a more detailed look at
Composapy.SessionRegister a SessionDataFlowCreate and Save a DataFlowRun a DataFlowDataFlowObject and DataFlowRun ModulesDataFlowObject and DataFlowRun InputDataFlowRun ResultKeyRetrieve KeyRegister KeySearch for KeyQueryViewCreate and Connect to a QueryView DriverRun a QuerySQL Magic CommandsRun a Saved QueryViewAdditional InformationSessionReadTheDocs - SessionRegister a SessionComposapy looks for the environment variableAPPLICATION_URIby default (set by DataLabs). If you are using Composapy outside of the datalabs environment and theAPPLICATION_URIenvironment variable is not set, you can set it with keyword argumenturi. You can create a session with Windows Authentication (if you are in DataLab, this will be the same as the
key on the DataLab edit screen), [string] API Token (can be generated on the composable website), or with a [tuple[str, str]] username/password combination.fromcomposapy.sessionimportSessionfromcomposapy.authimportAuthModesession=Session(auth_mode=AuthMode.WINDOWS)session=Session(auth_mode=AuthMode.TOKEN,credentials="<your-api-token-here>",uri="http://localhost/CompAnalytics/")session=Session(auth_mode=AuthMode.FORM,credentials=("username","password"))After creating a session, register it (future operations use this session bydefault). Optionally, enable thesaveflag to write to a local config file for automatical loading/registration on next Composapy import.session.register()session.register(save=True)You can also callget_sessionto get the currently registered session.fromcomposapy.sessionimportget_sessionsession=get_session()DataFlowReadTheDocs - DataFlowCreate and Save a DataFlowDataFlowObject'scan be initialized with theDataFlow APImethod -create. It takes either a json string (kwargjson) or path to a json file (kwargfile_path) as parameters. Call thesavemethod on an unsavedDataFlowObjectto save it in the Composable database. Saving it will make give it anid, making it available for use in theComposable Designer.fromcomposapy.dataflow.apiimportDataFlowdataflow_object=DataFlow.create(file_path="simple-dataflow.json")# DataFlowObject(id=None)dataflow_object=DataFlow.create(file_path="simple-dataflow.json").save()# DataFlowObject(id=123456)Run a DataFlowTo run a saved DataFlow, you can retrieve theappIdin the DataFlow's URL.dataflow_run=DataFlow.run(444333)# DataFlowRun(id=444333)To run a DataFlow that has external input modules, use theexternal_inputskwarg, which is a dictionary with key equal to the external modules name field and the value equal to what you want to pass in.dataflow_run=DataFlow.run(444333,external_inputs={"a_string_external_input":"foo string"})# DataFlowRun(id=444333)DataFlowObject and DataFlowRun Modulesdataflow_run.modules[0]# Module(name='Calculator', type=Calculator)DataFlowObject and DataFlowRun Inputdataflow_object.modules.filter(name="calc module name")[0].inputs.first()# Input(name=Param1, type=Double, value=1.0)DataFlowRun Resultdataflow_run.modules.get(name="string module name").result# Result(name='foo name', type=String, value='foo value')KeyReadTheDocs - KeyRetrieve KeyA Composapy Key can be retrieved using the python Key API.fromcomposapy.key.apiimportKeykey_object=Key.get(123456)# KeyObject(name='some name', type='StringConnectionSettings')Optionally, if your key name is unique, you can retrieve it by name.key_object=Key.get(name="a unique name")# KeyObject(name='a unique name', type='SqlConnectionSettings')Register KeySimilar to Session, KeyObjects can be registered with thesave=Trueflag. This will save the key id in the working directory's "composapy.ini" file, which will then be loaded when the Composapy package is imported.key_object.register(save=True)You can retrieve the currently registered key_object withget_key_object.fromcomposapy.key.modelsimportget_key_objectget_key_object()# KeyObject(name='a unique name', type='SqlConnectionSettings')Search for KeyKeys can be searched by name. It returns a list of key objects.key_objects=Key.search("same name")# [KeyObject(name='same name', type='SqlConnectionSettings'), KeyObject(name='same name', type='StringConnectionSettings')]QueryViewReadTheDocs - QueryViewCreate and Connect to a QueryView Driverfromcomposapy.queryview.apiimportQueryViewdriver=QueryView.driver()# you can create a driver and then connect with a key...driver.connect(key_object)driver=QueryView.driver(key_object)# ... or create a driver using the key as an argumentIf there is a currently registered key, there is no need to pass the key to the driver object; the driver will be automatically initialized with the database connection.QueryView.driver()Run a Querydf=driver.run("select * from table_name")# returns a Pandas DataFrame of your querySQL Magic CommandsIn a notebook (IPython) environment, custom sql magic commands are loaded on import with Composapy. Note that both%sqland%%sqlrequire a registered Session and KeyObject. Both can be loaded on import if saved in the "composapy.ini" config file. Seeregister-sessionandregister-key.%sqlselect*fromsome_table--returnsthesameresultasQueryView.driver("select * from some_table")Instead of doing line magic (only takes that line as input), you can alternatively do cell magic (takes entire cell as input).select*fromsome_tablewheresome_idisnotnull%%sqlcell magic also supports options to customize the query execution, such as setting a timeout value or capturing the output of the query into a Python variable. Run %%sql? for detailed documentation.Run a Saved QueryViewTo run a saved QueryView, you can retrieve the ID in the QueryView's URL and invoke the staticrunmethod without needing to create a driver object.fromcomposapy.queryview.apiimportQueryViewQueryView.run(123456)# returns a Pandas DataFrame with the query resultsIf the QueryView has literal or filter inputs, you can optionally pass values and operators to therunmethod using theinputsargument. This argument must be a dictionary whose keys are the display name of the input. There are two possible formats for the dictionary values, depending on the kind of input:[LITERAL, FILTER] A single Python primitive (int,bool,str,float). The default operator will be used for filter inputs in this case.[FILTER] A tuple of size two in the form(val, operator)wherevalis the primitive input value andoperatoris one of the acceptable QueryView input operators (=, !=, >, <, >=, <=, LIKE)Note that multi-choice inputs are not currently supported. This feature will be added in a subsequent version of Composapy.QueryView.run(123456,inputs={"displayName1":"val1","displayName2":(100,"!=")})Additional InformationAlso, if you want to interface withpythonnetmore closely, you can find our csharp documentationhere.