Spaces:
Sleeping
Sleeping
Commit ·
519acb8
1
Parent(s): 568e71b
created first draft of frontend
Browse filesFiles changed (4)
- src/jellyscope/cli.pyAdded+35
- src/jellyscope/web/static/app.jsAdded+187
- src/jellyscope/web/templates/index.html+1-1
- tests/test_cli.pyAdded+46
1234567891011121314151617181920212223242526272829303132333435"""Command-line entry point for Jellyscope."""
import argparsefrom pathlib import Path
import uvicorn
from jellyscope.config import JellyscopeConfigfrom jellyscope.web import create_app
def main() -> None: parser = argparse.ArgumentParser(description="Jellyscope: JWST Jellyfish Galaxy Explorer") parser.add_argument("--host", default="127.0.0.1", help="Server host (default: 127.0.0.1)") parser.add_argument("--port", type=int, default=5000, help="Server port (default: 5000)") parser.add_argument( "--data-dir", default="data", help="Path to data directory (default: data)" ) parser.add_argument("--no-debug", action="store_true", help="Disable debug mode") args = parser.parse_args()
config = JellyscopeConfig( data_dir=Path(args.data_dir), host=args.host, port=args.port, debug=not args.no_debug, ) app = create_app(config) print(f"Starting Jellyscope on http://{config.host}:{config.port}") print(f"Data directory: {config.data_dir.resolve()}") uvicorn.run(app, host=config.host, port=config.port, log_level="info")
if __name__ == "__main__": main()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187/** * Jellyscope — Frontend controller * Manages state, API calls, Plotly rendering, and user interactions. */const state = { datacube: "nircam", channel: 7, // F200W default selectedClumps: new Set(), colorscale: "Viridis", dragmode: "pan", clumps: [],};
const plotlyConfig = { responsive: true, displayModeBar: true, modeBarButtonsToRemove: ["toImage", "sendDataToCloud"], scrollZoom: true,};
// Initialization.
async function init() { await loadClumpList(); await renderViewer(); setupEventListeners();}
function setupEventListeners() { document.getElementById("datacube-select").addEventListener("change", (e) => { state.datacube = e.target.value; renderViewer(); });
document.getElementById("filter-slider").addEventListener("input", (e) => { state.channel = parseInt(e.target.value); document.getElementById("filter-label").textContent = FILTERS[state.channel]; renderViewer(); });
document.getElementById("colorscale-select").addEventListener("change", (e) => { state.colorscale = e.target.value; renderViewer(); });
document.querySelectorAll(".mode-btn").forEach((btn) => { btn.addEventListener("click", () => { document.querySelectorAll(".mode-btn").forEach((b) => b.classList.remove("active")); btn.classList.add("active"); const mode = btn.dataset.mode; state.dragmode = mode; const viewer = document.getElementById("galaxy-viewer"); Plotly.relayout(viewer, {dragmode: mode}); }); });
document.getElementById("clump-filter").addEventListener("change", loadClumpList);}
// Clump List.async function loadClumpList() { const filter = document.getElementById("clump-filter"); let url = "/api/clumps"; let html = ""; if (filter.value) url += `?component=${filter.value}`;
const resp = await fetch(url); const data = await resp.json(); state.clumps = data.clumps; renderClumpList();}
function renderClumpList() { const container = document.getElementById("clump-list"); let html = ""; for (const c of state.clumps) { const isSelected = state.selectedClumps.has(c.clump_id); const clumpBadge = c.component == "disk" ? "disk" : "outside"; html += ` <div class="clump-item ${isSelected ? "selected" : ""}" onclick="toggleClumpSelection(${c.clump_id})"> <input type="checkbox" ${isSelected ? "checked" : ""} onclick="event.stopPropagation(); toggleClumpSelection(${c.clump_id})"> <span>Clump ${c.clump_id}</span> <span class="clump-badge ${clumpBadge}">${c.component}</span> <span style="color: var(--text-secondary); font-size: 10px; margin-left: auto;"> ${c.area_pix}px </span> </div>`; } container.innerHTML = html;}
function updateClumpListUI() { const clumps = document.querySelectorAll(".clump-item"); clumps.forEach((clump) => { const checkbox = clump.querySelector("input[type=checkbox]"); const clumpId = parseInt(clump.querySelector("span").textContent.replace("Clump ", "")); const isSelected = state.selectedClumps.has(clumpId); clump.classList.toggle("selected", isSelected); if (checkbox) checkbox.checked = isSelected; });}
// Galaxy Viewer.async function renderViewer() { const selectedStr = Array.from(state.selectedClumps).join(","); const url = `/api/viewer/${state.datacube}/${state.channel}?selected=${selectedStr}&colorscale=${state.colorscale}`; const resp = await fetch(url); const data = await resp.json(); const fig = data.figure;
fig.layout.dragmode = state.dragmode;
const viewer = document.getElementById("galaxy-viewer"); await Plotly.react(viewer, fig.data, fig.layout, plotlyConfig);
// Attach click handler viewer.removeAllListeners?.("plotly_click"); viewer.on("plotly_click", onViewerClick);}
async function onViewerClick(eventData) { if (!eventData.points || eventData.points.length == 0) return; const point = eventData.points[0]; const x = Math.round(point.x); const y = Math.round(point.y);
// Check if pixel belongs to a clump. const resp = await fetch(`/api/pixel/${x}/${y}/clump`); const data = await resp.json();
if (data.clump_id !== null) { toggleClumpSelection(data.clump_id); }}
async function onViewerSelected(eventData) { if (!eventData.points || eventData.points.length == 0) return;
const pixels = eventData.points .filter((p) => p.curveNumber == 0) // Only heatmap points. .map((p) => [Math.round(p.x), Math.round(p.y)]);
if(pixels.length === 0) return;}
// Clump selection.function toggleClumpSelection(clumpId) { if(state.selectedClumps.has(clumpId)) { state.selectedClumps.delete(clumpId); } else { state.selectedClumps.add(clumpId); }
updateClumpListUI(); renderViewer();
const selected = Array.from(state.selectedClumps); if (selected.length === 0) { clearPanels(); } else if (selected.length === 1) { showClumpDetails(selected[0]); }}
async function showClumpDetails(clumpId) { const propsResp = await fetch(`/api/clumps/${clumpId}`)
const propsData = await propsResp.json(); const props = propsData.properties; let html = '<table class="prop-table">'; for (const [key, val] of Object.entries(props)) { html += `<tr><td>${key}</td><td>${val}</td></tr>`; } html += "</table>"; document.getElementById("properties-content").innerHTML = html;}
// Helpersfunction clearPanels() { document.getElementById("properties-content").innerHTML = '<p class="placeholder">Click on a clump to see its properties</p>';}
// Startdocument.addEventListener("DOMContentLoaded", init);72 unmodified lines7374757677787972 unmodified lines</main><script>const FILTERS = {{ filters | tojson }};</script><script src="{{ url_for('static', path='app.js') }}"></script></body>72 unmodified lines7374757677787972 unmodified lines</main><script>const FILTERS = {{ filters | tojson | safe }};</script><script src="{{ url_for('static', path='app.js') }}"></script></body>
12345678910111213141516171819202122232425262728293031323334353637383940414243444546"""Tests for CLI entry point."""
from unittest.mock import patch
from jellyscope.cli import main
def test_main_default_args(): with ( patch("sys.argv", ["jellyscope", "--data-dir", "data"]), patch("jellyscope.cli.create_app") as mock_create_app, patch("jellyscope.cli.uvicorn") as mock_uvicorn, ): main() mock_create_app.assert_called_once() config = mock_create_app.call_args[0][0] assert config.host == "127.0.0.1" assert config.port == 5000 assert config.debug is True mock_uvicorn.run.assert_called_once()
def test_main_custom_args(): with ( patch( "sys.argv", [ "jellyscope", "--host", "0.0.0.0", "--port", "8080", "--no-debug", "--data-dir", "data", ], ), patch("jellyscope.cli.create_app") as mock_create_app, patch("jellyscope.cli.uvicorn") as mock_uvicorn, ): main() config = mock_create_app.call_args[0][0] assert config.host == "0.0.0.0" assert config.port == 8080 assert config.debug is False mock_uvicorn.run.assert_called_once()src/jellyscope/cli.pyAdded+35
src/jellyscope/web/static/app.jsAdded+187
src/jellyscope/web/templates/index.html+1-1
tests/test_cli.pyAdded+46