Save a file with python on glitch?

Hi,
I’m totally newb on glitch…
I’ve reached to setup a tornado server, with websocket support : all is working like a charm :wink:

But I need to store some dynamic data (3kb max), I want to save a json object, and reuse it later …
What is the way to do that ?

I reach to save it in my filesystem … but under the editor : it always show me an older version of it. (There is no refresh button in the ui) … will it persist after a reload ?! can’t understand the meaning of that

1 Like

Hi @manatian! Welcome to the Glitch support community! How have you been saving it in your filesystem? I think this should be doable. I could also take a look at the project itself if that’s easier – you can just share the project name here :slight_smile:

3 Likes

With a simple python code like that :

with open("data/kif.json","a+") as fid:
  fid.write( json )

And reading seems to work too …
but under the ui editor : it does’nt change

Oh interesting! When you say that it doesn’t change under the UI editor, do you mean that you open the file in the UI editor and the contents dont’ seem to have changed? If you run refresh in the console, does the file update?

3 Likes

yes that’s exaclty like you said !
if I “cat data/kif.json” : it display the latest changes
but not in the UI editor !

2 Likes

but if i type “refresh” in the console : all goes ok !
?!
I didn’t know this command !
BTW : it Could be ok for me

2 Likes

Amazing!! Yeah, it seems like sometimes the UI editor gets out of sync with the files (this is something I usually see if I’m creating/manipulating files through the command line, so I wondered if it could happen here also!) :slight_smile: Good luck with your project!

4 Likes

vim could also work
do vim data/kif.json
if you want to exit ctrl-c 2 times, and type :exit and press enter, closing the vim

i am pretty sure vim instantly pushes the changes to the ui editor in less than a minute too

The editor is showing the content of the file as it sees the git repository.

As its a data file, you probably don’t want to create new versions of the project as the app changes the file.

To prevent this, either have the file in a folder that git doesn’t version, like .data , or add the filepath to .gitignore

This will mean the file no longer shows in the editor though.

3 Likes

This (the Editor and backend not always being in sync) is expected behavior at the moment. The Editor only knows about Editor things, but picks up backend changes whenever refresh is executed.

2 Likes