33import json
44import requests
55import uvicorn
6- import os
7- import subprocess
86import logging
97import pandas as pd
108from fastapi import FastAPI , Request , testclient
1614from urllib .parse import urljoin
1715from typing import Callable , List , Union
1816
19- from .utils import _jupyter_nb , inform
17+ from .utils import _jupyter_nb , get_workbench_path
2018from .vetiver_model import VetiverModel
2119from .meta import VetiverMeta
2220from .helpers import api_data_to_frame , response_to_frame
2321
24- _log = logging .getLogger (__name__ )
25-
2622
2723class VetiverAPI :
2824 """Create model aware API
@@ -74,6 +70,7 @@ def __init__(
7470 self .model = model
7571 self .app_factory = app_factory
7672 self .app = app_factory ()
73+ self .workbench_path = None
7774
7875 if "check_ptype" in kwargs :
7976 check_prototype = kwargs .pop ("check_ptype" )
@@ -96,6 +93,14 @@ def _init_app(self):
9693 app = self .app
9794 app .openapi = self ._custom_openapi
9895
96+ @app .on_event ("startup" )
97+ async def startup_event ():
98+ logger = logging .getLogger ("uvicorn.error" )
99+ if self .workbench_path :
100+ logger .info (f"VetiverAPI starting at { self .workbench_path } " )
101+ else :
102+ logger .info ("VetiverAPI starting..." )
103+
99104 @app .get ("/" , include_in_schema = False )
100105 def docs_redirect ():
101106
@@ -264,25 +269,13 @@ def run(self, port: int = 8000, host: str = "127.0.0.1", **kw):
264269 >>> v_api.run() # doctest: +SKIP
265270 """
266271 _jupyter_nb ()
267- # check to see if in Posit Workbench, pulled from FastAPI section of user guide
268- # https://docs.posit.co/ide/server-pro/user/vs-code/guide/proxying-web-servers.html#running-fastapi-with-uvicorn # noqa
269- path = ""
270- if "RS_SERVER_URL" in os .environ and os .environ ["RS_SERVER_URL" ]:
271- path = (
272- subprocess .run (
273- f"echo $(/usr/lib/rstudio-server/bin/rserver-url -l { port } )" ,
274- stdout = subprocess .PIPE ,
275- shell = True ,
276- )
277- .stdout .decode ()
278- .strip ()
272+ self .workbench_path = get_workbench_path (port )
273+
274+ if self .workbench_path :
275+ uvicorn .run (
276+ self .app , port = port , host = host , root_path = self .workbench_path , ** kw
279277 )
280- # subprocess is run, new URL given
281- if len (path ) > 0 :
282- inform (_log , f"INFO: vetiver running at: { path } " )
283- uvicorn .run (self .app , port = port , host = host , root_path = path , ** kw )
284278 else :
285- inform (_log , f"INFO: vetiver running at: { host + ':' + port } " )
286279 uvicorn .run (self .app , port = port , host = host , ** kw )
287280
288281 def _custom_openapi (self ):
0 commit comments