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

NodeClassAlreadyDefined Exception in Streamlit Application due to Repeated Class Registration #826

Open
muelldlr opened this issue Aug 22, 2024 · 2 comments

Comments

@muelldlr
Copy link

Expected Behavior (Mandatory)

When repeatedly reloading a Streamlit application, classes should not be registered multiple times in the neomodel library in order to avoid a NodeClassAlreadyDefined exception. The application should be executed without errors, even if it is reloaded several times.

Actual Behavior (Mandatory)

When the Streamlit page is reloaded, a NodeClassAlreadyDefined exception occurs because the classes are registered again. This leads to an error and prevents the application from continuing to run.

How to Reproduce the Problem

Create a simple Streamlit application that uses neomodel classes.
Reload the Streamlit page several times.
Observe that a NodeClassAlreadyDefined exception is thrown on each reload.

Simple Example

import streamlit as st
from neomodel import StructuredNode, StringProperty, UniqueIdProperty, config

config.DATABASE_URL = 'bolt://neo4j:neo4j@localhost:7687'

class Component(StructuredNode):
    uid = UniqueIdProperty()
    name = StringProperty(unique_index=True)
    version = StringProperty()

st.write("This is a test application")

Screenshots (where it's possibile)

grafik

Specifications (Mandatory)

Currently used versions

Versions

  • OS: Microsoft Windows 10
  • Library: 5.3.2
  • Neo4j: 5.22.0
@miguelfg
Copy link

miguelfg commented Oct 7, 2024

Same here using Django running python manage.py runserver also with any of its parameters --noreload --nothreading.

Using:

  • Django==5.0.7
  • neo4j==5.12.0
  • neobolt==1.7.17
  • neomodel==5.1.2
  • django-neomodel==0.1.1

@mruge-shr
Copy link

with a little help from GPT4 i found this workaround that seems to work on mine

from neomodel.sync_.core import db  # Import the Neo4j Database singleton instance

def get_or_create_entity_node_class():
    """
    Retrieve the EntityNode class from the registry or define it if not present.
    """
    # Check if 'EntityNode' is already registered in the registry
    registry = db._NODE_CLASS_REGISTRY  # Access the internal registry
    for labels, node_class in registry.items():
        if 'EntityNode' in labels:
            print("EntityNode found in registry.")
            return node_class

    # If not found, define and register the EntityNode class
    class EntityNode(StructuredNode):
        """Top-level Neo4j node class for storing UUIDs and relationships."""
        uuid = UniqueIdProperty()
        related_to = RelationshipTo('EntityNode', 'RELATED_TO')

    # Register the new class in the registry
    db._NODE_CLASS_REGISTRY[frozenset(['EntityNode'])] = EntityNode
    print("EntityNode registered.")
    return EntityNode

# Usage:
EntityNode = get_or_create_entity_node_class()

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

No branches or pull requests

4 participants