<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Zhao Xinhao</title>
    <description>The latest articles on DEV Community by Zhao Xinhao (@zhao_xinhao_a6de9f3d23a77).</description>
    <link>https://dev.to/zhao_xinhao_a6de9f3d23a77</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3448879%2F74b3ce8b-03da-4197-81cd-bd19a766d9ee.png</url>
      <title>DEV Community: Zhao Xinhao</title>
      <link>https://dev.to/zhao_xinhao_a6de9f3d23a77</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/zhao_xinhao_a6de9f3d23a77"/>
    <language>en</language>
    <item>
      <title>Git Version Control and Github</title>
      <dc:creator>Zhao Xinhao</dc:creator>
      <pubDate>Thu, 11 Sep 2025 03:32:44 +0000</pubDate>
      <link>https://dev.to/zhao_xinhao_a6de9f3d23a77/git-version-control-acj</link>
      <guid>https://dev.to/zhao_xinhao_a6de9f3d23a77/git-version-control-acj</guid>
      <description>&lt;p&gt;1.Initialization&lt;br&gt;
In root path:&lt;br&gt;
&lt;code&gt;git init&lt;/code&gt;&lt;br&gt;
2.Create .gitignore (Ignore unnecessary files)&lt;br&gt;
&lt;code&gt;nano .gitignore&lt;/code&gt;&lt;br&gt;
fill in with&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;venv/

instance/
todo.db

__pycache__/
*.py[cod]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3.Add files to the staging area&lt;br&gt;
&lt;code&gt;git add .&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;4.Submit changes&lt;br&gt;
&lt;code&gt;git commit -m "Initial commit: basic Flask todo app with add task functionality"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;5.Check file status&lt;br&gt;
&lt;code&gt;git status&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;6.Set golbal information &lt;br&gt;
&lt;code&gt;git config --global user.name "Your username"&lt;br&gt;
git config --global user.email "your e-mail@example.com"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;7.Check current configuration&lt;br&gt;
&lt;code&gt;git config --global --list&lt;br&gt;
git config --list&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;8.Associate remote repository and push&lt;br&gt;
bash&lt;br&gt;
&lt;code&gt;git remote add origin https://github.com/Your-username/Your-repository.git&lt;br&gt;
git push -u origin master&lt;/code&gt;&lt;br&gt;
After setting, you can push like this &lt;br&gt;
&lt;code&gt;git add .&lt;br&gt;
git commit -m "Something notices"&lt;br&gt;
git push&lt;/code&gt;&lt;/p&gt;

</description>
      <category>git</category>
      <category>github</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Simple Calculator in Python</title>
      <dc:creator>Zhao Xinhao</dc:creator>
      <pubDate>Thu, 28 Aug 2025 02:47:50 +0000</pubDate>
      <link>https://dev.to/zhao_xinhao_a6de9f3d23a77/simple-calculator-in-python-2i1k</link>
      <guid>https://dev.to/zhao_xinhao_a6de9f3d23a77/simple-calculator-in-python-2i1k</guid>
      <description>&lt;p&gt;1.calculator.py&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from fastapi import FastAPI, HTTPException
from fastapi.responses import FileResponse
from pydantic import BaseModel

class CalculationRequest(BaseModel):
    num1: float
    num2: float

app = FastAPI()

@app.post("/add")
async def add(request: CalculationRequest):
    return {"result": request.num1 + request.num2}

@app.post("/subtract")
async def subtract(request: CalculationRequest):
    return {"result": request.num1 - request.num2}

@app.post("/multiply")
async def multiply(request: CalculationRequest):
    return {"result": request.num1 * request.num2}

@app.post("/divide")
async def divide(request: CalculationRequest):
    epsilon = 1e-10
    #return{"result": request.num2-epsilon}
    if abs(request.num2) &amp;lt; epsilon:
        raise HTTPException(status_code=400, detail="Cannot divide by zero!")
    return {"result": request.num1 / request.num2}

