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で始める やまなし温泉めぐり
Search
Yuuki Shimizu
July 15, 2021
Programming
0
470
FastAPIで始める やまなし温泉めぐり
2021.7.15
信玄パイ LT大会 みんなの FastAPI LT
Yuuki Shimizu
July 15, 2021
Tweet
Share
More Decks by Yuuki Shimizu
See All by Yuuki Shimizu
Perlで痩せる
yuukis
1
700
Pythonで遊べるLEDボードの紹介
yuukis
0
71
今話題のMCPサーバーをFastAPIでサッと作ってみた
yuukis
0
230
地域ITコミュニティの活性化とAWSに移行してみた話
yuukis
0
410
Gemini API を試した話と山梨でオススメのものを語るLT
yuukis
0
81
山梨のIT勉強会コミュニティを盛り上げるためにアレ作ってみた!
yuukis
0
75
FastAPI を活用した オープンデータAPI の作成
yuukis
0
1k
Slack の困りごとを何とかしてみた①
yuukis
0
90
Pythonでslackの困りごとを何とかしてみた①
yuukis
0
330
Other Decks in Programming
See All in Programming
High-Level Programming Languages in AI Era -Human Thought and Mind-
hayat01sh1da
PRO
0
700
GraphRAGの仕組みまるわかり
tosuri13
8
520
PicoRuby on Rails
makicamel
2
120
たった 1 枚の PHP ファイルで実装する MCP サーバ / MCP Server with Vanilla PHP
okashoi
1
220
アンドパッドの Go 勉強会「 gopher 会」とその内容の紹介
andpad
0
290
Quand Symfony, ApiPlatform, OpenAI et LangChain s'allient pour exploiter vos PDF : de la théorie à la production…
ahmedbhs123
0
120
Discover Metal 4
rei315
2
110
iOS 26にアップデートすると実機でのHot Reloadができない?
umigishiaoi
0
110
20250704_教育事業におけるアジャイルなデータ基盤構築
hanon52_
4
310
Hypervel - A Coroutine Framework for Laravel Artisans
albertcht
1
110
なぜ適用するか、移行して理解するClean Architecture 〜構造を超えて設計を継承する〜 / Why Apply, Migrate and Understand Clean Architecture - Inherit Design Beyond Structure
seike460
PRO
3
730
dbt民主化とLLMによる開発ブースト ~ AI Readyな分析サイクルを目指して ~
yoshyum
2
260
Featured
See All Featured
It's Worth the Effort
3n
185
28k
Facilitating Awesome Meetings
lara
54
6.4k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
26
2.9k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.4k
The Pragmatic Product Professional
lauravandoore
35
6.7k
VelocityConf: Rendering Performance Case Studies
addyosmani
331
24k
StorybookのUI Testing Handbookを読んだ
zakiyama
30
5.9k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
10
940
Building an army of robots
kneath
306
45k
Documentation Writing (for coders)
carmenintech
72
4.9k
We Have a Design System, Now What?
morganepeng
53
7.7k
A better future with KSS
kneath
239
17k
Transcript
2021. 7. 15 ͠Έͣ Ώ͏͖ 1 ʲ৴ݰύΠ LTେձ ΈΜͳͷ FastAPIʳ
FastAPI Ͱ࢝ΊΔ ·ͳ͠ ԹઘΊ͙Γ
͓·͑୭Αʁ ͠Έͣ Ώ͏͖ • Android / iOS ϓϩάϥϚ • ࢁསੜ·Ε
ࢁསҭͪ ˞ݱࡏ౦ژʹग़Ք͗த 5 2
͡Ίʹ ઌ݄։࠵͞Εͨ৴ݰύΠͷ FastAPI ษڧձ ʮFastAPI Ͱϋδϝϧ PythonʯͰֶΜͩ͜ͱ Λࢀߟʹɺࢼ͠ʹ API Λ࡞ͯ͠Έ·ͨ͠ͷͰ
൸࿐͠·͢ ʢษڧձۀͰࢀՃͰ͖ͳ͔ͬͨͷͰɺࣗश͖ͯ͠·͠ ͨɻࢿྉͷڞ༗͋Γ͕ͱ͏͍͟͝·͢ʂʣ 3
มΘΓ·͕͢ɺ օ͞Μɺ Թઘ ͖Ͱ͔͢ʁ 4
ࢁསԹઘ͕͍ͬͺ͍ʂ • ଟ༷ͳઘ࣭ ◦ “શ෦Ͱ10छྨ͋Δઘ࣭ͷ͏ͪɺࢁསʹ9छྨͷ ઘ࣭͕͋ΓɺશࠃͰτοϓϨϕϧ” • ܠ؍ͷྑ͞ ◦ “ૣே༦Ε࣌ʹଠཅͷޫͰߚ͘છ·Δ࢜ࢁɺ
ߕຍͷ༦Ε͔ΒܠʹҠΔॠؒͷඒ͍͠ܠ ৭ɺࣗવ๛͔ͳܢ୩ඒͳͲɺ༷ʑͳܠ৭ΛԹઘʹ ਁ͔Γͳ͕Βָ͠Ή͜ͱ͕Ͱ͖·͢” 5 ग़యɿ·ͳཱͪ͠دΓඦ໊౬ ؼলͷࡍʹΑཱͪ͘دͬͯ·͢
ࢁསͷԹઘࢪઃΛฦ٫͢ΔAPI Λ FastAPIͰ࡞ͬͯΈͨ 6
ࢁསݝͷΦʔϓϯσʔλΛར༻ 7
8 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 Docker Host (VPS) Nginx ϦόʔεϓϩΩγ onsen.yamanashi.dev:443 App
Container FastAPI localhost:45280 main.py ࢁསݝ WebαΠτ PDF PDF tiangolo/uvicorn-gunicorn-fastapi :python3.8-alpine3.10 ্هͷΠϝʔδΛϕʔεʹ openjdk11 ΛΠϯετʔϧ ͨ͠ͷΛ༻ ࠓճݝαΠτʹෛՙ͕͔͔Βͳ͍Α͏ɺ PDFΛࣄલʹίϯςφʹίϐʔ
DEMO 14 https://onsen.yamanashi.dev
͍͞͝ʹ 15
ࡶײɾ·ͱΊ ʮFastAPI Ͱ࢝ΊΔ ·ͳ͠ ԹઘΊ͙Γʯ • FastAPI Φʔϓϯσʔλ͔Βखܰʹ API Λ࡞͢Δͷʹྑͦ͞͏
◦ Tabula ͱΈ߹ΘͤΔ͜ͱͰɺPDF ϑΝΠϧ FastAPI ʹࡌͤΔ͜ͱ͕Մೳ ◦ PDFϑΝΠϧͷมʹ͕͔͔͍࣌ؒͬͯΔͷͰɺதؒσʔλΛอଘ͢Δͷ ͕͍Δ͔ • ͓ؾʹೖΓͷԹઘࢪઃΛථ͢Δ API ͷ࡞ೝূࢼͯ͠Έ͍ͨ • ·ͩߦͬͨ͜ͱͷͳ͍ԹઘࢪઃΛ৭ʑΕͯྑ͔ͬͨʂ 16
ʲิʳ࠷ۙ ͓ؾʹೖΓͷؼΓԹઘ ᶃΈ͔͞ͷ౬ ԘαφͰ͓ഽεϕεϕʹʂ ᶄ࡚ѴԹઘ ମʹͼͬ͠Γ͘๐ͷࢎԹઘ ᶅ૿ͷ౬ ԹͷݯઘͰ࣌ؒΏͬ͘Γਁ͔ΕΔ 17 ΦεεϝͷԹઘࢪઃ͕͋ͬͨΒ
ੋඇڭ͍͑ͯͩ͘͞ʂ