Reading contents of a local file?

94 views
Skip to first unread message

Steph Reed

unread,
Sep 7, 2017, 10:37:06 PM9/7/17
to brython
Hi all,

I'm looking to use Brython to automate some AutoCAD related tasks I do a lot at work by processing local DXF (text based) files.

I have most of the code I need already in Python which I've translated over from an existing app I wrote in Java, but am stuck reading local files with Brython.

I started with the Drag and Drop example and based on an earlier discussion had a look at this sample

Unfortunately I still can't make any progress, what am I missing?

From the Drag & Drop example, it seems we receive a list of File objects so I tried opening the first one with open( f, 'rt' ) but get this error:

ReferenceError: $OpenFile is not defined
    at Object.$url_open [as open] (brython.js:5927)
    at handleFileSelect0 (eval at run_script (brython.js:4131), <anonymous>:50:41)

It does seem I have a real handle to the file, I can obtain info about it, but how can I read it into a string?

Thanks 




Francisco Javier Vazquez Umbria

unread,
Sep 8, 2017, 4:46:04 AM9/8/17
to bry...@googlegroups.com
you can't read local files with brython. brython uses the XML_HTTP_REQUEST given by the browser to load aditional files. it restricts to 'same origin' or you need a webserver to perform a cross-site request.

--
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+unsubscribe@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/7bf3540d-cdde-4ba7-a73b-40df26242670%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Steph Reed

unread,
Sep 8, 2017, 5:05:24 AM9/8/17
to brython
Thanks for your reply.

In the link I posted a user claims they did it, and if it's not possible, what is the point of the File Drag & Drop demo on the Brython website?

It seems quite odd that we can obtain a file handle for local files, check their size, check last modified etc. but can't read them.

Maybe using that file handle I can read it with Javascript then somehow get it back to Brython?


On Friday, September 8, 2017 at 4:46:04 PM UTC+8, Francisco Javier Vazquez Umbria wrote:
you can't read local files with brython. brython uses the XML_HTTP_REQUEST given by the browser to load aditional files. it restricts to 'same origin' or you need a webserver to perform a cross-site request.
2017-09-08 4:37 GMT+02:00 Steph Reed <gothica...@gmail.com>:
Hi all,

I'm looking to use Brython to automate some AutoCAD related tasks I do a lot at work by processing local DXF (text based) files.

I have most of the code I need already in Python which I've translated over from an existing app I wrote in Java, but am stuck reading local files with Brython.

I started with the Drag and Drop example and based on an earlier discussion had a look at this sample

Unfortunately I still can't make any progress, what am I missing?

From the Drag & Drop example, it seems we receive a list of File objects so I tried opening the first one with open( f, 'rt' ) but get this error:

ReferenceError: $OpenFile is not defined
    at Object.$url_open [as open] (brython.js:5927)
    at handleFileSelect0 (eval at run_script (brython.js:4131), <anonymous>:50:41)

It does seem I have a real handle to the file, I can obtain info about it, but how can I read it into a string?

Thanks 




--
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.

Francisco Javier Vazquez Umbria

unread,
Sep 8, 2017, 8:11:43 AM9/8/17
to bry...@googlegroups.com
you never can go out of the browser 'sandbox'. a file picker javascript widget only allows to access the picked file, not navigate across the filesystem.

also, since brython is a python->javascript JIT translator, has the same limitations as native javascript:
https://stackoverflow.com/questions/371875/local-file-access-with-javascript
https://caniuse.com/#search=file
https://www.html5rocks.com/en/tutorials/file/dndfiles/

To unsubscribe from this group and stop receiving emails from it, send an email to brython+unsubscribe@googlegroups.com.

To post to this group, send email to bry...@googlegroups.com.

Kiko

unread,
Sep 8, 2017, 9:31:29 AM9/8/17
to bry...@googlegroups.com

2017-09-08 4:37 GMT+02:00 Steph Reed <gothica...@gmail.com>:
Hi all,

I'm looking to use Brython to automate some AutoCAD related tasks I do a lot at work by processing local DXF (text based) files.

I have most of the code I need already in Python which I've translated over from an existing app I wrote in Java, but am stuck reading local files with Brython.

I started with the Drag and Drop example and based on an earlier discussion had a look at this sample

Unfortunately I still can't make any progress, what am I missing?

From the Drag & Drop example, it seems we receive a list of File objects so I tried opening the first one with open( f, 'rt' ) but get this error:

ReferenceError: $OpenFile is not defined
    at Object.$url_open [as open] (brython.js:5927)
    at handleFileSelect0 (eval at run_script (brython.js:4131), <anonymous>:50:41)

