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 [2]:
import pandas_datareader as pdr
ibm_data = pdr.get_data_yahoo('IBM')

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


Out[3]:
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 [6]:
tbl = scisheets_widget.SciSheetTable()
tbl.load_df(income_data)
tbl



In [6]:
tbl._model_row_header


Out[6]:
u'[0, 1, 2, 3, 4]'

In [7]:
tbl._model_header


Out[7]:
u'["GEOID", "State", "2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013"]'

In [21]:
tbl._model_row_header.encode('utf-8')


Out[21]:
'[0, 1, 2, 3, 4]'

In [36]:
import ast

In [39]:
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')


Out[39]:
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 [12]:
income_data.to_json(orient='split')


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

In [38]:
data_dic


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

In [25]:
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 [27]:
z=cols
z.update(ind)
z.update(data)

In [28]:
z


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

In [29]:
json.dumps(z)


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

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


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-33-05783029e308> in <module>()
----> 1 pd.read_json(json.dumps(z))

/Applications/miniconda3/envs/scisheets_env/lib/python2.7/site-packages/pandas/io/json/json.pyc in read_json(path_or_buf, orient, typ, dtype, convert_axes, convert_dates, keep_default_dates, numpy, precise_float, date_unit, encoding, lines)
    352         obj = FrameParser(json, orient, dtype, convert_axes, convert_dates,
    353                           keep_default_dates, numpy, precise_float,
--> 354                           date_unit).parse()
    355 
    356     if typ == 'series' or obj is None:

/Applications/miniconda3/envs/scisheets_env/lib/python2.7/site-packages/pandas/io/json/json.pyc in parse(self)
    420 
    421         else:
--> 422             self._parse_no_numpy()
    423 
    424         if self.obj is None:

/Applications/miniconda3/envs/scisheets_env/lib/python2.7/site-packages/pandas/io/json/json.pyc in _parse_no_numpy(self)
    637         if orient == "columns":
    638             self.obj = DataFrame(
--> 639                 loads(json, precise_float=self.precise_float), dtype=None)
    640         elif orient == "split":
    641             decoded = dict((str(k), v)

/Applications/miniconda3/envs/scisheets_env/lib/python2.7/site-packages/pandas/core/frame.pyc in __init__(self, data, index, columns, dtype, copy)
    273                                  dtype=dtype, copy=copy)
    274         elif isinstance(data, dict):
--> 275             mgr = self._init_dict(data, index, columns, dtype=dtype)
    276         elif isinstance(data, ma.MaskedArray):
    277             import numpy.ma.mrecords as mrecords

/Applications/miniconda3/envs/scisheets_env/lib/python2.7/site-packages/pandas/core/frame.pyc in _init_dict(self, data, index, columns, dtype)
    409             arrays = [data[k] for k in keys]
    410 
--> 411         return _arrays_to_mgr(arrays, data_names, index, columns, dtype=dtype)
    412 
    413     def _init_ndarray(self, values, index, columns, dtype=None, copy=False):

/Applications/miniconda3/envs/scisheets_env/lib/python2.7/site-packages/pandas/core/frame.pyc in _arrays_to_mgr(arrays, arr_names, index, columns, dtype)
   5494     # figure out the index, if necessary
   5495     if index is None:
-> 5496         index = extract_index(arrays)
   5497     else:
   5498         index = _ensure_index(index)

/Applications/miniconda3/envs/scisheets_env/lib/python2.7/site-packages/pandas/core/frame.pyc in extract_index(data)
   5533 
   5534         if not indexes and not raw_lengths:
-> 5535             raise ValueError('If using all scalar values, you must pass'
   5536                              ' an index')
   5537 

ValueError: If using all scalar values, you must pass an index

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 [5]:
tbl1._widget._model_data


Out[5]:
u''

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


widget initialized

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(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 [ ]: