From 484b5fed1a95270f2f1dbeb83d67b02d400a5b5c Mon Sep 17 00:00:00 2001 From: patrick Date: Tue, 30 Jul 2019 13:12:45 +0200 Subject: [PATCH 1/2] adding header_visible toggle to hide or show the title/header in mpl figure --- ipympl/backend_nbagg.py | 3 +++ js/src/mpl_widget.js | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/ipympl/backend_nbagg.py b/ipympl/backend_nbagg.py index 94a07899..b5f04c58 100644 --- a/ipympl/backend_nbagg.py +++ b/ipympl/backend_nbagg.py @@ -149,6 +149,9 @@ class Canvas(DOMWidget, FigureCanvasWebAggCore): toolbar = Instance(Toolbar, allow_none=True).tag(sync=True, **widget_serialization) toolbar_visible = Bool(True).tag(sync=True) toolbar_position = Enum(['top', 'bottom', 'left', 'right'], default_value='left').tag(sync=True) + + header_visible = Bool(True).tag(sync=True) + _closed = Bool(True) # Must declare the superclass private members. diff --git a/js/src/mpl_widget.js b/js/src/mpl_widget.js index c8c8585d..ee547cdf 100644 --- a/js/src/mpl_widget.js +++ b/js/src/mpl_widget.js @@ -16,6 +16,7 @@ var MPLCanvasModel = widgets.DOMWidgetModel.extend({ _view_module: 'jupyter-matplotlib', _model_module_version: '^'+ version, _view_module_version: '^' + version, + header_visible: true, toolbar: null, toolbar_visible: true, toolbar_position: 'horizontal' @@ -64,6 +65,7 @@ var MPLCanvasView = widgets.DOMWidgetView.extend({ model_events: function() { this.model.on('msg:custom', this.on_comm_message.bind(this)); + this.model.on('change:header_visible', this.update_header_visible.bind(this)); this.model.on('change:toolbar_visible', this.update_toolbar_visible.bind(this)); this.model.on('change:toolbar_position', this.update_toolbar_position.bind(this)); }, @@ -78,6 +80,11 @@ var MPLCanvasView = widgets.DOMWidgetView.extend({ this.send_message('initialized'); }, + update_header_visible: function() { + this.header.style.display = this.model.get('header_visible') ? '': 'none'; + this.request_resize(); + }, + update_toolbar_visible: function() { this.toolbar_view.el.style.display = this.model.get('toolbar_visible') ? '' : 'none'; this.request_resize(); From cc99d611ef29bdf89761f25ef208595815d84489 Mon Sep 17 00:00:00 2001 From: Patrick Date: Fri, 23 Aug 2019 13:53:49 +0200 Subject: [PATCH 2/2] added example notebook for toolbar_visible --- examples/header_visible.ipynb | 98 +++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 examples/header_visible.ipynb diff --git a/examples/header_visible.ipynb b/examples/header_visible.ipynb new file mode 100644 index 00000000..5544f08d --- /dev/null +++ b/examples/header_visible.ipynb @@ -0,0 +1,98 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "%matplotlib widget" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "ExecuteTime": { + "end_time": "2019-08-07T08:07:06.908562Z", + "start_time": "2019-08-07T08:07:06.602919Z" + } + }, + "outputs": [], + "source": [ + "import matplotlib.pyplot as plt" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "fig, ax = plt.subplots()\n", + "fig.canvas.layout.width = '7in'\n", + "fig.canvas.layout.height= '5in'\n", + "\n", + "# if I hide the header here, I get a libpng error\n", + "# fig.canvas.header_visible = False\n", + "\n", + "ax.plot([1,2,3], [4,5,3])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# hiding after rendering works\n", + "fig.canvas.header_visible = False" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# hiding together with calls to toolbar options, work.\n", + "fig, ax = plt.subplots()\n", + "fig.canvas.layout.width = '7in'\n", + "fig.canvas.layout.height= '5in'\n", + "\n", + "fig.canvas.toolbar_visible = False\n", + "fig.canvas.header_visible = False\n", + "\n", + "ax.plot([1,2,3], [4,5,3])" + ] + }, + { + "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 +}