4
$\begingroup$

I noticed that I have a bad habit of saving notebooks with huge graphics in them and it's starting to impact storage and load times a little much. I figured I could make a routine to point out the few large cells that were wasting most of the space.

My first draft is supposed to move to the top culprit and show an info dialog, like so:

screenshot

I would like to know

  1. Does a similar tool already exist, either in Mathematica or written by a third party somewhere?

  2. This is my first foray into trying to navigate from spot to spot within and between notebooks; does anyone see any function usages I'm getting obviously wrong? The hyperlinking-around-notebook behavior does not seem too consistent; there are lots of Notebook-related functions and the practical differences between them all aren't clear to me yet. (Code below.)

Again, if someone else already built a waaaay better this-kinda-tool, you can just point me at that and we'll call it a day.


ClearAll[memShame];
memShame[nb_:SelectedNotebook[]]:=Module[{bcs,maxPos},
bcs=ByteCount/@NotebookRead/@Cells[];
maxPos=First@FirstPosition[bcs,Max@bcs];

SelectionMove[nb,Before,Notebook];
SelectionMove[nb,After,Cell,maxPos-1];

CreateWindow[DialogNotebook[{
Grid[
({PercentForm[#[[1]]*#[[2]]/N@Total[bcs],3],OutputForm@Row[{Ceiling[#[[1]]/1000.],"k"}],#[[2]]}&)
/@SortBy[Tally[bcs],-First[#]&][[;;UpTo@10]],Alignment->Right, Frame -> All, FrameStyle -> Directive[{
GrayLevel[0.65]}], ItemSize -> Full],DefaultButton["Okay",SelectionMove[nb,After,Notebook];DialogReturn[]]}],WindowTitle->"Fat check...",WindowSize->All];
]

As a small aside, I was also going to make the top entries clickable to zoom to each one, with buttons on that row to ignore, delete the output cell, or maybe even another option to mark the input cell as Initialization so all the graphics will pop back up on next load. Other timesaving or neat suggestions for this are appreciated. (Also, if there's a faster approach than spamming ByteCount on cells...?)

$\endgroup$
2
  • 1
    $\begingroup$ Please see this question. In the answers there is a very useful palette for finding large cells. mathematica.stackexchange.com/questions/87102/… $\endgroup$ Sep 20, 2021 at 19:37
  • $\begingroup$ @GustavoDelfino Thanks for that, I was pretty sure I was reinventing the wheel with this one. $\endgroup$
    – Trev
    Sep 20, 2021 at 19:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.