Skip to content

Instantly share code, notes, and snippets.

@bollwyvl
Last active December 14, 2021 02:41
Show Gist options
  • Save bollwyvl/f62914339b09581f780b17d755cf25bf to your computer and use it in GitHub Desktop.
Save bollwyvl/f62914339b09581f780b17d755cf25bf to your computer and use it in GitHub Desktop.
IDatasette

IDatasette

Binder

TODO:

  • binder
name: jupyter-datasette
channels:
- conda-forge/label/jupyterlab_rc
- conda-forge/label/jupyterlab_server_rc
- conda-forge
- nodefaults
dependencies:
- doit
- ipywidgets
- jupyterlab >=3.0.0rc5
- jupyterlab_server >=2.0.0rc1
- nodejs >=14,<15
- pip
- python >=3.8,<3.9
- jupyter-server-proxy
- importnb
- geopandas
# datasette
- aiofiles >=0.4,<0.6
- asgiref >=3.2.10,<3.3
- click >=7.1.1,<7.2
- httpx >=0.15
- itsdangerous >=1.1,<2
- janus >=0.4,<0.6
- jinja2 >=2.10.3,<2.12.0
- pint >=0.9,<1
- pluggy >=0.13.0,<1
- python >=3.6
- pyyaml >=5.3,<6
- uvicorn >=0.11,<1
# datasette-graphql
- graphene >=2.0
- wrapt
- pip:
- jupyterlab-widgets >=1.0.0a5
# datasette
- datasette >=0.51a0
- libspatialite
# datasette plugins and dependencies
- datasette-cluster-map
- datasette-graphql
- datasette-jellyfish
- datasette-json-html
- datasette-leaflet-geojson
- datasette-pretty-json
- datasette-render-images
- datasette-saved-queries
- datasette-sqlite-fts4
- datasette-vega
- markdown-to-sqlite
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Running Datasette in IPython"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os, datasette.app, datasette.cli, uvicorn, asyncio, IPython, sqlite3\n",
"from pathlib import Path\n",
"from jupyter_server.serverapp import ServerApp, web, ioloop\n",
"from notebook.base.handlers import IPythonHandler\n",
"from traitlets.config import LoggingConfigurable \n",
"import socket\n",
"import ipywidgets"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def get_unused_port():\n",
" \"\"\" Get an unused port by trying to listen to any random port.\n",
" Probably could introduce race conditions if inside a tight loop.\n",
" \"\"\"\n",
" sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n",
" sock.bind((\"localhost\", 0))\n",
" sock.listen(1)\n",
" port = sock.getsockname()[1]\n",
" sock.close()\n",
" return port"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class IDatasette(datasette.app.Datasette):\n",
" pass"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def make_a_datasette_server(files, app_args=None, prefix=None, port=None, start=True, proxy=False):\n",
" app_args = app_args or {}\n",
" uvi_args = dict(port=port or get_unused_port())\n",
" \n",
" if prefix is None:\n",
" prefix = os.environ.get(\"JUPYTERHUB_SERVICE_PREFIX\")\n",
" if prefix and proxy:\n",
" prefix += f\"proxy/{port}/\"\n",
" \n",
" if prefix is None:\n",
" prefix = \"/\"\n",
" if proxy:\n",
" prefix += f\"proxy/{port}/\"\n",
" \n",
" app_args.update(config=dict(base_url=prefix))\n",
" app = IDatasette(files=files, **app_args)\n",
" config = uvicorn.Config(app.app(), **uvi_args)\n",
" server = uvicorn.Server(config=config)\n",
" task = None\n",
" if start:\n",
" task = asyncio.ensure_future(server.serve())\n",
" return app, server, task"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"From [the python docs](https://docs.python.org/3/library/sqlite3.html)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def make_a_stocks_database():\n",
" stocks = Path(\"stocks.sqlite\")\n",
" stocks.exists() and stocks.unlink()\n",
" conn = sqlite3.connect(stocks)\n",
" c = conn.cursor()\n",
" c.execute(\"\"\"\n",
" CREATE TABLE stocks\n",
" (date text, trans text, symbol text, qty real, price real)\n",
" \"\"\")\n",
" c.execute(\"INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14)\")\n",
" conn.commit()\n",
" conn.close()\n",
" return stocks"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def make_a_datasette_control_panel(app, server):\n",
" start = ipywidgets.Button(icon=\"play\", description=\"Start\", button_style=\"primary\")\n",
" start.on_click(lambda _: ioloop.IOLoop.current().add_callback(server.serve))\n",
" shutdown = ipywidgets.Button(icon=\"trash\", description=\"Shutdown\", button_style=\"danger\")\n",
" shutdown.on_click(lambda _: ioloop.IOLoop.current().add_callback(server.shutdown))\n",
" return ipywidgets.HBox([\n",
" start,\n",
" shutdown,\n",
" ipywidgets.HTML(f\"\"\"\n",
" <a href=\"{app.config(\"base_url\")}\">Datasette</a>\n",
" \"\"\")\n",
" ])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"if __name__ == \"__main__\":\n",
" stocks = make_a_stocks_database()\n",
" app, server, _ = make_a_datasette_server([stocks], port=8000, proxy=True, start=False)\n",
" ui = make_a_datasette_control_panel(app, server)\n",
" IPython.display.display(ui)"
]
}
],
"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.8.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment