在本教程中,我們將使用FastAPI 建立一個基本的部落格應用程式作為後端,使用HTML 和CSS 作為前端,以及 JSON 文件,用於執行基本的CRUD(建立、讀取、更新、刪除)操作。
FastAPI 是一個使用 Python 建立 API 的現代 Web 框架,以其簡單、速度和內建的非同步操作支援而聞名。
下面的實作看起來像這樣:
先決條件
開始之前,請確保您已安裝以下軟體:
- Python 3.7+
- FastAPI
- Uvicorn(用於運行 FastAPI 應用程式)
要安裝FastAPI和Uvicorn,您可以使用pip:
pip install fastapi uvicorn python-multipart
專案結構
專案的架構如下:
/blog_app ├── static │ └── style.css ├── templates │ ├── index.html │ ├── post.html │ ├── create_post.html ├── blog.json ├── main.py
第 1 步:設定 FastAPI
建立一個 main.py 文件,其中將包含 FastAPI 應用程式。
from fastapi import FastAPI, Request, Form from fastapi.responses import HTMLResponse, RedirectResponse from fastapi.staticfiles import StaticFiles from fastapi.templating import Jinja2Templates import json import os app = FastAPI() app.mount("/static", StaticFiles(directory="static"), name="static") templates = Jinja2Templates(directory="templates") # Load or initialize blog data BLOG_FILE = "blog.json" if not os.path.exists(BLOG_FILE): with open(BLOG_FILE, "w") as f: json.dump([], f) def read_blog_data(): with open(BLOG_FILE, "r") as f: return json.load(f) def write_blog_data(data): with open(BLOG_FILE, "w") as f: json.dump(data, f) @app.get("/", response_class=HTMLResponse) async def home(request: Request): blogs = read_blog_data() return templates.TemplateResponse("index.html", {"request": request, "blogs": blogs}) @app.get("/post/{post_id}", response_class=HTMLResponse) async def read_post(request: Request, post_id: int): blogs = read_blog_data() post = blogs[post_id] if 0 <h2> <strong>第 2 步:設定 HTML 和 CSS</strong> </h2> <p>在範本資料夾中,建立以下 HTML 檔案:</p> <p>index.html<br> 該文件將列出所有部落格文章。 <br> </p> <pre class="brush:php;toolbar:false"> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Blog App</title> <link rel="stylesheet" href="/static/style.css"> <h1 id="Blog-Posts">Blog Posts</h1> <a href="/create_post">Create New Post</a> <div> {% for post in blogs %} <div class="post"> <h2 id="post-title">{{ post.title }}</h2> <p>{{ post.content[:100] }}...</p> <a href="/post/%7B%7B%20loop.index0%20%7D%7D">Read more</a> <form method="post" action="/delete_post/%7B%7B%20loop.index0%20%7D%7D"> <button type="submit">Delete</button> </form> </div> {% endfor %} </div>
post.html
此文件將顯示部落格文章的完整內容。
<meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>{{ post.title }}</title> <link rel="stylesheet" href="/static/style.css"> <h1 id="post-title">{{ post.title }}</h1> <p>{{ post.content }}</p> <a href="/">Back to Home</a>
create_post.html
該文件將包含用於建立新帖子的表單。
<meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Create a New Post</title> <link rel="stylesheet" href="/static/style.css"> <h1 id="Create-a-New-Post">Create a New Post</h1>Back to Home
第 3 步:使用 CSS 設計樣式
在 static 資料夾中,建立一個 style.css 檔案以添加一些基本樣式。
body { font-family: Arial, sans-serif; padding: 20px; background-color: #f0f0f0; } h1 { color: #333; } a { text-decoration: none; color: #0066cc; } .post { background-color: #fff; padding: 10px; margin-bottom: 15px; border-radius: 5px; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); } button { background-color: #ff4d4d; border: none; padding: 5px 10px; color: white; border-radius: 3px; cursor: pointer; } button:hover { background-color: #ff1a1a; } input, textarea { width: 100%; padding: 8px; margin-bottom: 10px; }
第 4 步:執行應用程式
現在一切都已設定完畢,請使用 Uvicorn 執行 FastAPI 應用程式。
uvicorn main:app --reload
在瀏覽器中造訪http://127.0.0.1:8000,您應該會看到部落格主頁。
作為作業,您可以使用資料庫 ?️ 而不僅僅是 JSON 來建立全端 Web 應用程式。
使用資料庫,您可以添加更多功能、提高效能並增強整體 UI/UX。以獲得更豐富的使用者體驗。
這就是本部落格的全部內容了!請繼續關注更多更新並繼續建立令人驚嘆的應用程式! ?✨
以上是使用 FastAPI、HTML、CSS 和 JSON 建立簡單的部落格應用程式的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Arraysinpython,尤其是Vianumpy,ArecrucialInsCientificComputingfortheireftheireffertheireffertheirefferthe.1)Heasuedfornumerericalicerationalation,dataAnalysis和Machinelearning.2)Numpy'Simpy'Simpy'simplementIncressionSressirestrionsfasteroperoperoperationspasterationspasterationspasterationspasterationspasterationsthanpythonlists.3)inthanypythonlists.3)andAreseNableAblequick

你可以通過使用pyenv、venv和Anaconda來管理不同的Python版本。 1)使用pyenv管理多個Python版本:安裝pyenv,設置全局和本地版本。 2)使用venv創建虛擬環境以隔離項目依賴。 3)使用Anaconda管理數據科學項目中的Python版本。 4)保留系統Python用於系統級任務。通過這些工具和策略,你可以有效地管理不同版本的Python,確保項目順利運行。

numpyarrayshaveseveraladagesoverandastardandpythonarrays:1)基於基於duetoc的iMplation,2)2)他們的aremoremoremorymorymoremorymoremorymoremorymoremoremory,尤其是WithlargedAtasets和3)效率化,效率化,矢量化函數函數函數函數構成和穩定性構成和穩定性的操作,製造

數組的同質性對性能的影響是雙重的:1)同質性允許編譯器優化內存訪問,提高性能;2)但限制了類型多樣性,可能導致效率低下。總之,選擇合適的數據結構至關重要。

到CraftCraftExecutablePythcripts,lollow TheSebestPractices:1)Addashebangline(#!/usr/usr/bin/envpython3)tomakethescriptexecutable.2)setpermissionswithchmodwithchmod xyour_script.3)

numpyArraysareAreBetterFornumericalialoperations andmulti-demensionaldata,而learthearrayModuleSutableforbasic,內存效率段

numpyArraySareAreBetterForHeAvyNumericalComputing,而lelethearRayModulesiutable-usemoblemory-connerage-inderabledsswithSimpleDatateTypes.1)NumpyArsofferVerverVerverVerverVersAtility andPerformanceForlargedForlargedAtatasetSetsAtsAndAtasEndCompleXoper.2)

ctypesallowscreatingingangandmanipulatingc-stylarraysinpython.1)usectypestoInterfacewithClibrariesForperfermance.2)createc-stylec-stylec-stylarraysfornumericalcomputations.3)passarraystocfunctions foreforfunctionsforeffortions.however.however,However,HoweverofiousofmemoryManageManiverage,Pressiveo,Pressivero


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Atom編輯器mac版下載
最受歡迎的的開源編輯器

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

禪工作室 13.0.1
強大的PHP整合開發環境