Skip to content

Commit

Permalink
style changes and ground work for LLM integration
Browse files Browse the repository at this point in the history
  • Loading branch information
darenr committed Jan 4, 2024
1 parent 1999cf4 commit 846c520
Show file tree
Hide file tree
Showing 7 changed files with 15,863 additions and 15,745 deletions.
38 changes: 21 additions & 17 deletions example.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
doe: "a deer, a female deer"
ray: "a drop of golden sun"
pi: 3.14159
xmas: true
french-hens: 3
calling-birds:
- huey
- dewey
- louie
- fred
xmas-fifth-day:
calling-birds: four
golden-rings: 5
partridges:
count: 1
location: "a pear tree"
turtle-doves: two
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
16 changes: 6 additions & 10 deletions experimental/pb1.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
from bokeh.plotting import figure
from bokeh.resources import CDN
from bokeh.embed import components
from bokeh.palettes import GnBu3, OrRd3

import os
import pandas as pd
import numpy as np

from bokeh.models import ColumnDataSource
import numpy as np
import pandas as pd
from bokeh.embed import components
from bokeh.models import BoxZoomTool, ColumnDataSource, HoverTool
from bokeh.palettes import GnBu3, OrRd3
from bokeh.plotting import figure
from bokeh.resources import CDN
from bokeh.transform import dodge
from bokeh.models import BoxZoomTool, HoverTool

from jinja2 import Template


Expand Down
31,434 changes: 15,755 additions & 15,679 deletions index.html

Large diffs are not rendered by default.

31 changes: 19 additions & 12 deletions kitchen_sink.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,29 @@

import numpy as np
import pandas as pd
import plotly.express as px
import pydataset
import yaml
import plotly.express as px

logging.basicConfig(level=logging.INFO)

from report_creator import (
Base,
Statistic,
Blocks,
Collapse,
DataTable,
Group,
Image,
Json,
Markdown,
Plot,
Python,
ReportCreator,
Section,
Select,
Statistic,
Text,
Yaml,
Image,
)

if __name__ == "__main__":
Expand All @@ -33,15 +34,15 @@
df1.Age = [24, 18, 22]

fig1 = df1.plot.bar(x="Name", y="Age").get_figure()

df2 = px.data.stocks()
fig2 = px.line(df2, x='date', y=['GOOG', 'AAPL', 'AMZN', 'FB', 'NFLX', 'MSFT'])
fig2 = px.line(df2, x="date", y=["GOOG", "AAPL", "AMZN", "FB", "NFLX", "MSFT"])

with open(__file__, "r") as f:
example_python = f.read()

with open("example.yaml", "r") as f:
yaml_data = yaml.safe_load(f.read())
datastructure = yaml.safe_load(f.read())

with open("example.txt", "r") as f:
example_text = f.read()
Expand All @@ -53,8 +54,8 @@

view = Blocks(
Collapse(
"Code (kitchen_sink.py) to create this report",
Python(example_python, label="kitchen_sink.py"),
label="Code (kitchen_sink.py) to create this report",
),
Group(
Statistic(
Expand Down Expand Up @@ -90,9 +91,15 @@
example_text,
label="Ready Player One",
),
Yaml(
yaml_data,
label="Random Yaml",
Group(
Yaml(
datastructure,
label="Kubernetes Creating a Deployment as YAML",
),
Json(
datastructure,
label="Kubernetes Creating a Deployment as JSON",
),
),
Section(),
Markdown(example_md, label="Example Markdown"),
Expand Down Expand Up @@ -120,8 +127,8 @@
),
Group(
Image("https://sufipathoflove.files.wordpress.com/2019/02/prim.jpg"),
label="La Primavera – Botticelli"
)
label="La Primavera – Botticelli",
),
),
)

Expand Down
5 changes: 3 additions & 2 deletions report_creator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@
)
from .report_creator import (
Base,
Statistic,
Blocks,
Collapse,
DataTable,
Group,
Image,
Json,
Markdown,
Plot,
Python,
ReportCreator,
Section,
Select,
Statistic,
Text,
Yaml,
Image,
)
59 changes: 42 additions & 17 deletions report_creator/report_creator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import base64
import io
import json
import logging
import os
from string import Template
Expand Down Expand Up @@ -30,6 +31,7 @@ class Base:
def __init__(self, label: str):
self.label = label

@strip_whitespace
def to_html(self):
return ""

Expand All @@ -39,7 +41,7 @@ def to_html(self):

