{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# GraphQL\n", "\n", "If you want to try out this notebook with a live Python kernel, use mybinder:\n", "\n", "\"https://mybinder.org/badge_logo.svg\"\n", "\n", "vaex-graphql is a plugin package that exposes a DataFrame via a GraphQL interface. This allows easy sharing of data or aggregations/statistics or machine learning models to frontends or other programs with a standard query languages.\n", "\n", "(Install with `$ pip install vaex-graphql`, no conda-forge support yet)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
# pclass survived name sex age sibsp parch ticket fare cabin embarked boat body home_dest
0 1 True Allen, Miss. Elisabeth Walton female29.0 0 0 24160 211.3375B5 S 2 nan St Louis, MO
1 1 True Allison, Master. Hudson Trevor male 0.91671 2 113781 151.55 C22 C26S 11 nan Montreal, PQ / Chesterville, ON
2 1 False Allison, Miss. Helen Loraine female2.0 1 2 113781 151.55 C22 C26S None nan Montreal, PQ / Chesterville, ON
3 1 False Allison, Mr. Hudson Joshua Creighton male 30.0 1 2 113781 151.55 C22 C26S None 135.0 Montreal, PQ / Chesterville, ON
4 1 False Allison, Mrs. Hudson J C (Bessie Waldo Daniels)female25.0 1 2 113781 151.55 C22 C26S None nan Montreal, PQ / Chesterville, ON
... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
1,3043 False Zabour, Miss. Hileni female14.5 1 0 2665 14.4542 None C None 328.0 None
1,3053 False Zabour, Miss. Thamine femalenan 1 0 2665 14.4542 None C None nan None
1,3063 False Zakarian, Mr. Mapriededer male 26.5 0 0 2656 7.225 None C None 304.0 None
1,3073 False Zakarian, Mr. Ortin male 27.0 0 0 2670 7.225 None C None nan None
1,3083 False Zimmerman, Mr. Leo male 29.0 0 0 315082 7.875 None S None nan None
" ], "text/plain": [ "# pclass survived name sex age sibsp parch ticket fare cabin embarked boat body home_dest\n", "0 1 True Allen, Miss. Elisabeth Walton female 29.0 0 0 24160 211.3375 B5 S 2 nan St Louis, MO\n", "1 1 True Allison, Master. Hudson Trevor male 0.9167 1 2 113781 151.55 C22 C26 S 11 nan Montreal, PQ / Chesterville, ON\n", "2 1 False Allison, Miss. Helen Loraine female 2.0 1 2 113781 151.55 C22 C26 S None nan Montreal, PQ / Chesterville, ON\n", "3 1 False Allison, Mr. Hudson Joshua Creighton male 30.0 1 2 113781 151.55 C22 C26 S None 135.0 Montreal, PQ / Chesterville, ON\n", "4 1 False Allison, Mrs. Hudson J C (Bessie Waldo Daniels) female 25.0 1 2 113781 151.55 C22 C26 S None nan Montreal, PQ / Chesterville, ON\n", "... ... ... ... ... ... ... ... ... ... ... ... ... ... ...\n", "1,304 3 False Zabour, Miss. Hileni female 14.5 1 0 2665 14.4542 None C None 328.0 None\n", "1,305 3 False Zabour, Miss. Thamine female nan 1 0 2665 14.4542 None C None nan None\n", "1,306 3 False Zakarian, Mr. Mapriededer male 26.5 0 0 2656 7.225 None C None 304.0 None\n", "1,307 3 False Zakarian, Mr. Ortin male 27.0 0 0 2670 7.225 None C None nan None\n", "1,308 3 False Zimmerman, Mr. Leo male 29.0 0 0 315082 7.875 None S None nan None" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import vaex\n", "df = vaex.datasets.titanic()\n", "df" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "OrderedDict([('df',\n", " OrderedDict([('min',\n", " OrderedDict([('age', 0.1667), ('fare', 0.0)])),\n", " ('mean',\n", " OrderedDict([('age', 29.8811345124283),\n", " ('fare', 33.29547928134572)])),\n", " ('max',\n", " OrderedDict([('age', 80.0), ('fare', 512.3292)])),\n", " ('groupby',\n", " OrderedDict([('sex',\n", " OrderedDict([('count', [466, 843]),\n", " ('mean',\n", " OrderedDict([('age',\n", " [28.6870706185567,\n", " 30.585232978723408])]))]))]))]))])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "result = df.graphql.execute(\"\"\"\n", " {\n", " df {\n", " min {\n", " age\n", " fare\n", " }\n", " mean {\n", " age\n", " fare\n", " }\n", " max {\n", " age\n", " fare\n", " }\n", " groupby {\n", " sex {\n", " count\n", " mean {\n", " age\n", " }\n", " }\n", " }\n", " }\n", " }\n", " \"\"\")\n", "result.data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Pandas support\n", "After importing vaex.graphql, vaex also installs a pandas accessor, so it is also accessible for Pandas DataFrames." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "df_pandas = df.to_pandas_df()" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "OrderedDict([('df',\n", " OrderedDict([('row',\n", " [OrderedDict([('name', 'Anderson, Mr. Harry'),\n", " ('survived', True)]),\n", " OrderedDict([('name',\n", " 'Andrews, Miss. Kornelia Theodosia'),\n", " ('survived', True)])])]))])" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df_pandas.graphql.execute(\"\"\"\n", " {\n", " df(where: {age: {_gt: 20}}) {\n", " row(offset: 3, limit: 2) {\n", " name\n", " survived\n", " }\n", " }\n", " }\n", " \"\"\"\n", ").data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Server\n", "\n", "\n", "The easiest way to learn to use the GraphQL language/vaex interface is to launch a server, and play with the GraphiQL graphical interface, its autocomplete, and the schema explorer.\n", "\n", "We try to stay close to the Hasura API: https://docs.hasura.io/1.0/graphql/manual/api-reference/graphql-api/query.html\n", "\n", "\n", "A server can be started from the command line:\n", " \n", " `$ python -m vaex.graphql myfile.hdf5`\n", " \n", "Or from within Python using [df.graphql.serve](../api.rst#vaex.graphql.DataFrameAccessorGraphQL.serve)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## GraphiQL\n", "See https://github.com/mariobuikhuizen/ipygraphql for a graphical widget, or a [mybinder to try out a live example](https://mybinder.org/v2/gh/mariobuikhuizen/ipygraphql/master?filepath=example.ipynb).\n", "![image](https://user-images.githubusercontent.com/1765949/66774282-8b057400-eec1-11e9-97ac-5b40f37bd30f.gif)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3" } }, "nbformat": 4, "nbformat_minor": 2 }