{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Embedding WebAgg\n\nThis example demonstrates how to embed Matplotlib WebAgg interactive plotting\nin your own web application and framework. It is not necessary to do all this\nif you merely want to display a plot in a browser or use Matplotlib's built-in\nTornado-based server \"on the side\".\n\nThe framework being used must support web sockets.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import argparse\nimport io\nimport json\nimport mimetypes\nfrom pathlib import Path\nimport signal\nimport socket\n\ntry:\n import tornado\nexcept ImportError as err:\n raise RuntimeError(\"This example requires tornado.\") from err\nimport tornado.httpserver\nimport tornado.ioloop\nimport tornado.web\nimport tornado.websocket\n\nimport numpy as np\n\nimport matplotlib as mpl\nfrom matplotlib.backends.backend_webagg import (\n FigureManagerWebAgg, new_figure_manager_given_figure)\nfrom matplotlib.figure import Figure\n\n\ndef create_figure():\n \"\"\"\n Creates a simple example figure.\n \"\"\"\n fig = Figure()\n ax = fig.add_subplot()\n t = np.arange(0.0, 3.0, 0.01)\n s = np.sin(2 * np.pi * t)\n ax.plot(t, s)\n return fig\n\n\n# The following is the content of the web page. You would normally\n# generate this using some sort of template facility in your web\n# framework, but here we just use Python string formatting.\nhtml_content = \"\"\"\n\n
\n \n \n \n \n \n \n\n \n\n