Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Beginning PyData with Django and Jupyter

kuma127
September 17, 2019

Beginning PyData with Django and Jupyter

PyCon JP 2019 9/17 16:15~

kuma127

September 17, 2019
Tweet

More Decks by kuma127

Other Decks in Programming

Transcript

  1. 2019/9/17
    DjangoͱJupyterͰ࢝ΊΔPyData
    ۽୩ ୓໵
    TwitterIDɿ@kumappp27

    View full-size slide

  2. ͓લɺ୭Α
    ✤ ʮ۽୩ʯʢϋϯυϧωʔϜɿ͘·ͽʣͱ͍͍·͢
    ✤ TwitterID:@kumappp27
    ✤ ਓࡐܥͷձࣾͷWebΤϯδχΞ
    ✤ Python͸झຯ։ൃͰΑ͘ར༻ɺ

    େ޷͖ͳ໺ٿͷγεςϜΛ࡞੒
    ✤ #pyconjp ॳొஃͰ͢ʂυۓுͯ͠·͕͓ٓ͘͢͠ئ͍
    ͠·͢ʂ

    View full-size slide

  3. ຊ೔ͷ͓୊

    View full-size slide

  4. DjangoͱJupyterͰ࢝ΊΔPyData

    View full-size slide

  5. ൃදʹࢸ͖͔͚ͬͨͬᶃ
    ✤ ໺ٿͷσʔλ෼ੳ͕ͨ͘͠Pythonελʔτ
    ✤ Jupyter NotebookΛѪ༻
    ✤ Webͷ஌ࣝ΋໾ཱͭ͜ͱ͕൑໌ʢεΫϨΠϐϯάͱ͔

    View full-size slide

  6. Web΋Data΋ɺͲͬͪ΋΍Γ͍ͨʂ

    View full-size slide

  7. ൃදʹࢸ͖͔͚ͬͨͬᶄ
    ✤ ௐ΂ͯΈͨΒ
    DjangoͷϞσϧΛJupyter্Ͱ࢖͑Δ
    ✤ ͔͠΋؆୯ɺͦͯ͠ສೳʂ
    ✤ ͜Ε͸ར༻ɾڞ༗͢Δ͔͠ͳ͍ʂ

    View full-size slide

  8. ຊτʔΫͷ໨త
    ✤ Django×Jupyterͳ؀ڥͷྑ͞Λ఻͑Δ͜ͱ
    ✤ ΦʔϧΠϯϫϯ؀ڥͰ࡞ۀ΋ֶश΋ḿΔ͜ͱ

    View full-size slide

  9. ΞδΣϯμ
    ✤ ࿩͢͜ͱ͸ԼهΛ༧ఆ͍ͯ͠·͢
    1. DjangoͱJupyterͷ؆୯ͳ঺հ
    2. ਪ͍ͨ͠ཧ༝
    3. Django×Jupyterͷ૊Έ߹Θ͞ΔΠϝʔδ
    4. ؀ڥߏஙํ๏
    5. ΦεεϝϥΠϒϥϦɾϓϥάΠϯҰྫ

    View full-size slide

  10. DjangoͱJupyterͷ؆୯ͳ঺հ

    View full-size slide

  11. Djangoʹ͍ͭͯ
    ✤ Djangoʢδϟϯΰʣͱ͸ɺPythonͰ࣮૷͞ΕͨWeb
    ϑϨʔϜϫʔΫɻMVCσβΠϯύλʔϯʹ؇΍͔ʹै
    ͏ɻʢWikipediaΑΓʣ
    ✤ ڧྗͳORM͕͋Δ͜ͱͱɺϑϧελοΫͰ͋Δ఺͕
    ࠾༻ཧ༝ɻ

    View full-size slide

  12. Jupyterʹ͍ͭͯ
    ✤ ࠓճ͸Jupyterͷఏڙ͢ΔαʔϏεͷ಺

    ʮJupyter NotebookʯΛର৅ɻ
    ✤ Jupyter Notebook͸ɺίʔυͷଈ࣮࣌ߦ΍ՄࢹԽɺυ
    Ωϡϝϯτͷ࡞੒͕ߦ͑ΔWebΞϓϦέʔγϣϯɻ

    View full-size slide

  13. Django×JupyterΛਪ͍ͨ͠ཧ༝

    View full-size slide

  14. ਪ͍ͨ͠ཧ༝
    1. ϥΠϒϥϦΛ1ճΠϯετʔϧ͢Δ͚ͩ
    2. DjangoٴͼJupyterͷϓϥάΠϯ͕๛෋

    ˠΧόʔͰ͖Δൣғ͕޿͘ͳΔ

    View full-size slide

  15. ͭ·Γ…
    ✤ खࠒʹDjango(Web)ͱJupyter(Data)ͷೋ౛ྲྀͳ

    ສೳ؀ڥ͕࡞ΕΔ

    View full-size slide

  16. Django×Jupyterͷ૊Έ߹Θ͞ΔΠϝʔδ

    View full-size slide

  17. DjangoͱJupyter͕૊Έ߹Θ͞ΔΠϝʔδ
    ✤ DjangoͷϞσϧΛJupyter্Ͱ࢖͑Δʮ͚ͩʯ
    ✤ ओʹDjangoͷϞσϧΛར༻ͨ͠ڧྗͳORMͷԸܙΛ
    Jupyter্Ͱत͔Δܗ

    View full-size slide

  18. ຊདྷJupyterͰDBʹΞΫηε͢Δ࣌
    ✤ Jupyter্Ͱɺ௚઀DBʹͭͳ͙Πϝʔδ
    SQL౳

    View full-size slide

  19. JupyterͰ௚઀DBʹͭͳ͙࣌ͷ໰୊఺
    ✤ ඞཁͳॲཧ͕ଟ͍

    - Jupyter্Ͱೝূઃఆ΍

    ɹ઀ଓॲཧ͕ඞཁ

    - جຊతʹSQLͰσʔλͷ

    ɹ΍ΓऔΓΛߦ͏ඞཁ͋Γ

    View full-size slide

  20. Django×JupyterͰ͸
    ✤ DjangoΛհͯ͠ɺDBʹΞΫηε͢ΔΠϝʔδ
    ORM

    View full-size slide

  21. DBར༻͕؆୯ʹ
    ✤ ࡞ۀͷ؆ུԽ

    - ઀ଓઃఆ͸ɺ

    վΊͯ΍Δඞཁͳ͠

    - DjangoͷORM͕࢖͑ΔͷͰɺ

    σʔλॲཧ΋ϥΫ

    View full-size slide

  22. ૊Έ߹ΘͤΔͱ͜Μͳ͜ͱ͕ग़དྷΔ

    View full-size slide

  23. WebΞϓϦσʔλΛαΫοͱ෼ੳ
    ✤ WebΞϓϦͷσʔλΛɺͦͷ··Jupyter্Ͱར༻Ͱ͖Δ
    ✤ طଘ؀ڥʹ΋େ͖ͳӨڹ͸ͳ͠
    ✤ άϥϑදࣔ΍εϥΠυԽ΋༰қ

    View full-size slide

  24. εΫϦϓτΛརศతʹ࣮ߦͰ͖Δ
    ✤ WebΞϓϦ্ͷσʔλΛCSV౳ʹՃ޻͢Δɺ

    ͱ͍ͬͨ࡞ۀ΋ஞҰڍಈΛ֬ೝ͠ͳ͕Β࣮ߦͰ͖Δ
    ✤ pandas΋࢖͑ΔͷͰՃ޻΋ϥΫ
    ✤ ※౰વ࠷ॳ͸ςετ؀ڥͰ࣮ߦ͢Δɺͱ͍ͬͨ࠷௿ݶͷ
    ഑ྀ͸ඞཁ

    View full-size slide

  25. ·ͱΊΔͱ…
    ✤ Web্ͷσʔλΛѻ͏ͨΊͷɺ

    ΠϯλʔϑΣʔεʹ

    Jupyter͕ͳͬͯ͘Ε͍ͯΔʂ

    View full-size slide

  26. Django×Jupyterͷ؀ڥߏஙํ๏

    View full-size slide

  27. Djangoͷ؀ڥߏங

    View full-size slide

  28. Djangoͷ؀ڥߏங
    ✤ جຊతʹ͸ެࣜαΠτ௨Γͷߏஙํ๏ͰOK
    ✤ $ pip install django
    ✤ $ conda install django (Anacondaͷ৔߹)
    ✤ ※͍ͣΕͷ৔߹΋Ծ૝؀ڥΛཱͯͯΠϯετʔϧ͢Δ
    ͷ͕Φεεϝ

    View full-size slide

  29. ΞϓϦͷ࡞੒
    ✤ JupyterΛಋೖ͢ΔͨΊʹɺͱΓ͋͑ͣΞϓϦΛ࡞੒

    $ django-admin startproject test_project

    $ cd test_project

    $ python manage.py startapp test_app
    ✤ ʮtest_projectʯͱ͍͏ϓϩδΣΫτʹʮtest_appʯͱ
    ͍͏ΞϓϦΛ࡞੒
    ✤ ※ৄ͘͠͸ެࣜυΩϡϝϯτࢀর

    (https://docs.djangoproject.com/ja/2.2/)

    View full-size slide

  30. Jupyterͷಋೖ

    View full-size slide

  31. ಋೖ͸ͱͬͯ΋؆୯
    ✤ django-extensions ΛΠϯετʔϧ͢Δ͚ͩʂ
    ✤ pipͰΠϯετʔϧ͢Δ৔߹͸ 

    $ pip install django-extensions
    ✤ setting.pyͷINSTALLED_APPSʹҎԼΛ௥Ճ

    ‘django_extensions'
    ✤ ࢀߟɿhttps://github.com/django-extensions/django-extensions

    View full-size slide

  32. Jupyterͷىಈ΋ϥΫνϯ
    ✤ manage.pyͷ͋ΔϑΥϧμͰҎԼίϚϯυΛ࣮ߦ

    $ python manage.py shell_plus —notebook
    ✤ ىಈʹ੒ޭͨ͠Βɺදࣔ͞ΕͨURLΛϒϥ΢βͰΞΫηε
    ✤ $ jupyter notebook Λ࣮ߦ͢Δͷͱ΄΅ಉ͡ʂ

    View full-size slide

  33. DjangoͷϞσϧΛJupyterͰར༻͢Δ
    ✤ Jupyter্ͰϞσϧΛimport͢Δ͚ͩͰ

    ௨ৗ௨ΓϞσϧ͕ར༻Մೳʂ
    ✤ ෳࡶͳઃఆ౳͸ෆཁ
    ✤ ※؀ڥʹΑͬͯ͸Լه4ߦͷ࣮ߦ͕ඞཁʹͳΓ·͢

    View full-size slide

  34. ར༻Πϝʔδ
    ✤ DjangoͷORMͰऔಘͨ͠σʔλΛσʔλϑϨʔϜʹม׵

    View full-size slide

  35. ʢ͓·͚ʣDockerͷઃఆͱ஫ҙ
    ✤ جຊతͳ࡞Γํ͸ࠓճ͸ׂѪɺ

    ৄ͘͠͸DockerެࣜυΩϡϝϯτʹͯ

    ʢhttps://docs.docker.com/compose/django/ʣ
    ✤ Dockerʹͯઌड़ͷJupyterͷಋೖ·Ͱߦ͏ࡍɺ

    Jupyter༻ͷϙʔτͷ͚͕݀͋ඞਢͳͷͰ஫ҙ

    ΞΫηεͰ͖ͳ͘ͳΔʢܦݧஊʣ

    View full-size slide

  36. ΦεεϝϥΠϒϥϦɾϓϥάΠϯҰྫ

    View full-size slide

  37. ΦεεϝϥΠϒϥϦɾϓϥάΠϯͷҰྫ
    ✤ Django΍JupyterΛར༻͢Δ্Ͱ

    ໾ཱͭϥΠϒϥϦ΍ϓϥάΠϯΛग़དྷΔݶΓ঺հ
    ✤ ଞʹ΋ͨ͘͞Μ͋ΔͷͰɺڵຯΛ࣋ͬͨํ͸ௐ΂ͯΈ
    ͍ͯͩ͘͞

    View full-size slide

  38. PyDataʹ͓͍ͯͷΦεεϝ
    ✤ Numpy

    ଟ࣍ݩ഑ྻΛαϙʔτ͢Δɺ਺஋ܭࢉ༻Ϟδϡʔϧ

    ػցֶशͷԋࢉ౳ʹศར
    ✤ pandas

    σʔλϑϨʔϜܗࣜͷ഑ྻΛఏڙ͢ΔϥΠϒϥϦ

    σʔλͷՄࢹԽɾՃ޻ʹ༏ΕΔ
    ✤ matplotlib

    Python΍NumpyͷͨΊͷάϥϑඳըϥΠϒϥϦ
    ✤ ※ػցֶशपΓͷπʔϧ͸ࢲ͕ະܦݧͷͨΊ֎͍ͯ͠·͢…

    View full-size slide

  39. Jupyterʹ͓͍ͯͷΦεεϝ
    ✤ nbextensions

    Jupyterʹ֦ுػೳΛಋೖग़དྷΔ

    LaTeXͷ؀ڥͩͬͨΓίʔυ੔ܗͩͬͨΓ
    ✤ RISE

    JupyterΛεϥΠυܗࣜʹม׵Ͱ͖Δ

    ඳըͨ͠άϥϑΛͦͷ··εϥΠυࢿྉʹ͢Δ࣌ͳͲʹΦεεϝ

    View full-size slide

  40. Djangoʹ͓͍ͯͷΦεεϝ
    ✤ Django REST Framework

    Web API Λߏங͢ΔͨΊͷπʔϧ

    ໘౗ͳAPIͷઃఆ౳͕؆ུԽͰ͖Δ
    ✤ Django-filter

    ↑ͷDRFͱ૊Έ߹ΘͤͯݕࡧػೳΛ௥ՃͰ͖Δ
    ✤ Django-pandas

    ϞσϧͷσʔλΛσʔλϑϨʔϜܗࣜʹม׵Ͱ͖Δ

    ͦͯ͠σʔλͷՄࢹԽɾՃ޻ʹར༻Ͱ͖Δ

    View full-size slide

  41. Django×JupyterΛ࢖ͬͯΈͯ
    ✤ Webͱσʔλ૒ํͷ஌ݟΛಘΕͨ

    - σʔλॲཧ΋ɺWeb։ൃ΋ಉ͡؀ڥͰͰ͖Δ

    - Ξ΢τϓοτͷੵΈॏͶ͕ՄࢹԽ

    - Ϟνϕʔγϣϯͷҡ࣋ͱɺ࣮ӡ༻Λҙֶࣝͨ͠श
    ✤ ͜Ε͔ΒPyDataΛ࢝ΊΔͳΒɺͥͻDjango×JupyterΛʂ

    View full-size slide

  42. Ҏ্ɺ͝੩ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ʂ

    View full-size slide