Skip to content

Instantly share code, notes, and snippets.

@mzechmeister
Last active June 16, 2023 18:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mzechmeister/7f7ce55dec843f4e6464403a74e71ecc to your computer and use it in GitHub Desktop.
Save mzechmeister/7f7ce55dec843f4e6464403a74e71ecc to your computer and use it in GitHub Desktop.
desmos load data file
// insert everything in the browser console and a "file" button will appear in the submenu
function addfilebutton() {
if (document.querySelector("#file2xy") || // exists already
!document.querySelector(".dcg-action-newimage")) // unclick case
return
pic = document.querySelector(".dcg-action-newimage").parentNode
var div = document.createElement('div');
div.innerHTML = '<style>.dcg-calculator-api-container .dcg-icon-new-file::before { content: "\\e205";}</style><div class="dcg-new-item dcg-action-newfile" onclick="file2tab.click()"><i class="dcg-icon-new-file dcg-expression-icon" aria-hidden="true"></i>Datei2tab</div><input id="file2tab" type="file" style="display: none" onchange="tabFromFile()"></div>'
pic.after(div)
var div = document.createElement('div');
div.innerHTML = '<div class="dcg-new-item dcg-action-newfile" onclick="file2xy.click()"><i class="dcg-icon-new-file dcg-expression-icon" aria-hidden="true"></i>Datei2xy</div><input id="file2xy" type="file" style="display: none" onchange="xyFromFile()"></div>'
pic.after(div)
}
document.querySelector(".dcg-add-expression-container").onclick = addfilebutton
async function xyFromFile() {
f = event.target.files[0];
console.log(f)
t = await f.text();
rows = []
lines = t.split("\n")
for (line of lines) {
if (line) {
rows.push(line.split(RegExp(" +")).slice(0, 2).join())
}
}
state = Calc.getState()
state.expressions.list.push({
type: "expression",
latex: "d=[("+rows.join("), (")+")]"
})
Calc.setState(state)
}
async function tabFromFile() {
f = event.target.files[0];
console.log(f)
t = await f.text();
col0 = {latex: "k_{1}", values: [], hidden: true, id: "26", color: "#6042a6"}
col1 = {latex: "x_{1}", values: [], hidden: true, id: "26", color: "#6042a6"}
col2 = {latex: "y_{1}", values: []}
lines = t.split("\n")
for ([i,line] of lines.entries()) {
if (line) {
cols = line.split(RegExp(" +"));
col0.values.push(i+1);
col1.values.push(cols[0]);
col2.values.push(cols[1])
}
}
state = Calc.getState()
state.expressions.list.push({
type: "table",
columns: [col0, col1, col2]
})
Calc.setState(state)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment