Skip to content

Commit 31803dd

Browse files
authored
Multi-query tables (#234)
1 parent a574aed commit 31803dd

File tree

7 files changed

+47
-24
lines changed

7 files changed

+47
-24
lines changed

pgml-dashboard/app/apps.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ class AppConfig(AppConfig):
77
name = "app"
88

99
def ready(self):
10-
register_serializer('yml', 'django.core.serializers.pyyaml')
10+
register_serializer("yml", "django.core.serializers.pyyaml")

pgml-dashboard/app/static/css/base.css

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,8 +606,12 @@ main turbo-frame:first-of-type .notebook-cell {
606606
margin-top: 1rem;
607607
}
608608

609+
.notebook-rendering .markdown-body {
610+
margin-top: 1rem;
611+
}
612+
609613
.notebook-rendering {
610-
/* margin-top: 1rem; */
614+
max-width: 80vw;
611615
}
612616

613617
.markdown-body.notebook-contents {

pgml-dashboard/app/views/notebooks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def index(request):
77
request,
88
"notebooks.html",
99
{
10-
"notebooks": Notebook.objects.order_by('id').all(),
10+
"notebooks": Notebook.objects.order_by("id").all(),
1111
"topic": "notebooks",
1212
},
1313
)

pgml-dashboard/notebooks/models.py

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -77,32 +77,38 @@ def render(self):
7777
if self.cell_type == NotebookCell.SQL:
7878
execution_start = timezone.now()
7979

80+
results = []
8081
with connection.cursor() as cursor:
81-
try:
82-
cursor.execute(self.contents)
83-
84-
if cursor.description:
85-
columns = [col[0] for col in cursor.description]
86-
rows = cursor.fetchall()
87-
82+
queries = self.contents.split(";\n") # Eh.
83+
for query in queries:
84+
try:
85+
cursor.execute(query)
86+
87+
if cursor.description:
88+
columns = [col[0] for col in cursor.description]
89+
rows = cursor.fetchall()
90+
91+
result = render_to_string(
92+
"notebooks/sql.html",
93+
{
94+
"columns": columns,
95+
"rows": rows,
96+
},
97+
)
98+
results.append(result)
99+
else:
100+
# Not an error, but the formatting is helpful.
101+
result = render_to_string("notebooks/sql_error.html", {"error": str(cursor.statusmessage)})
102+
results.append(result)
103+
except Exception as e:
88104
result = render_to_string(
89-
"notebooks/sql.html",
105+
"notebooks/sql_error.html",
90106
{
91-
"columns": columns,
92-
"rows": rows,
107+
"error": str(e),
93108
},
94109
)
95-
else:
96-
# Not an error, but the formatting is helpful.
97-
result = render_to_string("notebooks/sql_error.html", {"error": str(cursor.statusmessage)})
98-
except Exception as e:
99-
result = render_to_string(
100-
"notebooks/sql_error.html",
101-
{
102-
"error": str(e),
103-
},
104-
)
105-
self.rendering = result
110+
results.append(result)
111+
self.rendering = "\n".join(results)
106112
self.execution_time = timezone.now() - execution_start
107113

108114
elif self.cell_type == NotebookCell.MARKDOWN:

pgml-dashboard/notebooks/static/notebooks/js/cell.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export default class extends Controller {
77
'undo',
88
'play',
99
'type',
10+
'cancelEdit',
1011
];
1112

1213
connect() {
@@ -85,6 +86,11 @@ export default class extends Controller {
8586
this.codeMirror.addKeyMap(disableKeyMap)
8687
}
8788
}
89+
90+
cancelEdit(event) {
91+
event.preventDefault()
92+
this.cancelEditTarget.requestSubmit()
93+
}
8894
}
8995

9096
const scrollToBottom = () => {

pgml-dashboard/notebooks/templates/notebooks/cell.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,15 @@
1818
<button type="submit" class="button notebook-button" title="Execute cell" data-cell-target="play">
1919
<span class="material-symbols-outlined">play_arrow</span>
2020
</button>
21+
<button class="button notebook-button" title="Cancel edit" data-action="cell#cancelEdit">
22+
<span class="material-symbols-outlined">cancel</span>
23+
</button>
2124
</div>
2225
</div>
2326
</form>
27+
<!-- Cancel edit and refetch the cell -->
28+
<form class="hidden" action="{% url 'notebooks/cell/get' notebook.pk cell.pk %}?bust_cache={{ bust_cache }}" method="get" data-cell-target="cancelEdit">
29+
</form>
2430
</div>
2531
{% endif %}
2632

pgml-dashboard/notebooks/views.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def notebook(request, pk):
3030
"next_cell_number": next_cell_number,
3131
"title": f"{notebook.name} - PostgresML",
3232
"topic": "notebooks",
33+
"bust_cache": time.time(),
3334
},
3435
)
3536

0 commit comments

Comments
 (0)