Package: metabase-query-api
Metabase Query APIThis package will help Data workers get data fromMetabasequestions more easily and effectively. It only focuses on theCard Query API,Dataset APIand does not include other Metabase APIs.FeaturesGet question data in any data format provided by Metabase (JSON, CSV, XLSX).Input question URL and Metabase Session. No need to provide parameters payload.JSON results have the same column sort order as the browser.Automatically check if Metabase session is available.Allow retry if an error occurs due to server slowdown.Allows entering multiple param values in bulk, suitable for retrieving data for a large number of ids, usingasynciotechnique.Support both saved questions (card) and unsaved questions (dataset).Installationpip install metabase-query-apiInstructionImport packagefrommetabase_query_api.sync_queryimportexport_questionfrommetabase_query_api.async_queryimportexport_question_bulk_filter_valuesimportasyncioGet question dataCopy the question URL in the browser. Note that you must fill in the necessary parameters before copying.Use a different API to get theMetabase Session. Or you can use thisChrome extensionto get it.Special parameters:retry_attemptsdefaults to0, use it when your Metabase server is often slow.data_formatdefaults to'json', accepted values are'json','csv','xlsx'.custom_retry_errorsdefaults to[], use it to force retry with errors on you server. There is no need to fill in the full name of the error because the condition is string contains.Export question data to a JSON variablesession='c65f769b-eb4a-4a12-b0be-9596294919fa'# Saved question URLurl='https://your-domain.com/question/123456-example?your_param_slug=SomeThing'# Unsaved question URLurl='https://your-domain.com/question#eW91cl9xdWVyeQ=='json_data=export_question(url=url,session=session,retry_attempts=5)Export question data to an Excel filexlsx_data=export_question(url=url,session=session,data_format='xlsx',retry_attempts=5)withopen('file.xlsx','wb')asfile:file.write(xlsx_data)Export question data to a CSV filecsv_data=export_question(url=url,session=session,data_format='csv',retry_attempts=5)withopen('file.csv','wb')asfile:file.write(csv_data)Get question data with bulk param valuesThis function is suitable for retrieving data with a large number of values that need to be filled in a param, usually an id field.It will split your list of values into multiple parts, each containing up to 2000 values.It then sends multiple asynchronous requests to get the data. Once completed, the data pieces will be merged into one.⚠️ Note:Using this function may slow down your Metabase server.Special parameters:bulk_filter_slug: Saved question -> parameter slug in URL, unsaved question -> Field Name as field_name.bulk_values_listis a list of values.chunk_sizedefault, and the maximum is2000. If your data has duplicates for each filter value, reduce the chunk size. Because each piece of data only contains 2000 lines.retry_attemptsdefaults to10, use it when your Metabase server is often slow.custom_retry_errorsdefaults to[], use it to force retry with errors on you server. There is no need to fill in the full name of the error because the condition is string contains.session='c65f769b-eb4a-4a12-b0be-9596294919fa'# Saved question URLurl='https://your-domain.com/question/123456-example?your_param_slug=SomeThing'# Unsaved question URLurl='https://your-domain.com/question#eW91cl9xdWVyeQ=='bulk_filter_slug='order_id'bulk_values_list=['12345','...','98765']json_data=asyncio.run(export_question_bulk_filter_values(url=url,session=session,bulk_filter_slug=bulk_filter_slug,bulk_values_list=bulk_values_list,chunk_size=2000,retry_attempts=10))# Save to CSV/Excel fileimportpandasaspddf=pd.DataFrame(json_data)df.to_csv('file.csv',index=False)df.to_excel('file.xlsx',index=False)