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

How to read in ydoc binary from file? #155

Open
ihasdapie opened this issue Mar 25, 2024 · 2 comments
Open

How to read in ydoc binary from file? #155

ihasdapie opened this issue Mar 25, 2024 · 2 comments

Comments

@ihasdapie
Copy link

Not sure if this is a trivial question or not, but i've been trying to load in a ydoc binary from a file on disk to no success. So far I've tried:

local_doc = YDoc()

def get_fbytes(fpath):
    with open(fpath, 'rb') as f:
        return f.read()
sv = get_fbytes(FNAME)
apply_update(local_doc, sv)

and then getting elements out of local_doc. However, this is not working.

How can I load in and work with a ydoc from a file? Also, what is the recommended way of going through the elements of a ydoc? I saw another issue that suggests there's no good interface for this via ypy yet. If this is the case, how can we check if a key exists? Since all the get_{text, array, ...} methods will create the element if it doesn't already exist.

If someone can point me in the right direction for where to make these changes to support this in the library, i'd be happy to make a PR.

Thanks!

@ihasdapie ihasdapie changed the title read in ydoc binary How to read in ydoc binary from file? Mar 25, 2024
@davidbrochart
Copy link
Collaborator

It's unlikely that you get an answer, since ypy is unmaintained.
You might want to look at pycrdt.

@alexlopashev
Copy link

alexlopashev commented Jan 2, 2025

@ihasdapie, assuming your file was created with contents generated by y_py.encode_state_as_update(y_doc), here's how our project settled on reading data from file into YDoc:

y_doc = y_py.YDoc()

# file is getting its contents from google cloud storage blob is  a google cloud storage blob, so you need to adjust this section to your circumstances
ephemeral_file = io.BytesIO()
blob.download_to_file(ephemeral_file)
ephemeral_file.seek(0)

current_serialized_state: y_py.YDocUpdate = ephemeral_file.read()
y_py.apply_update(y_doc, current_serialized_state)

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

3 participants