Skip to content

Commit

Permalink
#6103 python API for output containers
Browse files Browse the repository at this point in the history
  • Loading branch information
rbidas committed Oct 12, 2017
1 parent d668615 commit 90f66dc
Show file tree
Hide file tree
Showing 5 changed files with 271 additions and 0 deletions.
1 change: 1 addition & 0 deletions beakerx/beakerx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from .plot import *
from .easyform import *
from .tabledisplay import *
from .output_container import *
from ._version import version_info, __version__

def _jupyter_nbextension_paths():
Expand Down
15 changes: 15 additions & 0 deletions beakerx/beakerx/output_container/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2017 TWO SIGMA OPEN SOURCE, LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from .outputcontainer import *
74 changes: 74 additions & 0 deletions beakerx/beakerx/output_container/outputcontainer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Copyright 2017 TWO SIGMA OPEN SOURCE, LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from beakerx.beakerx_widgets import BeakerxDOMWidget, BeakerxBox
from traitlets import Int, Long, Unicode, Bool, Dict


class OutputContainer(BeakerxDOMWidget):
model = Dict().tag(sync=True)

def __init__(self):
super(OutputContainer, self).__init__()

def addItem(self, *args):
pass

def setLayoutManager(self, layoutManager):
pass


class OutputContainerLayoutManager(BeakerxBox):
_view_module = Unicode('beakerx').tag(sync=True)
_model_module = Unicode('beakerx').tag(sync=True)
_model_module_version = Unicode('*').tag(sync=True)
_view_module_version = Unicode('*').tag(sync=True)
borderDisplayed = Bool(False).tag(sync=True)

def __init__(self):
pass

def setBorderDisplayed(self, borderDisplayed):
pass


class TabbedOutputContainerLayoutManager(OutputContainerLayoutManager):
def __init__(self):
super(TabbedOutputContainerLayoutManager, self).__init__()


class CyclingOutputContainerLayoutManager(OutputContainerLayoutManager):
_view_name = Unicode('CyclingDisplayBoxView').tag(sync=True)
_model_name = Unicode('CyclingDisplayBoxModel').tag(sync=True)
period = 5000

def __init__(self):
super(CyclingOutputContainerLayoutManager, self).__init__()

def setPeriod(self, miliseconds):
self.period = miliseconds


class AbstractGridLayoutManager(OutputContainerLayoutManager):
columns = 0

def __init__(self, columns):
super(AbstractGridLayoutManager, self).__init__()
print ("col: ", columns)
self.columns = columns


class GridOutputContainerLayoutManager(AbstractGridLayoutManager):
def __init__(self, columns):
super(GridOutputContainerLayoutManager, self).__init__(columns)
4 changes: 4 additions & 0 deletions beakerx/beakerx/plot/chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ def add(self, item):
def getYAxes(self):
return self.chart.rangeAxes

def setShowLegend(self, show):
self.chart.show_legend = show
self.model = self.chart.transform()
return self

class CategoryPlot(BeakerxDOMWidget):
_view_name = Unicode('PlotView').tag(sync=True)
Expand Down
177 changes: 177 additions & 0 deletions doc/contents/OutputContainersAndLayoutManagers_python.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Output Containers and Layout Managers\n",
"\n",
"Output containers are objects that hold a collection of other objects, and displays all its contents, even when they are complex interactive objects and MIME type.\n",
"By default the contents are just stacked up on the page, but you can configure them to get tabs, grid, cycling, or other layout methods.\n",
"There is also a class for having no visible output at all: hidden outputs.\n",
"\n",
"## Hidden Outputs"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# something we don't want to see\n",
"from beakerx import *\n",
"import pandas as pd\n",
"def big(x):\n",
" if (x < 2):\n",
" return x\n",
" else:\n",
" return [x, big(x - 1), big(x - 2)]\n",
"\n",
"x = big(10)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Stacked Output Containers"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"o = OutputContainer()\n",
"o.addItem(\"simplest example\")\n",
"o.addItem([2, 3, 5, 7])\n",
"#o.addItem(HTML(\"<h1>title</h1>\"))\n",
"o.addItem(None)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"rates = pd.read_csv('demoResources/interest-rates.csv')\n",
"c = Color(120, 120, 120, 100)\n",
"plot1 = Plot(initWidth= 300, initHeight= 400) \n",
"plot1.add(Points(x= rates.y1, y=rates.y30, size= 3, displayName=\"y1 vs y30\"))\n",
"plot1.add(Points(x= rates.m3, y=rates.y5, size= 3, displayName=\"m3 vs y5\"))\n",
"plot1.add(Line(x= rates.m3, y=rates.y5, color= c))\n",
"plot1.add(Line(x= rates.y1, y=rates.y30, color= c))\n",
"\n",
"plot2 = SimpleTimePlot(rates, [\"m3\", \"y1\"], showLegend=False, initWidth= 300, initHeight= 400)\n",
"plot3 = SimpleTimePlot(rates, [\"y5\", \"y10\"], showLegend=False, initWidth= 300, initHeight= 400)\n",
"table = rates\n",
"#OutputCell.HIDDEN"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Tabbed Output Containers"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"t = TableDisplay(table);\n",
"\n",
"l = TabbedOutputContainerLayoutManager()\n",
"l.setBorderDisplayed(False)\n",
"o = OutputContainer()\n",
"o.setLayoutManager(l)\n",
"o.addItem(plot1, \"Scatter with History\")\n",
"o.addItem(plot2, \"Short Term\")\n",
"o.addItem(plot3, \"Long Term\")\n",
"o.addItem(t, \"1990/01\")\n",
"o"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Grid Output Containers"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plot1.setShowLegend(False)\n",
"bars = CategoryPlot(initWidth= 300, initHeight= 400)\n",
"bars.add(CategoryBars(value= [[1.1, 2.4, 3.8], [1, 3, 5]]))\n",
"\n",
"lg = GridOutputContainerLayoutManager(3)\n",
"\n",
"og = OutputContainer()\n",
"og.setLayoutManager(lg)\n",
"og.addItem(plot1, \"Scatter with History\")\n",
"og.addItem(plot2, \"Short Term\")\n",
"og.addItem(plot3, \"Long Term1\")\n",
"og.addItem(plot3, \"Long Term2\")\n",
"og.addItem(table, \"1990/01\")\n",
"og.addItem(bars, \"Bar Chart\")\n",
"\n",
"og"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Cycling Output Container"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"l = CyclingOutputContainerLayoutManager()\n",
"l.setPeriod(2345); # milliseconds\n",
"l.setBorderDisplayed(False);\n",
"o = OutputContainer()\n",
"o.setLayoutManager(l)\n",
"o.addItem(plot1, \"Scatter with History\")\n",
"o.addItem(plot2, \"Short Term\")\n",
"o.addItem(plot3, \"Long Term\")\n",
"o"
]
}
],
"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.5.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit 90f66dc

Please sign in to comment.