Skip to content

Commit

Permalink
visualize your data using django-excel
Browse files Browse the repository at this point in the history
  • Loading branch information
chfw committed Jul 7, 2017
1 parent 499d60a commit 0ee6019
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 14 deletions.
27 changes: 25 additions & 2 deletions .moban.d/docs/source/index.rst.jj2
Expand Up @@ -197,9 +197,9 @@ exported as a single sheet file.
Render an excel-alike html in a browser
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

In the tutorial, you have seen the rendering of the excel-alike table. First of
In previous section, you have seen the rendering of the excel-alike table. First of
all, the credits goes to `handsontable developers <https://handsontable.com/>`_.
`pyexcel-handsontable` as renderer plugin to pyexcel v0.5.0 bring it to
`pyexcel-handsontable`_ as renderer plugin to pyexcel v0.5.0 bring it to
pyexcel developers.

Here is how it is done. Simply put in 'handsontable.html' instead of 'xls' as
Expand Down Expand Up @@ -299,6 +299,29 @@ set exported as a single sheet file as:
.. image:: custom-export.png


Visualize your data
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Let's go to the admin page and update some votes for the choices.

.. image:: admin-vote.png

In my case, I have updated all of them and have gotten something like this:

.. image:: votes-handson-table.png

Now, let's look at the survey result(http://localhost:8000/polls/survey_result/)
for "What's your favorite IDE?":

.. image:: survey-result.png

`pyexcel-pygal`_ provide you the common data visualization capability to show
your data intuitively. Here is the code to achieve that:

.. literalinclude:: ../../polls/views.py
:lines: 192-217


{%endblock%}

{%block api_table%}
Expand Down
1 change: 1 addition & 0 deletions .moban.d/tests/requirements.txt
Expand Up @@ -5,4 +5,5 @@ pyexcel-ods3>=0.1.0
pyexcel-xls>=0.1.0
pyexcel-xlsx>=0.1.0
pyexcel-handsontable
pyexcel-pygal
{%endblock%}
9 changes: 3 additions & 6 deletions README.rst
Expand Up @@ -11,9 +11,6 @@ django-excel - Let you focus on data, instead of file formats
.. image:: https://codecov.io/github/pyexcel/django-excel/coverage.png
:target: https://codecov.io/github/pyexcel/django-excel

.. image:: https://img.shields.io/gitter/room/gitterHQ/gitter.svg
:target: https://gitter.im/pyexcel/Lobby

.. image:: https://readthedocs.org/projects/django-excel/badge/?version=latest
:target: http://django-excel.readthedocs.org/en/latest/

