跳转到内容
项目模板·Pro

FastAPI 短链接服务

用 Python、FastAPI 和 SQLite 写的短链接服务。生成短代码、跳转并统计点击,附带单页表单和 pytest 测试。需要 Linux 环境中的 Python 3.10 或更新版本及 pip;会安装 14 个固定版本的包。

安装它需要 PocketCode Pro。

如果你的 Android 设备上装了 PocketCode,这会在应用里打开此页面。

预览

文件

README.md
main.py
pytest.ini
requirements-dev.txt
requirements.txt
shortener/__init__.py
shortener/app.py
shortener/codes.py
shortener/home.py
shortener/store.py
tests/test_links.py

README.md

# Link shortener: FastAPI + SQLite A small web API in Python with FastAPI: paste a long link, get a short code, and `/<code>` sends you on to the long one while it counts the clicks. Links are kept in SQLite (the `sqlite3` module that comes with Python). It has a one-page form at `/`, typed request and response models with Pydantic, input checks, and tests. ## Run it in PocketCode You need the app's Linux environment with Python 3.10 or newer and pip (the Developer pack in Terminal › Linux includes both). 1. Open the terminal in this project's folder (Open Terminal Here, in the editor menu). 2. Install FastAPI and the Uvicorn server (14 packages, versions pinned in `requirements.txt`; needs internet): ```sh python3 -m pip install --break-system-packages -r requirements.txt ``` 3. Start the server: ```sh python3 main.py ``` Or open `main.py` and tap Run (the Run panel should say Python 3 (Linux), since the packages are installed there). 4. Open http://localhost:8000 in the DevBrowser (or in the phone's browser) and shorten a link. The short link opens the long one. Stop it with Ctrl+C in the terminal, or Stop in the Run panel. Links are kept in `data/links.db`. If you prefer a virtual environment: `python3 -m venv .venv`, then `. .venv/bin/activate` and `pip install -r requirements.txt`. ## Try the API From a second terminal tab: ```sh

此预览已截短。安装后可获得完整内容。

1.0.0 的变更

首个版本。

相关

Flask + SQLite 网页应用Pro项目模板用 Flask 写的阅读清单:服务端渲染页面和普通表单,数据存入 SQLite,另有一个小型 JSON API。测试使用 Flask 的测试客户端。需要 Linux 环境中的 Python 和 pip,会安装 7 个固定版本的包。Python + SQLite 命令行工具Pro项目模板终端里的记账工具:添加和列出支出、按月按类别汇总、导出 CSV。只用标准库(argparse、sqlite3),无需用 pip 安装任何东西,并附带 unittest 测试。需要 Linux 环境中的 Python。Python CSV 报表Pro项目模板把销售 CSV 变成终端摘要和带柱状图的 HTML 页面的脚本:按月、类别和产品汇总。只用标准库,附带示例数据和测试。需要 Linux 环境中的 Python。Express + SQLite 任务 APIPro项目模板基于 Express 5 的任务 API,支持筛选、分页并附带测试。数据存入 node:sqlite,无需编译原生模块。需要带 Node.js 22.13 或更新版本的 Linux 环境;npm install 约下载 3.5 MB。无依赖的 Node.js JSON APIPro项目模板基于 node:http 的笔记 API:创建、列出、修改、删除,带输入校验和 JSON 错误信息,数据保存到文件。无需安装任何东西:在 server.js 上点 Run,应用自带的 Node.js 会在 localhost:3000 启动它。附带测试。Next.js + Tailwind + i18nPro项目模板Next.js 15 项目,带 next-intl 和 Tailwind CSS,在 /en 和 /es 提供一个英语和西班牙语页面。深色模式跟随系统。需要带 Node.js 的 Linux 环境;首次 npm install 约下载 400 MB。