Scala API to the Table Widget

This API is being improved.


In [ ]:
val table = new TableDisplay(new CSV().readFile("../resources/data/interest-rates.csv"))

table

In [ ]:
import com.twosigma.beakerx.scala.table._
import com.twosigma.beakerx.scala.fileloader.CSV
import com.twosigma.beakerx.table.format.TableDisplayStringFormat
import java.util.concurrent.TimeUnit

val display = new TableDisplay(new CSV().readFile("../resources/data/interest-rates.csv"))
//show all time columns in days
display.setStringFormatForTimes(TimeUnit.DAYS)
//min 4, max 6 decimal places for all doubles
display.setStringFormatForType(ColumnType.Double, TableDisplayStringFormat.getDecimalFormat(4,6))
//setting for a column takes precidence over the type
display.setStringFormatForColumn("m3", TableDisplayStringFormat.getDecimalFormat(0, 0))
//set the alignment
display.setAlignmentProviderForType(ColumnType.Double, TableDisplayAlignmentProvider.RIGHT_ALIGNMENT)
display.setAlignmentProviderForColumn("m3", TableDisplayAlignmentProvider.CENTER_ALIGNMENT)

display

In [ ]:
display.setStringFormatForTimes(TimeUnit.HOURS)

display

In [ ]:
import com.twosigma.beakerx.scala.table._
import com.twosigma.beakerx.scala.fileloader.CSV
import com.twosigma.beakerx.table.format.TableDisplayStringFormat
import com.twosigma.beakerx.table.renderer.TableDisplayCellRenderer
import java.util.concurrent.TimeUnit

val display2 = new TableDisplay(new CSV().readFile("../resources/data/interest-rates.csv"))
//right now, the only renderer option is for data bars
display2.setRendererForType(ColumnType.Double, TableDisplayCellRenderer.getDataBarsRenderer())
//use the false parameter to hide the String value
display2.setRendererForColumn("y10", TableDisplayCellRenderer.getDataBarsRenderer(false))

display2

In [ ]:
import com.twosigma.beakerx.scala.table._
import com.twosigma.beakerx.scala.fileloader.CSV
import com.twosigma.beakerx.table.format.TableDisplayStringFormat
import com.twosigma.beakerx.table.renderer.TableDisplayCellRenderer

val display3 = new TableDisplay(new CSV().readFile("../resources/data/interest-rates.csv"))
display3.setStringFormatForType(ColumnType.Double, TableDisplayStringFormat.getDecimalFormat(9,9))
//freeze a column
display3.setColumnFrozen("y1", true)
//freeze a column to the right
display3.setColumnFrozenRight("y10", true)
//hide a column
display3.setColumnVisible("y30", false)

//explicitly set column order/visiblity
display3.setColumnOrder(List("m3", "y1", "y5", "time", "y2")) //Columns in the list will be shown in the provided order. Columns not in the list will be hidden.
display3

In [ ]:
import com.twosigma.beakerx.scala.table._
import com.twosigma.beakerx.scala.fileloader.CSV
import com.twosigma.beakerx.table.format.TableDisplayStringFormat
import com.twosigma.beakerx.table.renderer.TableDisplayCellRenderer
import com.twosigma.beakerx.table.highlight.TableDisplayCellHighlighter

def display4 = new TableDisplay(new CSV().readFile("../resources/data/interest-rates.csv"))
display4.addCellHighlighter(TableDisplayCellHighlighter.getHeatmapHighlighter("m3", TableDisplayCellHighlighter.FULL_ROW))

//the following two overloads should also be supported
//set the min and max used for calculating the color
//display4.addCellHighlighter(TableDisplayCellHighlighter.getHeatmapHighlighter("y1", TableDisplayCellHighlighter.FULL_ROW, 0, 5))
//set the colors used for the min and max
//display4.addCellHighlighter(TableDisplayCellHighlighter.getHeatmapHighlighter("m6", TableDisplayCellHighlighter.SINGLE_COLUMN, null, null, Color.YELLOW, Color.BLUE))

display4

In [ ]:
import com.twosigma.beakerx.scala.table._
import com.twosigma.beakerx.scala.fileloader.CSV

val map = Seq(Map("a" -> 1, "b" -> 2, "c" -> 3),
               Map("a" -> 4, "b" -> 5, "c" -> 6),
               Map("a" -> 7, "b" -> 8, "c" -> 9))

val display5 = new TableDisplay(map)

display5.addCellHighlighter(new CellHighlighter {
       override def apply(row: Integer, column: Integer, display: com.twosigma.beakerx.table.TableDisplay): Color = {
            if (display.getValues.get(row).get(column).asInstanceOf[Int] > 5) Color.RED else Color.GREEN
       }
})

display5

In [ ]:
import com.twosigma.beakerx.scala.table._
import com.twosigma.beakerx.scala.fileloader.CSV
import com.twosigma.beakerx.chart.Color
import com.twosigma.beakerx.table.highlight.ThreeColorHeatmapHighlighter

val display6 = new TableDisplay(new CSV().readFile("../resources/data/interest-rates.csv"))
display6.addCellHighlighter(TableDisplayCellHighlighter.getHeatmapHighlighter("m3", 0, 8, Color.ORANGE, Color.PINK))
display6.addCellHighlighter(TableDisplayCellHighlighter.getHeatmapHighlighter("m6", HighlightStyle.SINGLE_COLUMN, 6, 8, Color.BLACK, Color.PINK))