@app.get("/")
def read_root():
    return FileResponse("index.html")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2.index.html&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html&amp;gt;
    &amp;lt;head&amp;gt;
        &amp;lt;title&amp;gt;Calculator App&amp;lt;/title&amp;gt;
    &amp;lt;/head&amp;gt;
    &amp;lt;body&amp;gt;
        &amp;lt;h1&amp;gt;Calculator&amp;lt;/h1&amp;gt;
        &amp;lt;input type="text" id="num1" placeholder="Enter num1"&amp;gt;
        &amp;lt;input type="text" id="num2" placeholder="Enter num2"&amp;gt;
        &amp;lt;div&amp;gt;
            &amp;lt;button onclick="add()"&amp;gt;+&amp;lt;/button&amp;gt;
            &amp;lt;button onclick="subtract()"&amp;gt;-&amp;lt;/button&amp;gt;
            &amp;lt;button onclick="multiply()"&amp;gt;*&amp;lt;/button&amp;gt;
            &amp;lt;button onclick="divide()"&amp;gt;/&amp;lt;/button&amp;gt;
        &amp;lt;/div&amp;gt; 
        &amp;lt;div id="result"&amp;gt;&amp;lt;/div&amp;gt;

        &amp;lt;script&amp;gt;
            async function calculate(operation) {
                const num1 = document.getElementById("num1").value;
                const num2 = document.getElementById("num2").value;

                if (!num1 || !num2) {
                    document.getElementById("result").innerText = "Please enter both numbers";
                    return;
                }

                try {
                    const response = await fetch(`http://localhost:8000/${operation}`,{
                        method: "POST",
                        headers: { "Content-Type":"application/json"},
                        body: JSON.stringify({
                        num1: num1,
                        num2: num2
                    })
                });

                    const data = await response.json();

                    if(!response.ok){
                        document.getElementById("result").innerText = `Error: ${data.detail}`;
                    }else{
                        document.getElementById('result').innerHTML = `
                            &amp;lt;p&amp;gt;Result: ${data.result}&amp;lt;/p&amp;gt;
                            &amp;lt;p&amp;gt;Operation: ${num1} ${getOperationSymbol(operation)} ${num2} = ${data.result}&amp;lt;/p&amp;gt;
                        `;
                    }
                } catch (error) {
                    document.getElementById("result").innerText = `Network error: ${error.message}`;
                }
            }

            function getOperationSymbol(operation) {
                const symbols = {
                    'add': '+',
                    'subtract': '-',
                    'multiply': '*',
                    'divide': '/'
                };
                return symbols[operation];
            }

            function add() { calculate('add'); }
            function subtract() { calculate('subtract'); }
            function multiply() { calculate('multiply'); }
            function divide() { calculate('divide'); }
        &amp;lt;/script&amp;gt;
    &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3.Display&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb54i3zkemljauffrstis.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb54i3zkemljauffrstis.png" alt=" " width="800" height="470"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>fastapi</category>
    </item>
    <item>
      <title>Weather Query in Python</title>
      <dc:creator>Zhao Xinhao</dc:creator>
      <pubDate>Mon, 25 Aug 2025 08:52:22 +0000</pubDate>
      <link>https://dev.to/zhao_xinhao_a6de9f3d23a77/weather-query-in-python-2844</link>
      <guid>https://dev.to/zhao_xinhao_a6de9f3d23a77/weather-query-in-python-2844</guid>
      <description>&lt;p&gt;1.Install FastAPI and tools&lt;/p&gt;

&lt;p&gt;bash&lt;br&gt;
&lt;code&gt;pip install fastapi uvicorn requests python-dotenv&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;2.Create a folder for project &lt;br&gt;
Open terminal and run:&lt;/p&gt;

