import networkx


def draw_graph(g: networkx.Graph, fname: str):
    """Draw a graph using pygraphviz and return the AGraph object.

    Parameters
    ----------
    g :
        A graph to draw.
    fname :
        The name of the file to write the graph to.

    Returns
    -------
    :
        A graphviz graph object.
    """
    g = g.copy()
    for node in g.nodes:
        if "\\" in g.nodes[node]["label"]:
            g.nodes[node]["label"] = g.nodes[node]["label"].replace("\\", "[backslash]")
    ag = networkx.nx_agraph.to_agraph(g)
    # Add some visual styles to the graph
    ag.node_attr["shape"] = "plaintext"
    ag.graph_attr["splines"] = True
    ag.graph_attr["rankdir"] = "TD"
    ag.draw(fname, prog="dot")
    return ag