class Blocks:
# vertically stacked compoments
def __init__(self, *components: Base):
def __init__(self, *components: Base, prompt: str = None):
self.components = components
logging.info(f"Blocks {len(self.components)} components")

Expand All @@ -62,7 +64,7 @@ def to_html(self):

class Group:
# horizontally stacked compoments
def __init__(self, *components: Base, label=None):
def __init__(self, *components: Base, label=None, prompt: str = None):
self.components = components
self.label = label
logging.info(f"Group {len(self.components)} components {label=}")
Expand All @@ -89,10 +91,10 @@ def to_html(self):


class Collapse:
def __init__(self, label: str, *components: Base):
def __init__(self, *components: Base, label: str = None):
self.components = components
self.label = label
logging.info(f"Collapse {len(self.components)} components")
logging.info(f"Collapse {len(self.components)} components, {label=}")

@strip_whitespace
def to_html(self):
Expand Down Expand Up @@ -237,33 +239,38 @@ def __init__(self, fig, label=None):

@strip_whitespace
def to_html(self) -> str:

html = "<div class='plot_wrapper'>"

if self.label:
html += f"<h3 class='block-bordered'>{self.label}</h3>"

if isinstance(self.fig, matplotlib.figure.Figure):
tmp = io.BytesIO()
self.fig.set_figwidth(10)
self.fig.tight_layout()
self.fig.savefig(tmp, format="png")
tmp.seek(0)
b64image = base64.b64encode(tmp.getvalue()).decode("utf-8").replace("\n", "")
b64image = (
base64.b64encode(tmp.getvalue()).decode("utf-8").replace("\n", "")
)
html += f'<br/><img src="data:image/png;base64,{b64image}">'
else:
import plotly

if isinstance(self.fig, plotly.graph_objs._figure.Figure):
tmp = io.StringIO()
self.fig.write_html(tmp)
html += tmp.getvalue()
html += tmp.getvalue()
else:
raise ValueError(f"Expected matplotlib.figure.Figure, got {type(self.fig)}, try obj.get_figure()")

raise ValueError(
f"Expected matplotlib.figure.Figure, got {type(self.fig)}, try obj.get_figure()"
)

html += "</div>"

return html


##############################


Expand All @@ -272,6 +279,7 @@ def __init__(self, label: str = None):
Base.__init__(self, label=label)
logging.info(f"Section")

@strip_whitespace
def to_html(self):
if self.label:
return f"<br/><div><hr/><h2>{self.label}</h2></div>"
Expand Down Expand Up @@ -319,6 +327,7 @@ def __init__(self, *components: Base):
f"Select {len(self.components)} components: {', '.join([c.label for c in self.components])}"
)

@strip_whitespace
def to_html(self):
"""
<div class="tab">
Expand All @@ -343,12 +352,12 @@ def to_html(self):
<p>Tokyo is the capital of Japan.</p>
</div>
"""

# assemble the button bar for the tabs
html = """<div class="tab">"""
for i, component in enumerate(self.components):
logging.info(f"creating tab: {component.label}")
extra="id='defaultOpen'" if i==0 else ""
logging.info(f"creating tab: {component.label}")
extra = "id='defaultOpen'" if i == 0 else ""
html += f"""<button class="tablinks" onclick="openTab(event, '{component.label}')" {extra}>{component.label}</button>"""
html += """</div>"""

Expand Down Expand Up @@ -378,7 +387,7 @@ def to_html(self):
else:
formatted_text = f"<pre><code class='language-{self.language}'>{self.code.strip()}</code></pre>"

return f"<div class='block-bordered'>{formatted_text}</div>"
return f"""<div>{formatted_text}</div>"""


##############################
Expand All @@ -389,11 +398,14 @@ def __init__(self, code: str, label=None):
Language.__init__(self, code, "python", label=label)


##############################


class Yaml(Language):
def __init__(self, data: Union[Dict, List], label=None):
Language.__init__(
self,
yaml.dump(data, sort_keys=True, indent=2),
yaml.dump(data, indent=2),
"yaml",
label=label,
)
Expand All @@ -402,6 +414,19 @@ def __init__(self, data: Union[Dict, List], label=None):
##############################


class Json(Language):
def __init__(self, data: Union[Dict, List], label=None):
Language.__init__(
self,
json.dumps(data, indent=2),
"json",
label=label,
)


##############################


class ReportCreator:
def __init__(self, title: str):
self.title = title
Expand Down
Loading

0 comments on commit 846c520

Please sign in to comment.