Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Script to Automatically upload README #5367

Closed
marvin-robot opened this issue Jan 25, 2022 · 0 comments
Closed

Script to Automatically upload README #5367

marvin-robot opened this issue Jan 25, 2022 · 0 comments

Comments

@marvin-robot
Copy link
Member

Archived from the Prefect Public Slack Community

apanasenco: Automatically uploading a flow's Python docs to the flow's README in the cloud UI

Here's a Python script I wrote to both register a flow in Prefect and also upload its https://pdoc3.github.io|pdoc3-generated documentation to the flow's https://docs.prefect.io/orchestration/ui/flow.html#read-me|README.

Usage:

python register.py -p my_project -m module.that.contains.flow.variable

Source of register.py:

from types import ModuleType
from typing import Union

import click
import pdoc
from prefect import Client
from prefect.utilities.graphql import with_args

def docfilter(docobject):
    print(docobject)

@click.command()
@click.option(
    "--project",
    "-p",
    help="The name of the Prefect project to register this flow in. Required.",
    required=True,
)
@click.option(
    "--module",
    "-m",
    help="A python module name containing the flow to register. Required.",
    required=True,
)
def register_flow(project: str, module: Union[ModuleType, str], **kwargs):
    pdoc3_module = pdoc.Module(module=module)
    flow = pdoc3_module.obj.flow 
    flow_id = flow.register(project_name=project, **kwargs)
    client = Client()
    flow_group_id = client.graphql(
        {
            "query": {
                with_args(
                    "flow",
                    {
                        "where": {
                            "id": {
                                "_eq": flow_id,
                            },
                        },
                    }
                ): {
                    "flow_group_id"
                }
            }
        }
    )["data"]["flow"][0]["flow_group_id"]
    docfilter = lambda doc: doc.name in [t.name for t in flow.tasks]
    pdoc3_module = pdoc.Module(module=module, docfilter=docfilter)
    module_markdown = pdoc3_module.text()
    # Do some post-processing to keep Prefect engine from misinterpreting
    # the markdown.
    module_markdown = module_markdown.replace(":   ",":\n").replace("\n:",":").\
                      replace("\n    ","\n")
    ret = client.graphql(
        {
            "mutation": {
                with_args(
                    "set_flow_group_description",
                    {
                        "input": {
                            "flow_group_id": flow_group_id,
                            "description": module_markdown,
                        },
                    }
                ): {
                    "success"
                }
            }
        }
    )
    if not ret["data"]["set_flow_group_description"]["success"]:
        raise RuntimeError("Failed to set flow group README")


if __name__ == "__main__":
    register_flow()

apanasenco: Result looks something like:

kevin701: <@ULVA73B9P> archive “Script to Automatically upload README”

Original thread can be found here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant