ノーコードでクラウド上のデータとの連携を実現。
詳細はこちら →CData
こんにちは!ウェブ担当の加藤です。マーケ関連のデータ分析や整備もやっています。
Python エコシステムには、多くのモジュールがあり、システム構築を素早く効率的に行うことができます。CData Python Connector for SurveyMonkey を使うことで、pandas モジュールとDash フレームワークでSurveyMonkey にデータ連携するアプリケーションを効率的に開発することができます。本記事では、pandas、Dash とCData Connector を使って、SurveyMonkey に連携して、SurveyMonkey データ をビジュアライズするシンプルなウェブアプリを作る方法をご紹介します。
CData Python Connectors は、以下のような特徴を持った製品です。
まずは、pip で必要なモジュールおよびフレームワークをインストールします:
pip install pandas pip install dash pip install dash-daq
必要なモジュールとフレームワークがインストールされたら、ウェブアプリを開発していきます。コードのスニペットは以下の通りです。フルコードは記事の末尾に掲載しているので、参考にしてください。
まず、CData Connector を含むモジュールをインポートします:
import os import dash import dash_core_components as dcc import dash_html_components as html import pandas as pd import cdata.surveymonkey as mod import plotly.graph_objs as go
接続文字列を使ってデータへの接続を確立します。connect 関数を使ってCData SurveyMonkey Connector からSurveyMonkey データ との接続を確立します。
cnxn = mod.connect("OAuthClientId=MyOAuthClientId;OAuthClientSecret=MyOAuthClientSecret;CallbackURL=http://localhost:portNumber;InitiateOAuth=GETANDREFRESH;OAuthSettingsLocation=/PATH/TO/OAuthSettings.txt")")
SurveyMonkey はOAuth 2 認証標準を利用しています。SurveyMonkey がアンケートの回答を読むためにこれを必要とすることを考えると、アンケートを読むアカウントには有料プランのサブスクリプションが必要です。
SurveyMonkey への接続に使用できる認証方法は2つあります。
個人用トークンを使用して、自分のデータをテストし、アクセスします。個人用トークンを取得するには、ヘルプの「Creating a Custom OAuth App」の手順に従って、次の接続プロパティを設定します。
CData 製品はすでにSurveyMonkey にOAuth アプリケーションとして登録されています。そのため、デフォルトでは、自動的に埋め込みクレデンシャルを使用して接続します。
独自のカスタムOAuth アプリを使用したい場合は、ヘルプのCustom Credentials を参照してください。
read_sql 関数を使って、padas からSQL 文を発行し、DataFrame に結果を格納します。
df = pd.read_sql("""SELECT RespondentId, ChoiceId FROM MySurvey_Responses WHERE ChoiceText = 'blue'""", cnxn)
DataFrame に格納されたクエリ結果を使って、ウェブアプリにname、stylesheet、title を設定していきます。
app_name = 'dash-surveymonkeyedataplot' external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css'] app = dash.Dash(__name__, external_stylesheets=external_stylesheets) app.title = 'CData + Dash'
次に、SurveyMonkey データ をベースにした棒グラフを作詞し、アプリのレイアウトを設定します。
trace = go.Bar(x=df.RespondentId, y=df.ChoiceId, name='RespondentId') app.layout = html.Div(children=[html.H1("CData Extention + Dash", style={'textAlign': 'center'}), dcc.Graph( id='example-graph', figure={ 'data': [trace], 'layout': go.Layout(alt='SurveyMonkey MySurvey_Responses Data', barmode='stack') }) ], className="container")
接続、アプリ、レイアウトを定義したら、アプリを実行してみましょう。以下のコードで実行できます。
if __name__ == '__main__': app.run_server(debug=True)
最後に、Python でウェブアプリを起動してブラウザでSurveyMonkey データ を見てみましょう。
python surveymonkey-dash.py
ちゃんとデータが表示できてますね!
SurveyMonkey Python Connector の30日の無償トライアル をぜひダウンロードして、SurveyMonkey データ への接続をPython アプリやスクリプトから簡単に作成してみてください。
import os import dash import dash_core_components as dcc import dash_html_components as html import pandas as pd import cdata.surveymonkey as mod import plotly.graph_objs as go cnxn = mod.connect("OAuthClientId=MyOAuthClientId;OAuthClientSecret=MyOAuthClientSecret;CallbackURL=http://localhost:portNumber;InitiateOAuth=GETANDREFRESH;OAuthSettingsLocation=/PATH/TO/OAuthSettings.txt") df = pd.read_sql("SELECT RespondentId, ChoiceId FROM MySurvey_Responses WHERE ChoiceText = 'blue'", cnxn) app_name = 'dash-surveymonkeydataplot' external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css'] app = dash.Dash(__name__, external_stylesheets=external_stylesheets) app.title = 'CData + Dash' trace = go.Bar(x=df.RespondentId, y=df.ChoiceId, name='RespondentId') app.layout = html.Div(children=[html.H1("CData Extention + Dash", style={'textAlign': 'center'}), dcc.Graph( id='example-graph', figure={ 'data': [trace], 'layout': go.Layout(alt='SurveyMonkey MySurvey_Responses Data', barmode='stack') }) ], className="container") if __name__ == '__main__': app.run_server(debug=True)