Import fails silently - Logging notebook

Implemented altocumulus’s answer from (javascript - D3 console.log in a function chain - Stack Overflow)

…in a notebook:

Works in the notebook but when I import it into a notebook and try to use one of the functions I get nothing. No errors and no output.

Don’t know if it is of interest but originally I had coded his object exactly from StackOverflow but received errors, his comment about wrapping the object in parens fixed it…

That’s some peculiarity of Observable’s code. The first code block chokes on an “Unexpected Token” and, if you look closely at the top right corner of that code block, you’ll see some icon resembling a magic wand which has a tooltip reading “Objects need to be parenthesized to differentiate them from blocks”. To make it work, simply wrap your logger object in parentheses like so: logger = ({ log() { }, /****/ });. – altocumulus [22 hours ago]

When the page first loads I get this.


When I run that cell again, it works.

Not sure how to fix that, does it need to wait for the

Object.assign(d3.selection.prototype, logger);

To finish before running?

you can name the assignment cell and then add it as a dependancy (whose value is not used)

1 Like

Does the logger have to be assigned to every cell. ie can it be assigned at a notebook level and used on any cell?

You could

d3Prime = assignLogger, d3

and use d3Prime instead of d3

Or, export d3Prime as d3 from a notebook that does a similar trick so you can keep using the name d3

1 Like

Works as an import across a notebook now. Thanks Tom!

Published at: observablehq.com/@kalmdown/altocumuluss-logging

1 Like