&lt;p&gt;bash&lt;br&gt;
&lt;code&gt;mkdir Weather Query&lt;br&gt;
cd Weather Query&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;3.Create a .envfile to store API key securely&lt;br&gt;
Inside the Weather Query folder, create a new file named .env,open the file and add this line(we'll get the actual API key next):&lt;/p&gt;

&lt;p&gt;plaintext&lt;br&gt;
&lt;code&gt;OPENWEATHER_API_KEY=your_api_key_here&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;4.Create main.py&lt;br&gt;
Inside Weather Query, create a new file named main.py&lt;/p&gt;

&lt;p&gt;python&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import requests
import os
from dotenv import load_dotenv

# Load API key from .env
load_dotenv()  # This looks for the .env file automatically
API_KEY = os.getenv("OPENWEATHER_API_KEY")  # Reads your key

app = FastAPI()

class CityRequest(BaseModel):
    city: str  # This defines what data your API expects

@app.post("/weather")
def get_weather(city: CityRequest):
    url = f"http://api.openweathermap.org/data/2.5/weather?q={city.city}&amp;amp;appid={API_KEY}&amp;amp;units=metric"
    response = requests.get(url)

    if response.status_code != 200:
        raise HTTPException(status_code=400, detail="City not found or API error")

    weather_data = response.json()
    return {
        "city": city.city,
        "temperature": weather_data["main"]["temp"],
        "weather": weather_data["weather"][0]["description"]
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;5.Start the FastAPI Server&lt;br&gt;
In terminal(inside the Weather Query folder),run:&lt;br&gt;
bash&lt;br&gt;
&lt;code&gt;uvicorn main:app --reload&lt;/code&gt;&lt;br&gt;
Expected Output:&lt;br&gt;
INFO: Uvicorn running on &lt;a href="http://127.0.0.1:8000" rel="noopener noreferrer"&gt;http://127.0.0.1:8000&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;6.Test the API in Browser&lt;br&gt;
Open browser and go to &lt;a href="http://localhost:8000/docs" rel="noopener noreferrer"&gt;http://localhost:8000/docs&lt;/a&gt;, find the POST / weather endpoint, click "Try it out", enter a city name(e.g., {"city": "Tokyo"}), click "Execute".&lt;/p&gt;

&lt;p&gt;Expected Output:​​&lt;br&gt;
{&lt;br&gt;
  "city": "Tokyo",&lt;br&gt;
  "temperature": 25.5,&lt;br&gt;
  "weather": "clear sky"&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;7.Try Errors&lt;br&gt;
Enter an invalid city(e.g.,{"city": "Hogwarts"})&lt;/p&gt;

&lt;p&gt;json&lt;br&gt;
&lt;code&gt;"detail": "City not found or API error"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;8.Create weather.html And Add Basic Html Structure&lt;br&gt;
html&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html&amp;gt;
&amp;lt;head&amp;gt;
    &amp;lt;title&amp;gt;Weather App&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
    &amp;lt;h1&amp;gt;Weather Check&amp;lt;/h1&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;9.Add City Input and Button&lt;br&gt;
html&lt;br&gt;
&lt;code&gt;&amp;lt;input type="text" id="city" placeholder="Enter city name"&amp;gt;&lt;br&gt;
&amp;lt;button onclick="getWeather()"&amp;gt;Get Weather&amp;lt;/button&amp;gt;&lt;br&gt;
&amp;lt;div id="result"&amp;gt;&amp;lt;/div&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;10.Add JavaScript to weather.html&lt;br&gt;
html&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script&amp;gt;
    async function getWeather() {
        const city = document.getElementById("city").value;
        const response = await fetch("http://localhost:8000/weather", {
            method: "POST",
            headers: { "Content-Type": "application/json" },
            body: JSON.stringify({ city: city })
        });
        const data = await response.json();
        if (data.detail) {
            document.getElementById("result").innerText = `Error: ${data.detail}`;
        } else {
            document.getElementById("result").innerHTML = `
                &amp;lt;p&amp;gt;City: ${data.city}&amp;lt;/p&amp;gt;
                &amp;lt;p&amp;gt;Temperature: ${data.temperature}°C&amp;lt;/p&amp;gt;
                &amp;lt;p&amp;gt;Weather: ${data.weather}&amp;lt;/p&amp;gt;
            `;
        }
    }
&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;fetch():Calls FastAPI endpoint(POST /weather)with the city name.&lt;br&gt;
response.json():Converts the API response to JSON.&lt;br&gt;
Error Handling:Shows errors(e.g., "City not found")or displays weather data.&lt;br&gt;
innerHTML:Inserts HTML into the result div.&lt;br&gt;
async/await：Waits for the API call to finish before proceeding.&lt;/p&gt;

&lt;p&gt;11.Serve the HTML File&lt;br&gt;
Update main.py(FastAPI) to serve weather.html at the root URL(/):&lt;br&gt;
python&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Lets FastAPI send a file(like weathter.html)as a response
from fastapi.responses import FileResponse

# Serve the HTML file at the root URL
@app.get("/")
def read_root():
    return FileResponse("weather.html")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;12.Run the Server and Check the Browser&lt;br&gt;
&lt;code&gt;uvicorn main:app --reload&lt;/code&gt;&lt;br&gt;
go to &lt;a href="http://localhost:8000/" rel="noopener noreferrer"&gt;http://localhost:8000/&lt;/a&gt; ,you will see&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F91vi4vyp3h1d21l2hufa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F91vi4vyp3h1d21l2hufa.png" alt=" " width="800" height="385"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjup25krzfx15o3pi4oh9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjup25krzfx15o3pi4oh9.png" alt=" " width="723" height="359"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>fastapi</category>
    </item>
    <item>
      <title>Hello World service in Python</title>
      <dc:creator>Zhao Xinhao</dc:creator>
      <pubDate>Sun, 24 Aug 2025 09:23:54 +0000</pubDate>
      <link>https://dev.to/zhao_xinhao_a6de9f3d23a77/hello-world-service-in-python-c52</link>
      <guid>https://dev.to/zhao_xinhao_a6de9f3d23a77/hello-world-service-in-python-c52</guid>
      <description>&lt;p&gt;A "Hello World" service does one thing: it responds to request with the text "Hello, World!".&lt;/p&gt;

&lt;p&gt;FastAPI is modern,fast,and provides automatic API documentation.&lt;br&gt;
Here's a guide to creating a Hello World service with FastAPI:&lt;/p&gt;

&lt;p&gt;1.Installation&lt;br&gt;
First,install the required packages:&lt;/p&gt;

&lt;p&gt;bash&lt;br&gt;
&lt;code&gt;pip install fastapi uvicorn&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Unicorn is a super-fast server for Python that runs web applications.&lt;br&gt;
FasAPI to create the application, and Uvicorn to start it and handle incoming web requests.&lt;/p&gt;

&lt;p&gt;2.Basic Hello World Service&lt;br&gt;
Create a file main.py:&lt;br&gt;
simplest:&lt;br&gt;
python&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def hello_world():
    return "helloworld"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;improve:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from fastapi import FastAPI

# Create FastAPI instance
app = FastAPI(
    title = "Hello World API",
    description = "A simply Hello World service",
    version = "1.0.0"
)

# Define a simple GET endpoint
@app.get("/")
async def hello_world():
    return {"message": "Hello, World!"}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3.Running the Service&lt;br&gt;
Using uvicorn directly&lt;/p&gt;

&lt;p&gt;bash&lt;br&gt;
&lt;code&gt;uvicorn main:app --reload&lt;/code&gt; or&lt;br&gt;
&lt;code&gt;uvicorn main:app --reload --host 0.0.0.0 --port 8000&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Means:&lt;br&gt;
"Uvicorn,please run the FastAPI app named app from the main.py file.Watch for code changes and reload automatically.Let any device on the network connect to it, and serve it on port 8000."&lt;/p&gt;

&lt;p&gt;4.Testing the Service&lt;br&gt;
Once running, we can access:&lt;br&gt;
    &lt;a href="http://localhost:8000" rel="noopener noreferrer"&gt;http://localhost:8000&lt;/a&gt; or &lt;a href="http://127.0.0.1:8000" rel="noopener noreferrer"&gt;http://127.0.0.1:8000&lt;/a&gt;&lt;br&gt;
And you will get the {"message":"Hello, World!"} in brower.&lt;br&gt;
It's done！If you want to keep going, here are next steps:&lt;/p&gt;

&lt;p&gt;5.Add a New Endpoint&lt;br&gt;
Try creating a new "greet" endpoint that takes a name as input:&lt;/p&gt;

&lt;p&gt;python&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Define a GET endpoint about greet
@app.get("/greet/{name}")
def greet(name: str):
    return {"message": f"Hello, {name}!"}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Test it at: &lt;a href="http://localhost:8000/greet/Alice" rel="noopener noreferrer"&gt;http://localhost:8000/greet/Alice&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;6.Try a POST Request&lt;br&gt;
Accept data(like a username)from the user:&lt;/p&gt;

&lt;p&gt;python&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from pydantic import BaseModel

class User(BaseModel):
    username: str

# Define a Post endpoint about login
@app.post("/login")
def login(user: User):
    return {"message": f"Welcome, {user.username}!"}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using FastAPI's /docs page,go to &lt;a href="http://localhost:8000/docs" rel="noopener noreferrer"&gt;http://localhost:8000/docs&lt;/a&gt;, find POST /login,click"Try it out",enter the json,and hit "Execute".Then you send the data to server.&lt;/p&gt;

</description>
      <category>fastapi</category>
    </item>
  </channel>
</rss>
