Spaces:
Sleeping
Sleeping
Update app.py
Browse files210 unmodified lines2112122132142152162172182192202212226 unmodified lines2292302312322332342354 unmodified lines24024124224324424512 unmodified lines2582592602612622632642652662672682692 unmodified lines2722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503511 unmodified line353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453210 unmodified linesconst radius = Math.min(w, h) * 0.36;// node positions around circle (add abbrev)const n = NODES.length;function angleFor(i) { return (i / n) * 2 * Math.PI; }const nodePos = NODES.map((name,i) => {const ang = angleFor(i) - Math.PI/2; // start at topconst ab = (name.length > 7 ? name.slice(0,5) + "…" : name);return {name: name,abbrev: ab,6 unmodified linesconst nameToIndex = {};NODES.forEach((nm,i)=> nameToIndex[nm]=i);// small node circles and labels outsideconst group = svg.append("g").selectAll("g").data(nodePos).enter().append("g").attr("transform", d => `translate(${d.x},${d.y})`);4 unmodified lines.style("stroke-width", 1).style("cursor", "pointer");group.append("text").attr("x", d => Math.cos(d.angle) * (radius + 26)).attr("y", d => Math.sin(d.angle) * (radius + 26))12 unmodified lines.style("cursor","pointer").text(d => d.abbrev);// bezier helperfunction bezierPath(x0,y0,x1,y1,above=true) {const mx = (x0 + x1)/2;const my = (y0 + y1)/2;const dx = mx;const dy = my;const len = Math.sqrt(dx*dx + dy*dy) || 1;const ux = dx/len, uy = dy/len;const offset = (above ? -1 : 1) * Math.max(30, radius*0.9);2 unmodified linesreturn `M ${x0} ${y0} Q ${cx} ${cy} ${x1} ${y1}`;}// stroke width scaleconst allW = [].concat(BUYS.map(d=>d[2]), SELLS.map(d=>d[2]), TRANSFERS.map(d=>d[2]));const wmin = Math.min(...(allW.length?allW:[1]));const wmax = Math.max(...(allW.length?allW:[1]));const stroke = d3.scaleLinear().domain([wmin, Math.max(wmax,1)]).range([1.0, 6.0]);// buys (top)const buyGroup = svg.append("g").attr("class","buys");BUYS.forEach(b => {const a = b[0], c = b[1], wt = b[2];if (!(a in nameToIndex) || !(c in nameToIndex)) return;const s = nodePos[nameToIndex[a]];const t = nodePos[nameToIndex[c]];const path = bezierPath(s.x,s.y,t.x,t.y,true);buyGroup.append("path").attr("d", path).attr("fill","none").attr("stroke","#2e8540").attr("stroke-width", stroke(wt)).attr("stroke-linecap","round").attr("opacity", 0.92).attr("data-src", a).attr("data-tgt", c).on("mouseover", function() { d3.select(this).attr("opacity",1); }).on("mouseout", function() { d3.select(this).attr("opacity",0.92); });});// sells (bottom)const sellGroup = svg.append("g").attr("class","sells");SELLS.forEach(s => {const c = s[0], a = s[1], wt = s[2];if (!(c in nameToIndex) || !(a in nameToIndex)) return;const sp = nodePos[nameToIndex[c]];const tp = nodePos[nameToIndex[a]];const path = bezierPath(sp.x,sp.y,tp.x,tp.y,false);sellGroup.append("path").attr("d", path).attr("fill","none").attr("stroke","#c0392b").attr("stroke-width", stroke(wt)).attr("stroke-linecap","round").attr("stroke-dasharray","4,3").attr("opacity",0.86).attr("data-src", c).attr("data-tgt", a).on("mouseover", function() { d3.select(this).attr("opacity",1); }).on("mouseout", function() { d3.select(this).attr("opacity",0.86); });});// transfers (grey chords)const transferGroup = svg.append("g").attr("class","transfers");TRANSFERS.forEach(tr => {const sname = tr[0], tname = tr[1], wt = tr[2];if (!(sname in nameToIndex) || !(tname in nameToIndex)) return;const sp = nodePos[nameToIndex[sname]];const tp = nodePos[nameToIndex[tname]];const mx = (sp.x + tp.x)/2;const my = (sp.y + tp.y)/2;const cx = mx * 0.3, cy = my * 0.3;const path = `M ${sp.x} ${sp.y} Q ${cx} ${cy} ${tp.x} ${tp.y}`;transferGroup.append("path").attr("d", path).attr("fill","none").attr("stroke","#7d7d7d").attr("stroke-width", stroke(wt)).attr("opacity",0.7).attr("data-src", sname).attr("data-tgt", tname).on("mouseover", function() { d3.select(this).attr("opacity",1); }).on("mouseout", function() { d3.select(this).attr("opacity",0.7); });});// loops (external arcs)const loopGroup = svg.append("g").attr("class","loops");LOOPS.forEach(lp => {const a = lp[0], c = lp[1], b = lp[2];if (!(a in nameToIndex) || !(b in nameToIndex)) return;1 unmodified lineconst sb = nodePos[nameToIndex[b]];const mx = (sa.x + sb.x)/2;const my = (sa.y + sb.y)/2;const len = Math.sqrt((sa.x - sb.x)*(sa.x - sb.x) + (sa.y - sb.y)*(sa.y - sb.y));const outward = Math.max(40, radius*0.28 + len * 0.12);const ndx = mx, ndy = my;const nlen = Math.sqrt(ndx*ndx + ndy*ndy) || 1;const ux = ndx/nlen, uy = ndy/nlen;const cx = mx + ux * outward;const cy = my + uy * outward;const path = `M ${sa.x} ${sa.y} Q ${cx} ${cy} ${sb.x} ${sb.y}`;loopGroup.append("path").attr("d", path).attr("fill", "none").attr("stroke", "#227a6d").attr("stroke-width", 2.8).attr("opacity",0.95).on("mouseover", function() { d3.select(this).attr("opacity",1); }).on("mouseout", function() { d3.select(this).attr("opacity",0.95); });});// interactivity: focus on nodefunction setOpacityFor(nodeName) {group.selectAll("circle").style("opacity", d => (d.name === nodeName ? 1.0 : 0.18));group.selectAll("text").style("opacity", d => (d.name === nodeName ? 1.0 : 0.28));buyGroup.selectAll("path").style("opacity", function() {const src = this.getAttribute("data-src");const tgt = this.getAttribute("data-tgt");return (src === nodeName || tgt === nodeName) ? 0.98 : 0.06;});sellGroup.selectAll("path").style("opacity", function() {const src = this.getAttribute("data-src");const tgt = this.getAttribute("data-tgt");return (src === nodeName || tgt === nodeName) ? 0.98 : 0.06;});transferGroup.selectAll("path").style("opacity", function() {const src = this.getAttribute("data-src");const tgt = this.getAttribute("data-tgt");return (src === nodeName || tgt === nodeName) ? 0.98 : 0.06;});loopGroup.selectAll("path").style("opacity", 0.95);}function resetOpacity() {group.selectAll("circle").style("opacity", 1.0);group.selectAll("text").style("opacity", 1.0);buyGroup.selectAll("path").style("opacity", 0.92);sellGroup.selectAll("path").style("opacity", 0.86);transferGroup.selectAll("path").style("opacity", 0.7);loopGroup.selectAll("path").style("opacity", 0.95);const box = document.getElementById("info-box");if (box) box.innerHTML = "<b>Click a node</b> to view details here.";}// get connections for info boxfunction getConnections(nodeName) {let buys = BUYS.filter(x => x[0] === nodeName || x[1] === nodeName).map(x => (x[0] === nodeName ? x[1] : x[0]));let sells = SELLS.filter(x => x[0] === nodeName || x[1] === nodeName).map(x => (x[0] === nodeName ? x[1] : x[0]));let transfers = TRANSFERS.filter(x => x[0] === nodeName || x[1] === nodeName).map(x => (x[0] === nodeName ? x[1] : x[0]));let loops = LOOPS.filter(x => x[0] === nodeName || x[2] === nodeName).map(x => (x[0] === nodeName ? x[2] : x[0]));// unique and sortedfunction uniq(arr){ return Array.from(new Set(arr)).sort(); }return {buys: uniq(buys), sells: uniq(sells), transfers: uniq(transfers), loops: uniq(loops)};}function showInfo(nodeName) {const box = document.getElementById("info-box");const con = getConnections(nodeName);box.innerHTML = `<div style="font-size:14px;"><b>${nodeName}</b></div><div style="height:6px"></div><div style="font-size:12px"><b>Connected nodes</b></div><div style="margin-top:6px; font-size:12px;"><div><b>Buys:</b> ${con.buys.length ? con.buys.join(", ") : "<span style='color:#666'>None</span>"}</div><div><b>Sells:</b> ${con.sells.length ? con.sells.join(", ") : "<span style='color:#666'>None</span>"}</div><div><b>Transfers:</b> ${con.transfers.length ? con.transfers.join(", ") : "<span style='color:#666'>None</span>"}</div><div><b>Loops:</b> ${con.loops.length ? con.loops.join(", ") : "<span style='color:#666'>None</span>"}</div></div>`;}// click handlersgroup.selectAll("circle").style("cursor","pointer").on("click", function(e,d) {setOpacityFor(d.name);showInfo(d.name);if (e && e.stopPropagation) e.stopPropagation();});group.selectAll("text").style("cursor","pointer").on("click", function(e,d) {setOpacityFor(d.name);showInfo(d.name);if (e && e.stopPropagation) e.stopPropagation();});document.getElementById("arc-reset").onclick = resetOpacity;svg.on("click", function(event) {if (event.target.tagName === "svg") resetOpacity();});}210 unmodified lines2112122132142152162172182192202212226 unmodified lines2292302312322332344 unmodified lines23924024124224324424512 unmodified lines2582592602612622632642652662672682 unmodified lines2712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323331 unmodified line335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437210 unmodified linesconst radius = Math.min(w, h) * 0.36;// nodes with abbrevconst n = NODES.length;function angleFor(i) { return (i / n) * 2 * Math.PI; }const nodePos = NODES.map((name,i) => {const ang = angleFor(i) - Math.PI/2;const ab = (name.length > 7 ? name.slice(0,5)+"…" : name);return {name: name,abbrev: ab,6 unmodified linesconst nameToIndex = {};NODES.forEach((nm,i)=> nameToIndex[nm]=i);const group = svg.append("g").selectAll("g").data(nodePos).enter().append("g").attr("transform", d => `translate(${d.x},${d.y})`);4 unmodified lines.style("stroke-width", 1).style("cursor", "pointer");// short label on loadgroup.append("text").attr("x", d => Math.cos(d.angle) * (radius + 26)).attr("y", d => Math.sin(d.angle) * (radius + 26))12 unmodified lines.style("cursor","pointer").text(d => d.abbrev);// helper curvesfunction bezierPath(x0,y0,x1,y1,above=true) {const mx = (x0 + x1)/2;const my = (y0 + y1)/2;const dx = mx, dy = my;const len = Math.sqrt(dx*dx + dy*dy) || 1;const ux = dx/len, uy = dy/len;const offset = (above ? -1 : 1) * Math.max(30, radius*0.9);2 unmodified linesreturn `M ${x0} ${y0} Q ${cx} ${cy} ${x1} ${y1}`;}const allW = [].concat(BUYS.map(d=>d[2]), SELLS.map(d=>d[2]), TRANSFERS.map(d=>d[2]));const wmin = Math.min(...(allW.length?allW:[1]));const wmax = Math.max(...(allW.length?allW:[1]));const stroke = d3.scaleLinear().domain([wmin, Math.max(wmax,1)]).range([1.0, 6.0]);// groupsconst buyGroup = svg.append("g");BUYS.forEach(b => {const a = b[0], c = b[1], wt = b[2];if (!(a in nameToIndex) || !(c in nameToIndex)) return;const s = nodePos[nameToIndex[a]];const t = nodePos[nameToIndex[c]];buyGroup.append("path").attr("d", bezierPath(s.x,s.y,t.x,t.y,true)).attr("fill","none").attr("stroke","#2e8540").attr("stroke-width", stroke(wt)).attr("opacity", 0.92).attr("data-src", a).attr("data-tgt", c);});const sellGroup = svg.append("g");SELLS.forEach(s => {const c = s[0], a = s[1], wt = s[2];if (!(c in nameToIndex) || !(a in nameToIndex)) return;const sp = nodePos[nameToIndex[c]];const tp = nodePos[nameToIndex[a]];sellGroup.append("path").attr("d", bezierPath(sp.x,sp.y,tp.x,tp.y,false)).attr("fill","none").attr("stroke","#c0392b").attr("stroke-width", stroke(wt)).attr("stroke-dasharray","4,3").attr("opacity",0.86).attr("data-src", c).attr("data-tgt", a);});const transferGroup = svg.append("g");TRANSFERS.forEach(tr => {const sname = tr[0], tname = tr[1], wt = tr[2];if (!(sname in nameToIndex) || !(tname in nameToIndex)) return;const sp = nodePos[nameToIndex[sname]];const tp = nodePos[nameToIndex[tname]];const mx = (sp.x + tp.x)/2, my = (sp.y + tp.y)/2;transferGroup.append("path").attr("d", `M ${sp.x} ${sp.y} Q ${mx*0.3} ${my*0.3} ${tp.x} ${tp.y}`).attr("fill","none").attr("stroke","#7d7d7d").attr("stroke-width", stroke(wt)).attr("opacity",0.7).attr("data-src", sname).attr("data-tgt", tname);});const loopGroup = svg.append("g");LOOPS.forEach(lp => {const a = lp[0], c = lp[1], b = lp[2];if (!(a in nameToIndex) || !(b in nameToIndex)) return;1 unmodified lineconst sb = nodePos[nameToIndex[b]];const mx = (sa.x + sb.x)/2;const my = (sa.y + sb.y)/2;const len = Math.sqrt((sa.x-sb.x)**2 + (sa.y-sb.y)**2);const outward = Math.max(40, radius*0.28 + len*0.12);const nlen = Math.sqrt(mx*mx + my*my)||1;const ux = mx/nlen, uy = my/nlen;const cx = mx + ux*outward, cy = my + uy*outward;loopGroup.append("path").attr("d", `M ${sa.x} ${sa.y} Q ${cx} ${cy} ${sb.x} ${sb.y}`).attr("fill","none").attr("stroke","#227a6d").attr("stroke-width", 2.8).attr("opacity",0.95);});// opacity controlfunction setOpacityFor(nodeName) {group.selectAll("circle").style("opacity", d => d.name===nodeName ? 1 : 0.18);group.selectAll("text").style("opacity", d => d.name===nodeName ? 1 : 0.28);function isConn(path) {const src = path.getAttribute("data-src");const tgt = path.getAttribute("data-tgt");return (src===nodeName || tgt===nodeName);}buyGroup.selectAll("path").style("opacity", function(){ return isConn(this)?0.98:0.06; });sellGroup.selectAll("path").style("opacity", function(){ return isConn(this)?0.98:0.06; });transferGroup.selectAll("path").style("opacity", function(){ return isConn(this)?0.98:0.06; });}function resetOpacity() {group.selectAll("circle").style("opacity",1);group.selectAll("text").style("opacity",1);buyGroup.selectAll("path").style("opacity",0.92);sellGroup.selectAll("path").style("opacity",0.86);transferGroup.selectAll("path").style("opacity",0.7);loopGroup.selectAll("path").style("opacity",0.95);updateLabels(null, new Set());document.getElementById("info-box").innerHTML = "<b>Click a node</b> to view details here.";}// detect connections for label switchingfunction getConnections(nodeName) {let buys = BUYS.filter(x => x[0]===nodeName || x[1]===nodeName).map(x => (x[0]===nodeName ? x[1] : x[0]));let sells = SELLS.filter(x => x[0]===nodeName || x[1]===nodeName).map(x => (x[0]===nodeName ? x[1] : x[0]));let transfers = TRANSFERS.filter(x => x[0]===nodeName || x[1]===nodeName).map(x => (x[0]===nodeName ? x[1] : x[0]));let loops = LOOPS.filter(x => x[0]===nodeName || x[2]===nodeName).map(x => (x[0]===nodeName ? x[2] : x[0]));return new Set([].concat(buys, sells, transfers, loops));}// dynamic label switchingfunction updateLabels(selected, connectedSet) {group.selectAll("text").text(d => {if (selected && (d.name === selected || connectedSet.has(d.name))) {return d.name; // show full name}return d.abbrev; // otherwise short});}// info box updatesfunction showInfo(nodeName) {const con = getConnections(nodeName);const box = document.getElementById("info-box");box.innerHTML = `<div style="font-size:14px;"><b>${nodeName}</b></div><div style="margin-top:6px; font-size:12px;"><b>Connected nodes</b></div><div style="margin-top:6px; font-size:12px;"><div><b>Buys:</b> ${[...con].join(", ") || "<span style='color:#777'>None</span>"}</div></div>`;}// click handlersfunction selectNode(d) {const nodeName = d.name;setOpacityFor(nodeName);showInfo(nodeName);const connected = getConnections(nodeName);updateLabels(nodeName, connected);}group.selectAll("circle").on("click", function(e,d){selectNode(d);if (e.stopPropagation) e.stopPropagation();});group.selectAll("text").on("click", function(e,d){selectNode(d);if (e.stopPropagation) e.stopPropagation();});document.getElementById("arc-reset").onclick = resetOpacity;svg.on("click", function(event){if (event.target.tagName === "svg") resetOpacity();});}
app.py+91-107