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
Yuuki Shimizu
September 11, 2021
Programming
0
890
FastAPI を活用した オープンデータAPI の作成
2021.9.11
Python Charity Talks in Japan 2021.09
Yuuki Shimizu
September 11, 2021
Tweet
Share
More Decks by Yuuki Shimizu
See All by Yuuki Shimizu
Gemini API を試した話と山梨でオススメのものを語るLT
yuukis
0
49
山梨のIT勉強会コミュニティを盛り上げるためにアレ作ってみた!
yuukis
0
45
FastAPIで始める やまなし温泉めぐり
yuukis
0
390
Slack の困りごとを何とかしてみた①
yuukis
0
81
Pythonでslackの困りごとを何とかしてみた①
yuukis
0
270
Pythonでお絵かき①
yuukis
0
320
Pythonを使ってみんなでワイワイしたい
yuukis
0
170
Pythonで始めるお手軽ダイエット(2)
yuukis
0
350
PythonでフィットネスデータをIFTTTに繋いでみた
yuukis
1
260
Other Decks in Programming
See All in Programming
rbs-inlineを導入してYARDからRBSに移行する
euglena1215
1
240
仮想ファイルシステムを導入して開発環境のストレージ課題を解消する
segadevtech
2
490
Architecture Decision Record (ADR)
nearme_tech
PRO
1
650
What you can do with Ruby on WebAssembly
kateinoigakukun
0
130
Amazon Neptuneで始める初めてのグラフDB ー グラフDBを使う意味を考える ー
satoshi256kbyte
2
250
The Sequel to a Dream of Ruby Parser's Grammar
ydah
1
200
Some more adventure of Happy Eyeballs
coe401_
2
170
LangChainの現在とv0.3にむけて
os1ma
4
810
XStateでReactに秩序を与えたい
gizm000
0
690
unique パッケージから学ぶ interning と weak reference @ Asakusa.go#3
karamaru
1
230
状態管理ライブラリZustandの導入から運用まで
k1tikurisu
3
440
ECMAScript、Web標準の型はどう管理されているか / How ECMAScript and Web standards types are maintained
petamoriken
3
390
Featured
See All Featured
A better future with KSS
kneath
235
17k
Building Flexible Design Systems
yeseniaperezcruz
325
37k
Mobile First: as difficult as doing things right
swwweet
221
8.8k
No one is an island. Learnings from fostering a developers community.
thoeni
18
2.9k
Agile that works and the tools we love
rasmusluckow
327
20k
Creatively Recalculating Your Daily Design Routine
revolveconf
215
12k
Writing Fast Ruby
sferik
623
60k
Designing with Data
zakiwarfel
98
5k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
Web Components: a chance to create the future
zenorocha
308
41k
Adopting Sorbet at Scale
ufuk
73
8.9k
Scaling GitHub
holman
458
140k
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αʔόى্ͪ͛Λ༰қʹ͠ɺΦʔϓϯσʔλͷར༻ଅਐʹ ܨ͍͛ͨ