Skip to content

Instantly share code, notes, and snippets.

@nmreadelf
Last active July 10, 2025 07:06
Show Gist options
  • Save nmreadelf/5b07d6552568d08f921865f14418c275 to your computer and use it in GitHub Desktop.
Save nmreadelf/5b07d6552568d08f921865f14418c275 to your computer and use it in GitHub Desktop.
run env

first step new direcotry

install pip package

pip install "fastapi[standard]"

copy sample code to main.py

then run sample code

fastapi dev main.py or python -m fastapi dev main.py

sample code

from fastapi import FastAPI

app = FastAPI()


@app.get("/")
async def root():
    return {"message": "Hello World"}

@app.get("/users/{user}")
async def users(user):
    return {"message": f"hello {user}"}

@app.get("/items/{item_id}")
async def read_item(item_id):
    return {"item_id": item_id}

add email path

/email/{email}

return email

add task

return returen html content

2025-06-07 2th

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()

UsersEmail = []

@app.get("/")
async def root():
    return {"message": "Hello World"}

@app.get("/email_address/{email_address}")
async def email_address(email_address):
    return {"confirmed email address": f"{email_address}"}


@app.get("/info/emails")
async def infoEmails():
    return {"emails": UsersEmail}

# task1 dedup user emails
@app.get("/your_email_address/{your_email_address}", response_class=HTMLResponse)
async def your_email_address(your_email_address):
    UsersEmail.append(your_email_address)
    return "ok"

# task2 交互式保存用户邮件地址信息
@app.get("/index", response_class=HTMLResponse)
async def index():
    return f"""
<!DOCTYPE html>
<html>
<head>
<title>哔哩哔哩 (゜-゜)つロ 干杯~-bilibili</title>
</head>
<body>

<h1>You entered email address as below: </h1>

<input>in put your email address</input>

</body>
</html>
"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>User Input Example</title>
<style>
/* Optional: Some basic styling to make it look nicer */
body {
font-family: sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f4f4f4;
margin: 0;
}
.container {
background: white;
padding: 2em;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
text-align: center;
}
input[type="text"] {
padding: 10px;
width: 250px;
margin-bottom: 15px;
border: 1px solid #ccc;
border-radius: 4px;
}
button {
padding: 10px 20px;
border: none;
background-color: #007BFF;
color: white;
border-radius: 4px;
cursor: pointer;
}
button:hover {
background-color: #0056b3;
}
</style>
</head>
<body>
<div class="container">
<h2>Show Input in Alert</h2>
<p>Enter some text below and click submit.</p>
<!-- The user types their text here -->
<input type="text" id="userInput" placeholder="Type something here...">
<!-- The button that will trigger the JavaScript function -->
<button onclick="showAlert()">Submit</button>
</div>
<script>
// This is the JavaScript function that gets called when the button is clicked
function showAlert() {
// 1. Find the input element on the page using its ID
const inputElement = document.getElementById('userInput');
// 2. Get the text that the user typed into the input field
const userText = inputElement.value;
// 3. Create the message for the alert box
const alertMessage = "You entered: " + userText;
// 4. Show the alert box with the user's text
alert(alertMessage);
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment