feat(cli): live local dashboard for monitor - #1365
Open
davidberenstein1957 wants to merge 4 commits into
Open
Conversation
Add `codecarbon monitor --ui`, serving a single self-contained page over a stdlib HTTP server so a run can be watched while it happens, without the carbonboard extra, carbonserver or any network access. Output handlers can now opt into `live_out` on every measurement via `live_out_every_measure`, instead of once per `api_call_interval`. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1365 +/- ##
==========================================
+ Coverage 91.39% 91.58% +0.18%
==========================================
Files 49 50 +1
Lines 5056 5145 +89
==========================================
+ Hits 4621 4712 +91
+ Misses 435 433 -2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Measure codecarbon/viz/live.py instead of omitting it with the legacy Dash dashboard, and cover the paths the new feature added: the --ui/--ui-port/--ui-host wiring (serving and busy-port branches), task_out, out, the non-loopback bind warning, idempotent exit, and the live_out_every_measure handling in the tracker. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
The live chart read cpu/gpu/ram_power off EmissionsData, which holds the average since start() (the _*_power_sum accumulators are never reset), so the 'live power' line was a running mean that ramped up and then barely decayed: measured 8.5 -> 35.7 -> 10.4 W of real power against a monotone 8.5 -> 22.6 -> 20.4 W average over the same idle/busy/idle run. The tracker now hands the handler the last measured power, and handlers opt into the per-measure cadence by defining `on_measure` instead of the `live_out_every_measure` class attribute that widened BaseOutput for one consumer. LiveDashboardOutput leaves `out`/`live_out` as no-ops so the chart has a single feed of comparable samples. Also append the live handler to output_handlers rather than replacing the list. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1348
What it adds
codecarbon monitor --uiserves a live view of the current run onhttp://127.0.0.1:8050: current power, cumulative gCO2eq, energy, elapsed time, a CPU/GPU/RAM power chart over the last samples, the detected hardware, and per-task rows when tasks are used.It is a stdlib
ThreadingHTTPServeron a daemon thread plus one self-contained HTML file. No new dependency, no build step, no database, no CDN, no network access — it works air-gapped and does not need thecarbonboardextra orcarbonserver.User-facing surface
codecarbon monitor --ui [--ui-port 8050] [--ui-host 127.0.0.1], also on the wrapped form (codecarbon monitor --ui -- python train.py).codecarbon.viz.live.LiveDashboardOutput, usable like any other output handler:EmissionsTracker(output_handlers=[LiveDashboardOutput()])./(page),/data(JSON),/health.docs/how-to/visualize.md, options indocs/reference/cli.md.One change outside the new files
live_outwas only called everyapi_call_intervalmeasures (5 minutes with the defaults), which is far too coarse for a live view. Handlers can now setlive_out_every_measure = Trueto be called on every measurement instead, receiving the total withdelta=None— the delta is deliberately not computed there, since_compute_emissions_deltaconsumes state used by the periodic call. Existing handlers are unaffected: the attribute defaults toFalseand is read withgetattr, so duck-typed custom handlers keep working.Verified
uv run pytest tests/test_live_dashboard.py -q— 8 tests: bounded history,/datapayload,/and 404,/health, concurrent read/write returning valid JSON, port released onexit(), busy port stays inert, and the tracker feeding a sample per measure.tests/test_custom_handler.py,tests/test_emissions_tracker.py,tests/cli,tests/test_docs_examples.py,tests/test_package_integrity.py.codecarbon monitor --ui --offline --country-iso-code FRA --measure-power-secs 3, samples appearing every 3 s and the page rendering.black --checkandruff checkscoped to the touched files (remaining ruff hits incli/main.pyare pre-existing).Deliberately left out
emissions.csvhistory — that is carbonboard's job; this only shows the current run.ui,ui_port,ui_host). Flags seem enough for an interactive foreground command; happy to add the config keys if wanted.🤖 Generated with Claude Code