def get_graph(self, graph_name=None, graph_id=None, owner_email=None): """Get a graph with the given graph_name or graph_id. Args: graph_name (str, optional): Name of the graph to be fetched. Defaults to None. graph_id (int, optional): ID of the graph to be fetched. Defaults to None. owner_email (str, optional): Email of the owner of the graph. Defaults to None. Returns: Graph or None: Graph object, if graph with the given 'graph_name' or 'graph_id' exists; otherwise None. Raises: Exception: If both 'graph_name' and 'graph_id' are None. GraphSpaceError: If error response is received from the GraphSpace API. Examples: Getting a graph by name: >>> # Connecting to GraphSpace >>> from graphspace_python.api.client import GraphSpace >>> graphspace = GraphSpace('*****@*****.**', 'user1') >>> # Fetching a graph >>> graph = graphspace.get_graph(graph_name='My Sample Graph') >>> graph.get_name() u'My Sample Graph' Getting a graph by id: >>> graph = graphspace.get_graph(graph_id=65930) >>> graph.get_name() u'My Sample Graph' Note: Refer to the `tutorial <../tutorial/tutorial.html#fetching-a-graph-from-graphspace>`_ for more about fetching graphs. """ if graph_id is not None: graph_by_id_path = GRAPHS_PATH + str(graph_id) return APIResponse('graph', self.client._make_request("GET", graph_by_id_path) ).graph if graph_name is not None: query = { 'owner_email': self.client.username if owner_email is None else owner_email, 'names[]': graph_name } if owner_email is not None and owner_email != self.client.username: query.update({'is_public': 1}) response = self.client._make_request("GET", GRAPHS_PATH, url_params=query) if response.get('total', 0) > 0: return APIResponse('graph', response.get('graphs')[0] ).graph else: return None raise Exception('Both graph_id and graph_name can\'t be none!')
def get_group(self, group_name=None, group_id=None): """Get a group with the given group_name or group_id, where the requesting user is a member. Args: group_name (str, optional): Name of the group to be fetched. Defaults to None. group_id (int, optional): ID of the group to be fetched. Defaults to None. Returns: Group or None: Group object, if group with the given 'group_name' or 'group_id' exists; otherwise None. Raises: Exception: If both 'group_name' and 'group_id' are None. GraphSpaceError: If error response is received from the GraphSpace API. Examples: Getting a group by name: >>> # Connecting to GraphSpace >>> from graphspace_python.api.client import GraphSpace >>> graphspace = GraphSpace('*****@*****.**', 'user1') >>> # Fetching a group >>> group = graphspace.get_group(group_name='My Sample Group') >>> group.get_name() u'My Sample Group' Getting a group by id: >>> group = graphspace.get_group(group_id=198) >>> group.get_name() u'My Sample Graph' Note: Refer to the `tutorial <../tutorial/tutorial.html#fetching-a-group-from-graphspace>`_ for more about fetching groups. """ if group_id is not None: group_by_id_path = GROUPS_PATH + str(group_id) return APIResponse( 'group', self.client._make_request("GET", group_by_id_path)).group if group_name is not None: response = self.client._make_request("GET", GROUPS_PATH, url_params={ 'member_email': self.client.username, 'name': group_name }) if response.get('total', 0) > 0: return APIResponse('group', response.get('groups')[0]).group else: return None raise Exception('Both group_id and group_name can\'t be none!')
def get_all_groups(self, limit=20, offset=0): """Get groups where the requesting user is a member. Args: offset (int, optional): Offset the list of returned entities by this number. Defaults to 0. limit (int, optional): Number of entities to return. Defaults to 20. Returns: List[Group]: List of groups where the requesting user is a member. Raises: GraphSpaceError: If error response is received from the GraphSpace API. Example: >>> # Connecting to GraphSpace >>> from graphspace_python.api.client import GraphSpace >>> graphspace = GraphSpace('*****@*****.**', 'user1') >>> # Fetching all groups >>> groups = graphspace.get_all_groups(limit=5) >>> groups[0].get_name() u'Test Group' """ query = { 'limit': limit, 'offset': offset, 'member_email': self.client.username } return APIResponse( 'group', self.client._make_request("GET", GROUPS_PATH, url_params=query)).groups
def post_group(self, group): """Create a group for the requesting user. Args: group (GSGroup or Group): Object having group details, such as name, description. Returns: Group: Saved group on GraphSpace. Raises: GraphSpaceError: If error response is received from the GraphSpace API. Example: >>> # Connecting to GraphSpace >>> from graphspace_python.api.client import GraphSpace >>> graphspace = GraphSpace('*****@*****.**', 'user1') >>> # Creating a group >>> from graphspace_python.graphs.classes.gsgroup import GSGroup >>> group = GSGroup(name='My Sample Group', description='sample group') >>> # Saving group on GraphSpace >>> graphspace.post_graph(group) Note: Refer to the `tutorial <../tutorial/tutorial.html#creating-a-group>`_ for more about posting groups. """ headers = { 'Accept': 'application/json', 'Content-Type': 'application/x-www-form-urlencoded' } data = group.json() data.update({'owner_email': self.client.username}) return APIResponse('group', self.client._make_request("POST", GROUPS_PATH, data=data, headers=headers) ).group
def get_my_graphs(self, tags=None, limit=20, offset=0): """Get graphs created by the requesting user. Args: tags (List[str], optional): Search for graphs with the given given list of tag names. In order to search for graphs with given tag as a substring, wrap the name of the tag with percentage symbol. For example, %xyz% will search for all graphs with 'xyz' in their tag names. Defaults to None. offset (int, optional): Offset the list of returned entities by this number. Defaults to 0. limit (int, optional): Number of entities to return. Defaults to 20. Returns: List[Graph]: List of graphs owned by the requesting user. Raises: GraphSpaceError: If error response is received from the GraphSpace API. Examples: Getting your graphs: >>> # Connecting to GraphSpace >>> from graphspace_python.api.client import GraphSpace >>> graphspace = GraphSpace('*****@*****.**', 'user1') >>> # Fetching my graphs >>> graphs = graphspace.get_my_graphs(limit=5) >>> graphs[0].get_name() u'test' Getting your graphs by tags: >>> graphs = graphspace.get_my_graphs(tags=['Kegg-networks'], limit=5) >>> graphs[0].get_name() u'KEGG-Wnt-signaling-pathway' """ query = { 'owner_email': self.client.username, 'limit': limit, 'offset': offset } if tags is not None: query.update({'tags[]': tags}) return APIResponse( 'graph', self.client._make_request("GET", GRAPHS_PATH, url_params=query)).graphs
def post_graph(self, graph): """Posts NetworkX graph to the requesting users account on GraphSpace. Args: graph (GSGraph or Graph): Object having graph details, such as name, graph_json, style_json, is_public, tags. Returns: Graph: Saved graph on GraphSpace. Raises: GraphSpaceError: If error response is received from the GraphSpace API. Example: >>> # Connecting to GraphSpace >>> from graphspace_python.api.client import GraphSpace >>> graphspace = GraphSpace('*****@*****.**', 'user1') >>> # Creating a graph >>> from graphspace_python.graphs.classes.gsgraph import GSGraph >>> G = GSGraph() >>> G.set_name('My Sample Graph') >>> G.set_tags(['sample']) >>> G.add_node('a', popup='sample node popup text', label='A') >>> G.add_node('b', popup='sample node popup text', label='B') >>> G.add_edge('a', 'b', directed=True, popup='sample edge popup') >>> G.add_edge_style('a', 'b', directed=True, edge_style='dotted') >>> # Saving graph on GraphSpace >>> graphspace.post_graph(G) Note: Refer to the `tutorial <../tutorial/tutorial.html#creating-a-graph>`_ for more about posting graphs. """ data = graph.json() data.update({'owner_email': self.client.username}) return APIResponse( 'graph', self.client._make_request("POST", GRAPHS_PATH, data=data)).graph
def update_graph_layout(self, layout, layout_name=None, layout_id=None, graph_name=None, graph_id=None, graph=None, owner_email=None): """Update a layout with given layout_id or layout_name for a graph with the given graph_name, graph_id or the graph object itself. Args: layout (GSLayout or Layout): Object having layout details, such as name, is_shared, style_json, positions_json. layout_name (str, optional): Name of the layout to be updated. Defaults to None. layout_id (int, optional): ID of the layout to be updated. Defaults to None. graph_name (str, optional): Name of the graph. Defaults to None. graph_id (int, optional): ID of the graph. Defaults to None. graph (GSGraph or Graph, optional): Object having graph details, such as name, graph_json, style_json, is_public, tags. Defaults to None. owner_email (str, optional): Email of owner of layout. Defaults to None. Returns: Layout: Updated layout on GraphSpace. Raises: Exception: If both 'layout_name' and 'layout_id' are None and layout object has no 'name' or 'id' attribute; or if both 'graph_name' and 'graph_id' are None and neither graph object has 'name' or 'id' attribute nor layout object has 'graph_id' attribute; or if graph or layout doesnot exist. GraphSpaceError: If error response is received from the GraphSpace API. Examples: Updating a layout by creating a new layout and replacing the existing layout: >>> # Connecting to GraphSpace >>> from graphspace_python.api.client import GraphSpace >>> graphspace = GraphSpace('*****@*****.**', 'user1') >>> # Creating the new layout >>> L = GSLayout() >>> L.set_node_position('a', y=102, x=238.1) >>> L.add_node_style('a', shape='octagon', color='green', width=60, height=60) >>> L.set_name('My Sample Layout') >>> L.set_is_shared(1) >>> # Updating to replace the existing layout >>> graphspace.update_graph_layout(L, graph_id=65390) Another way of updating a layout by fetching and editing the existing layout: >>> # Fetching the layout >>> layout = graphspace.get_graph_layout(name='My Sample Layout') >>> # Modifying the fetched layout >>> layout.set_node_position('a', y=30, x=211) >>> layout.add_node_style('a', shape='roundrectangle', color='green', width=45, height=45) >>> layout.set_is_shared(0) >>> # Updating layout >>> graphspace.update_graph_layout(layout) If you also provide 'layout_name' or 'layout_id' as param then the update will be performed for that layout having the given name or id: >>> graphspace.update_graph_layout(L, layout_id=1087, graph_id=65390) Note: Refer to the `tutorial <../tutorial/tutorial.html#updating-a-layout-on-graphspace>`_ for more about updating layouts. """ if hasattr(layout, 'graph_id'): graph_id = layout.graph_id if graph is not None: if hasattr(graph, 'id'): graph_id = graph.id elif hasattr(graph, 'name'): graph_name = graph.name if graph_id is None and graph_name is None: raise Exception( 'Both graph_id and graph_name can\'t be none when graph object has no \'name\' or \'id\' attribute and layout object has no \'graph_id\' attribute!' ) if graph_id is None: graph = self.get_graph(graph_name=graph_name) if graph is None or graph.id is None: raise Exception( 'Graph with name `%s` doesnt exist for user `%s`!' % (graph_name, self.client.username)) else: graph_id = graph.id if layout_id is not None or hasattr(layout, 'id'): layout_id = layout_id if layout_id is not None else layout.id layout_by_id_path = LAYOUTS_PATH.format(graph_id) + str(layout_id) return APIResponse( 'layout', self.client._make_request("PUT", layout_by_id_path, data=layout.json())).layout if layout_name is not None or hasattr(layout, 'name'): layout_name = layout_name if layout_name is not None else layout.name layout1 = self.get_graph_layout(graph_id=graph_id, layout_name=layout_name, owner_email=owner_email) if layout1 is None or layout1.id is None: raise Exception( 'Layout with name `%s` of graph with graph_id=%s doesnt exist for user `%s`!' % (layout_name, graph_id, self.client.username)) else: layout_by_id_path = LAYOUTS_PATH.format(graph_id) + str( layout1.id) return APIResponse( 'layout', self.client._make_request("PUT", layout_by_id_path, data=layout.json())).layout raise Exception( 'Both layout_id and layout_name can\'t be none when layout object has no \'name\' or \'id\' attribute!' )
def get_shared_graph_layouts(self, graph_name=None, graph_id=None, graph=None, limit=20, offset=0): """Get layouts shared with the requesting user for the graph with given graph_name, graph_id or graph object. Args: graph_name (str, optional): Name of the graph. Defaults to None. graph_id (int, optional): ID of the graph. Defaults to None. graph (GSGraph or Graph, optional): Object having graph details, such as name, graph_json, style_json, is_public, tags. Defaults to None. offset (int, optional): Offset the list of returned entities by this number. Defaults to 0. limit (int, optional): Number of entities to return. Defaults to 20. Returns: List[Layout]: List of layouts shared with the requesting user. Raises: Exception: If both 'graph_name' and 'graph_id' are None and graph object has no 'name' or 'id' attribute; or if graph doesnot exist. GraphSpaceError: If error response is received from the GraphSpace API. Examples: Getting shared graph layouts by graph id: >>> # Connecting to GraphSpace >>> from graphspace_python.api.client import GraphSpace >>> graphspace = GraphSpace('*****@*****.**', 'user1') >>> # Fetching shared graph layouts >>> layouts = graphspace.get_shared_graph_layouts(graph_id=65390, limit=5) >>> layouts[0].get_name() u'My Sample Layout' Getting shared graph layouts by graph name: >>> layouts = graphspace.get_shared_graph_layouts(graph_name='My Sample Graph', limit=5) Getting shared graph layouts by graph object itself: >>> graph = graphspace.get_graph(graph_name='My Sample Graph') >>> layouts = graphspace.get_shared_graph_layouts(graph=graph, limit=5) """ if graph is not None: if hasattr(graph, 'id'): graph_id = graph.id elif hasattr(graph, 'name'): graph_name = graph.name if graph_id is None and graph_name is None: raise Exception( 'Both graph_id and graph_name can\'t be none when graph object has no \'name\' or \'id\' attribute!' ) if graph_id is None: graph = self.get_graph(graph_name=graph_name) if graph is None or graph.id is None: raise Exception( 'Graph with name `%s` doesnt exist for user `%s`!' % (graph_name, self.client.username)) else: graph_id = graph.id query = {'limit': limit, 'offset': offset, 'is_shared': 1} layouts_path = LAYOUTS_PATH.format(graph_id) return APIResponse( 'layout', self.client._make_request("GET", layouts_path, url_params=query)).layouts
def get_graph_layout(self, layout_name=None, layout_id=None, graph_name=None, graph_id=None, graph=None, owner_email=None): """Get a layout with given layout_id or layout_name for the graph with given graph_id, graph_name or graph object. Args: layout_name (str, optional): Name of the layout to be fetched. Defaults to None. layout_id (int, optional): ID of the layout to be fetched. Defaults to None. graph_name (str, optional): Name of the graph. Defaults to None. graph_id (int, optional): ID of the graph. Defaults to None. graph (GSGraph or Graph, optional): Object having graph details, such as name, graph_json, style_json, is_public, tags. Defaults to None. owner_email (str, optional): Email of owner of layout. Defaults to None. Returns: Layout or None: Layout object, if layout with the given 'layout_name' or 'layout_id' exists; otherwise None. Raises: Exception: If both 'layout_name' and 'layout_id' are None; or if both 'graph_name' and 'graph_id' are None and graph object has no 'name' or 'id' attribute; or if graph doesnot exist. GraphSpaceError: If error response is received from the GraphSpace API. Examples: Getting a layout by name: >>> # Connecting to GraphSpace >>> from graphspace_python.api.client import GraphSpace >>> graphspace = GraphSpace('*****@*****.**', 'user1') >>> # Fetching a layout >>> layout = graphspace.get_graph_layout(layout_name='My Sample Layout', graph_id=65390) >>> layout.get_name() u'My Sample Layout' Getting a layout by id: >>> layout = graphspace.get_graph_layout(layout_id=1087, graph_id=65390) >>> layout.get_name() u'My Sample Layout' Getting a layout by providing graph name: >>> layout = graphspace.get_graph_layout(layout_id=1087, graph_name='My Sample Graph') >>> layout.get_name() u'My Sample Layout' Getting a layout by providing graph object as param: >>> graph = graphspace.get_graph(graph_name='My Sample Graph') >>> layout = graphspace.get_graph_layout(layout_id=1087, graph=graph) >>> layout.get_name() u'My Sample Layout' Note: Refer to the `tutorial <../tutorial/tutorial.html#fetching-a-layout-from-graphspace>`_ for more about fetching layouts. """ if graph is not None: if hasattr(graph, 'id'): graph_id = graph.id elif hasattr(graph, 'name'): graph_name = graph.name if graph_id is None and graph_name is None: raise Exception( 'Both graph_id and graph_name can\'t be none when graph object has no \'name\' or \'id\' attribute!' ) if graph_id is None: graph = self.get_graph(graph_name=graph_name) if graph is None or graph.id is None: raise Exception( 'Graph with name `%s` doesnt exist for user `%s`!' % (graph_name, self.client.username)) else: graph_id = graph.id if layout_id is not None: layout_by_id_path = LAYOUTS_PATH.format(graph_id) + str(layout_id) return APIResponse( 'layout', self.client._make_request("GET", layout_by_id_path)).layout if layout_name is not None: query = { 'owner_email': self.client.username if owner_email is None else owner_email, 'name': layout_name } if owner_email is not None and owner_email != self.client.username: query.update({'is_shared': 1}) layouts_path = LAYOUTS_PATH.format(graph_id) response = self.client._make_request("GET", layouts_path, url_params=query) if response.get('total', 0) > 0: return APIResponse('layout', response.get('layouts')[0]).layout else: return None raise Exception('Both layout_id and layout_name can\'t be none!')
def post_graph_layout(self, layout, graph_name=None, graph_id=None, graph=None): """Create a layout for a graph provided the graph_name, graph_id or the graph object itself. Args: layout (GSLayout or Layout): Object having layout details, such as name, is_shared, style_json, positions_json. graph_name (str, optional): Name of the graph. Defaults to None. graph_id (int, optional): ID of the graph. Defaults to None. graph (GSGraph or Graph, optional): Object having graph details, such as name, graph_json, style_json, is_public, tags. Defaults to None. Returns: Layout: Saved layout on GraphSpace. Raises: Exception: If both 'graph_name' and 'graph_id' are None and graph object has no 'name' or 'id' attribute; or if graph doesnot exist. GraphSpaceError: If error response is received from the GraphSpace API. Examples: Saving a layout when graph id is known: >>> # Connecting to GraphSpace >>> from graphspace_python.api.client import GraphSpace >>> graphspace = GraphSpace('*****@*****.**', 'user1') >>> # Creating a layout >>> from graphspace_python.graphs.classes.gslayout import GSLayout >>> L = GSLayout() >>> L.set_node_position('a', y=38.5, x=67.3) >>> L.add_node_style('a', shape='ellipse', color='green', width=60, height=60) >>> L.set_name('My Sample Layout') >>> L.set_is_shared(1) >>> # Saving layout on GraphSpace >>> graphspace.post_graph_layout(L, graph_id=65390) Saving a layout when graph name is known: >>> graphspace.post_graph_layout(L, graph_name='My Sample Graph') Saving a layout by passing graph object itself as param: >>> graph = graphspace.get_graph(graph_name='My Sample Graph') >>> graphspace.post_graph_layout(L, graph=graph) Note: Refer to the `tutorial <../tutorial/tutorial.html#creating-a-layout>`_ for more about posting layouts. """ if graph is not None: if hasattr(graph, 'id'): graph_id = graph.id elif hasattr(graph, 'name'): graph_name = graph.name if graph_id is None and graph_name is None: raise Exception( 'Both graph_id and graph_name can\'t be none when graph object has no \'name\' or \'id\' attribute!' ) if graph_id is None: graph = self.get_graph(graph_name=graph_name) if graph is None or graph.id is None: raise Exception( 'Graph with name `%s` doesnt exist for user `%s`!' % (graph_name, self.client.username)) else: graph_id = graph.id data = layout.json() data.update({ 'graph_id': graph_id, 'owner_email': self.client.username }) layouts_path = LAYOUTS_PATH.format(graph_id) return APIResponse( 'layout', self.client._make_request("POST", layouts_path, data=data)).layout
def get_group_graphs(self, group_name=None, group_id=None, group=None): """Get graphs shared with a group provided the group_name, group_id or the group object itself. Args: group_name (str, optional): Name of the group. Defaults to None. group_id (int, optional): ID of the group. Defaults to None. group (GSGroup or Group, optional): Object having group details, such as name, description. Defaults to None. Returns: List[Graph]: List of graphs belonging to the group. Raises: Exception: If both 'group_name' and 'group_id' are None and group object has no 'name' or 'id' attribute; or if group doesnot exist. GraphSpaceError: If error response is received from the GraphSpace API. Examples: Getting graphs of a group when group name is known: >>> # Connecting to GraphSpace >>> from graphspace_python.api.client import GraphSpace >>> graphspace = GraphSpace('*****@*****.**', 'user1') >>> # Fetching group graphs >>> graphs = graphspace.get_group_graphs(group_name='My Sample Group') >>> graphs[0].get_name() u'My Sample Graph' Getting graphs of a group when group id is known: >>> graphs = graphspace.get_group_graphs(group_id=198) >>> graphs[0].get_name() u'My Sample Graph' Getting graphs of a group by passing group object itself as param: >>> group = graphspace.get_group(group_name='My Sample Group') >>> graphs = graphspace.get_group_graphs(group=group) >>> graphs[0].get_name() u'My Sample Graph' Note: Refer to the `tutorial <../tutorial/tutorial.html#fetching-graphs-shared-with-a-group>`_ for more about fetching graphs of a group. """ if group is not None: if hasattr(group, 'id'): group_id = group.id elif hasattr(group, 'name'): group_name = group.name if group_id is not None: group_graphs_path = GROUPS_PATH + str(group_id) + '/graphs' return APIResponse( 'graph', self.client._make_request("GET", group_graphs_path)).graphs if group_name is not None: group = self.get_group(group_name=group_name) if group is None or group.id is None: raise Exception( 'Group with name `%s` doesnt exist for user `%s`!' % (group_name, self.client.username)) else: group_graphs_path = GROUPS_PATH + str(group.id) + '/graphs' return APIResponse( 'graph', self.client._make_request("GET", group_graphs_path)).graphs raise Exception( 'Both group_id and group_name can\'t be none when group object has no \'name\' or \'id\' attribute!' )
def update_group(self, group, group_name=None, group_id=None): """Update a group on GraphSpace with the provided group object. If group_name or group_id is also provided then the update will be performed for that group having the given name or id. Args: group (GSGroup or Group): Object having group details, such as name, description. group_name (str, optional): Name of the group to be updated. Defaults to None. group_id (int, optional): ID of the group to be updated. Defaults to None. Returns: Group: Updated group on GraphSpace. Raises: Exception: If both 'group_name' and 'group_id' are None and group object has no 'name' or 'id' attribute; or if group doesnot exist. GraphSpaceError: If error response is received from the GraphSpace API. Examples: Updating a group by creating a new group and replacing the existing group: >>> # Connecting to GraphSpace >>> from graphspace_python.api.client import GraphSpace >>> graphspace = GraphSpace('*****@*****.**', 'user1') >>> # Creating the new group >>> group = GSGroup(name='My Sample Group', description='updated sample group') >>> # Updating to replace the existing group >>> group = graphspace.update_group(group) >>> group.get_description() u'updated sample group' Another way of updating a group by fetching and editing the existing group: >>> # Fetching the group >>> group = graphspace.get_group(group_name='My Sample Group') >>> # Modifying the fetched group >>> group.set_description('updated sample group') >>> # Updating group >>> group = graphspace.update_group(group) >>> group.get_description() u'updated sample group' If you also provide 'group_name' or 'group_id' as param then the update will be performed for that group having the given name or id: >>> graphspace.update_group(group, group_id=198) Note: Refer to the `tutorial <../tutorial/tutorial.html#updating-a-group-on-graphspace>`_ for more about updating groups. """ headers = { 'Accept': 'application/json', 'Content-Type': 'application/x-www-form-urlencoded' } if group_id is not None or hasattr(group, 'id'): group_id = group_id if group_id is not None else group.id group_by_id_path = GROUPS_PATH + str(group_id) return APIResponse( 'group', self.client._make_request("PUT", group_by_id_path, data=group.json(), headers=headers)).group if group_name is not None or hasattr(group, 'name'): group_name = group_name if group_name is not None else group.name group1 = self.get_group(group_name=group_name) if group1 is None or group1.id is None: raise Exception( 'Group with name `%s` doesnt exist for user `%s`!' % (group_name, self.client.username)) else: group_by_id_path = GROUPS_PATH + str(group1.id) return APIResponse( 'group', self.client._make_request("PUT", group_by_id_path, data=group.json(), headers=headers)).group raise Exception( 'Both group_id and group_name can\'t be none when group object has no \'name\' or \'id\' attribute!' )
def unset_default_graph_layout(self, graph_name=None, graph_id=None, graph=None): """Unsets the current default layout of a graph provided the graph_name, graph_id or graph object itself. Args: graph_name (str, optional): Name of the graph. Defaults to None. graph_id (int, optional): ID of the graph. Defaults to None. graph (GSGraph or Graph, optional): Object having graph details, such as name, graph_json, style_json, is_public, tags. Defaults to None. Returns: Graph: Updated graph on GraphSpace. Raises: Exception: If both 'graph_name' and 'graph_id' are None and graph object has no 'name' or 'id' attribute; or if graph doesnot exist. GraphSpaceError: If error response is received from the GraphSpace API. Examples: Unset default graph layout by graph name: >>> # Connecting to GraphSpace >>> from graphspace_python.api.client import GraphSpace >>> graphspace = GraphSpace('*****@*****.**', 'user1') >>> # Unset the default graph layout >>> graph = graphspace.unset_default_graph_layout(graph_name='My Sample Graph') >>> assert graph.default_layout_id is None Unset default graph layout by graph id: >>> graph = graphspace.unset_default_graph_layout(graph_id=65930) Unset default graph layout by graph object: >>> graph = graphspace.get_graph(graph_name='My Sample Graph') >>> graph = graphspace.unset_default_graph_layout(graph=graph) Note: Refer to the `tutorial <../tutorial/tutorial.html#unset-default-layout-for-a-graph>`_ for more about unsetting a default graph layout. """ if graph is not None: if hasattr(graph, 'id'): graph_id = graph.id elif hasattr(graph, 'name'): graph_name = graph.name if graph_id is not None: graph_by_id_path = GRAPHS_PATH + str(graph_id) return APIResponse( 'graph', self.client._make_request("PUT", graph_by_id_path, data={'default_layout_id': 0})).graph if graph_name is not None: graph = self.get_graph(graph_name=graph_name) if graph is None or graph.id is None: raise Exception( 'Graph with name `%s` doesnt exist for user `%s`!' % (graph_name, self.client.username)) else: graph_by_id_path = GRAPHS_PATH + str(graph.id) return APIResponse( 'graph', self.client._make_request("PUT", graph_by_id_path, data={'default_layout_id': 0})).graph raise Exception( 'Both graph_id and graph_name can\'t be none when graph object has no \'name\' or \'id\' attribute!' )
def set_default_graph_layout(self, graph_name=None, graph_id=None, graph=None, layout_name=None, layout_id=None, layout=None): """Set a default layout (provided the layout_name, layout_id or layout object) for a graph with given graph_name, graph_id or graph object itself. Args: graph_name (str, optional): Name of the graph. Defaults to None. graph_id (int, optional): ID of the graph. Defaults to None. graph (GSGraph or Graph, optional): Object having graph details, such as name, graph_json, style_json, is_public, tags. Defaults to None. layout_name (str, optional): Name of the layout. Defaults to None. layout_id (int, optional): ID of the layout. Defaults to None. layout (GSLayout or Layout): Object having layout details, such as name, is_shared, style_json, positions_json. Returns: Graph: Updated graph on GraphSpace. Raises: Exception: If both 'layout_name' and 'layout_id' are None and layout object has no 'name' or 'id' attribute; or if both 'graph_name' and 'graph_id' are None and neither graph object has 'name' or 'id' attribute nor layout object has 'graph_id' attribute; or if graph or layout doesnot exist. GraphSpaceError: If error response is received from the GraphSpace API. Examples: Setting a default layout when graph name is known: >>> # Connecting to GraphSpace >>> from graphspace_python.api.client import GraphSpace >>> graphspace = GraphSpace('*****@*****.**', 'user1') >>> # Setting default layout for the graph >>> graph = graphspace.set_default_graph_layout(graph_name='My Sample Graph', layout_id=1087) >>> graph.default_layout_id 1087 Setting a default layout when graph id is known: >>> graph = graphspace.set_default_graph_layout(graph_id=65930, layout_id=1087) >>> graph.default_layout_id 1087 Setting a default layout when graph object is passed as param: >>> graph = graphspace.get_graph(graph_name='My Sample Graph') >>> graph = graphspace.set_default_graph_layout(graph=graph, layout_id=1087) >>> graph.default_layout_id 1087 Setting a default layout by passing layout name: >>> graph = graphspace.set_default_graph_layout(graph_id=65930, layout_name='My Sample Layout') >>> graph.default_layout_id 1087 Setting a default layout by only passing layout object as param: >>> layout = graphspace.get_graph_layout(graph_id=65930, layout_name='My Sample Layout') >>> graph = graphspace.set_default_graph_layout(layout=layout) >>> graph.default_layout_id 1087 Note: Refer to the `tutorial <../tutorial/tutorial.html#setting-a-default-layout-for-a-graph>`_ for more about setting default graph layout. """ if graph is not None: if hasattr(graph, 'id'): graph_id = graph.id elif hasattr(graph, 'name'): graph_name = graph.name if hasattr(layout, 'graph_id'): graph_id = layout.graph_id if graph_id is None and graph_name is None: raise Exception( 'Both graph_id and graph_name can\'t be none when graph object has no \'name\' or \'id\' attribute and layout object has no \'graph_id\' attribute!' ) if layout is not None: if hasattr(layout, 'id'): layout_id = layout.id elif hasattr(layout, 'name'): layout_name = layout.name if layout_id is None and layout_name is None: raise Exception( 'Both layout_id and layout_name can\'t be none when layout object has no \'name\' or \'id\' attribute!' ) if layout_id is None: layout = self.client.get_graph_layout(layout_name=layout_name, graph_id=graph_id) if layout is None or layout.id is None: raise Exception( 'Layout with name `%s` of graph with graph_id=%s doesnt exist for user `%s`!' % (layout_name, graph_id, self.client.username)) else: layout_id = layout.id if graph_id is not None: graph_by_id_path = GRAPHS_PATH + str(graph_id) return APIResponse( 'graph', self.client._make_request( "PUT", graph_by_id_path, data={'default_layout_id': layout_id})).graph if graph_name is not None: graph = self.get_graph(graph_name=graph_name) if graph is None or graph.id is None: raise Exception( 'Graph with name `%s` doesnt exist for user `%s`!' % (graph_name, self.client.username)) else: graph_by_id_path = GRAPHS_PATH + str(graph.id) return APIResponse( 'graph', self.client._make_request( "PUT", graph_by_id_path, data={'default_layout_id': layout_id})).graph
def unpublish_graph(self, graph_name=None, graph_id=None, graph=None): """Makes a graph privately viewable. Args: graph_name (str, optional): Name of the graph. Defaults to None. graph_id (int, optional): ID of the graph. Defaults to None. graph (GSGraph or Graph, optional): Object having graph details, such as name, graph_json, style_json, is_public, tags. Defaults to None. Returns: Graph: Updated graph on GraphSpace. Raises: Exception: If both 'graph_name' and 'graph_id' are None and graph object has no 'name' or 'id' attribute; or if graph doesnot exist. GraphSpaceError: If error response is received from the GraphSpace API. Examples: Make graph private by name: >>> # Connecting to GraphSpace >>> from graphspace_python.api.client import GraphSpace >>> graphspace = GraphSpace('*****@*****.**', 'user1') >>> # Unpublishing the graph >>> graph = graphspace.unpublish_graph(graph_name='My Sample Graph') >>> graph.get_is_public() 0 Make graph private by id: >>> graph = graphspace.unpublish_graph(graph_id=65930) >>> graph.get_is_public() 0 Make graph private by passing graph object itself as param: >>> graph = graphspace.get_graph(graph_name='My Sample Graph') >>> graphspace.unpublish_graph(graph=graph) Note: Refer to the `tutorial <../tutorial/tutorial.html#making-a-graph-private-on-graphspace>`_ for more about making a graph private. """ if graph is not None: if hasattr(graph, 'id'): graph_id = graph.id elif hasattr(graph, 'name'): graph_name = graph.name if graph_id is not None: graph_by_id_path = GRAPHS_PATH + str(graph_id) return APIResponse( 'graph', self.client._make_request("PUT", graph_by_id_path, data={'is_public': 0})).graph if graph_name is not None: graph = self.get_graph(graph_name=graph_name) if graph is None or graph.id is None: raise Exception( 'Graph with name `%s` doesnt exist for user `%s`!' % (graph_name, self.client.username)) else: graph_by_id_path = GRAPHS_PATH + str(graph.id) return APIResponse( 'graph', self.client._make_request("PUT", graph_by_id_path, data={'is_public': 0})).graph raise Exception( 'Both graph_id and graph_name can\'t be none when graph object has no \'name\' or \'id\' attribute!' )
def update_graph(self, graph, graph_name=None, graph_id=None, owner_email=None): """Update a graph on GraphSpace with the provided graph object. If graph_name or graph_id is also provided then the update will be performed for that graph having the given name or id. Args: graph (GSGraph or Graph): Object having graph details, such as name, graph_json, style_json, is_public, tags. graph_name (str, optional): Name of the graph to be updated. Defaults to None. graph_id (int, optional): ID of the graph to be updated. Defaults to None. owner_email (str, optional): Email of owner of the graph. Defaults to None. Returns: Graph: Updated graph on GraphSpace. Raises: Exception: If both 'graph_name' and 'graph_id' are None and graph object has no 'name' or 'id' attribute; or if graph doesnot exist. GraphSpaceError: If error response is received from the GraphSpace API. Examples: Updating a graph by creating a new graph and replacing the existing graph: >>> # Connecting to GraphSpace >>> from graphspace_python.api.client import GraphSpace >>> graphspace = GraphSpace('*****@*****.**', 'user1') >>> # Creating the new graph >>> G = GSGraph() >>> G.add_node('a', popup='sample node popup text', label='A updated') >>> G.add_node('b', popup='sample node popup text', label='B updated') >>> G.add_edge('a', 'b', directed=True, popup='sample edge popup') >>> G.add_edge_style('a', 'b', directed=True, edge_style='dotted') >>> G.set_name('My Sample Graph') >>> G.set_is_public(1) >>> # Updating to replace the existing graph >>> graphspace.update_graph(G) Another way of updating a graph by fetching and editing the existing graph: >>> # Fetching the graph >>> graph = graphspace.get_graph(graph_name='My Sample Graph') >>> # Modifying the fetched graph >>> graph.add_node('z', popup='sample node popup text', label='Z') >>> graph.add_node_style('z', shape='ellipse', color='green', width=90, height=90) >>> graph.add_edge('a', 'z', directed=True, popup='sample edge popup') >>> graph.set_is_public(1) >>> # Updating graph >>> graphspace.update_graph(graph) If you also provide 'graph_name' or 'graph_id' as param then the update will be performed for that graph having the given name or id: >>> graphspace.update_graph(G, graph_id=65930) Note: Refer to the `tutorial <../tutorial/tutorial.html#updating-a-graph-on-graphspace>`_ for more about updating graphs. """ if graph_id is not None or hasattr(graph, 'id'): graph_id = graph_id if graph_id is not None else graph.id graph_by_id_path = GRAPHS_PATH + str(graph_id) return APIResponse( 'graph', self.client._make_request("PUT", graph_by_id_path, data=graph.json())).graph if graph_name is not None or hasattr(graph, 'name'): graph_name = graph_name if graph_name is not None else graph.name graph1 = self.get_graph(graph_name=graph_name, owner_email=owner_email) if graph1 is None or graph1.id is None: raise Exception( 'Graph with name `%s` doesnt exist for user `%s`!' % (graph_name, self.client.username)) else: graph_by_id_path = GRAPHS_PATH + str(graph1.id) return APIResponse( 'graph', self.client._make_request("PUT", graph_by_id_path, data=graph.json())).graph raise Exception( 'Both graph_id and graph_name can\'t be none when graph object has no \'name\' or \'id\' attribute!' )