It does seem I have a real handle to the file, I can obtain info about it, but how can I read it into a string?

The file should be on the server or uploaded to a server using a post request, for instance.

If you are working in you own computer wouldn't it be easier to use a desktop app and classic CPython to solve your problem? Something like PyQt also has drag&drop capabilities and you can use the whole CPython ecosystem of libraries to solve the problem.

If you are not working in your own computer maybe you can expand on your problem to see if it is possible to help you.
 

Thanks 




--
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/7bf3540d-cdde-4ba7-a73b-40df26242670%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
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+unsubscribe@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/af9b4e0e-6a07-4ff1-ba7a-fd31b673f231%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to bry...@googlegroups.com.

Steph Reed

unread,
Sep 8, 2017, 10:18:54 PM9/8/17
to brython
My situation is a little odd.

* Very heavily restricted corporate network.
* Installation of Python environment not allowed
* Existing Java desktop app requires upto 4 weeks of 'security' testing before IT will install to users - not ideal for small / frequent changes
* Uploading files to any server outside company not allowed (and theoretically blocked by firewall)
* Using in-browser processing by Javascript is allowed as long as data is not sent back to server.

Files are basically large (30 - 45Mb) text based files exported from AutoCAD.

The workflow I would like to achieve is as follows:

1) Users opens web page
2) User drags & drops single file (or uses Filepicker)
3a) Page shows some info / calculations /  deviation from required standards etc.
3B) Page may make an updated DXF file (text) available for user to save to disk

I've found it's fairly trivial to read a local text file into a Javascript variable using the tutorial here: https://www.html5rocks.com/en/tutorials/file/dndfiles/
This works great, even allows me to slice the file if too big for processing in one go.

That leaves only a remaining issues I can see, can I get that Javascript variable (which now contains the content of the local file) into Brython? 
Even better if the JS part of it could be done with Brython directly, but that's doing it with JS is not a big issue.

Thanks loads for your help.



André

unread,
Sep 9, 2017, 7:00:26 AM9/9/17
to brython


On Friday, 8 September 2017 23:18:54 UTC-3, Steph Reed wrote:
My situation is a little odd.

* Very heavily restricted corporate network.
* Installation of Python environment not allowed
* Existing Java desktop app requires upto 4 weeks of 'security' testing before IT will install to users - not ideal for small / frequent changes
* Uploading files to any server outside company not allowed (and theoretically blocked by firewall)
* Using in-browser processing by Javascript is allowed as long as data is not sent back to server.

Files are basically large (30 - 45Mb) text based files exported from AutoCAD.

The workflow I would like to achieve is as follows:

1) Users opens web page
2) User drags & drops single file (or uses Filepicker)
3a) Page shows some info / calculations /  deviation from required standards etc.
3B) Page may make an updated DXF file (text) available for user to save to disk

I've found it's fairly trivial to read a local text file into a Javascript variable using the tutorial here: https://www.html5rocks.com/en/tutorials/file/dndfiles/
This works great, even allows me to slice the file if too big for processing in one go.

That leaves only a remaining issues I can see, can I get that Javascript variable (which now contains the content of the local file) into Brython? 

// javascript code
window.javascript_var = file_content
 

# Brython
from browser import window

# you can access window.javascript_var

André

kikocorreoso

unread,
Sep 9, 2017, 7:14:17 AM9/9/17
to bry...@googlegroups.com
If files are 30-40 Mb and you need a lot of process and a lot of files brython wouldn't be fast.

Right now I can't but later, if possible, I will try to provide a working example.
--
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.

Jonathan Verner

unread,
Sep 11, 2017, 9:52:36 AM9/11/17
to brython
See https://github.com/brython-dev/brython/pull/677 for a Pythonic (in my opinion at least ;-)) solution to your problem.

Steph Reed

unread,
Sep 12, 2017, 1:12:46 AM9/12/17
to brython

@ Andre

Thanks. that's what I was looking for, works great. I think getting info between JS and Brython will be useful in future for me too.

@ Kiko

Yes, 40Mb file would take a while to process if it was all needed, luckily I'm actually only concerned with a very small part of the data. Basically I read the file line-by-line but only store the bits I'm interested in, a couple of hundred K at most, and then process that. If the file size becomes and issue I will ask users to do a cleanup before processing (ie. Save as new name with all non relevant stuff deleted), as they already have to do this when importing DXF files into other in-house software.

@ Jonathan

Wow, that's very cool! I've not got back to the project for a few days but hope to have some time this week so I'm going to try this method out, thanks loads for that!

Thanks to everyone who's provided input and suggestions, very helpful.
Reply all
Reply to author
Forward
0 new messages