Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
davegaeddert committed Jul 5, 2024
1 parent 65638c9 commit d4b1524
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 3 deletions.
28 changes: 28 additions & 0 deletions bolt-charts/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
BSD 3-Clause License

Copyright (c) 2023, Dropseed, LLC

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3 changes: 3 additions & 0 deletions bolt-charts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!-- This file is compiled from bolt-charts/bolt/vendor/README.md. Do not edit this file directly. -->

# bolt-charts
1 change: 1 addition & 0 deletions bolt-charts/bolt/charts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# bolt-charts
Empty file.
21 changes: 21 additions & 0 deletions bolt-charts/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[tool.poetry]

name = "bolt-charts"
packages = [
{ include = "bolt" },
]

version = "0.0.0"
description = ""
authors = ["Dave Gaeddert <[email protected]>"]
license = "MIT"
# readme = "README.md"

[tool.poetry.dependencies]
python = "^3.11"
plotly = ">5.0.0"
pandas = "*"

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
74 changes: 73 additions & 1 deletion bolt-staff/bolt/staff/admin/cards/charts.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,78 @@
import datetime

import plotly.express as px
import pandas as pd
from .base import Card
from bolt.db.models import Count
from bolt.db.models.functions import TruncDate

class Chart:
def render_html(self):
config = {
"displayModeBar": False,
"scrollZoom": False,
"responsive": True,
# "staticPlot": True,
}
return self.get_figure().to_html(full_html=False, config=config)#, include_plotlyjs=False)

def render_image(self):
return self.get_figure().to_image(format="png")

def __html__(self):
return self.render_html()


class BarChart(Chart):
def __init__(self, *, dataframe, x, y):
self.dataframe = dataframe
self.x = x
self.y = y

def get_figure(self):
fig = px.bar(self.dataframe, x=self.x, y=self.y)
return fig



class TrendCard(Card):
template_name = "admin/cards/trend.html"

model = None
trend_field = "created_at"

# default behavior can be querysets and models?
# override if custom objects, but rare?

def get_template_context(self):
context = super().get_template_context()
context["chart"] = self.get_chart()
return context

def get_chart(self):
filters = {
f"{self.trend_field}__range": self.datetime_range.as_tuple(),
}
data = (
self.model.objects.filter(**filters)
.annotate(date=TruncDate(self.trend_field))
.values("date")
.annotate(count=Count("id"))
.order_by("date")
)

dataframe = pd.DataFrame.from_records(
data,
columns=["date", "count"],
)

# fill the zeroes for the missing dates
dataframe = dataframe.set_index("date").reindex(self.datetime_range).fillna(0).reset_index()

return BarChart(
dataframe=dataframe,
x="date",
y="count",
)


class ChartCard(Card):
Expand Down
9 changes: 8 additions & 1 deletion bolt-staff/bolt/staff/admin/templates/admin/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
{{ request.user }}
</span>
</div>
<a class="ml-2 hover:text-white hover:underline flex-shrink-0" href="{{ url('logout') }}">Log out</a>
<a class="flex-shrink-0 ml-2 hover:text-white hover:underline" href="{{ url('logout') }}">Log out</a>
</div>
</div>
<div id="admin-content" data-toggle-class="fixed ml-64 -mr-64" class="flex-grow overflow-auto text-black border-l bg-paper lg:rounded-lg lg:mt-3 lg:mb-3 lg:mr-3 lg:ml-64 border-black/10">
Expand Down Expand Up @@ -145,6 +145,13 @@ <h1 class="text-2xl font-semibold text-stone-700">
</form>
</div>
</div>
{#
<!-- Tell tailwind about potential col-span sizes for cards -->
<div class="col-span-1"></div>
<div class="col-span-2"></div>
<div class="col-span-3"></div>
<div class="col-span-4"></div>
#}
<div class="grid grid-cols-1 gap-6 mt-4 mb-6 sm:grid-cols-2 lg:grid-cols-4">
{% for card in cards %}
<section class="col-span-{{ card.size.value }}">
Expand Down
5 changes: 5 additions & 0 deletions bolt-staff/bolt/staff/admin/templates/admin/cards/trend.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% extends "admin/cards/base.html" %}

{% block content %}
{{ chart }}
{% endblock %}
2 changes: 1 addition & 1 deletion bolt/bolt/templates/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Templates

Render HTML templates using Jinja.
Render templates using Jinja2.

Templates are typically rendered in `TemplateViews`,
but you can also render them directly to strings for emails or other use cases.
Expand Down

0 comments on commit d4b1524

Please sign in to comment.