ノーコードでクラウド上のデータとの連携を実現。
詳細はこちら →Square Connector の30日間無償トライアルをダウンロード
30日間の無償トライアルへ製品の詳細
Square Python Connector 相談したいSquare へのデータ連携用のPython Connecotr ライブラリ。 pandas、SQLAlchemy、Dash、petl などの主要なPython ツールにSquare をシームレスに統合。
CData
こんにちは!ウェブ担当の加藤です。マーケ関連のデータ分析や整備もやっています。
Python エコシステムには、多くのモジュールがあり、システム構築を素早く効率的に行うことができます。CData Python Connector for Square を使うことで、pandas モジュールとDash フレームワークでSquare にデータ連携するアプリケーションを効率的に開発することができます。本記事では、pandas、Dash とCData Connector を使って、Square に連携して、Square データ をビジュアライズするシンプルなウェブアプリを作る方法をご紹介します。
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.square as mod import plotly.graph_objs as go
接続文字列を使ってデータへの接続を確立します。connect 関数を使ってCData Square Connector からSquare データ との接続を確立します。
cnxn = mod.connect("OAuthClientId=MyAppId;OAuthClientSecret=MyAppSecret;CallbackURL=http://localhost:33333;LocationId=MyDefaultLocation;InitiateOAuth=GETANDREFRESH;OAuthSettingsLocation=/PATH/TO/OAuthSettings.txt")")
Square OAuth 認証標準を使用します。OAuth を使用して認証するには、Square にアプリを登録してOAuthClientId、OAuthClientSecret、CallbackURL を入手します。OAuth の使用方法については、ヘルプドキュメントの「はじめに」セクションをご覧ください。
追加でLocationId を指定する必要がある場合があります。Locations テーブルをクエリすることでLocations のId を取得できます。または、クエリの検索項目にLocationId を設定することもできます。
read_sql 関数を使って、padas からSQL 文を発行し、DataFrame に結果を格納します。
df = pd.read_sql("""SELECT Reason, RefundedMoneyAmount FROM Refunds WHERE Type = 'FULL'""", cnxn)
DataFrame に格納されたクエリ結果を使って、ウェブアプリにname、stylesheet、title を設定していきます。
app_name = 'dash-squareedataplot' external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css'] app = dash.Dash(__name__, external_stylesheets=external_stylesheets) app.title = 'CData + Dash'
次に、Square データ をベースにした棒グラフを作詞し、アプリのレイアウトを設定します。
trace = go.Bar(x=df.Reason, y=df.RefundedMoneyAmount, name='Reason') 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='Square Refunds Data', barmode='stack') }) ], className="container")
接続、アプリ、レイアウトを定義したら、アプリを実行してみましょう。以下のコードで実行できます。
if __name__ == '__main__': app.run_server(debug=True)
最後に、Python でウェブアプリを起動してブラウザでSquare データ を見てみましょう。
python square-dash.py
ちゃんとデータが表示できてますね!
Square Python Connector の30日の無償トライアル をぜひダウンロードして、Square データ への接続をPython アプリやスクリプトから簡単に作成してみてください。
import os import dash import dash_core_components as dcc import dash_html_components as html import pandas as pd import cdata.square as mod import plotly.graph_objs as go cnxn = mod.connect("OAuthClientId=MyAppId;OAuthClientSecret=MyAppSecret;CallbackURL=http://localhost:33333;LocationId=MyDefaultLocation;InitiateOAuth=GETANDREFRESH;OAuthSettingsLocation=/PATH/TO/OAuthSettings.txt") df = pd.read_sql("SELECT Reason, RefundedMoneyAmount FROM Refunds WHERE Type = 'FULL'", cnxn) app_name = 'dash-squaredataplot' 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.Reason, y=df.RefundedMoneyAmount, name='Reason') 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='Square Refunds Data', barmode='stack') }) ], className="container") if __name__ == '__main__': app.run_server(debug=True)