Skip to content

Latest commit

 

History

History
50 lines (36 loc) · 4.49 KB

AGENTS.md

File metadata and controls

50 lines (36 loc) · 4.49 KB

AGENTS.md

What this repo is

plotlyshare is a small Python package that adds a custom plotly renderer (renderer='plotlyshare') so fig.show() uploads the interactive plot to the PlotlyShare web app (https://plotlyshare.pages.dev) and returns a shareable link. The whole library is 3 files in plotlyshare/. Development happens on the v2 branch (main holds the legacy v1 code).

Commands

Package management and builds use uv (not pip directly):

uv build -o dist/$(uv version --short)   # build (see `task build` below)
task build                              # checks dist/<version> doesn't exist + version is free on TestPyPI, then builds
task upload-test                        # publish to TestPyPI (index configured in pyproject.toml)
task upload                             # publish to PyPI
  • Taskfile.yml (taskfile.dev) is the current build entrypoint; the makefile is the legacy v1 equivalent (uses py -m build/twine) — don't update it unless asked.
  • Releases require bumping version in pyproject.toml first. task build fails if dist/<version> exists or the version already exists on TestPyPI.
  • TestPyPI is declared as an explicit = true uv index in pyproject.toml; task upload-test targets it via --index testpypi.
  • There is no test suite, linter, or CI configured. The test CLI subcommand (below) tests the user's credentials, not the code.

CLI subcommands (python -m plotlyshare / uv run -m plotlyshare)

Defined in plotlyshare/__main__.py: setup, test, config, show_config.

  • setup prompts for a CLI_TOKEN (format UID//TOKEN), tests the connection, and writes config.
  • config prints the config and opens it in $EDITOR if set.
  • __main__.py hard-asserts exactly one argument from the allowed list — adding a subcommand means updating allowed_commands.

Architecture / data flow

  1. Importing plotlyshare (plotlyshare/__init__.py) reads ~/.config/plotlyshare/config.json. If setup was done, it registers the renderer globally: pio.renderers['plotlyshare'] = PlotlyShareRenderer(config, {}). If not, it prints an error telling the user to run setup. Note: a bare import plotlyshare with no valid config does nothing beyond that error message — the renderer registration is the import's entire side effect.
  2. fig.show(renderer='plotlyshare') calls PlotlyShareRenderer.render(fig) (plotlyshare/custom_plotly_renderer.py):
    • Serializes via pio.to_json (orjson engine).
    • optimize_template (default true at setup): if the figure's layout.template matches a built-in plotly template, replaces it with the template name to shrink the upload.
    • Generates a random two-word name (wonderwords: adjective + noun) + ordinal date (e.g. "mushy pancake 24th Jul"), unless plot_name was passed to fig.show() (plotly forwards unknown kwargs to the renderer's render; the renderer reads self.plot_name and resets it to None after each render).
    • POSTs to {base_url}/api/upload?user={uid} with a Bearer CLI_TOKEN header, prints the returned link/size via rich, optionally opens the browser.

Gotchas

  • Config location: ~/.config/plotlyshare/config.json (both __init__.py and __main__.py build this path independently; keep them in sync). Keys: setup_done, UID, CLI_TOKEN, open_browser_after_upload, optimize_template.
  • base_url override: the env var PLOTLYSHARE_BASE_URL overrides https://plotlyshare.pages.dev — useful for local dev against a self-hosted frontend.
  • Windows emoji crash: __main__.py reconfigures stdout/stderr to UTF-8 because piped Windows stdout defaults to cp1252, which crashes on emoji output. Preserve this if touching CLI output code.
  • Lazy templates: pio.templates lazy-loads, so you can't naively iterate .items() on it — render() uses pio.templates.keys() + .to_plotly_json() comparison for that reason (noted in a comment).
  • Auth: CLI_TOKEN is sent as Authorization: Bearer <token>; the UID goes in the query string (?user={uid}), both in upload and in the connection test (/api/auth/ping_cli).
  • Style: the codebase uses tabs for indentation, rich for all user-facing CLI output (occasionally mixing plain print for format-control reasons, with an apologetic comment), and type hints only sporadically.

v1 vs v2

v2 (this branch) dropped the Google Drive/OAuth flow of v1 for the token-based backend. v1 artifacts still exist (makefile, plotlyshare.egg-info/, old dist/ versions); ignore them when working on v2.