feat(run): add Flask and FastAPI run configurations (#6647)

Flask and FastAPI apps could only run as ordinary scripts. Adds Flask server and FastAPI run configuration types: Flask runs python -m flask run with FLASK_APP / FLASK_DEBUG (read by every Flask version), host, port and extra options; FastAPI runs python -m uvicorn <module:app> with reload, host, port and options. Both use the Python interpreter choice and debug with debugpy; under the debugger Flask runs with --no-debugger, and without subprocess attach the reloader is turned off so breakpoints still stop.

A top-level (or src/) app.py, wsgi.py, main.py or app/main.py assigning Flask(...) or FastAPI(...) to a module-level name gets a detected configuration. The list of Python run kinds is now one function (ide_run::is_python_kind) instead of copies in the host, Docker, process and SDK code.

Closes #6532

Co-authored-by: wildcatDownstairs <[email protected]>
Цей коміт міститься в:
LU XIANGRUIтаwildcatDownstairs authored and GitHub committed 2026-11-29 13:25:10 +08:00
1 джерело 9ee8f67e9f
коміт e30ae3f6b8
21 files changed
+513 -23

No files matched your search

+27
Переглянути файл
@@ -1,6 +1,7 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import type { RunConfig } from "@/platform/ipc";
import { usePlatform } from "@/platform/platformInfo";
import { useProject } from "@/platform/project";
import { useFlutterDevices } from "@/workbench/flutterDevices";
import { isJinjaTemplate } from "../breakpoints/languages";
import { debugUnsupportedReason, launchConfigFor, withEnvFiles } from "../launch";
@@ -249,6 +250,32 @@ describe("run config → launch config", () => {
expect(asgi.jinja).toBeUndefined();
});
it("debugpy: Flask without the in-browser debugger, and no reloader unless subprocesses are attached", () => {
const debug = (kind: string, args: string[]) =>
launchConfigFor(rc({ kind, command: "python3", args, debugAdapter: "debugpy" }), "/p")!.args;
expect(debug("flask", ["-m", "flask", "run", "--port", "5001"])).toEqual([
"run",
"--no-debugger",
"--port",
"5001",
]);
expect(debug("python", ["-m", "flask", "--app", "app", "run", "--no-debugger"])).toEqual([
"--app",
"app",
"run",
"--no-debugger",
]);
expect(debug("fastapi", ["-m", "uvicorn", "main:app", "--reload"])).toEqual(["main:app", "--reload"]);
const settings = useProject.getState().settings;
try {
useProject.setState({ settings: { ...settings, debugger: { python: { attachSubprocess: false } } } as never });
expect(debug("flask", ["-m", "flask", "run"])).toEqual(["run", "--no-debugger", "--no-reload"]);
expect(debug("fastapi", ["-m", "uvicorn", "main:app", "--reload"])).toEqual(["main:app"]);
} finally {
useProject.setState({ settings });
}
});
it("Jinja2 templates: their extensions anywhere, markup or text in a templates folder", () => {
for (const p of [
"/p/mail/welcome.j2",
+10
Переглянути файл
@@ -163,6 +163,16 @@ function debugpyLaunch(rc: RunConfig, cwd: string, templates: TemplateBreakpoint
out.args = args.slice(1);
if (args[0].endsWith("manage.py")) extra.django = true;
}
// Werkzeug's in-browser debugger would catch exceptions before debugpy does. A reloader serves requests from
// a child process, which stops at breakpoints only when subprocesses are attached: without that, no reload.
const served = out.args ?? [];
const run = served.indexOf("run");
if (extra.module === "flask" && run >= 0) {
const off = ["--no-debugger", ...(dbg.python.attachSubprocess ? [] : ["--no-reload"])];
out.args = [...served.slice(0, run + 1), ...off.filter((a) => !served.includes(a)), ...served.slice(run + 1)];
} else if (extra.module === "uvicorn" && !dbg.python.attachSubprocess) {
out.args = served.filter((a) => a !== "--reload");
}
// A Django Template's breakpoint needs `django` however Django starts (uvicorn, gunicorn, pytest-django).
if (extra.module === "django" || templates.django) extra.django = true;
// Jinja2 templates' breakpoints: Flask, or one already in a template (`python app.py`). Not on for every