Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
FastAPI を活用した オープンデータAPI の作成
Search
しみず ゆうき
September 11, 2021
Programming
1.1k
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
FastAPI を活用した オープンデータAPI の作成
2021.9.11
Python Charity Talks in Japan 2021.09
しみず ゆうき
September 11, 2021
More Decks by しみず ゆうき
See All by しみず ゆうき
12年前のAndroidアプリを蘇らせた話
yuukis
0
67
EventBridgeで地域イベントのイベントストリームを作ってみた
yuukis
0
130
2025年 山梨の技術コミュニティを振り返る
yuukis
0
440
サイトを作ったらNFCタグキーホルダーを爆速で作れ!
yuukis
0
1.2k
Perlで痩せる
yuukis
1
900
Pythonで遊べるLEDボードの紹介
yuukis
0
150
今話題のMCPサーバーをFastAPIでサッと作ってみた
yuukis
0
710
地域ITコミュニティの活性化とAWSに移行してみた話
yuukis
0
620
Gemini API を試した話と山梨でオススメのものを語るLT
yuukis
0
120
Other Decks in Programming
See All in Programming
OpenSpecのproposalにbrainstormingを持たせてみた
tigertora7571
1
210
Google Apps Script で Ruby を動かす
kawahara
0
150
Apache Hive: Toward a Cloud Native Lakehouse
okumin
0
180
2年かけて Deno に DOMMatrix を実装した話 / How I implemented DOMMatrix in Deno over two years
petamoriken
0
210
型も通る、synthも通る、それでも危ない 〜AIのCDKの権限とコストを機械で検証する〜 / It Passes Type Checks, It Passes Synth Checks, but It’s Still Risky — Automatically Verifying Permissions and Costs in AI’s CDK —
seike460
PRO
1
540
数百円から始めるRuby電子工作
tarosay
0
140
運用ダッシュボードの設計を誰も教えてくれないのだけどみなさんどうしてるんですか? - チームに監視するという文化を根付かせるための第一歩を踏みたい -
satoshi256kbyte
1
100
為什麼你並不需要ViewModel / No, you don't need a ViewModel
lovee
1
490
AI Engineeringは、AIプロダクトだけのものか? 〜AIがソフトウェアを作る時代の新しい当たり前〜 / No AI in your product. AI Engineering in your development.
rkaga
4
390
複数の Claude Code が"放置"されてしまう問題をCLI ダッシュボードを自作して解決した話
sumihiro3
1
670
琵琶湖の水は止められてもNet--HTTPのリトライは止められない / You might be able to stop the water flow of Lake Biwa but you can't stop Net::HTTP retries
luccafort
PRO
0
640
変わらないものが、変わるものを決める — 意図駆動開発 × イベントソーシング × イミュータブル | What Doesn't Change Decides What Can — IDD × Event Sourcing × Immutability
tomohisa
0
1.5k
Featured
See All Featured
Effective software design: The role of men in debugging patriarchy in IT @ Voxxed Days AMS
baasie
0
470
Building a A Zero-Code AI SEO Workflow
portentint
PRO
0
660
Practical Orchestrator
shlominoach
191
12k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
990
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
31
3.3k
Unlocking the hidden potential of vector embeddings in international SEO
frankvandijk
0
890
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
3
1.1k
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
Become a Pro
speakerdeck
PRO
31
6.2k
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
790
Statistics for Hackers
jakevdp
799
230k
Code Reviewing Like a Champion
maltzj
528
40k
Transcript
2021. 9. 11 ͠Έͣ Ώ͏͖ 1 ʲPython Charity Talks in
Japan 2021.09ʳ FastAPI Λ׆༻ͨ͠ ΦʔϓϯσʔλAPI ͷ࡞
͓·͑୭Αʁ ͠Έͣ Ώ͏͖ • ϞόΠϧΞϓϦ ΤϯδχΞ ◦ Android/iOS • ࢁསݝߕࢢ
ग़ • Python Shingen.py Ͱ৮ΕΔ͘Β͍ 2
sli.do Ͱ࣭Λड͚͚͍ͯ·͢ʂ 3
͡Ίʹ • ݱࡏɺࢁསݝͷΦʔϓϯσʔλαΠτͰ 12,000݅Λ͑Δσʔλ͕ެ։͞Ε͍ͯΔ • ϞόΠϧΞϓϦͰར༻͍ͨ͠߹ɺWeb API Ͱఏڙ͞Ε͍ͯΔͱखܰʹࢼ͢͜ͱ͕Ͱ͖ͯ خ͍͠ •
Shingen.py ͷษڧձͰ FastAPI Λࢼ͢ػձ͕ ͋ΓɺΦʔϓϯσʔλར༻ʹ׆༻Ͱ͖ͳ͍͔ ݕ౼ͨ͠ 4
FastAPI ͱ • OpenAPI ʹج͍ͮͯ࡞ΒΕ͍ͯ Δ Python ϑϨʔϜϫʔΫ • ࡞Γ͢͞Λҙࣝ
• ʮૣ͍ɾ͍ɾ؆୯ʯ ◦ ։ൃ͕ૣ͍ ◦ ͦΕͳΓʹύϑΥʔϚϯε͕ग़Δ ʢ͍ʣ ◦ ؆୯ʹ࡞ΕΔ 5 from typing import Optional from fastapi import FastAPI app = FastAPI() @app.get("/") def read_root(): return {"Hello": "World"} @app.get("/items/{item_id}") def read_item(item_id: int, q: Optional[str] = None): return return {"item_id": item_id, "q": q}
ྫͱͯ͠ɺ ࢁསͷԹઘࢪઃΛฦ٫͢ΔAPI Λ FastAPI Ͱ࡞͢Δ 6 ͪͳΈʹ ࢁས ઘ࣭͕ଟ༷ɺܠ؍ͷྑ͍Թઘ͕ͱͯଟ͍Ͱ͢ʂ
ݝͷΦʔϓϯσʔλΛར༻ 7
8 PDF !!
PDF Ͱେৎʂ PDF Λͦͷ··ϩʔυͯ͠มޙɺ FastAPI Ͱฦ٫͢ΔΑ͏࣮͠·ͨ͠ 9 Python ศརͰ͢Ͷ
chezou/tabula-py • PDF ϑΝΠϧͷදΛ pandas ͷ DataFrame ΦϒδΣΫτʹ ม͢ΔϥΠϒϥϦ ◦
CSVɺTSVɺJSON ϑΝΠϧʹม ͢Δ͜ͱՄೳ • OCR πʔϧͰͳ͍ • Java 8 Ҏ͕߱ඞཁ 10
main.py - ᶃ PDF ಡΈࠐΈ def check_columns(df, previous_df): difference1 =
set(df.keys()) - set(previous_df.keys()) difference2 = set(previous_df.keys()) - set(df.keys()) return (len(difference1) == 0 and len(difference2) == 0) 11 def get_data(pdf_path): previous_df = pd.DataFrame() dfs = tabula.read_pdf(pdf_path, lattice=True, pages = 'all') for df in dfs: # ෳϖʔδͷදΛ݁߹͢Δ if (check_columns(df, previous_df)): df = pd.concat([previous_df, df]) previous_df = df return previous_df PDFΛಡΈࠐΈɺDataFrame Φϒ δΣΫτΛฦ٫͢Δ ෳϖʔδʹ·͕ͨΔදͷ໊߲ Λൺֱ͠ɺಉ͡ද͔Ͳ͏͔Λఆ ͢Δʢ্ͷ͔ؔΒݺΕΔʣ
main.py - ᶄ API ࡞ 12 app = FastAPI() pdf_path
= "h3012011.pdf" @app.get("/") def read_root(): data = get_data(pdf_path) json_data = data.to_json(orient = 'records') return json.loads(json_data) @app.get("/area/{area}") def read_item(area: str): data = get_data(pdf_path) df_mask = data['ࢢொଜ໊'] == area data = data[df_mask] json_data = data.to_json(orient = 'records') return json.loads(json_data) [get] / શ݅ฦ٫͢Δ API [get] /area/{area} ࢦఆ͞ΕͨࢢொଜͷΈฦ٫͢Δ API
Docker Λ༻ 13 VPS Nginx (ϦόʔεϓϩΩγ) opendata.yamanashi.dev /api/onsen Docker コンテナ
FastAPI localhost:xxxxx main.py ࢁསݝ WebαΠτ CSV PDF tiangolo/uvicorn-gunicorn-fastapi :python3.8-alpine3.10 ্هͷΠϝʔδΛϕʔεʹ openjdk11 ΛΠϯετʔϧ ͨ͠ͷΛ༻
DEMO 14 https://opendata.yamanashi.dev/api/onsen
ࢁསݝΦʔϓϯσʔλAPIϓϩδΣΫτ • ϓϩδΣΫτ αΠτ ◦ ݝͷΦʔϓϯσʔλαΠτͰެ։ ͞Ε͍ͯΔσʔλΛ API Ͱఏڙ •
GitHub ◦ ιʔείʔυΛެ։ ◦ ߋ৽͢Δ͜ͱͰࣗಈσϓϩΠ • DockerHub ◦ FastAPIɺTabula ͕࣮ߦՄೳͳ Docker ΠϝʔδΛఏڙ 15 https://opendata.yamanashi.dev
ຊϓϩδΣΫτ͕ࢦ͢ͱ͜Ζ 16 • ଞͷΦʔϓϯσʔλͷAPIల։Λ༰қʹ͢Δ ◦ ϦϙδτϦΛෳ͠ɺmain.py ΛΧελϚΠζ͢Ε OK • ΦʔϓϯσʔλAPIαʔόͷى্ͪ͛Λ༰қʹ͢Δ
◦ Docker ڥ͕͋ΕϫϯϥΠφʔͰى্ͪ͛Մೳ σʔλར༻ʹ͍ͭͯ ࢁསݝΦʔϓϯσʔλαΠτར༻ن ʹै͏ඞཁ͕͋Γ·͢
͍͞͝ʹ 17
·ͱΊ 18 ʮFastAPI Λ׆༻ͨ͠ ΦʔϓϯσʔλAPI ͷ࡞ʯ • FastAPI Φʔϓϯσʔλ͔Βखܰʹ API
Λ࡞͢Δͷʹ͍͍ͯΔ ◦ Tabula ͱΈ߹ΘͤΔ͜ͱͰɺPDF ϑΝΠϧ FastAPI ʹࡌͤΔ͜ͱ͕ Մೳ • ࢁསݝΦʔϓϯσʔλAPI ϓϩδΣΫτΛى্ͪ͛ ◦ API࡞ɾAPIαʔόى্ͪ͛Λ༰қʹ͠ɺΦʔϓϯσʔλͷར༻ଅਐʹ ܨ͍͛ͨ