How to save data to a file

384 views
Skip to first unread message

Vitali Coltuclu

unread,
Mar 30, 2015, 10:40:46 PM3/30/15
to bry...@googlegroups.com
Hello,

I would like to save data from the text area to a file. My attempt doesn't work:

data = doc["txt_area"].html
with open("test.txt", 'w') as f:
    f
.write(data)


I would appreciate any comments/suggestions.

Thank you,
Vitali

Kiko

unread,
Mar 31, 2015, 7:38:43 AM3/31/15
to bry...@googlegroups.com
Have a look here to see how it could be made with js: http://runnable.com/U5HC9xtufQpsu5aj/use-javascript-to-save-textarea-as-a-txt-file

 
Thank you,
Vitali

--
You received this message because you are subscribed to the Google Groups "brython" group.
To unsubscribe from this group and stop receiving emails from it, send an email to brython+u...@googlegroups.com.
To post to this group, send email to bry...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/brython/e8d7baa9-01d8-4582-b5e0-e5fd115f03ed%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Vitali Coltuclu

unread,
Mar 31, 2015, 7:43:24 PM3/31/15
to bry...@googlegroups.com
Thank you, kiko, for your reply. So my understanding is that brython itself does not support saving data to a file.

Best,
Vitali

Billy Earney

unread,
Mar 31, 2015, 10:39:02 PM3/31/15
to bry...@googlegroups.com
you could try:

from browser import local_storage
local_storage.storage["test.txt"]=data

Since this is a browser environment, you cannot write to the OS.  This is probably the easiest way to emulate a file system in a browser environment.


Kiko

unread,
Apr 1, 2015, 2:19:50 AM4/1/15
to bry...@googlegroups.com
2015-04-01 4:39 GMT+02:00 Billy Earney <billy....@gmail.com>:
you could try:

from browser import local_storage
local_storage.storage["test.txt"]=data

Since this is a browser environment, you cannot write to the OS.  This is probably the easiest way to emulate a file system in a browser environment.


As Billy stated, it is not safe to let the browser access to the file system. With a Blob object you can create a ''virtual file'' and then create a link to it to save it.

It is very easy to create your own python write function using the example linked in my previous email ;-)
If you have any trouble with it just post it here.
 

Billy Earney

unread,
Apr 1, 2015, 1:38:23 PM4/1/15
to bry...@googlegroups.com
There's an idea..

what about expanding the open function so that if someone appends a file/path with 'localstorage:', we know to read/write to local storage, and otherwise it is a URL call?

This is kind of like sqlite3 with its open function with an argument of ":memory:", which means to write the database only to memory and not disk.

What do you guys think?  Is this a bad idea?

Juan Carlos

unread,
Apr 1, 2015, 2:52:24 PM4/1/15
to bry...@googlegroups.com
On Wed, Apr 1, 2015 at 2:38 PM, Billy Earney <billy....@gmail.com> wrote:
There's an idea..

what about expanding the open function so that if someone appends a file/path with 'localstorage:', we know to read/write to local storage, and otherwise it is a URL call?

This is kind of like sqlite3 with its open function with an argument of ":memory:", which means to write the database only to memory and not disk.

Nope. I think Brython open() should mimic Python3 open(), not mocking a Database Query Language,

A helper tiny module to download a file from a localstorage file should be better I think, as seamless as posible, that should work even when offline.

Reply all
Reply to author
Forward
0 new messages