Skip to content

Phase 2: 描画経路のコード生成化と、Pythonコードタブの刷新 - #2

Merged
mioupa merged 12 commits into
mainfrom
phase2-codegen
Oct 7, 2026
Merged

mioupa merged 12 commits into
mainfrom
phase2-codegen

Conversation

@mioupa

@mioupa mioupa commented Oct 7, 2026

Copy link
Copy Markdown
Owner

要件定義書9章の Phase 2(P1–P7、A8)を実装しました。Phase 1 で残した2件(複数系列の棒グラフでカテゴリ名が消える、カスタムコードのエラーが英語のまま出る)も直しています。

変更内容

描画経路の一本化(P1、P7)

  • GUI の描画を「設定 → dataprep.plan_plot(検査・警告)→ codegen.generate_script(スクリプト生成)→ runner(実行)→ PNG」の1経路にした。plotting.py は削除。
  • 新規モジュール: py/mplgui/dataprep.py、codegen.py、formats.py(保存形式の表とファイル名の規則。循環 import を避けるため)。
  • 生成するスクリプトは pandas / matplotlib / numpy だけを使う。構成はヘッダ(import、日本語フォント)→ 読込 → 描画に使う行 → 図と軸 → 系列 → 軸 → グリッドと凡例 → 余白 → 保存と表示(fig.savefig + plt.show())。列は df.iloc[:, N] で参照し、列名はコメントに書く。
  • 自動描画では、読込部と出力部を空行にした版(行番号は表示と同じ)に、読込済みの df を渡して実行する(P5)。
  • 列名・ラベルなどデータ由来の文字列は、すべて literal()(repr)と comment_text() を通す。CSV の列名を細工したコード注入を防ぐ。

Pythonコードタブ(P2–P6、A8)

  • タブ名を「Pythonコード」にした。GUI と同期中は生成したスクリプトを表示し、描画と同じ世代で更新する。
  • 「コピー」「.py で保存」を追加。
  • 「編集」で編集モードに入る。編集中は GUI の変更でコードも図も変えず、「GUI と同期していません」と表示する。「GUI から再生成」で同期に戻る。Ctrl/Cmd+Enter で実行できる。
  • 編集モードでは、Pyodide の作業フォルダに元のファイル名で置いたファイルを、スクリプトの読込部がそのまま読む(P5)。
  • 実行結果欄に print の出力と、行番号・該当行付きのトレースバックを表示する。ステータスは日本語(例:「11行目、NameError: 定義されていない名前を使っています」)(P6)。
  • 編集したコードに GUI の設定を上書き適用しない(A8)。編集中の図の「保存」は、編集したコードの図を、保存設定だけを使って書き出す。

その他

  • ファイル選択欄が、選択後も「選択されていません」と表示されていたのを直した(選んだファイル名を欄の中に表示する)。
  • スマホ幅(375 px)でタブバーがはみ出し、横スクロールが出ていたのを直した。

Phase 1 との PNG 比較(27通り、dpi 100)

21通りはバイト単位で一致。差が出た6通りは、すべて棒グラフのカテゴリ名(Phase 1 は set_xscale が目盛を消し、0, 1, 2… を表示していた)。

ケース 結果
折れ線・散布図・1系列の棒(数値の X)、第2軸、対数軸、範囲、グリッド、凡例、余白、除外行、Y 自動、変換できない値の警告、同名列など 一致
複数系列の棒(4通り) カテゴリ名が出るようになった(修正)
1系列の棒で X が文字列(2通り) 同じ原因で、カテゴリ名が出るようになった(修正。オーナー確認済み)

保存経路(dpi 120)の PNG・透過 PNG・JPG・PDF も一致。SVG は matplotlib のランダムな ID の差だけ(ID をそろえると一致)。

計測値(docs/PERFORMANCE.md「Phase 2 後」)

項目(ms、中央値 / 最小 / 最大) Phase 1 後 Phase 2 後 比較
コールド起動 3,650 / 3,593 / 6,581 3,607 / 3,578 / 6,407 同等
ウォーム起動 3,462 / 3,432 / 3,469 3,452 / 3,440 / 3,458 同等
初回ファイル読込 → 最初のプロット 186 / 184 / 187 178 / 175 / 181 良い(-4%)
設定変更 → プロット更新(デバウンス込み) 474 / 469 / 476 466 / 460 / 477 同等
保存(PNG) 257 / 246 / 262 246 / 211 / 253 同等
初回 .xlsx 読込 → 最初のプロット 336 / 335 / 339 329 / 327 / 332 良い(-2%)
編集モードでの実行(新規) – 438 / 431 / 464 –

10,000行×5系列で、設定変更から再描画まで 466 ms(デバウンス込み)。1秒以内の要件を満たす。悪くなった項目はない。

オーナーが決めたこと(2026-10-08。AGENTS.md §13 に記録)

  • 旧 beta タブの名前空間(get_plot_data、旧形式の settings dict、plotted_count)は互換を残さず廃止。
  • スクリプトの末尾は fig.savefig(...)(GUI の「保存」と同じ形式・解像度)と plt.show()。
  • 編集は「編集」ボタンで始める。useCustomCode は削除。
  • 編集中に GUI を変えても、コードも図も変えない。
  • 1系列の棒グラフで X が文字列の場合も、カテゴリ名を出す(修正)。
  • 折れ線・散布図で X が数値でない列のときは、Phase 1 と同じ見た目を保つ(生成コードに ax.set_xscale("linear") を入れる)。Phase 4 の D5(日時軸)で見直す。

要件定義書との差(未対応。判断待ち)

  • 4.1 のファイル構成案に、dataprep.py と formats.py がない。
  • P3 の「前置き行数」は Phase 4(D3)で入る設定のため、読込部には skiprows= を足す場所をコメントで示すだけにした。

テスト

  • 単体テスト: 399 passed(codegen、dataprep、runner、API、設定など。生成コードの実行と artist の確認、コード注入の防止、全フィクスチャで生成した読込部が loader と同じ DataFrame を作ることを含む)
  • E2E: 41 passed(Pythonコードタブ11件を追加。E2E_BASE_PATH=/matplotlib_gui/ でも 41 passed)
  • 生成コードの一致確認(要件定義書7章): ダウンロードした .py をデータと同じフォルダに置いて CPython で実行し、ブラウザの図と要素(軸の数、系列数、ラベル、凡例、目盛のラベル、スケール、軸範囲)が一致することを3通りで確認

🤖 Generated with Claude Code

mioupa and others added 12 commits October 8, 2026 01:57
dataprep.plan_plot decides everything that needs the DataFrame, codegen turns
settings into a readable matplotlib script, and the loader now keeps the
encoding, separator and sheet name for the script's load section.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Both the preview and the saved image now run the script codegen produces
(runner.run_script), the Python API returns the script, output and a figure
summary, and edit-mode scripts run against the uploaded file in the work
folder. The legacy custom-code namespace and GUI overrides are removed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…e API

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add an edit-mode run row to the benchmark and the Phase 2 results.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mioupa
mioupa merged commit 6c43aab into main Oct 7, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant