Skip to content

Commit 39e96ad

Browse files
authored
Merge pull request #19 from isabelizimm/rapidoc-visual
Rapidoc visual
2 parents d219be2 + 40a2dbd commit 39e96ad

3 files changed

Lines changed: 53 additions & 31 deletions

File tree

vetiver/handlers/sklearn_vt.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ def __init__(self, model, ptype_data, save_ptype):
1919
self.save_ptype = save_ptype
2020

2121
def create_description(self):
22-
"""Create description for sklearn model"""
23-
desc = f"Scikit-learn model of type {type(self.model)}"
22+
"""Create description for sklearn model
23+
"""
24+
desc = f"Scikit-learn {self.model.__class__} model"
2425
return desc
2526

2627
def vetiver_create_meta(

vetiver/server.py

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
from fastapi import FastAPI, Request
2-
from fastapi.responses import HTMLResponse
2+
from fastapi.responses import HTMLResponse, RedirectResponse
3+
from fastapi.staticfiles import StaticFiles
4+
from fastapi.openapi.utils import get_openapi
5+
36
import uvicorn
4-
from typing import Callable, Optional, Union, List
57
import requests
68
import pandas as pd
9+
from typing import Callable, Optional, Union, List
710

811
from .vetiver_model import VetiverModel
912
from .utils import _jupyter_nb
@@ -47,35 +50,43 @@ def __init__(
4750

4851
def _init_app(self):
4952
app = self.app_factory()
53+
app.openapi = self._custom_openapi
5054

51-
@app.get("/")
52-
async def main_app():
53-
return {"msg": "root path"}
54-
55-
# redirect to docs?
56-
# def docs_redirect():
57-
# return RedirectResponse("/rapidoc")
55+
@app.get("/", include_in_schema=False)
56+
def docs_redirect():
57+
return RedirectResponse("/__docs__")
5858

59-
@app.get("/ping", include_in_schema=False)
59+
@app.get("/ping", include_in_schema=True)
6060
async def ping():
6161
return {"ping": "pong"}
6262

63-
@app.get("/rapidoc", response_class=HTMLResponse)
64-
async def rapidoc():
63+
@app.get("/__docs__", response_class=HTMLResponse, include_in_schema=False)
64+
async def rapidoc_pg():
6565
return f"""
66-
<!doctype html>
67-
<html>
68-
<head>
69-
<meta charset="utf-8">
70-
<script
71-
type="module"
72-
src="https://unpkg.com/rapidoc@9.1.4/dist/rapidoc-min.js"
73-
></script>
74-
</head>
75-
<body>
76-
<rapi-doc spec-url="{app.openapi_url}"></rapi-doc>
77-
</body>
78-
</html>
66+
<!doctype html>
67+
<html>
68+
<head>
69+
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1,user-scalable=yes">
70+
<title>RapiDoc</title>
71+
<script type="module" src="https://unpkg.com/rapidoc@9.1.3/dist/rapidoc-min.js"></script>
72+
</script></head>
73+
<body>
74+
<rapi-doc spec-url="{app.openapi_url}"
75+
id="thedoc" render-style="read" schema-style="tree"
76+
show-components="true" show-info="true" show-header="true"
77+
allow-search="true"
78+
show-side-nav="false"
79+
allow-authentication="false" update-route="false" match-type="regex"
80+
theme="light"
81+
header-color="#F2C6AC"
82+
primary-color = "#8C2D2D">
83+
<img
84+
slot="logo"
85+
width="55"
86+
src="https://raw.githubusercontent.com/rstudio/hex-stickers/master/SVG/vetiver.svg"
87+
</rapi-doc>
88+
</body>
89+
</html>
7990
"""
8091

8192
if self.check_ptype == True:
@@ -146,6 +157,18 @@ def run(self):
146157
_jupyter_nb()
147158
uvicorn.run(self.app, port=self.port, host=self.host)
148159

160+
def _custom_openapi(self):
161+
if self.app.openapi_schema:
162+
return self.app.openapi_schema
163+
openapi_schema = get_openapi(
164+
title=self.model.model_name + " model API",
165+
version="0.1.3",
166+
description=self.model.description,
167+
routes=self.app.routes,
168+
)
169+
openapi_schema["info"]["x-logo"] = {"url": "../docs/figures/logo.svg"}
170+
self.app.openapi_schema = openapi_schema
171+
return self.app.openapi_schema
149172

150173
def predict(endpoint, data: dict, **kw):
151174
"""Make a prediction from model endpoint
@@ -188,7 +211,7 @@ def _batch_data(pred_data):
188211

189212

190213
def vetiver_endpoint(url="http://127.0.0.1:8000/predict"):
191-
"""Wrap url
214+
"""Wrap url where VetiverModel will be deployed
192215
193216
Parameters
194217
----------

vetiver/vetiver_model.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,8 @@ def __init__(
5757
self.save_ptype = save_ptype
5858
self.ptype = translator.ptype()
5959
self.model_name = model_name
60+
self.description = description if description else translator.create_description()
6061
self.versioned = versioned
61-
self.description = (
62-
translator.create_description() if description == None else description
63-
)
6462
self.metadata = translator.vetiver_create_meta(
6563
metadata, required_pkgs=["vetiver"]
6664
)

0 commit comments

Comments
 (0)