mirror of
https://github.com/gradio-app/gradio.git
synced 2024-12-21 02:19:59 +08:00
add CORS
This commit is contained in:
parent
90387df466
commit
0a5f1d2851
43
build/lib/gradio/cors.py
Normal file
43
build/lib/gradio/cors.py
Normal file
@ -0,0 +1,43 @@
|
||||
import SimpleHTTPServer
|
||||
class CORSHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
|
||||
def send_head(self):
|
||||
"""Common code for GET and HEAD commands.
|
||||
This sends the response code and MIME headers.
|
||||
Return value is either a file object (which has to be copied
|
||||
to the outputfile by the caller unless the command was HEAD,
|
||||
and must be closed by the caller under all circumstances), or
|
||||
None, in which case the caller has nothing further to do.
|
||||
"""
|
||||
path = self.translate_path(self.path)
|
||||
f = None
|
||||
if os.path.isdir(path):
|
||||
if not self.path.endswith('/'):
|
||||
# redirect browser - doing basically what apache does
|
||||
self.send_response(301)
|
||||
self.send_header("Location", self.path + "/")
|
||||
self.end_headers()
|
||||
return None
|
||||
for index in "index.html", "index.htm":
|
||||
index = os.path.join(path, index)
|
||||
if os.path.exists(index):
|
||||
path = index
|
||||
break
|
||||
else:
|
||||
return self.list_directory(path)
|
||||
ctype = self.guess_type(path)
|
||||
try:
|
||||
# Always read in binary mode. Opening files in text mode may cause
|
||||
# newline translations, making the actual size of the content
|
||||
# transmitted *less* than the content-length!
|
||||
f = open(path, 'rb')
|
||||
except IOError:
|
||||
self.send_error(404, "File not found")
|
||||
return None
|
||||
self.send_response(200)
|
||||
self.send_header("Content-type", ctype)
|
||||
fs = os.fstat(f.fileno())
|
||||
self.send_header("Content-Length", str(fs[6]))
|
||||
self.send_header("Last-Modified", self.date_time_string(fs.st_mtime))
|
||||
self.send_header("Access-Control-Allow-Origin", "*")
|
||||
self.end_headers()
|
||||
return f
|
@ -39,6 +39,8 @@ def document(cls_set):
|
||||
inp = {}
|
||||
inp["name"] = cls.__name__
|
||||
doc = inspect.getdoc(cls)
|
||||
if doc.startswith("DEPRECATED"):
|
||||
continue
|
||||
inp["doc"] = "\n".join(doc.split("\n")[:-1])
|
||||
inp["type"] = doc.split("\n")[-1].split("type: ")[-1]
|
||||
inp["params"], inp["params_doc"] = get_params(cls.__init__)
|
||||
|
@ -206,7 +206,7 @@ class Radio(InputComponent):
|
||||
class Dropdown(InputComponent):
|
||||
"""
|
||||
Component creates a dropdown of which only one can be selected. Provides string representing selected choice as an argument to the wrapped function.
|
||||
Input type: str
|
||||
Input type: Union[str, int]
|
||||
"""
|
||||
|
||||
def __init__(self, choices, type="value", label=None):
|
||||
|
@ -58,8 +58,6 @@ class Interface:
|
||||
verbose (bool): whether to print detailed information during launch.
|
||||
examples (List[List[Any]]): sample inputs for the function; if provided, appears below the UI components and can be used to populate the interface. Should be nested list, in which the outer list consists of samples and each inner list consists of an input corresponding to each input component.
|
||||
live (bool): whether the interface should automatically reload on change.
|
||||
show_input (bool): if False, removes the input from the interface and underlays it in the output.
|
||||
show_output (bool): if False, removes the output from the interface and overlays it in the input.
|
||||
capture_session (bool): if True, captures the default graph and session (needed for Tensorflow 1.x)
|
||||
title (str): a title for the interface; if provided, appears above the input and output components.
|
||||
description (str): a description for the interface; if provided, appears above the input and output components.
|
||||
|
@ -151,6 +151,12 @@ def serve_files_in_background(interface, port, directory_to_serve=None, server_n
|
||||
self.send_header("Content-type", "application/json")
|
||||
self.end_headers()
|
||||
|
||||
def end_headers(self):
|
||||
self.send_header('Access-Control-Allow-Origin', '*')
|
||||
self.send_header('Access-Control-Allow-Methods', 'GET, POST')
|
||||
self.send_header('Cache-Control', 'no-store, no-cache, must-revalidate')
|
||||
return super(HTTPHandler, self).end_headers()
|
||||
|
||||
def translate_path(self, path):
|
||||
path = SimpleHTTPRequestHandler.translate_path(self, path)
|
||||
relpath = os.path.relpath(path, os.getcwd())
|
||||
|
@ -39,6 +39,8 @@ def document(cls_set):
|
||||
inp = {}
|
||||
inp["name"] = cls.__name__
|
||||
doc = inspect.getdoc(cls)
|
||||
if doc.startswith("DEPRECATED"):
|
||||
continue
|
||||
inp["doc"] = "\n".join(doc.split("\n")[:-1])
|
||||
inp["type"] = doc.split("\n")[-1].split("type: ")[-1]
|
||||
inp["params"], inp["params_doc"] = get_params(cls.__init__)
|
||||
|
@ -206,7 +206,7 @@ class Radio(InputComponent):
|
||||
class Dropdown(InputComponent):
|
||||
"""
|
||||
Component creates a dropdown of which only one can be selected. Provides string representing selected choice as an argument to the wrapped function.
|
||||
Input type: str
|
||||
Input type: Union[str, int]
|
||||
"""
|
||||
|
||||
def __init__(self, choices, type="value", label=None):
|
||||
|
@ -58,8 +58,6 @@ class Interface:
|
||||
verbose (bool): whether to print detailed information during launch.
|
||||
examples (List[List[Any]]): sample inputs for the function; if provided, appears below the UI components and can be used to populate the interface. Should be nested list, in which the outer list consists of samples and each inner list consists of an input corresponding to each input component.
|
||||
live (bool): whether the interface should automatically reload on change.
|
||||
show_input (bool): if False, removes the input from the interface and underlays it in the output.
|
||||
show_output (bool): if False, removes the output from the interface and overlays it in the input.
|
||||
capture_session (bool): if True, captures the default graph and session (needed for Tensorflow 1.x)
|
||||
title (str): a title for the interface; if provided, appears above the input and output components.
|
||||
description (str): a description for the interface; if provided, appears above the input and output components.
|
||||
|
@ -151,6 +151,12 @@ def serve_files_in_background(interface, port, directory_to_serve=None, server_n
|
||||
self.send_header("Content-type", "application/json")
|
||||
self.end_headers()
|
||||
|
||||
def end_headers(self):
|
||||
self.send_header('Access-Control-Allow-Origin', '*')
|
||||
self.send_header('Access-Control-Allow-Methods', 'GET, POST')
|
||||
self.send_header('Cache-Control', 'no-store, no-cache, must-revalidate')
|
||||
return super(HTTPHandler, self).end_headers()
|
||||
|
||||
def translate_path(self, path):
|
||||
path = SimpleHTTPRequestHandler.translate_path(self, path)
|
||||
relpath = os.path.relpath(path, os.getcwd())
|
||||
|
Loading…
Reference in New Issue
Block a user