Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a way to create a new notebook? #58

Open
skybrian opened this issue Jul 30, 2023 · 5 comments
Open

Add a way to create a new notebook? #58

skybrian opened this issue Jul 30, 2023 · 5 comments

Comments

@skybrian
Copy link

It might be nice to be able to create a new notebook file and start typing, without having to download an existing notebook.

@GordonSmith
Copy link
Owner

You can - you just need to create new file with one of the following extensions:

  • ojs
  • omd
  • ojsnb

@skybrian
Copy link
Author

skybrian commented Aug 9, 2023

Could you explain how?

When I create an new file with an ojs extension, it says "Observable JavaScript (OJS)" in the lower right, but it just behaves like a regular JavaScript file, and I don't see how to create a new cell.

By contrast, for Jupyter, there is a "Create: New Jupyter Notebook" command. It creates an unsaved file named "Untitled-1.ipynb" with a blank cell. I can type '2+2' and shift-return, and it execute and return 4.

The only way I see to get an ojs notebook that works is with the "OJS: Download Notebook" command.

Edit: I see now, the extension to use is .ojsnb. But what are the other extensions for?

@GordonSmith
Copy link
Owner

An OJS file is essentially one big Observable Cell, without being limited to one statement, so the following would be valid:

md`# Hello Wold ${mol}`;

mol = 42;

{
  const context = DOM.context2d(width, height);
  let frame;
  
  (function tick() {
    const x = (Math.sin(Date.now() / 1000) + 1) / 2 * (width - 2 * radius) + radius;
    context.clearRect(0, 0, width, height);
    context.beginPath();
    context.arc(x, height / 2, radius, 0, 2 * Math.PI);
    context.fill();
    frame = requestAnimationFrame(tick);
  })();
  
  invalidation.then(() => cancelAnimationFrame(frame));  
  return context.canvas;
}

radius = 11;
height = 22;

Press the preview to see it:
image

OMD docs are essentially markdown documents, that expect valid ojs inside code pips:

image

@skybrian
Copy link
Author

skybrian commented Aug 9, 2023

Thanks!

Clearly, I wasn't reading very carefully. Sorry about that!

In retrospect, I picked the .ojs extension because it was the first one listed and you use 'OJS' as an abbreviation for the Observable JS extension itself, so I thought that was the main file extension to use. But .ojsnb is the one that behaves like a notebook.

I think that for the other extensions, if it's going to display the result for each 'cell', it might make sense to also display the variable name? Showing 'height = 22' in the output would be more meaningful than just '22'.

Displaying the source code that generated the variable might make sense too? On the Observable HQ website, you can choose whether to expand or collapse the source code for each cell. Similarly, it would make sense for the author of a Markdown document to somehow decide whether to show the source code for each 'cell' as well as what the expression evaluated to.

@GordonSmith
Copy link
Owner

Thanks for the feedback - In the past I have debated this with myself as well and with the addition of the "ojsnb" support decided that it catered for the folks who wanted similar features.

I think the OMD format has scope to add additional meta information (next to the pips) per code cell, but for OJS it would be an all or nothing type switch (like a "debug" mode in the preview pane).

FWIW - If you use the "export to HTML" feature:
image

You will see that I am simply reusing the official Observable "Inspector" to render the cells.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants