I want to use a python data source in Re: Dash to get query results

Introduction

Re: Dash is for engineers who don't want to make screens It's very convenient to create a screen or graph just by forming a query. But do you want to get a little more data than just making queries? What's more, do you want to combine query results from different data sources? ??

Re: Dash has a "python data source" With a little setting, you will be able to mess with data with python. This was very convenient, so this time I will introduce the main functions and usage from the setting method.

Make python data source available

By default, python data sources are disabled. If you add the following settings, you can specify python from the data source.

Modify /opt/redash/.env

/opt/redash/.env


export REDASH_ADDITIONAL_QUERY_RUNNERS=redash.query_runner.python

Reflect the settings

sudo supervisorctl restart redash_server

Add new data source

From Data Sources, click + New Data Sources. Since Python can be selected for Type, select it, enter an appropriate name in Name, and save Allows you to select a Python data source when creating a query.

redash_datasource.png

redash_datasource_select.png

Prepare the data you want to go to

Prepare the data before you go to school in advance. I will omit the details in this article, but let's set the periodic execution and acquire and cache only the necessary data at the necessary timing.

This time, I will prepare for the purpose of "summing and displaying the numerical values between different data sources". Two simple query results are prepared as shown below.

Data ①

Make a note of the query ID of the created query. This time this query is query ID = 76

redash_data1.png

Data ②

Make a note of the query ID of the created query. This time this query is query ID = 77

redash_data2.png

Let's try the data

What kind of assumption aside w From query results obtained from two different data sources Let's try to add up the COUNTs with the same ID and display them.

The whole code I wrote roughly

Get the query result using API, write the process you want to do with python, and set the result with API OK

#Get the query result of the specified query ID
queryResults1 = get_query_result(76)
queryResults2 = get_query_result(77)

result = {}
for rows1 in queryResults1['rows']:
    for rows2 in queryResults2['rows']:
        if rows1['id'] == rows2['id']:
            #Add row data
            add_result_row(result, {
                'id'    :rows1['id'],
                'count' :rows1['count'] + rows2['count']
            })
            break

#Specify the column name
add_result_column(result, 'id', '', 'integer')
add_result_column(result, 'count', '', 'integer')

redash_result.png

Key API

API Contents Example
get_query_result(queryId) Get the query result of the specified query ID get_query_result(1)
add_result_row(result, values) Add row data add_result_row(result, {'id':rows['id'],'count':rows['count']})
add_result_column(result, columnName, friendlyName, columnType) Specify the column name add_result_column(result, 'id', '', 'integer')
execute_query(dataSourceName, query) Execute query on dataSourceName to get the result execute_query('testDataSource', 'select * FROM test WHERE id = 1')

For more information, please read Actual Re: dash internal source

If you want to use a module

It can be used by adding a module from the setting screen of Data Source. If you want to add more than one, add them separated by commas. After saving, you can use it just by importing it in the code.

redash_module.png

at the end

Since you can write code in python normally, you can do a little complicated processing. I think that the range of usage will expand. Above all, it's great when you don't have to make a screen. .. Please use it!

Related article

I want to upgrade Re: Dash Summary of useful tricks with Re: Dash

Recommended Posts

I want to use a python data source in Re: Dash to get query results
Try to get CloudWatch metrics with re: dash python data source
I want to create a window in Python
I want to easily implement a timeout in python
I want to write in Python! (2) Let's write a test
I want to randomly sample a file in Python
I want to work with a robot in python.
I want to use the R dataset in python
[Python] I want to get a common set between numpy
I tried "How to get a method decorated in Python"
I want to make input () a nice complement in python
I want to use a wildcard that I want to shell with Python remove
I want to print in a comprehension
I want to use jar from python
I want to build a Python environment
How to get a stacktrace in python
[Python memo] I want to get a 2-digit hexadecimal number from a decimal number
I want to convert a table converted to PDF in Python back to CSV
I want to color a part of an Excel string in Python
A memorandum because I stumbled on trying to use MeCab in Python
I want to do a monkey patch only partially safely in Python
I want to do Dunnett's test in Python
I want to use MATLAB feval with python
I want to make a game with Python
I tried to get CloudWatch data with Python
I want to merge nested dicts in Python
I want to use Temporary Directory with Python2
I want to get League of Legends data ③
I want to get League of Legends data ②
I want to use ceres solver from python
I want to get League of Legends data ①
I want to write to a file with Python
I want to display the progress in Python!
Use os.getenv to get environment variables in Python
I want to use Python in the environment of pyenv + pipenv on Windows 10
I want to create a priority queue that can be updated in Python (2.7)
Use PIL in Python to extract only the data you want from Exif
Python program is slow! I want to speed up! In such a case ...
I want to get the file name, line number, and function name in Python 3.4
I want to write in Python! (1) Code format check
I want to iterate a Python generator many times
I want to give a group_id to a pandas data frame
I want to transition with a button in flask
I want to use self in Backpropagation (tf.custom_gradient) (tensorflow)
Even in JavaScript, I want to see Python `range ()`!
I tried to implement a pseudo pachislot in Python
[Python] I want to make a nested list a tuple
I want to write in Python! (3) Utilize the mock
I tried to summarize how to use pandas in python
I want to run a quantum computer with Python
I want to do something in Python when I finish
I want to manipulate strings in Kotlin like Python!
[Python] I want to add a static directory with Flask [I want to use something other than static]
[Python] I want to use only index when looping a list with a for statement
I want to get custom data attributes of html as elements using Python Selenium
I want to use a network defined by myself in PPO2 of Stable Baselines
I want to write a triple loop and conditional branch in one line in python
I want to be able to analyze data with Python (Part 3)
I tried to implement a one-dimensional cellular automaton in Python
I want to do something like sort uniq in Python
I want to start a lot of processes from python