Demonstration of Use Case

  • Users can enter step by step explanations of changes made to a SciSheet in a Jupyter notebook

Load necessary packages


In [1]:
import json

import numpy as np
import pandas as pd

from jupyter_scisheets_widget import scisheets_widget

Load data into the notebook


In [ ]:
import pandas_datareader as pdr
ibm_data = pdr.get_data_yahoo('IBM')

In [2]:
income_data = pd.read_csv('income_data.csv', sep=';')
income_data


Out[2]:
GEOID State 2005 2006 2007 2008 2009 2010 2011 2012 2013
0 04000US01 Alabama 37150 37952 42212 44476 39980 40933 42590 43464 41381
1 04000US02 Alaska 55891 56418 62993 63989 61604 57848 57431 63648 61137
2 04000US04 Arizona 45245 46657 47215 46914 45739 46896 48621 47044 50602
3 04000US05 Arkansas 36658 37057 40795 39586 36538 38587 41302 39018 39919
4 04000US06 California 51755 55319 55734 57014 56134 54283 53367 57020 57528

In [ ]:
income_data['test_col'] = income_data['2006']

In [ ]:

BUG currently seems to be coming from handonstable rendering

Display the loaded data as a scisheet widget


In [ ]:
import ast

df = income_data
model_data = df.to_json(orient='split')
model_data = ast.literal_eval(model_data)
model_data_2 = json.dumps(model_data)
tbl._model_data = json.dumps(model_data)

In [ ]:
model_data_2

In [ ]:
tbl._model_data

In [ ]:
%%javascript

var json_model = this.model.get('_model_data')
console.log(json_model)

In [3]:
tbl = scisheets_widget.SciSheetTable()
tbl.load_df(income_data)
tbl



In [ ]:
income_data

In [ ]:
bob = income_data.copy()

In [ ]:
bob['House Party 2'] = 5

In [ ]:
income_data

In [ ]:
bob

In [ ]:
from IPython.display import display

In [ ]:
display(tbl)

In [6]:
tbl2._widget._model_data


Out[6]:
u''

In [4]:
tbl2 = scisheets_widget.HandsonDataFrame(income_data)
tbl2.show()


widget initialized

In [ ]:
scisheets_widget.HandsonDataFrame(income_data)._widget._model_data

In [ ]:
import ast

In [ ]:
data_dic = {}
data_dic['columns'] = ast.literal_eval(tbl._model_header)
data_dic['index'] = ast.literal_eval(tbl._model_row_header)
data_dic['data'] = ast.literal_eval(tbl._model_data)
pd.read_json(json.dumps(data_dic), orient='split')

In [ ]:
income_data.to_json(orient='split')

In [ ]:
data_dic

In [ ]:
cols = {'columns': tbl._model_header.encode('utf-8')}
ind = {'index': tbl._model_row_header.encode('utf-8')}
data = {'data': tbl._model_data.encode('utf-8')}

In [ ]:
z=cols
z.update(ind)
z.update(data)

In [ ]:
z

In [ ]:
json.dumps(z)

In [ ]:
pd.read_json(json.dumps(z))

In [ ]:
model_data = df.to_json(orient='split')
model_data = ast.literal_eval(model_data)
self._model_data = json.dumps(model_data['data'])
self._model_header = json.dumps(model_data['columns'])
self._model_row_header = json.dumps(model_data['index'])

In [ ]:
tbl1._widget._model_data

In [ ]:
tbl1 = scisheets_widget.HandsonDataFrame(income_data)
tbl1.show()

In [ ]:
import ipywidgets as widgets

print(widgets.Widget.unobserve.__doc__)

In [ ]:
tbl1._on_displayed()

In [ ]:
tbl1._widget._model_header

In [ ]:
tbl_ibm = scisheets_widget.SciSheetTable()
tbl_ibm.load_df(ibm_data)
tbl_ibm

In [ ]:
tbl = scisheets_widget.SciSheetTable()
tbl.load_df(income_data[['GEOID', 'State']])
tbl

In [ ]:


In [ ]:


In [ ]:
%%html
<div id="example1" class="hot handsontable"></div>

<script src="https://docs.handsontable.com/0.34.4/bower_components/handsontable/dist/handsontable.full.js"></script>
<link type="text/css" rel="stylesheet" href="https://docs.handsontable.com/0.34.4/bower_components/handsontable/dist/handsontable.full.min.css">

In [ ]:
%%javascript
document.addEventListener("DOMContentLoaded", function() {

  var
    data1 = [
      ['', 'Tesla', 'Nissan', 'Toyota', 'Honda', 'Mazda', 'Ford'],
      ['2017', 10, 11, 12, 13, 15, 16],
      ['2018', 10, 11, 12, 13, 15, 16],
      ['2019', 10, 11, 12, 13, 15, 16],
      ['2020', 10, 11, 12, 13, 15, 16],
      ['2021', 10, 11, 12, 13, 15, 16]
    ],
    container1 = document.getElementById('example1'),
    settings1 = {
      data: data1
    },
    hot1;
  
  container1.append('allice')
  hot1 = new Handsontable(container1, settings1);
  data1[0][1] = 'Ford'; // change "Kia" to "Ford" programmatically
  hot1.render();
  
});

In [ ]:
%%javascript
this.element.append('steve')

