1+ import json
2+ import logging
13import re
4+ import webbrowser
5+ from textwrap import dedent
6+ from typing import Callable , List , Union
7+ from urllib .parse import urljoin
8+ from warnings import warn
9+
210import httpx
3- import json
11+ import pandas as pd
412import requests
513import uvicorn
6- import logging
7- import pandas as pd
814from fastapi import FastAPI , Request
915from fastapi .exceptions import RequestValidationError
1016from fastapi .openapi .utils import get_openapi
11- from fastapi .responses import HTMLResponse , RedirectResponse , PlainTextResponse
12- from textwrap import dedent
13- from warnings import warn
14- from urllib .parse import urljoin
15- from typing import Callable , List , Union
17+ from fastapi .responses import HTMLResponse , PlainTextResponse , RedirectResponse
1618
19+ from .helpers import api_data_to_frame , response_to_frame
20+ from .meta import VetiverMeta
1721from .utils import _jupyter_nb , get_workbench_path
1822from .vetiver_model import VetiverModel
19- from .meta import VetiverMeta
20- from .helpers import api_data_to_frame , response_to_frame
2123
2224
2325class VetiverAPI :
@@ -251,7 +253,7 @@ async def custom_endpoint(input_data: Request):
251253 else :
252254 return predictions
253255
254- def run (self , port : int = 8000 , host : str = "127.0.0.1" , ** kw ):
256+ def run (self , port : int = 8000 , host : str = "127.0.0.1" , quiet_open = False , ** kw ):
255257 """
256258 Start API
257259
@@ -261,6 +263,8 @@ def run(self, port: int = 8000, host: str = "127.0.0.1", **kw):
261263 An integer that indicates the server port that should be listened on.
262264 host : str
263265 A valid IPv4 or IPv6 address, which the application will listen on.
266+ quiet_open : bool
267+ If host is a localhost address, try to automatically open API in browser
264268
265269 Examples
266270 -------
@@ -273,7 +277,13 @@ def run(self, port: int = 8000, host: str = "127.0.0.1", **kw):
273277 """
274278 _jupyter_nb ()
275279 self .workbench_path = get_workbench_path (port )
276-
280+ if port and host :
281+ try :
282+ if host == "127.0.0.1" and not quiet_open :
283+ # quality of life for developing APIs locally
284+ webbrowser .open (f"http://{ host } :{ port } " )
285+ except Exception :
286+ pass
277287 if self .workbench_path :
278288 uvicorn .run (
279289 self .app , port = port , host = host , root_path = self .workbench_path , ** kw
0 commit comments