display6.addCellHighlighter(new ThreeColorHeatmapHighlighter("y1", HighlightStyle.SINGLE_COLUMN, 4, 6, 8, new Color(247,106,106), new Color(239,218,82), new Color(100,189,122)))

//wipe out all highlighting
//display6.removeAllCellHighlighters()
display6

In [ ]:
import com.twosigma.beakerx.scala.table._
import com.twosigma.beakerx.scala.fileloader.CSV
import com.twosigma.beakerx.chart.Color
import com.twosigma.beakerx.table.highlight.ThreeColorHeatmapHighlighter

val table = new TableDisplay(Seq(Seq(1,2,3), 
                                 Seq(3,4,5), 
                                 Seq(6,2,8), 
                                 Seq(6,2,8), 
                                 Seq(6,2,8), 
                                 Seq(6,4,8), 
                                 Seq(6,2,8), 
                                 Seq(6,2,8), 
                                 Seq(6,5,8)), 
                                 Seq("a", "b", "b"), 
                                 Seq("double", "double", "double"))

table.addCellHighlighter(TableDisplayCellHighlighter.getUniqueEntriesHighlighter("b", HighlightStyle.FULL_ROW))
table

In [ ]:
import com.twosigma.beakerx.scala.table._

val mapList4 = Seq(
   Map("a" -> 1, "b" -> 2, "c" -> 3),
   Map("a" -> 4, "b" -> 5, "c" -> 6),
   Map("a" -> 7, "b" -> 8, "c" -> 9)
)

val display7 = new TableDisplay(mapList4)

//run tagged cell on action
display7.addContextMenuItem("run misc_formatting", "misc_formatting");
display7.setDoubleClickAction("misc_formatting");

//add a context menu item
display7.addCellHighlighter(new CellHighlighter {
       override def apply(row: Integer, column: Integer, display: com.twosigma.beakerx.table.TableDisplay): Color = {
            if (display.getValues.get(row).get(column).asInstanceOf[Int] > 5) Color.RED else Color.GREEN
       }
})

display7

In [ ]:
import com.twosigma.beakerx.scala.table._
import com.twosigma.beakerx.scala.fileloader.CSV

val mapList4 = List(
   Map("a" -> 1, "b" -> 2, "c" -> 3),
   Map("a" -> 4, "b" -> 5, "c" -> 6),
   Map("a" -> 7, "b" -> 8, "c" -> 9)
)

val display7 = new TableDisplay(mapList4)

//add a context menu item
display7.addContextMenuItem("print", new ContextMenuAction {
       override def apply(row: Integer, column: Integer, display: com.twosigma.beakerx.table.TableDisplay): Unit = {
            print (display.getValues.get(row).get(column))
       }
})

//run tagged cell on action
display7.addContextMenuItem("run misc_formatting", "misc_formatting");
display7.setDoubleClickAction("misc_formatting");

display7

In [ ]:
import com.twosigma.beakerx.scala.table._
import com.twosigma.beakerx.scala.fileloader.CSV
import com.twosigma.beakerx.chart.Color
import java.{lang, util}

val mapList5 = List(
 Map("firstCol" -> 1, "secondCol" -> 2, "thirdCol" -> 3),
 Map("firstCol" -> 4, "secondCol" -> 5, "thirdCol" -> 6),
 Map("firstCol" -> 9, "secondCol" -> 8, "thirdCol" -> 7))

val td4 = new TableDisplay(mapList5)

td4.setTooltip(new TooltipAction {
    override def apply(row: Integer, col: Integer, display: com.twosigma.beakerx.table.TableDisplay): String = {
         "The value is: " + display.getValues().get(row).get(col)
    }
})

//set the font size and color
td4.setDataFontSize(15)
td4.setHeaderFontSize(30)

val colors = Seq(
            Seq(Color.LIGHT_GRAY, Color.GRAY, Color.RED),
            Seq(Color.YELLOW, Color.ORANGE, Color.RED),
            Seq(Color.MAGENTA, Color.BLUE, Color.BLACK))

td4.setFontColorProvider(new FontColorProvider {
    override def apply(row: Integer, col: Integer, display: com.twosigma.beakerx.table.TableDisplay): Color = {
        colors(row)(col)
    }
})

td4.setRowFilter(new RowFilter {
override def apply(row: Integer, values: util.List[util.List[_]]): lang.Boolean = {
        if (values.get(row).get(0).asInstanceOf[Int] > 0) true else false
    }
})

//set vertical headers
//you can also do this in the right-click menu
td4.setHeadersVertical(true)

td4

In [ ]:
val abc = 0 // test variable
val mapList = Seq(
   Map("a" -> 1, "b" -> 2, "c" -> 3),
   Map("a" -> 4, "b" -> 5, "c" -> 6),
   Map("a" -> 7, "b" -> 8, "c" -> 8)
)

In [ ]:
import com.twosigma.beakerx.scala.table._
    
val display1 = new TableDisplay(mapList)

//add a context menu item
display1.addContextMenuItem("print", new ContextMenuAction {
       override def apply(row: Integer, column: Integer, display: com.twosigma.beakerx.table.TableDisplay): Unit = {
            print (display.getValues.get(row).get(column))
       }
})

display1

In [ ]:
import com.twosigma.beakerx.scala.table._
    
val display2 = new TableDisplay(mapList)

//run tagged cell on action
display2.addContextMenuItem("run print cell", "print_cell");
display2.setDoubleClickAction("print_cell");

display2

In [ ]: