{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Birdy WPSClient example with Emu WPS" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from birdy import WPSClient" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Use Emu WPS\n", "https://github.com/bird-house/emu" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "emu = WPSClient(url=\"http://localhost:5000/wps\")\n", "emu_i = WPSClient(url=\"http://localhost:5000/wps\", progress=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Get Infos about `hello`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "emu.hello?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Run `hello`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "emu.hello(name=\"Birdy\").get()[0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Run a long running process" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "result = emu_i.sleep(delay=\"1.0\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "result.get()[0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Run a process returning a reference to a text document" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "emu.chomsky(times=\"5\").get()[0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Pass a local file to a remote process\n", "\n", "The client can look up local files on this machine and embed their content in the WPS request to the server. Just set the path to the file or an opened file-like object. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fn = \"/tmp/text.txt\"\n", "with open(fn, \"w\") as f:\n", " f.write(\"Just an example\")\n", "emu.wordcounter(text=fn).get(asobj=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Automatically convert the output to a Python object\n", "\n", "The client is able to convert input objects into strings to create requests, and also convert output strings into python objects. This can be demonstrated with the `inout` process, which simply takes a variety of `LiteralInputs` of different data types and directs them to the output without any change. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "emu.inout?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import datetime as dt\n", "\n", "result = emu.inout(\n", " string=\"test\",\n", " int=1,\n", " float=5.6,\n", " boolean=True,\n", " time=\"15:45\",\n", " datetime=dt.datetime(2018, 12, 12),\n", " text=None,\n", " dataset=None,\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Get result as object" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "result.get(asobj=True).text" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Example with multiple_outputs" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Similarly, the `multiple_outputs` function returns a `text/plain` file. The converter will automatically convert the text file into a string." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "out = emu.multiple_outputs(1).get(asobj=True)[0]\n", "print(out)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "... or use the metalink library on the referenced metalink file:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "out = emu.multiple_outputs(1).get(asobj=False)[0]\n", "print(out)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from metalink import download\n", "\n", "download.get(out, path=\"/tmp\", segmented=False)" ] } ], "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": 4 }