Folders

118 views
Skip to first unread message

Donald Jacobs

unread,
Dec 12, 2022, 9:04:58 AM12/12/22
to desmos-api-discuss
How would I create and store expression inside a folder through the javascript?

ja...@desmos.com

unread,
Dec 12, 2022, 3:03:02 PM12/12/22
to desmos-api-discuss
The API does not currently expose a way to place expressions into folders.

In many cases, if you want to make a graph with a complex structure, it's easiest to create it using the calculator directly, and then use `getState` to get a representation of what you created and `setState` to load it back into the API later.

I don't know if that's possible for your use case, but when it is possible, it's often a lot simpler than trying to create the entire state through API calls.

Yuri Sulyma

unread,
Jan 22, 2025, 4:40:59 PMJan 22
to desmos-api-discuss
Is this still the case? Replacing the entire state can be slow so I would prefer to do it with setExpression({ folderId: "etc" }), but this doesn't seem to work.

Chris Lusto

unread,
Apr 14, 2025, 10:51:57 AMApr 14
to desmos-api-discuss
This is still the case. We're a bit conservative about letting you set this property because in the past we've changed the way that folders know about their children. Right now an expression points to its parent folder (if applicable); in the past folders instead held onto a list of their children. So code relying on the direction of the arrows in the parent-child graph of folders and expressions is brittle. And we don't have an explicit method to place expressions in folders that abstracts away that implementation detail. Though we get this from time to time as a feature request (like right now... noted :) ).

Nate Rickett

unread,
Jun 30, 2025, 6:41:13 PMJun 30
to desmos-api-discuss
The getState and setState preserve folder locations. You could use a list of expressions you want to have in advance, then load the page and move them into a folder, and give yourself a button that saves the getState(). You can keep that as a JSON somewhere in your file system or an object in your script, then pass it back to setState and those variables will still be in the folder you put them in. If you want to go against the documentation advice for treating the getState() as opaque, then you could do this...

calcInstance.setExpression({
type: 'folder',
id: 'testFolder',
title: 'Try this out',
secret: true
});
calcInstance.setExpression({
id: 'test',
latex: `t_{est} = 42`,
secret: true
});
const testString = JSON.stringify(calcInstance.getState());
let putFolder = testString.replace('"id":"test"', '"id":"test","folderId":"testFolder"');
calcInstance.setState(JSON.parse(putFolder));


Nate Rickett

unread,
Jun 30, 2025, 8:14:53 PMJun 30
to desmos-api-discuss
Looks like the JSON.parse is unnecessary because setState accepts the stringify version, too. 
Reply all
Reply to author
Forward
0 new messages