Skip to content

Commit

Permalink
add suggested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
sinisaos committed Jun 29, 2023
1 parent 6d8909a commit da9fe50
Show file tree
Hide file tree
Showing 13 changed files with 168 additions and 20,316 deletions.
20,286 changes: 33 additions & 20,253 deletions admin_ui/package-lock.json

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions admin_ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@
"@fortawesome/vue-fontawesome": "^0.1.10",
"@types/js-cookie": "^2.2.6",
"axios": "^0.21.2",
"chart.js": "^4.3.0",
"chart.js": "^2.9.4",
"core-js": "^3.6.5",
"flatpickr": "^4.6.9",
"highcharts": "^11.0.1",
"is-svg": "^4.3.1",
"js-cookie": "^2.2.1",
"json-bigint": "^1.0.0",
Expand Down
2 changes: 1 addition & 1 deletion admin_ui/src/components/ChartsNav.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<li v-bind:key="chartConfig.title" v-for="chartConfig in chartConfigs">
<router-link
:to="{
name: 'getCharts',
name: 'chart',
params: { chartSlug: chartConfig.chart_slug }
}"
class="subtle"
Expand Down
15 changes: 15 additions & 0 deletions admin_ui/src/components/ChartsPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,37 @@
<div>
<h3>{{ chartConfigs.title }}</h3>
<pie-chart
width="650px"
height="450px"
:download="{ background: '#ffffff' }"
v-if="chartConfigs.chart_type == 'Pie'"
:data="chartConfigs.data"
></pie-chart>
<line-chart
width="650px"
height="450px"
:download="{ background: '#ffffff' }"
v-else-if="chartConfigs.chart_type == 'Line'"
:data="chartConfigs.data"
></line-chart>
<column-chart
width="650px"
height="450px"
:download="{ background: '#ffffff' }"
v-else-if="chartConfigs.chart_type == 'Column'"
:data="chartConfigs.data"
></column-chart>
<bar-chart
width="650px"
height="450px"
:download="{ background: '#ffffff' }"
v-else-if="chartConfigs.chart_type == 'Bar'"
:data="chartConfigs.data"
></bar-chart>
<area-chart
width="650px"
height="450px"
:download="{ background: '#ffffff' }"
v-else-if="chartConfigs.chart_type == 'Area'"
:data="chartConfigs.data"
></area-chart>
Expand Down
23 changes: 0 additions & 23 deletions admin_ui/src/main.less
Original file line number Diff line number Diff line change
Expand Up @@ -183,29 +183,6 @@ button {
.ql-picker-label {
color: white
}

.highcharts-background {
fill: @dark_grey;

}

.highcharts-axis-labels.highcharts-xaxis-labels {
text[style] {
fill: white !important;
}
}

.highcharts-axis-labels.highcharts-yaxis-labels {
text[style] {
fill: white !important;
}
}

.highcharts-axis.highcharts-xaxis {
.highcharts-axis-line {
stroke: white !important;
}
}
}

#app {
Expand Down
4 changes: 2 additions & 2 deletions admin_ui/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import store from "./store"
import i18n from "./i18n"
import "./fontawesome"
import Chartkick from 'vue-chartkick'
import Highcharts from 'highcharts'
import Chart from 'chart.js'

/*****************************************************************************/