In [ ]:
data = [{"name": "Lisp", "value": 1}, {"name": "Scala", "value": 2}, {"name": "Perl", "value": 4}, {"name": "Java", "value": 5}, {"name": "C++", "value": 8}, {"name": "Python", "value": 10}]

In [ ]:
tbl = scisheets_widget.SciSheetTable()
tbl.load_list(income_data)
tbl

Testing different ways to convert df to json


In [ ]:
income_data.to_json(orient='index')

In [ ]:
income_data.to_json(orient='values')

In [ ]:
income_data.to_json(orient='records')

In [ ]:
income_data.to_json(orient='columns')

In [ ]:
income_data.to_json(orient='split')

In [ ]:
bob = income_data.to_json(orient='split')

In [ ]:
import ast
bob2 = ast.literal_eval(bob)

In [ ]:
for key, value in bob2.items():
    print("Key:")
    print(key)

In [ ]:
bob2['columns']

In [ ]:
json.dumps(bob2['columns'])

In [ ]:
bob2['columns']

In [ ]:


In [ ]:
'''Illustrates how to detect an assignment for a DataFrame.'''

import pandas as pd

class VirtualDataFrame(object):
    def __init__(self, df):
        self._df = df.copy()
        
    def _value_changed_(self):
        if self._df.__setitem__:
            print "Setting %s to %s" % (str(key), str(value))
        
    def __setitem__(self, key, value):
        print "Setting %s to %s" % (str(key), str(value))

In [ ]:
class VirtualDataFrame2(object):
    def __init__(self, df):
        self._df = df
        
#     def _value_changed_(self):
#         if self._df.__setitem__:
#             print "Setting %s to %s" % (str(key), str(value))
        
#     def __setitem__(self, key, value):
#         print "Setting %s to %s" % (str(key), str(value))

In [ ]:
bob2 = VirtualDataFrame2(income_data)

In [ ]:
bob2._df.loc[3,'State'] = 'Louisiana'

In [ ]:
income_data

In [ ]:
bob = VirtualDataFrame(income_data)

In [ ]:
bob._df.loc[2,'State'] = 'Alex'

In [ ]:
income_data

In [ ]:
class with_called_attribute:
    def __init__(self, func):
        self.func = func
        self.called = False

    def __call__(self, *args, **kwargs):
        self.called = True
        self.func(*args, **kwargs)

@with_called_attribute
def run_some_thing(*args, **kwargs):
    print("You called with:", *args, **kwargs)

run_some_thing.called
#>>> False

run_some_thing(10)
#>>> You called with: 10

run_some_thing.called
#>>> True

In [ ]:
class Detector(object):
    def __init__(self, df):
        self.loadTableIsCalledForObjA = False
        self._df = df

    def __setitem__(self):
        if type(self._df) is pd.DataFrame:
            print('True')

In [ ]:
bob = Detector(income_data)

In [ ]:
bob._df.loc[1, 'State'] =

In [ ]:


In [ ]:
loadTable(income_data)

In [ ]:
detector.loadTableIsCalledForObjA

In [ ]:
bob._df.loc[1, 'State'] = 'Barber'

In [ ]:
bob['Alice'] = bob['State']

In [ ]:
income_data

In [ ]:
income_data

In [ ]:
import pandas as pd
import numpy as np

class MyDF(pd.DataFrame):
    @property
    def _constructor(self):
        return MyDF


mydf = MyDF(np.random.randn(3,4), columns=['A','B','C','D'])
print type(mydf)

mydf_sub = mydf[['A','C']]
print type(mydf_sub)

In [ ]:
import pandas as pd
import numpy as np

class MyDF(pd.DataFrame):
    _attributes_ = "myattr1,myattr2"

    def __init__(self, *args, **kw):
        super(MyDF, self).__init__(*args, **kw)
        if len(args) == 1 and isinstance(args[0], MyDF):
            args[0]._copy_attrs(self)

    def _copy_attrs(self, df):
        for attr in self._attributes_.split(","):
            df.__dict__[attr] = getattr(self, attr, None)

    @property
    def _constructor(self):
        def f(*args, **kw):
            df = MyDF(*args, **kw)
            self._copy_attrs(df)
            return df
        return f

mydf = MyDF(np.random.randn(3,4), columns=['A','B','C','D'])
print type(mydf)

mydf_sub = mydf[['A','C']]
print type(mydf_sub)

mydf.myattr1 = 1
mydf_cp1 = MyDF(mydf)
mydf_cp2 = mydf.copy()
print mydf_cp1.myattr1, mydf_cp2.myattr1

In [ ]:
mydf_cp1

In [ ]:
mydf_cp2

In [ ]:
def write(self):
        return dumps(
            self.obj,
            orient=self.orient,
            double_precision=self.double_precision,
            ensure_ascii=self.ensure_ascii,
            date_unit=self.date_unit,
            iso_dates=self.date_format == 'iso',
            default_handler=self.default_handler
        )

In [ ]:
import json

In [ ]:
json.dumps(income_data)

In [ ]:
import pandas._libs.json as json

In [ ]:
dumps = json.dumps

In [ ]:
dumps(income_data)

In [ ]:
import pandas._libs.

In [ ]:
print('help')

In [ ]:
print 'help'

In [ ]:


In [ ]:


In [ ]:


In [ ]: