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

[Question/Feature Request] local/global variables inside a note #21

Open
VictorJermiin opened this issue Jan 3, 2023 · 3 comments
Open
Labels
enhancement New feature or request

Comments

@VictorJermiin
Copy link

VictorJermiin commented Jan 3, 2023

I don't know if this is the right place to ask but I can't find any information about this.

Is it possible to have variables that span all the code blocks inside the same note?
(So I don't have to define the same variable more than once if I chose to make a new code block)
Something like this:
'''math
K = 200
S = 200
'''
here is a new code block
'''math
K + S => outputs: 400
'''

@gtg922r gtg922r added the enhancement New feature or request label Jan 3, 2023
@gtg922r
Copy link
Owner

gtg922r commented Jan 3, 2023

Thanks, I can definitely understand the use-case and want this myself.

I think it might be tricky because all code blocks in a note would have to be re-calculated, but I will definitely look in to it when i get some of the near-term features out the door.

@jwhitley
Copy link
Contributor

I'd thought about this when I was briefly contemplating having a go at a similar plugin. The main thing here is that e.g. numeralsMathBlockHandler should use one calculation context, and "accumulate" all new block content as it's repeatedly called. But on L227 scope is created anew on every handler invocation. I spent a bit of time researching this, but don't have a solid solution yet. I'll writeup something for the forum (a query on Discord didn't produce much) and will report back here if I get anything useful.

My best idea so far would be to attach the state to the Document via an incantation such as this.doc[this.manifest.id]. Obviously, that state would need to be setup or torn down when the plugin was loaded/unloaded. I believe that would correctly scope the mathjs state to an instance of a note in one window. But I'm not finding a lifecycle hook that lets the plugin know when rendering starts, and therefore when to reset that state ala this.doc[this.manifest.id] = {}. (also, any approach should be wrapped in this.{get,set}MathState() or the like)

@gtg922r
Copy link
Owner

gtg922r commented Jan 28, 2023

Some other things that come to mind to solve for this:

  • the ordering of rendering. You would want to ensure the document is rendered from top to bottom (and not in parallel) to ensure the state is repeatably and predictably propagated from block to block.
  • how to re-trigger rendering or calculation. For example if you set a variable at the top of a document you would, presumably, want to re-calculate and re-render any other blocks on the page to propagate new values

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