def get_output_instance(iface): if isinstance(iface, str): shortcut = OutputComponent.get_all_shortcut_implementations( )[iface] return shortcut[0](**shortcut[1]) elif isinstance(iface, OutputComponent): return iface else: raise ValueError("Output interface must be of type `str` or " "`OutputComponent`")
def generate(): in_demos, out_demos = get_demos() inputs = document(InputComponent.__subclasses__(), in_demos) outputs = document(OutputComponent.__subclasses__(), out_demos) interface_params = get_ins_and_outs(Interface.__init__) interface = { "doc": inspect.getdoc(Interface), "params": interface_params[1], "params_doc": interface_params[2], } launch_params = get_ins_and_outs(Interface.launch) launch = { "params": launch_params[1], "params_doc": launch_params[2], } return { "inputs": inputs, "outputs": outputs, "interface": interface, "launch": launch, }
for cls in 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__) inp["shortcuts"] = list(cls.get_shortcut_implementations().items()) docset.append(inp) return docset inputs = document(InputComponent.__subclasses__()) outputs = document(OutputComponent.__subclasses__()) interface_params = get_params(Interface.__init__) interface = { "doc": inspect.getdoc(Interface), "params": interface_params[0], "params_doc": interface_params[1], } launch_params = get_params(Interface.launch) launch = { "params": launch_params[0], "params_doc": launch_params[1], } with open("docs.json", "w") as docs: json.dump( {
def render_docs(): if os.path.exists("generated/colab_links.json"): with open("generated/colab_links.json") as demo_links_file: try: demo_links = json.load(demo_links_file) except ValueError: demo_links = {} else: # docs will be missing demo links demo_links = {} SCREENSHOT_FOLDER = "dist/assets/demo_screenshots" os.makedirs(SCREENSHOT_FOLDER, exist_ok=True) def get_function_documentation(func): doc_str = inspect.getdoc(func) func_doc, params_doc, return_doc = [], [], [] documented_params = {"self"} mode = "pre" for line in doc_str.split("\n"): if line.startswith("Parameters:"): mode = "in" continue if line.startswith("Returns:"): mode = "out" continue if "DEPRECATED" in line: continue if mode == "pre": func_doc.append(line) elif mode == "in": space_index = line.index(" ") colon_index = line.index(":") name = line[:space_index] documented_params.add(name) params_doc.append(( name, line[space_index + 2:colon_index - 1], line[colon_index + 2:], )) elif mode == "out": colon_index = line.index(":") return_doc.append( (line[1:colon_index - 1], line[colon_index + 2:])) params = inspect.getfullargspec(func) param_set = [] for i in range(len(params.args)): neg_index = -1 - i if params.args[neg_index] not in documented_params: continue if params.defaults and i < len(params.defaults): default = params.defaults[neg_index] if type(default) == str: default = '"' + default + '"' else: default = str(default) param_set.insert(0, (params.args[neg_index], default)) else: param_set.insert(0, (params.args[neg_index], )) return "\n".join(func_doc), param_set, params_doc, return_doc def get_class_documentation(cls): inp = {} inp["name"] = cls.__name__ doc = inspect.getdoc(cls) doc_lines = doc.split("\n") inp["doc"] = "\n".join(doc_lines[:-2]) inp["type"] = doc_lines[-2].split("type: ")[-1] inp["demos"] = doc_lines[-1][7:].split(", ") _, inp["params"], inp["params_doc"], _ = get_function_documentation( cls.__init__) inp["shortcuts"] = list(cls.get_shortcut_implementations().items()) if "interpret" in cls.__dict__: ( inp["interpret"], inp["interpret_params"], inp["interpret_params_doc"], _, ) = get_function_documentation(cls.interpret) _, _, _, inp["interpret_returns_doc"] = get_function_documentation( cls.get_interpretation_scores) return inp inputs = [ get_class_documentation(cls) for cls in InputComponent.__subclasses__() ] outputs = [ get_class_documentation(cls) for cls in OutputComponent.__subclasses__() ] interface_params = get_function_documentation(Interface.__init__) interface = { "doc": inspect.getdoc(Interface), "params": interface_params[1], "params_doc": interface_params[2], } launch_params = get_function_documentation(Interface.launch) launch = { "params": launch_params[1], "params_doc": launch_params[2], } load_params = get_function_documentation(Interface.load) load = { "params": load_params[1], "params_doc": load_params[2], "return_doc": load_params[3], } docs = { "input": inputs, "output": outputs, "interface": interface, "launch": launch, "load": load, } os.makedirs("generated", exist_ok=True) with open("src/docs_template.html") as template_file: template = Template(template_file.read()) output_html = template.render(docs=docs, demo_links=demo_links, navbar_html=navbar_html) os.makedirs(os.path.join("generated", "docs"), exist_ok=True) with open(os.path.join("generated", "docs", "index.html"), "w") as generated_template: generated_template.write(output_html)
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__) inp["shortcuts"] = list(cls.get_shortcut_implementations().items()) cls_name = cls.__name__ if cls_name in demos: inp["demos"] = demos.get(cls_name, []) docset.append(inp) return docset inputs = document(InputComponent.__subclasses__(), in_demos) outputs = document(OutputComponent.__subclasses__(), out_demos) interface_params = get_params(Interface.__init__) interface = { "doc": inspect.getdoc(Interface), "params": interface_params[0], "params_doc": interface_params[1], } launch_params = get_params(Interface.launch) launch = { "params": launch_params[0], "params_doc": launch_params[1], } with open("docs.json", "w") as docs: json.dump( {