Skip to content

Commit 28f9de3

Browse files
committed
Display CSV files as simple tables
Displays CSV files as a table, horizontally and verically scrollable. No pagination, sorting or filtering as yet.
1 parent 25645a9 commit 28f9de3

File tree

6 files changed

+58
-0
lines changed

6 files changed

+58
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{% load airlocktags %}
2+
{% as_csv_data contents as csv_data %}
3+
<div class="inline-block min-w-full align-middle max-w-full">
4+
{% #table %}
5+
{% #table_head class="bg-slate-200" id="csvTable" %}
6+
{% #table_row %}
7+
{% for header in csv_data.headers %}
8+
{% #table_header %}{{ header }}{% /table_header %}
9+
{% endfor %}
10+
{% /table_row %}
11+
{% /table_head %}
12+
13+
{% #table_body %}
14+
{% for row in csv_data.rows %}
15+
{% #table_row class="even:bg-slate-50" %}
16+
{% for cell in row %}
17+
{% #table_cell %}
18+
{{ cell }}
19+
{% /table_cell %}
20+
{% endfor %}
21+
{% /table_row %}
22+
{% endfor %}
23+
{% /table_body %}
24+
{% /table %}
25+
</div>

airlock/templates/file_browser/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@
182182
style="width: 100%;"
183183
>
184184
</iframe>
185+
{% elif path_item.suffix == '.csv' %}
186+
{% include "file_browser/csv.html" with contents=path_item.contents %}
185187
{% else %}
186188
<pre>{{ path_item.contents }}</pre>
187189
{% endif %}

airlock/templatetags/__init__.py

Whitespace-only changes.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import csv
2+
3+
from django import template
4+
5+
6+
register = template.Library()
7+
8+
9+
@register.simple_tag
10+
def as_csv_data(file_content):
11+
reader = csv.reader(file_content.splitlines(keepends=True))
12+
return {
13+
"headers": next(reader),
14+
"rows": list(reader),
15+
}

tests/integration/test_views.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ def test_workspace_view_with_html_file(client_with_permission):
8484
assert "foobar" in response.rendered_content
8585

8686

87+
def test_workspace_view_with_csv_file(client_with_permission):
88+
factories.write_workspace_file("workspace", "file.csv", "header1,header2\nFoo,Bar")
89+
response = client_with_permission.get("/workspaces/view/workspace/file.csv")
90+
for content in ["<table", "Foo", "Bar"]:
91+
assert content in response.rendered_content
92+
93+
8794
def test_workspace_view_with_404(client_with_permission):
8895
factories.create_workspace("workspace")
8996
response = client_with_permission.get("/workspaces/view/workspace/no_such_file.txt")

tests/unit/test_templatetags.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from airlock.templatetags.airlocktags import as_csv_data
2+
3+
4+
def test_file_content_as_csv_data():
5+
content = "Header1,Header2,Header3\n" "One,Two,Three\n" "Four,Five,Six\n"
6+
assert as_csv_data(content) == {
7+
"headers": ["Header1", "Header2", "Header3"],
8+
"rows": [["One", "Two", "Three"], ["Four", "Five", "Six"]],
9+
}

0 commit comments

Comments
 (0)