Skip to content

Commit

Permalink
before branch swap
Browse files Browse the repository at this point in the history
  • Loading branch information
koaning committed Jun 18, 2024
1 parent c9d5606 commit 318d7b3
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 10 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@ python -m pip install flask diskcache pandas altair


```python
from bundl.infomander import InfoMander
from mandr import InfoMander

mander = InfoMander()
# We use paths to explain where the dictionary is stored
mander = InfoMander('/org/usecase/train/1')
mander.add_info(...)
mander.add_logs(...)
mander.add_templates(...)
mander.add_views(...)
```

When ready to view, run flask:

```
python -m flask --app bundl.app run --reload
python -m flask --app mandr.app run --reload
```
3 changes: 1 addition & 2 deletions mandr/app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from flask import Flask, render_template, Response
from flask import Flask, Response
from pathlib import Path
import orjson
import json
from jinja2 import Template
from .infomander import InfoMander, VIEWS_KEY
Expand Down
7 changes: 7 additions & 0 deletions mandr/infomander.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ def __init__(self, path):

# This will be used for rendering templates into views
self.renderer = TemplateRenderer(self)

def _check_key(self, key):
if key in [ARTIFACTS_KEY, TEMPLATES_KEY, VIEWS_KEY, LOGS_KEY]:
raise ValueError(f'Cannot overwrite {key} key. This is reserved for internal use.')

def add_info(self, key, value, method='overwrite'):
if method == 'overwrite':
Expand All @@ -53,16 +57,19 @@ def _add_to_key(self, top_key, key, value):
self.add_info(top_key, orig)

def add_artifact(self, key, obj, **metadata):
self._check_key(key)
file_location = self.artifact_path / f'{key}.joblib'
if not file_location.parent.exists():
file_location.parent.mkdir(parents=True)
dump(obj, file_location)
self._add_to_key(ARTIFACTS_KEY, key, {'path': file_location, **metadata})

def add_view(self, key, html):
self._check_key(key)
self._add_to_key(VIEWS_KEY, key, html)

def add_template(self, key, template):
self._check_key(key)
if key in self.cache[VIEWS_KEY].keys():
raise ValueError(f'Cannot add template {key} because there is already a view with the same name.')
self._add_to_key('_templates', key, template)
Expand Down
13 changes: 10 additions & 3 deletions mandr/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
import altair as alt
import json
from jinja2 import Template
from sklearn.utils._estimator_html_repr import estimator_html_repr


def sklearn_model_repr(pipeline):
return estimator_html_repr(pipeline)


def scatter_chart(title, x, y, **kwargs):
Expand All @@ -23,13 +28,15 @@ def scatter_chart(title, x, y, **kwargs):


registry = {
'scatter-chart': scatter_chart
'scatter-chart': scatter_chart,
'sklearn-model-repr': sklearn_model_repr
}


class TemplateRenderer:
"""We do a few things on top of Jinja2 here"""
def __init__(self, datamander):
self.datamander = datamander
def __init__(self, mander):
self.mander = mander

def clean_value(self, val):
return val.replace('/>', '').replace('"', '').replace("'", '')
Expand Down
6 changes: 4 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@
name="mandr",
version="0.1.0",
packages=find_packages(),
install_requires=[],
)
install_requires=[
'Flask', 'srsly', 'scikit-learn', 'pandas', 'altair', 'diskcache', 'rich', 'markdown'
],
)
Empty file added tests/__init__.py
Empty file.
4 changes: 4 additions & 0 deletions tests/test_mander_dsl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"""
The mander comes with a special DSL to fetch the right elements from the cache.
This is tested here.
"""

0 comments on commit 318d7b3

Please sign in to comment.