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
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
しみず ゆうき
July 15, 2021
Programming
510
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
FastAPIで始める やまなし温泉めぐり
2021.7.15
信玄パイ LT大会 みんなの FastAPI LT
しみず ゆうき
July 15, 2021
More Decks by しみず ゆうき
See All by しみず ゆうき
12年前のAndroidアプリを蘇らせた話
yuukis
0
27
EventBridgeで地域イベントのイベントストリームを作ってみた
yuukis
0
120
2025年 山梨の技術コミュニティを振り返る
yuukis
0
410
サイトを作ったらNFCタグキーホルダーを爆速で作れ!
yuukis
0
1.2k
Perlで痩せる
yuukis
1
880
Pythonで遊べるLEDボードの紹介
yuukis
0
140
今話題のMCPサーバーをFastAPIでサッと作ってみた
yuukis
0
680
地域ITコミュニティの活性化とAWSに移行してみた話
yuukis
0
600
Gemini API を試した話と山梨でオススメのものを語るLT
yuukis
0
110
Other Decks in Programming
See All in Programming
Datadog × OpenTelemetry 入門と実践のあいだ
kn_to_maxpno
1
180
Webフレームワークの ベンチマークについて
yusukebe
0
180
メソッドのジェネリクスでGoの夢は広がるか? / Kyoto.go #65
utgwkk
3
960
Hunting Vulnerabilities in Symfony with LLMs
vinceamstoutz
0
560
AI時代のUIはどこへ行く?その2!
yusukebe
22
7.5k
才能?センス?知らん、 続けたもん勝ちだ。-- 結婚・出産・癌を越えてなお、私がプロダクトを創り続ける理由
16bitidol
1
450
IBM Bobを活用したレガシーアプリの最新化
oniak3ibm
PRO
1
220
Lessons from Spec-Driven Development
simas
PRO
0
220
任せる範囲はこう広がった / How the Scope of AI Delegation Has Expanded
nrslib
0
130
Javaの型とAI時代に型が大事な理由 / java types and type in AI era
kishida
2
150
Make SRE Operations Easier with Azure SRE Agent
kkamegawa
0
8.3k
トークンをケチるな、設計しろ:GitHub Copilotを賢く使うコンテキスト戦略
ochtum
0
190
Featured
See All Featured
AI Search: Implications for SEO and How to Move Forward - #ShenzhenSEOConference
aleyda
1
1.3k
Building a Scalable Design System with Sketch
lauravandoore
463
34k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
254
22k
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
370
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.5k
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
62
44k
Building Flexible Design Systems
yeseniaperezcruz
330
40k
Large-scale JavaScript Application Architecture
addyosmani
515
110k
Writing Fast Ruby
sferik
630
63k
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
62
55k
GraphQLの誤解/rethinking-graphql
sonatard
75
12k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
62k
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 ΦεεϝͷԹઘࢪઃ͕͋ͬͨΒ
ੋඇڭ͍͑ͯͩ͘͞ʂ