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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
しみず ゆうき
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
54
EventBridgeで地域イベントのイベントストリームを作ってみた
yuukis
0
120
2025年 山梨の技術コミュニティを振り返る
yuukis
0
420
サイトを作ったらNFCタグキーホルダーを爆速で作れ!
yuukis
0
1.2k
Perlで痩せる
yuukis
1
890
Pythonで遊べるLEDボードの紹介
yuukis
0
150
今話題のMCPサーバーをFastAPIでサッと作ってみた
yuukis
0
690
地域ITコミュニティの活性化とAWSに移行してみた話
yuukis
0
610
Gemini API を試した話と山梨でオススメのものを語るLT
yuukis
0
110
Other Decks in Programming
See All in Programming
フィードバックで育てるAI開発
kotaminato
1
120
act2-costs.pdf
sumedhbala
0
110
Performance Engineering for Everyone
elenatanasoiu
0
270
分散システム、なんですぐ死んでしまうん?耐障害性を高めたいあなたのためのレジリエンスパターン入門
mshibuya
7
6.2k
琵琶湖の水は止められても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
380
エンジニア向け会社紹介/Findy Company Profile
findyinc
6
360k
トークンをケチるな、設計しろ:GitHub Copilotを賢く使うコンテキスト戦略
ochtum
0
320
任せる範囲はこう広がった / How the Scope of AI Delegation Has Expanded
nrslib
1
260
自作OSでスライド発表する
uyuki234
1
3.8k
信頼性について考えてみる(SRE NEXT 2026 miniLT)
hayama17
0
190
ランチタイムLT会3周年!ランチタイムLT会を3年間続けられたお話
y0hgi
1
140
【やさしく解説 設計編・中級 #6】良いアーキテクチャとは ~ 一本の登り道の、行き先 ~
panda728
PRO
0
160
Featured
See All Featured
What does AI have to do with Human Rights?
axbom
PRO
1
2.2k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
Darren the Foodie - Storyboard
khoart
PRO
3
3.4k
Everyday Curiosity
cassininazir
0
260
BBQ
matthewcrist
89
10k
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
63
55k
Beyond borders and beyond the search box: How to win the global "messy middle" with AI-driven SEO
davidcarrasco
3
180
Designing for Performance
lara
611
70k
The Power of CSS Pseudo Elements
geoffreycrofte
82
6.4k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
360
30k
Are puppies a ranking factor?
jonoalderson
1
3.7k
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
590
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αʔόى্ͪ͛Λ༰қʹ͠ɺΦʔϓϯσʔλͷར༻ଅਐʹ ܨ͍͛ͨ