Beginning PyData with Django and Jupyter

685c9b622b253f8a53a8ef99b0453fb9?s=47 kuma127
September 17, 2019

Beginning PyData with Django and Jupyter

PyCon JP 2019 9/17 16:15~

685c9b622b253f8a53a8ef99b0453fb9?s=128

kuma127

September 17, 2019
Tweet

Transcript

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

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

    ✤ #pyconjp ॳొஃͰ͢ʂυۓுͯ͠·͕͓ٓ͘͢͠ئ͍ ͠·͢ʂ
  3. ຊ೔ͷ͓୊

  4. DjangoͱJupyterͰ࢝ΊΔPyData

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

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

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

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

  9. ΞδΣϯμ ✤ ࿩͢͜ͱ͸ԼهΛ༧ఆ͍ͯ͠·͢ 1. DjangoͱJupyterͷ؆୯ͳ঺հ 2. ਪ͍ͨ͠ཧ༝ 3. Django×Jupyterͷ૊Έ߹Θ͞ΔΠϝʔδ 4.

    ؀ڥߏஙํ๏ 5. ΦεεϝϥΠϒϥϦɾϓϥάΠϯҰྫ
  10. DjangoͱJupyterͷ؆୯ͳ঺հ

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

  12. Jupyterʹ͍ͭͯ ✤ ࠓճ͸Jupyterͷఏڙ͢ΔαʔϏεͷ಺
 ʮJupyter NotebookʯΛର৅ɻ ✤ Jupyter Notebook͸ɺίʔυͷଈ࣮࣌ߦ΍ՄࢹԽɺυ Ωϡϝϯτͷ࡞੒͕ߦ͑ΔWebΞϓϦέʔγϣϯɻ

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

  14. ਪ͍ͨ͠ཧ༝ 1. ϥΠϒϥϦΛ1ճΠϯετʔϧ͢Δ͚ͩ 2. DjangoٴͼJupyterͷϓϥάΠϯ͕๛෋
 ˠΧόʔͰ͖Δൣғ͕޿͘ͳΔ

  15. ͭ·Γ… ✤ खࠒʹDjango(Web)ͱJupyter(Data)ͷೋ౛ྲྀͳ
 ສೳ؀ڥ͕࡞ΕΔ

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

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

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

  19. JupyterͰ௚઀DBʹͭͳ͙࣌ͷ໰୊఺ ✤ ඞཁͳॲཧ͕ଟ͍
 - Jupyter্Ͱೝূઃఆ΍
 ɹ઀ଓॲཧ͕ඞཁ
 - جຊతʹSQLͰσʔλͷ
 ɹ΍ΓऔΓΛߦ͏ඞཁ͋Γ

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

  21. DBར༻͕؆୯ʹ ✤ ࡞ۀͷ؆ུԽ
 - ઀ଓઃఆ͸ɺ
 վΊͯ΍Δඞཁͳ͠
 - DjangoͷORM͕࢖͑ΔͷͰɺ
 σʔλॲཧ΋ϥΫ

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

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

  24. εΫϦϓτΛརศతʹ࣮ߦͰ͖Δ ✤ WebΞϓϦ্ͷσʔλΛCSV౳ʹՃ޻͢Δɺ
 ͱ͍ͬͨ࡞ۀ΋ஞҰڍಈΛ֬ೝ͠ͳ͕Β࣮ߦͰ͖Δ ✤ pandas΋࢖͑ΔͷͰՃ޻΋ϥΫ ✤ ※౰વ࠷ॳ͸ςετ؀ڥͰ࣮ߦ͢Δɺͱ͍ͬͨ࠷௿ݶͷ ഑ྀ͸ඞཁ

  25. ·ͱΊΔͱ… ✤ Web্ͷσʔλΛѻ͏ͨΊͷɺ
 ΠϯλʔϑΣʔεʹ
 Jupyter͕ͳͬͯ͘Ε͍ͯΔʂ

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

  27. Djangoͷ؀ڥߏங

  28. Djangoͷ؀ڥߏங ✤ جຊతʹ͸ެࣜαΠτ௨Γͷߏஙํ๏ͰOK ✤ $ pip install django ✤ $

    conda install django (Anacondaͷ৔߹) ✤ ※͍ͣΕͷ৔߹΋Ծ૝؀ڥΛཱͯͯΠϯετʔϧ͢Δ ͷ͕Φεεϝ
  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/)
  30. Jupyterͷಋೖ

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

    django-extensions ✤ setting.pyͷINSTALLED_APPSʹҎԼΛ௥Ճ
 ‘django_extensions' ✤ ࢀߟɿhttps://github.com/django-extensions/django-extensions
  32. Jupyterͷىಈ΋ϥΫνϯ ✤ manage.pyͷ͋ΔϑΥϧμͰҎԼίϚϯυΛ࣮ߦ
 $ python manage.py shell_plus —notebook ✤ ىಈʹ੒ޭͨ͠Βɺදࣔ͞ΕͨURLΛϒϥ΢βͰΞΫηε

    ✤ $ jupyter notebook Λ࣮ߦ͢Δͷͱ΄΅ಉ͡ʂ
  33. DjangoͷϞσϧΛJupyterͰར༻͢Δ ✤ Jupyter্ͰϞσϧΛimport͢Δ͚ͩͰ
 ௨ৗ௨ΓϞσϧ͕ར༻Մೳʂ ✤ ෳࡶͳઃఆ౳͸ෆཁ ✤ ※؀ڥʹΑͬͯ͸Լه4ߦͷ࣮ߦ͕ඞཁʹͳΓ·͢

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

  35. ʢ͓·͚ʣDockerͷઃఆͱ஫ҙ ✤ جຊతͳ࡞Γํ͸ࠓճ͸ׂѪɺ
 ৄ͘͠͸DockerެࣜυΩϡϝϯτʹͯ
 ʢhttps://docs.docker.com/compose/django/ʣ ✤ Dockerʹͯઌड़ͷJupyterͷಋೖ·Ͱߦ͏ࡍɺ
 Jupyter༻ͷϙʔτͷ͚͕݀͋ඞਢͳͷͰ஫ҙ
 ΞΫηεͰ͖ͳ͘ͳΔʢܦݧஊʣ

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

  37. ΦεεϝϥΠϒϥϦɾϓϥάΠϯͷҰྫ ✤ Django΍JupyterΛར༻͢Δ্Ͱ
 ໾ཱͭϥΠϒϥϦ΍ϓϥάΠϯΛग़དྷΔݶΓ঺հ ✤ ଞʹ΋ͨ͘͞Μ͋ΔͷͰɺڵຯΛ࣋ͬͨํ͸ௐ΂ͯΈ ͍ͯͩ͘͞

  38. PyDataʹ͓͍ͯͷΦεεϝ ✤ Numpy
 ଟ࣍ݩ഑ྻΛαϙʔτ͢Δɺ਺஋ܭࢉ༻Ϟδϡʔϧ
 ػցֶशͷԋࢉ౳ʹศར ✤ pandas
 σʔλϑϨʔϜܗࣜͷ഑ྻΛఏڙ͢ΔϥΠϒϥϦ
 σʔλͷՄࢹԽɾՃ޻ʹ༏ΕΔ ✤

    matplotlib
 Python΍NumpyͷͨΊͷάϥϑඳըϥΠϒϥϦ ✤ ※ػցֶशपΓͷπʔϧ͸ࢲ͕ະܦݧͷͨΊ֎͍ͯ͠·͢…
  39. Jupyterʹ͓͍ͯͷΦεεϝ ✤ nbextensions
 Jupyterʹ֦ுػೳΛಋೖग़དྷΔ
 LaTeXͷ؀ڥͩͬͨΓίʔυ੔ܗͩͬͨΓ ✤ RISE
 JupyterΛεϥΠυܗࣜʹม׵Ͱ͖Δ
 ඳըͨ͠άϥϑΛͦͷ··εϥΠυࢿྉʹ͢Δ࣌ͳͲʹΦεεϝ

  40. Djangoʹ͓͍ͯͷΦεεϝ ✤ Django REST Framework
 Web API Λߏங͢ΔͨΊͷπʔϧ
 ໘౗ͳAPIͷઃఆ౳͕؆ུԽͰ͖Δ ✤

    Django-filter
 ↑ͷDRFͱ૊Έ߹ΘͤͯݕࡧػೳΛ௥ՃͰ͖Δ ✤ Django-pandas
 ϞσϧͷσʔλΛσʔλϑϨʔϜܗࣜʹม׵Ͱ͖Δ
 ͦͯ͠σʔλͷՄࢹԽɾՃ޻ʹར༻Ͱ͖Δ
  41. ࠷ޙʹ

  42. Django×JupyterΛ࢖ͬͯΈͯ ✤ Webͱσʔλ૒ํͷ஌ݟΛಘΕͨ
 - σʔλॲཧ΋ɺWeb։ൃ΋ಉ͡؀ڥͰͰ͖Δ
 - Ξ΢τϓοτͷੵΈॏͶ͕ՄࢹԽ
 - Ϟνϕʔγϣϯͷҡ࣋ͱɺ࣮ӡ༻Λҙֶࣝͨ͠श ✤

    ͜Ε͔ΒPyDataΛ࢝ΊΔͳΒɺͥͻDjango×JupyterΛʂ
  43. Ҏ্ɺ͝੩ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ʂ