Comments (6)
Ah ha, I think I figured this out - one would need to write a custom encoder for this, yes?
E.g., I got this working with the Altair chart using the following encoder:
from scrapbook.encoders import registry as encoder_registry
# add encoder to the registry
class AltairEncoder(object):
def encode(self, scrap):
# scrap.data is any type, usually specific to the encoder name
scrap = scrap._replace(data=scrap.data.to_dict())
return scrap
def decode(self, scrap):
# scrap.data is one of [None, list, dict, *six.integer_types, *six.string_types]
scrap = scrap._replace(data=alt.Chart.from_dict(scrap.data))
return scrap
encoder_registry.register("altair", AltairEncoder())
Followed by
ch = alt.Chart(data=df).mark_point().encode(
x='a',
y='b'
)
sb.glue('chart', ch, 'altair')
Then I could do:
neat!
In addition, for things like just matplotlib figures, you could use:
sb.glue('mplfig', fig, 'display')
and then access the display data with:
# To grab the serialized image
nb.scraps['mplfig'].display['data']['image/png']
# To display again (if in another notebook)
nb.reglue('mplfig')
Let me know if that is correct, and if so then would it be useful for me to add an example to the docs showing these use-cases?
from scrapbook.
Just a note here to describe the use-case I have in mind.
Imagine that you're writing a paper after doing a collection of analyses with notebooks. You probably have multiple notebooks that have some markup in them, statistics that were run, and figures that were generated. You don't want to write your paper in those notebooks because they're too messy. However, you'd like to reference what happens in those notebooks so that there is one canonical source of truth, and so that the values / plots update as you update those notebooks.
I'm imagining using scrapbook for something like this. If you were writing the paper in rST, then with some custom rST directives, you could imagine things like
In this analysis, there was a significant effect (p=:scrapbook:`notebookID:scrapPValueID`). See Figure 1 for more details.
.. scrapbook-figure:
:notebook: notebookID
:scrap: scrapFigureID
:name: Figure 1
Caption
That could be a really interesting way to store pieces of an analyses in separate notebooks, and then read them in to a single "finished product" document
from scrapbook.
Sorry for the delayed response. Glad you got some time to play with the project!
For the first question you can actually save the plot as-is, it just requires an extra field to stop the data being saved as well as the display object:
data = np.random.randn(2, 100)
fig, ax = plt.subplots()
plt.close(fig)
scat = ax.scatter(*data, c=data[1])
sb.glue("fig1", fig, encoder='display', display=True)
The encoder='display'
is required for purely display objects. If you don't close the figure it will render twice since the glue call also renders the plot.
Then you can load with:
nb = sb.read_notebook('figure_gen.ipynb')
nb.reglue('fig1')
I am planning on improving this so it's more intuitive / figures out what to do automatically with the user needing to specify -- some of that is in master but it's still got some issues to fix.
from scrapbook.
Same for the second altair example:
import altair as alt
from vega_datasets import data
source = data.movies.url
fig2 = alt.Chart(source).mark_bar().encode(
alt.X("IMDB_Rating:Q", bin=True),
y='count()',
)
sb.glue('fig2', fig2, encoder='display', display=True)
then:
nb.reglue('fig2')
from scrapbook.
I do like the idea of adding some encoders for capturing the data and the display for various charting libraries. The matplotlib example I posted above only captures the display content and not the chart information itself. It was trying to do that encoding with the initial exception you hit, not knowing how to convert the figure object to a jsonizable type. This means you have to save the raw data in a separate scrap in the current version if you wanted to replot the data in a different way.
from scrapbook.
That could be a really interesting way to store pieces of an analyses in separate notebooks, and then read them in to a single "finished product" document
Yep that should be possible to do with some interface additions to an rst or markdown rendering engine. You can also imagine pointing to a notebook in github or a document store as the source for the plots and data points.
from scrapbook.
Related Issues (20)
- Problem with reading notebooks directly from S3 Buckets HOT 3
- 0.3.0 Release HOT 2
- Allow saving of dataframes HOT 5
- Python 3 only release change
- Incompatible with JupyterLab HOT 2
- Error message: Mime type unknown is not currently supported HOT 2
- Scrap of type Int not supported HOT 2
- Pandas DataFrame example raises error "No encoder found " HOT 2
- Importing ABC directly from collections was deprecated and will be removed in Python 3.10
- Highlight demo fails on Binder with KeyError: "kernel_manager_class" HOT 6
- Remove papermill dependency
- Python 3.9 compatibility HOT 3
- All old versions have been dropped from pypi? HOT 4
- Cool project, however, some suggestions for improvements HOT 2
- Authorize filter arguments to filter notebook to read given their name HOT 1
- Reverse operation for `glue` HOT 2
- What happened to pm.display? HOT 4
- scrap.Scrap as a dataclass
- Issue with scrapbook glue in R ipynb:
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from scrapbook.