code: def convert_networkx_graph(graph_proto, sentence, sent_idx):
      """
      Turns a networkx graph into a DependencyGraph from the proto file
      """
      for token in sentence.tokens:
          for word in token.words:
              add_token(graph_proto.token, word, token)
      for word in sentence.empty_words:
          add_token(graph_proto.token, word, None)
  
      dependencies = sentence._enhanced_dependencies
      for target in dependencies:
          if target == 0:
              # don't need to send the explicit root
              continue
          for source in dependencies.predecessors(target):
              if source == 0:
                  # unlike with basic, we need to send over the roots,
                  # as the enhanced can have loops
                  graph_proto.rootNode.append(len(graph_proto.node))
                  continue
              for deprel in dependencies.get_edge_data(source, target):
                  edge = graph_proto.edge.add()
                  if isinstance(source, int):
                      edge.source = source
                  else:
                      edge.source = source[0]
                      if source[1] != 0:
                          edge.sourceEmpty = source[1]
                  if isinstance(target, int):
                      edge.target = target
                  else:
                      edge.target = target[0]
                      if target[1] != 0:
                          edge.targetEmpty = target[1]
                  edge.dep = deprel
          node = graph_proto.node.add()
          node.sentenceIndex = sent_idx + 1
          # the nodes in the networkx graph are indexed from 1, not counting the root
          if isinstance(target, int):
              node.index = target
          else:
              node.index = target[0]
              if target[1] != 0:
                  node.emptyIndex = target[1]
      return graph_proto
  
imports: from collections import deque
  import subprocess
  from stanza.models.common.utils import misc_to_space_after
  from stanza.models.constituency.parse_tree import Tree
  from stanza.protobuf import DependencyGraph, FlattenedParseTree
  from stanza.server.client import resolve_classpath
label_desc: Use image node_id 8 for calling a global function with example usage: convert_networkx_graph(graph_proto, sentence, sent_idx) and returns: graph_proto