You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@willingc@MSeal
It would be helpful to provide a base class (interface) for encoders so that people don't get confused when trying to implement new encoders. Also I would like to have a "pickler" as one of the builtin encoders since oftentimes I need to embed open matplotlib figures for rework later; the use cases aren't limited to matplotlib figures either - its the picklable objects: users should have the freedom to save and restore any variable of their choice in interactive notebooks.
Implementation details follow
scrapbook_ext.py
importscrapbookassbimportscrapbook.encodersimportscrapbook.scrapsimportabc# encoder class interfaceclassBaseEncoder(abc.ABC):
defname(self):
...
defencodable(self, data):
...
defencode(self, scrap: sb.scraps.Scrap, **kwargs):
...
defdecode(self, scrap: sb.scraps.Scrap, **kwargs):
...
# pickle encoderimportbase64importpickleimportfunctools# TODO ref https://stackoverflow.com/a/38755760defpipeline(*funcs):
returnlambdax: functools.reduce(lambdaf, g: g(f), list(funcs), x)
classPickleEncoder(BaseEncoder):
ENCODER_NAME='pickle'defname(self):
returnself.ENCODER_NAMEdefencodable(self, data):
# TODOreturnTruedefencode(self, scrap: sb.scraps.Scrap, **kwargs):
_impl=pipeline(
functools.partial(pickle.dumps, **kwargs),
# NOTE .decode() makes sure its a UTF-8 string instead of byteslambdax: base64.b64encode(x).decode()
)
returnscrap._replace(
data=_impl(scrap.data)
)
defdecode(self, scrap: sb.scraps.Scrap, **kwargs):
_impl=pipeline(
base64.b64decode,
functools.partial(pickle.loads, **kwargs)
)
returnscrap._replace(data=_impl(scrap.data))
# TODO dill encoder# NOTE dill has a function `.pickles` to check if an object is encodable. so `encodable` does not have to return `True` regardless of data like `PickleEncoder` doesdefregister():
sb.encoders.registry.register(PickleEncoder())
Usage examples
notebook.ipynb
importscrapbookassbimportscrapbook_extassb_ext# register the encoder(s); currently required as the above implementation is a separate modulesb_ext.register()
importmatplotlib.pyplotaspltimportnumpyasnpimportiot=np.arange(0.0, 2.0, 0.01)
s=1+np.sin(2*np.pi*t)
fig, ax=plt.subplots(figsize=(5, 3.5))
ax.plot(t, s)
ax.set(xlabel='time (s)', ylabel='voltage (mV)')
ax.grid()
# glue this figure to the notebooksb.glue("figure:test", fig)
# sb.glue("figure:test", fig, encoder='pickle')
another_notebook.ipynb
importmatplotlibimportmatplotlib.pyplotasplt%matplotlibinline%configInlineBackend.figure_format='svg'importscrapbookassbimportscrapbook_extassb_ext# register the encoder(s); currently required as the above implementation is a separate modulesb_ext.register()
nb=sb.read_notebook('notebook.ipynb')
# display the figurenb.scraps['figure:test'].data
@willingc @MSeal
It would be helpful to provide a base class (interface) for encoders so that people don't get confused when trying to implement new encoders. Also I would like to have a "
pickle
r" as one of the builtin encoders since oftentimes I need to embed openmatplotlib
figures for rework later; the use cases aren't limited tomatplotlib
figures either - its the picklable objects: users should have the freedom to save and restore any variable of their choice in interactive notebooks.Implementation details follow
scrapbook_ext.py
Usage examples
notebook.ipynb
another_notebook.ipynb
See also: Example project
https://github.com/oakaigh/scrapbook-ext
The text was updated successfully, but these errors were encountered: