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

Implement compile.cell #3

Open
asg017 opened this issue Sep 13, 2019 · 3 comments
Open

Implement compile.cell #3

asg017 opened this issue Sep 13, 2019 · 3 comments
Labels
enhancement New feature or request
Milestone

Comments

@asg017
Copy link
Owner

asg017 commented Sep 13, 2019

compile.module takes in a "program" as string, parses with parseModule, and outputs a define function to create a runtime module.

compile.cell should take in a "cell" as a string, parse with parseCell, but I'm not sure what it should output.

The output should work for:

  1. Defining new variables - module.define
  2. Re-defining variables - module.redefine
  3. import variables - module.import
  4. Maybe deriving, e.g. import {...} with {...} ? But this probably goes with import above - module.derive
  5. Maybe deleting variables? But this doesnt really make sense (since you write code to delete a variable - maybe - variable.delete

A very rough guess could be:

const define = compile.module(`
a = 1 
b = 2
c = 3
`)

const module = runtime.module(define)

await module.value("a") // 1
await module.value("b") // 2
await module.value("c") // 3

let {define, redefine} = compile.cell(`a  =  b`)
redefine(module)
await module.value("a") // should now be 2

let {define} = compile.cell(`d = c`)
define(module)
await module.value("d") // should be 4 (including redefine example from above)

But idk how import cells could work like this :/

compile.cell could also take in a module as a parameter - for example:

const define = compile.module(`a = 1
b = 2
c= a + b`)

const module = runtime.module(define)

compile.cell(`a = 4`, module)

But I don't like this too much, since 1) how do you distinguish from define/redefine/imports, and 2) I want it to have a similar signature as compile.module (take in a string, return a function you can use to define).

@asg017 asg017 added enhancement New feature or request help wanted Extra attention is needed labels Sep 13, 2019
@asg017 asg017 added this to the v1 milestone Nov 14, 2019
@asg017
Copy link
Owner Author

asg017 commented Jan 23, 2020

@bryangingechen @davertron I just merged in #12 that implements compile.cell (v0.4.0), feel free to take a look at the new API and comment here about what you think! The README has a new section about how it works, and there's some live examples of it in this notebook under "Redefining cells".

I'm afraid the define/redefine might get confusing, and how define requires an observer to work properly (but redefine doesnt need it). I'm not 100% sure what the usecase for compile.cell would be, but I'd love to hear your thoughts if you have any!

@asg017 asg017 removed the help wanted Extra attention is needed label Jan 23, 2020
@davertron
Copy link

davertron commented Jan 24, 2020

@asg017 Awesome! I'll try and take a look at this in the next couple of days and see if I can work it in.

Just to give you some info about what the use case I have for compile.cell: I've built my own observablehq clone. I did this for a couple of reasons: 1) For fun, to learn more about how it works, and 2) Because I wanted a self-hosted version of this at my company that we could use and have access to our data (there's no way my company would be comfortable having a public tool like the real observablehq having access to our data). I also wanted to be able to modify the stdlib to be able to add helper methods for fetching data from our data sources etc.

Anyway, so I've basically used the runtime and your compiler to make my own version of observablehq, which means that I have an editor where people can create their own notebooks etc., which means I need to be able to define cells and redefine them on the fly as the user edits their notebook.

FWIW, this means I'm also very interested in your work to be able to compile notebooks into a module similar to how observablehq's api does it, because I'll want to allow people to import cells from my own notebooks etc. I was planning on trying to implement that myself at some point, so maybe that's something I could contribute back here instead (or use if someone else is already working on it).

But again, thanks for your awesome work on this, it's super cool and has been a fun learning experience!

@RandomFractals
Copy link

RandomFractals commented Oct 15, 2020

@bryangingechen @davertron I just merged in #12 that implements compile.cell (v0.4.0), feel free to take a look at the new API and comment here about what you think! The README has a new section about how it works, and there's some live examples of it in this notebook under "Redefining cells".

this is nice! for those who want to try compile.notebook I forked your original notebook & added my bits for loading notebook metadata to render it in an iframe. See Compiling Notebook section here ;)

https://observablehq.com/@randomfractals/an-unofficial-observablehq-compiler

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

No branches or pull requests

3 participants