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

Django as your data management framework

Python Porto
January 31, 2019

Django as your data management framework

Slides from Python Porto meetup #11. Examples can be found here: https://github.com/imankulov/django-data-management-example

If you come to Python to work with data, most likely you are intimately familiar with its data science ecosystem, which includes Anaconda, Jupyter and Pandas, among others. Probably though you are less familiar with the other side of Python, including frameworks, ORMs and template engines, traditionally used to create web applications.

This talk aims at blurring the border between data science and web development ecosystems. I will show how Django, extremely popular framework for creating web applications, can be successfully applied to some common tasks of data processing and data analysis and how it can be used seamlessly with your tools of choice.

The talk can be interesting for Python data analysts who want to extend their knowledge beyond the traditional DS/ML stack. It can also be helpful for beginners interested in web development with Django.

Python Porto

January 31, 2019
Tweet

More Decks by Python Porto

Other Decks in Programming

Transcript

  1. What can Django give to me? • Project structure •

    Object-relational mapping (ORM) • Admin panel • Model-view-controller framework (MVC)
  2. What can Django give to me? • Project structure •

    Object-relational mapping (ORM) • Admin panel • Model-view-controller framework (MVC)
  3. What can Django give to me? • Project structure •

    Object-relational mapping (ORM) • Admin panel • Model-view-controller framework (MVC)
  4. CREATE TABLE groups ( id BIGINT PRIMARY KEY AUTOINCREMENT, name

    TEXT, urlname TEXT, description TEXT, created DATETIME, … ); > communities/models.py class Group(models.Model): name = models.CharField(…) urlname = models.CharField(…) description = models.TextField() created = models.DateTimeField() … class Meta: db_table = ‘groups’
  5. Get a single object sql> SELECT * FROM groups WHERE

    id = 42 >>> Group.objects.get(id=42) <Group: Data Science Unplugged>
  6. Get a queryset sql> SELECT * FROM groups ... WHERE

    members > 400 ... AND created >= ‘2018-01-01’ ... ORDER BY members DESC ... LIMIT 10;
  7. Get a queryset sql> SELECT * FROM groups ... WHERE

    members > 400 ... AND created >= ‘2018-01-01’ ... ORDER BY members DESC ... LIMIT 10; >>> Group.objects.filter( members__gt=400, created__gt=datetime(2018, 1, 1) ).order_by(‘-members')[:10]
 <QuerySet [<Group: AI Lisbon>, …]>
  8. Convert to DataFrame queryset = Group.objects.filter(…) df = pd.DataFrame.from_records(groups.values()) city

    country ... urlname who 0 Lisbon PT ... ironhack-lisbon Ironhackers 1 Lisbon PT ... AI-Lisbon Members 2 Lisbon PT ... Lisbon-Artificial-Intelligence-Meetup Members 3 Lisbon PT ... Lisbon-Virtual-Currency-Meetup Members 4 Lisbon PT ... Zalando-Tech-Events-Lisbon Members 5 Lisbon PT ... Lisbon-Kaggle Kagglers 6 Lisbon PT ... Sketch-Design-Lisbon Members [7 rows x 13 columns]
  9. What can Django give to me? • Project structure •

    Object-relational mapping (ORM) • Admin panel • Model-view-controller framework (MVC)
  10. What can Django give to me? • Project structure •

    Object-relational mapping (ORM) • Admin panel • Model-view-controller framework (MVC)
  11. What can Django give to me? • Project structure •

    Object-relational mapping (ORM) • Admin panel • Model-view-controller framework (MVC) Model-template-view framework (MTV)
  12. > urls.py urlpatterns = [ path('simple/', views.simple_dashboard), path('input/', views.input_dashboard), …

    ] GET /input/?limit=20&order_by=… <HttpRequest: GET /input/?limit=20&order_by=…> > views.py def simple_dashboard(request): …
  13. <HttpRequest: GET /input/?limit=20&order_by=…> > views.py def input_dashboard(request): form = InputForm(…)

    … df = pd.DataFrame.from_values(…) return render( request, ‘dashboard.html’, {…}) <HttpResponse status_code=200, "text/html; charset=utf-8">
  14. <HttpRequest: GET /input/?limit=20&order_by=…> > views.py def input_dashboard(request): form = InputForm(…)

    … df = pd.DataFrame.from_values(…) return render( request, ‘dashboard.html’, {…}) <HttpResponse status_code=200, "text/html; charset=utf-8"> > templates/dashboard.html <form method="GET" action="."> <table> {{ form.as_table }} </table> <input type="submit" value="Filter"> </form> {{ df.to_html|safe }}
  15. What can Django give to me? • Project structure •

    Object-relational mapping (ORM) • Admin panel • Model-view-controller framework (MVC)
  16. Django hints for data people • Can I use Django

    from Jupyter Notebook? • You can, but you need to initialize your environment properly to make it explicit which project we are working with right now
  17. Django hints for data people • Can I use Django

    from Jupyter Notebook? • You can, but you need to initialize your environment properly to make it explicit which project we are working with right now
  18. Django hints for data people • What is the easiest

    way to display a plot in my Django view? • Use Pygal (https://pygal.org) which draws plots to SVG and put them right in your templates
  19. Django hints for data people • What is the easiest

    way to display a plot in my Django view? • Use Pygal (https://pygal.org) which draws plots to SVG and put them right in your templates > views.py @staff_member_required def plot_dashboard(request): groups = Group.objects.order_by('-members')[:10] df = pd.DataFrame.from_records(groups.values()) bar = pygal.Bar(…) bar.x_labels = df.name bar.add('Members', df.members) return render(request, 'plot_dashboard.html', { 'df': df, 'bar': bar.render(is_unicode=True), })
  20. Django hints for data people • What is the easiest

    way to display a plot in my Django view? • Use Pygal (https://pygal.org) which draws plots to SVG and put them right in your templates > views.py @staff_member_required def plot_dashboard(request): groups = Group.objects.order_by('-members')[:10] df = pd.DataFrame.from_records(groups.values()) bar = pygal.Bar(…) bar.x_labels = df.name bar.add('Members', df.members) return render(request, 'plot_dashboard.html', { 'df': df, 'bar': bar.render(is_unicode=True), }) > templates/plot_dashboard.html {{ bar|safe }} {{ df.to_html|safe }}
  21. Django hints for data people • What is the easiest

    way to display a plot in my Django view? • Use Pygal (https://pygal.org) which draws plots to SVG and put them right in your templates > views.py @staff_member_required def plot_dashboard(request): groups = Group.objects.order_by('-members')[:10] df = pd.DataFrame.from_records(groups.values()) bar = pygal.Bar(…) bar.x_labels = df.name bar.add('Members', df.members) return render(request, 'plot_dashboard.html', { 'df': df, 'bar': bar.render(is_unicode=True), }) > templates/plot_dashboard.html {{ bar|safe }} {{ df.to_html|safe }}
  22. Django hints for data people • Can I serve my

    Jupyter Notebooks with Django • It’s not straightforward, but you can convert them to HTML with nbconvert and serve this way
  23. Django hints for data people • Can I serve my

    Jupyter Notebooks with Django • It’s not straightforward, but you can convert them to HTML with nbconvert and serve this way > views.py class StringWriter(WriterBase): content = None def write(self, output, resources, **kw): self.content = output @staff_member_required def notebook(request): exporter = HTMLExporter( exclude_input=True, exclude_output_prompt=True) writer = StringWriter() app = NbConvertApp(writer=writer, exporter=exporter) app.convert_single_notebook(‘filename.ipynb’) return HttpResponse(writer.content)
  24. Django hints for data people • Can I serve my

    Jupyter Notebooks with Django • It’s not straightforward, but you can convert them to HTML with nbconvert and serve this way > views.py class StringWriter(WriterBase): content = None def write(self, output, resources, **kw): self.content = output @staff_member_required def notebook(request): exporter = HTMLExporter( exclude_input=True, exclude_output_prompt=True) writer = StringWriter() app = NbConvertApp(writer=writer, exporter=exporter) app.convert_single_notebook(‘filename.ipynb’) return HttpResponse(writer.content)
  25. Django hints for data people • How can I initiate

    long-running tasks from Django views? • Offload these tasks to workers. For example, use Celery 
 (http://celeryproject.org/)