Expand Down Expand Up @@ -47,7 +47,7 @@ Vue.filter("readable", function (value) {
/*****************************************************************************/

Vue.config.productionTip = false
Vue.use(Chartkick.use(Highcharts))
Vue.use(Chartkick.use(Chart))

new Vue({
i18n,
Expand Down
8 changes: 4 additions & 4 deletions admin_ui/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Login from './views/Login.vue'
import ChangePassword from './views/ChangePassword.vue'
import RowListing from './views/RowListing.vue'
import AddForm from './views/AddForm.vue'
import GetCharts from './views/GetCharts.vue'
import Chart from './views/Chart.vue'

Vue.use(Router)

Expand Down Expand Up @@ -39,9 +39,9 @@ export default new Router({
props: true
},
{
path: '/charts/:chartSlug/',
name: 'getCharts',
component: GetCharts,
path: '/charts/:chartSlug',
name: 'chart',
component: Chart,
props: true
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import ChartsPage from "../components/ChartsPage.vue"
import DetailViewBase from "../components/DetailViewBase.vue"
export default Vue.extend({
props: ["chartSlug"],
props: {
chartSlug: String
},
components: {
ChartsPage,
DetailViewBase
Expand Down
7 changes: 6 additions & 1 deletion docs/source/charts/examples/app.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import asyncio

from fastapi import FastAPI
from fastapi.routing import Mount
from home.tables import Director, Movie
Expand All @@ -16,10 +18,13 @@ async def director_movie_count():
return [[i["director"], i["count"]] for i in movies]


chart_data = asyncio.run(director_movie_count())


director_chart = ChartConfig(
title="Movie count",
chart_type="Pie",
data=director_movie_count,
data=chart_data,
)


Expand Down
10 changes: 5 additions & 5 deletions docs/source/charts/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@
Charts
======

Piccolo Admin can display different types of charts based on yours
Piccolo Admin can display different types of charts based on your
data. Five chart types are supported: ``Pie``, ``Line``, ``Column``,
``Bar`` and ``Area``.

Here's an example of charts usage, using FastAPI:

.. literalinclude:: ./examples/app.py

Piccolo Admin will then show a charts in the UI.
Piccolo Admin will then show a chart in the UI.

.. image:: ./images/charts_sidebar.png

.. image:: ./images/chart.png

.. warning::
.. hint::

The data format must be a ``list of lists``
(eg. ``[["Male", 7], ["Female", 3]]``).
The data format must be a list of lists
(e.g. ``[["Male", 7], ["Female", 3]]``).

-------------------------------------------------------------------------------

Expand Down
34 changes: 26 additions & 8 deletions piccolo_admin/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,6 @@ class FormConfigResponseModel(BaseModel):
description: t.Optional[str] = None


@dataclass
class ChartConfig:
"""
Used to specify charts, which are passed into ``create_admin``.
Expand All @@ -363,13 +362,24 @@ class ChartConfig:
``Column``, ``Bar`` and ``Area``.
:param data:
The data to be passed to the admin ui. The data format must be
a ``list of lists`` (eg. ``[["Male", 7], ["Female", 3]]``).
a list of lists (e.g. ``[["Male", 7], ["Female", 3]]``).
Here's a full example:
.. code-block:: python
async def director_movie_count():
import asyncio
import typing as t
from piccolo.query.methods.select import Count
from piccolo_admin.endpoints import (
create_admin,
ChartConfig,
ChartConfigResponseModel,
)
from movies.tables import Director, Movie
async def director_movie_count() -> t.List[ChartConfigResponseModel]:
movies = await Movie.select(
Movie.director.name.as_alias("director"),
Count(Movie.id)
Expand All @@ -380,16 +390,24 @@ async def director_movie_count():
# like [['George Lucas', 3], ...]
return [[i['director'], i['count']] for i in movies]
chart_data = asyncio.run(director_movie_count())
director_chart = ChartConfig(
title='Movie count',
chart_type="Pie", # or Bar or Line etc.
data=director_movie_count,
data=chart_data,
)
create_admin(charts=[director_chart])
"""

def __init__(self, title: str, chart_type: str, data: t.List[t.Any]):
def __init__(
self,
title: str,
chart_type: str,
data: t.List[t.List[t.Any]],
):
self.title = title
self.chart_slug = self.title.replace(" ", "-").lower()
self.chart_type = chart_type
Expand All @@ -400,7 +418,7 @@ class ChartConfigResponseModel(BaseModel):
title: str
chart_slug: str
chart_type: str
data: t.List[t.Any]
data: t.List[t.List[t.Any]]


def handle_auth_exception(request: Request, exc: Exception):
Expand Down Expand Up @@ -925,7 +943,7 @@ def get_user(self, request: Request) -> UserResponseModel:

def get_charts(self) -> t.List[ChartConfigResponseModel]:
"""
Returns all charts registered with the admin.
Returns all charts registered with the admin.
"""
return [
ChartConfigResponseModel(
Expand All @@ -939,7 +957,7 @@ def get_charts(self) -> t.List[ChartConfigResponseModel]:

def get_single_chart(self, chart_slug: str) -> ChartConfigResponseModel:
"""
Returns single chart.
Returns a single chart.
"""
chart = self.chart_config_map.get(chart_slug, None)
if chart is None:
Expand Down
38 changes: 30 additions & 8 deletions piccolo_admin/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,17 @@ def booking_endpoint(request: Request, data: BookingModel) -> str:
menu_group="Testing",
)


async def fetch_chart_data() -> t.List[t.List[t.Any]]:
return [
["George Lucas", 4],
["Peter Jackson", 6],
["Ron Howard", 1],
]


chart_data = asyncio.run(fetch_chart_data())

APP = create_admin(
[
movie_config,
Expand Down Expand Up @@ -447,18 +458,29 @@ def booking_endpoint(request: Request, data: BookingModel) -> str:
session_table=Sessions,
charts=[
ChartConfig(
title="Movie count",
title="Movie count Pie",
chart_type="Pie",
data=[
["George Lucas", 4],
["Peter Jackson", 6],
["Ron Howard", 1],
],
data=chart_data,
),
ChartConfig(
title="Movie count Line",
chart_type="Line",
data=chart_data,
),
ChartConfig(
title="Director gender",
title="Movie count Column",
chart_type="Column",
data=[["Male", 7], ["Female", 3]],
data=chart_data,
),
ChartConfig(
title="Movie count Bar",
chart_type="Bar",
data=chart_data,
),
ChartConfig(
title="Movie count Area",
chart_type="Area",
data=chart_data,
),
],
sidebar_links={
Expand Down
Loading

0 comments on commit da9fe50

Please sign in to comment.