Skip to content

Commit 8516f1e

Browse files
committed
improve fastapi example
1 parent 20bf143 commit 8516f1e

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

fastapi-example/main.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,26 @@ async def lifespan(_app: FastAPI):
2121
global http_client, openai_client
2222
async with AsyncClient() as _http_client:
2323
http_client = _http_client
24-
logfire.instrument_httpx(http_client, headers=True)
24+
logfire.instrument_httpx(http_client, capture_headers=True)
2525
yield
2626

2727

2828
app = FastAPI(lifespan=lifespan)
29+
logfire.instrument_fastapi(app, capture_headers=True)
2930
this_dir = Path(__file__).parent
3031
image_dir = Path(__file__).parent / 'images'
3132
image_dir.mkdir(exist_ok=True)
3233
app.mount('/static', StaticFiles(directory=image_dir), name='static')
3334

3435

3536
@app.get('/')
37+
@app.get('/display/{image:path}')
3638
async def main() -> FileResponse:
37-
return FileResponse(this_dir / 'index.html')
39+
return FileResponse(this_dir / 'page.html')
3840

3941

4042
class GenerateResponse(BaseModel):
41-
image_url: str = Field(serialization_alias='imageUrl')
43+
next_url: str = Field(serialization_alias='nextUrl')
4244

4345

4446
@app.post('/generate')
@@ -53,10 +55,10 @@ async def generate_image(prompt: str) -> GenerateResponse:
5355
r.raise_for_status()
5456
path = f'{uuid4().hex}.jpg'
5557
(image_dir / path).write_bytes(r.content)
56-
return GenerateResponse(image_url=f'/static/{path}')
58+
return GenerateResponse(next_url=f'/display/{path}')
5759

5860

5961
if __name__ == '__main__':
6062
import uvicorn
6163

62-
uvicorn.run(app, port=8000)
64+
uvicorn.run(app)

fastapi-example/index.html renamed to fastapi-example/page.html

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
font-family: "IBM Plex Sans", sans-serif;
1414
max-width: 800px;
1515
margin: 0 auto;
16-
padding: 80px 20px 20px;
16+
padding: 50px 20px 20px;
1717
background-color: #f5f5f5;
1818
}
1919

@@ -34,6 +34,7 @@
3434
width: 100%;
3535
padding: 10px;
3636
font-size: 16px;
37+
font-family: inherit;
3738
border: 2px solid #ddd;
3839
border-radius: 8px;
3940
box-sizing: border-box;
@@ -45,11 +46,17 @@
4546
border-color: #4caf50;
4647
}
4748

49+
a {
50+
text-decoration: none;
51+
}
52+
53+
a,
4854
button {
4955
background-color: #4caf50;
5056
color: white;
5157
padding: 15px 30px;
5258
font-size: 16px;
59+
font-family: inherit;
5360
border: none;
5461
border-radius: 8px;
5562
cursor: pointer;
@@ -84,6 +91,7 @@
8491

8592
.generated-image {
8693
max-width: 100%;
94+
max-height: 600px;
8795
height: auto;
8896
border-radius: 10px;
8997
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
@@ -134,7 +142,7 @@ <h1>Image Generator</h1>
134142
alt="Generated image"
135143
/>
136144
<br /><br />
137-
<button class="clear-btn" onclick="clearImage()">Clear</button>
145+
<a href="/" class="clear-btn" onclick="clearImage()">Clear</a>
138146
</div>
139147
</div>
140148

@@ -176,9 +184,9 @@ <h1>Image Generator</h1>
176184
throw new Error(`HTTP error! status: ${response.status}`);
177185
}
178186

179-
const { imageUrl } = await response.json();
187+
const { nextUrl } = await response.json();
180188

181-
showImage(imageUrl);
189+
window.location.href = nextUrl;
182190
} catch (error) {
183191
console.error("Error generating image:", error);
184192
showError("Failed to generate image. Please try again.");
@@ -207,15 +215,13 @@ <h1>Image Generator</h1>
207215
promptSection.classList.remove("hidden");
208216
}
209217

210-
function clearImage() {
211-
imageSection.classList.add("hidden");
212-
errorSection.classList.add("hidden");
213-
promptSection.classList.remove("hidden");
214-
promptInput.value = "";
215-
promptInput.focus();
218+
const path = window.location.pathname;
219+
const imageMatch = path.match(/^\/display\/(.+\.jpg)$/);
216220

217-
// Clean up the blob URL
218-
URL.revokeObjectURL(generatedImage.src);
221+
if (imageMatch) {
222+
const imageUrl = `/static/${imageMatch[1]}`;
223+
showLoading();
224+
showImage(imageUrl);
219225
}
220226
</script>
221227
</body>

0 commit comments

Comments
 (0)