{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Embedding WebAgg\n\n\nThis example demonstrates how to embed matplotlib WebAgg interactive\nplotting in your own web application and framework. It is not\nnecessary to do all this if you merely want to display a plot in a\nbrowser or use matplotlib's built-in Tornado-based server \"on the\nside\".\n\nThe framework being used must support web sockets.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import io\n\ntry:\n import tornado\nexcept ImportError:\n raise RuntimeError(\"This example requires tornado.\")\nimport tornado.web\nimport tornado.httpserver\nimport tornado.ioloop\nimport tornado.websocket\n\n\nfrom matplotlib.backends.backend_webagg_core import (\n FigureManagerWebAgg, new_figure_manager_given_figure)\nfrom matplotlib.figure import Figure\n\nimport numpy as np\n\nimport json\n\n\ndef create_figure():\n \"\"\"\n Creates a simple example figure.\n \"\"\"\n fig = Figure()\n a = fig.add_subplot(111)\n t = np.arange(0.0, 3.0, 0.01)\n s = np.sin(2 * np.pi * t)\n a.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 \n\n