Expand Down Expand Up @@ -79,12 +76,11 @@ The highlighted features are:
`pyexcel-ods`_ ods `odfpy`_ same as above
`pyexcel-odsr`_ ods(read only) lxml same as above
`pyexcel-text`_ (write only)json, rst, `tabulate`_ 2.6, 2.7, 3.3, 3.4
mediawiki, html, 3.5, 3.6, pypy
mediawiki, html, 3.5, pypy, pypy3
latex, grid, pipe,
orgtbl, plain simple
`pyexcel-handsontable`_ handsontable in html `handsontable`_ same as above
`pyexcel-pygal`_ svg chart `pygal`_ 2.7, 3.3, 3.4, 3.5
3.6, pypy
`pyexcel-pygal`_ svg chart `pygal`_ same as above [#f3]_
======================== ======================= =============== ==================

.. _pyexcel-io: https://github.com/pyexcel/pyexcel-io
Expand All @@ -111,6 +107,7 @@ The highlighted features are:
.. _pyexcel-matplotlib: https://github.com/pyexcel/pyexcel-matplotlib
.. _matplotlib: https://matplotlib.org

.. [#f3] coming soon
In order to manage the list of plugins installed, you need to use pip to add or remove
a plugin. When you use virtualenv, you can have different plugins per virtual
Expand Down
Binary file added doc/source/admin-vote.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 24 additions & 5 deletions doc/source/index.rst
Expand Up @@ -71,12 +71,11 @@ The highlighted features are:
`pyexcel-ods`_ ods `odfpy`_ same as above
`pyexcel-odsr`_ ods(read only) lxml same as above
`pyexcel-text`_ (write only)json, rst, `tabulate`_ 2.6, 2.7, 3.3, 3.4
mediawiki, html, 3.5, 3.6, pypy
mediawiki, html, 3.5, pypy, pypy3
latex, grid, pipe,
orgtbl, plain simple
`pyexcel-handsontable`_ handsontable in html `handsontable`_ same as above
`pyexcel-pygal`_ svg chart `pygal`_ 2.7, 3.3, 3.4, 3.5
3.6, pypy
`pyexcel-pygal`_ svg chart `pygal`_ same as above [#f3]_
======================== ======================= =============== ==================

.. _pyexcel-io: https://github.com/pyexcel/pyexcel-io
Expand All @@ -103,6 +102,7 @@ The highlighted features are:
.. _pyexcel-matplotlib: https://github.com/pyexcel/pyexcel-matplotlib
.. _matplotlib: https://matplotlib.org

.. [#f3] coming soon
In order to manage the list of plugins installed, you need to use pip to add or remove
a plugin. When you use virtualenv, you can have different plugins per virtual
Expand Down Expand Up @@ -377,9 +377,9 @@ exported as a single sheet file.
Render an excel-alike html in a browser
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

In the tutorial, you have seen the rendering of the excel-alike table. First of
In previous section, you have seen the rendering of the excel-alike table. First of
all, the credits goes to `handsontable developers <https://handsontable.com/>`_.
`pyexcel-handsontable` as renderer plugin to pyexcel v0.5.0 bring it to
`pyexcel-handsontable`_ as renderer plugin to pyexcel v0.5.0 bring it to
pyexcel developers.

Here is how it is done. Simply put in 'handsontable.html' instead of 'xls' as
Expand Down Expand Up @@ -479,6 +479,25 @@ set exported as a single sheet file as:
.. image:: custom-export.png


Visualize your data
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Let's go to the admin page and update some votes for the choices. In my case,
I have got something like this:

.. image:: votes-handson-table.png

Now, let's look at the survey result for "What's your favorite IDE?":

.. image:: survey-result.png

`pyexcel-pygal`_ provide you the common data visualization capability to show
your data intuitively. Here is the code to achieve that:

.. literalinclude:: ../../polls/views.py
:lines: 192-212


.. _data-types-and-its-conversion-funcs:

All supported data types
Expand Down
Binary file added doc/source/survey-result.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/source/votes-handson-table.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 4 additions & 1 deletion polls/urls.py
Expand Up @@ -19,5 +19,8 @@
views.embed_handson_table, name="embed_handson_view"),
url(r'^embedded_handson_view_single/',
views.embed_handson_table_from_a_single_table,
name="embed_handson_view")
name="embed_handson_view"),
# survey_result
url('^survey_result/',
views.survey_result, name='survey_result'),
]
28 changes: 28 additions & 0 deletions polls/views.py
Expand Up @@ -187,3 +187,31 @@ def embed_handson_table_from_a_single_table(request):
{
'handsontable_content': content.read()
})


def survey_result(request):
question = Question.objects.get(slug='ide')
query_sets = Choice.objects.filter(question=question)
column_names = ['choice_text', 'votes']

# Obtain a pyexcel sheet from the query sets
sheet = excel.pe.get_sheet(query_sets=query_sets,
column_names=column_names)
sheet.name_columns_by_row(0)
sheet.column.format('votes', int)

# Transform the sheet into an svg chart
svg = excel.pe.save_as(
array=[sheet.column['choice_text'], sheet.column['votes']],
dest_file_type='svg',
dest_chart_type='pie',
dest_title=question.question_text,
dest_width=600,
dest_height=400
)

return render(
request,
'survey_result.html',
dict(svg=svg.read())
)
5 changes: 5 additions & 0 deletions templates/survey_result.html
@@ -0,0 +1,5 @@
<html>
<body>
{{svg|safe}}
</body>
</html>
1 change: 1 addition & 0 deletions tests/requirements.txt
Expand Up @@ -8,3 +8,4 @@ pyexcel-ods3>=0.1.0
pyexcel-xls>=0.1.0
pyexcel-xlsx>=0.1.0
pyexcel-handsontable
pyexcel-pygal

0 comments on commit 0ee6019

Please sign in to comment.