import pandas as pd
from beakerx import *
from beakerx.object import beakerx

pd.read_csv('../resources/data/interest-rates.csv')

table = TableDisplay(pd.read_csv('../resources/data/interest-rates.csv'))
table.setAlignmentProviderForColumn('m3', TableDisplayAlignmentProvider.CENTER_ALIGNMENT)
table.setRendererForColumn("y10", TableDisplayCellRenderer.getDataBarsRenderer(False))
table.setRendererForType(ColumnType.Double, TableDisplayCellRenderer.getDataBarsRenderer(True))
table

df = pd.read_csv('../resources/data/interest-rates.csv')
df['time'] = df['time'].str.slice(0,19).astype('datetime64[ns]')
table = TableDisplay(df)
table.setStringFormatForTimes(TimeUnit.DAYS)
table.setStringFormatForType(ColumnType.Double, TableDisplayStringFormat.getDecimalFormat(4,6))
table.setStringFormatForColumn("m3", TableDisplayStringFormat.getDecimalFormat(0, 0))

table

table = TableDisplay(pd.read_csv('../resources/data/interest-rates.csv'))
table
#freeze a column
table.setColumnFrozen("y1", True)
#hide a column
table.setColumnVisible("y30", False)

table.setColumnOrder(["m3", "y1", "y5", "time", "y2"])

def config_tooltip(row, column, table):
      return "The value is: " + str(table.values[row][column])

table.setToolTip(config_tooltip)

table.setDataFontSize(16)
table.setHeaderFontSize(18)

table

mapListColorProvider = [
    {"a": 1, "b": 2, "c": 3},
    {"a": 4, "b": 5, "c": 6},
    {"a": 7, "b": 8, "c": 5}
]
tabledisplay = TableDisplay(mapListColorProvider)

colors = [
    [Color.LIGHT_GRAY, Color.GRAY, Color.RED],
    [Color.DARK_GREEN, Color.ORANGE, Color.RED],
    [Color.MAGENTA, Color.BLUE, Color.BLACK]
]

def color_provider(row, column, table):
    return colors[row][column]

tabledisplay.setFontColorProvider(color_provider)
tabledisplay

mapListFilter = [
   {"a":1, "b":2, "c":3},
   {"a":4, "b":5, "c":6},
   {"a":7, "b":8, "c":5}
]
display = TableDisplay(mapListFilter)

def filter_row(row, model):
    return model[row][1] == 8

display.setRowFilter(filter_row)

display

table = TableDisplay(pd.read_csv('../resources/data/interest-rates.csv'))
table.addCellHighlighter(TableDisplayCellHighlighter.getHeatmapHighlighter("m3", TableDisplayCellHighlighter.FULL_ROW))

table

"""
Explanation: Python API for Table Display
In addition to APIs for creating and formatting BeakerX's interactive table widget, the Python runtime configures pandas to display tables with the interactive widget instead of static HTML.
End of explanation
"""


beakerx.pandas_display_default()
pd.read_csv('../resources/data/interest-rates.csv')

"""
Explanation: Display mode: Pandas default
End of explanation
"""


beakerx.pandas_display_table()
pd.read_csv('../resources/data/interest-rates.csv')

"""
Explanation: Display mode: TableDisplay Widget
End of explanation
"""


TableDisplay([{'y1':4, 'm3':2, 'z2':1}, {'m3':4, 'z2':2}])

TableDisplay({"x" : 1, "y" : 2})

"""
Explanation: Recognized Formats
End of explanation
"""


mapList4 = [
   {"a":1, "b":2, "c":3},
   {"a":4, "b":5, "c":6},
   {"a":7, "b":8, "c":5}
]
display = TableDisplay(mapList4)

def dclick(row, column, tabledisplay):
    tabledisplay.values[row][column] = sum(map(int,tabledisplay.values[row]))

display.setDoubleClickAction(dclick)

def negate(row, column, tabledisplay):
    tabledisplay.values[row][column] = -1 * int(tabledisplay.values[row][column])

def incr(row, column, tabledisplay):
    tabledisplay.values[row][column] = int(tabledisplay.values[row][column]) + 1

display.addContextMenuItem("negate", negate)
display.addContextMenuItem("increment", incr)

display

mapList4 = [
   {"a":1, "b":2, "c":3},
   {"a":4, "b":5, "c":6},
   {"a":7, "b":8, "c":5}
]
display = TableDisplay(mapList4)

#set what happens on a double click
display.setDoubleClickAction("runDoubleClick")

display

print("runDoubleClick fired")
print(display.details)

"""
Explanation: Programmable Table Actions
End of explanation
"""


df = pd.read_csv('../resources/data/interest-rates.csv')
df.set_index(['m3'])

df = pd.read_csv('../resources/data/interest-rates.csv')
df.index = df['time']
df

"""
Explanation: Set index to DataFrame
End of explanation
"""


dataToUpdate = [
   {'a':1, 'b':2, 'c':3},
   {'a':4, 'b':5, 'c':6},
   {'a':7, 'b':8, 'c':9}
]
tableToUpdate = TableDisplay(dataToUpdate)

tableToUpdate

tableToUpdate.values[0][0] = 99
tableToUpdate.sendModel()

tableToUpdate.updateCell(2,"c",121)
tableToUpdate.sendModel()

"""
Explanation: Update cell
End of explanation
"""


table = TableDisplay({
                      'w': '$2 \\sigma$',
                      'x': '<em style="color:red">italic red</em>',
                      'y': '<b style="color:blue">bold blue</b>',
                      'z': 'strings without markup work fine too',
                      })
table.setStringFormatForColumn("Value", TableDisplayStringFormat.getHTMLFormat())
table

"""
Explanation: HTML format
HTML format allows markup and styling of the cell's content.  Interactive JavaScript is not supported however.
End of explanation
"""


TableDisplay({'Two Sigma': 'http://twosigma.com', 'BeakerX': 'http://BeakerX.com'})

"""
Explanation: Auto linking of URLs
The normal string format automatically detects URLs and links them.  An underline appears when the mouse hovers over such a string, and when you click it opens in a new window.
End of explanation
"""


