problem when read files from local computer in pyscript - file

When I run the Python file, the file works correctly, but when I run the Python file with the script, it gives an error in read txt file(file not found).
how can i solve this problem؟

Software written in any language (Python, JavaScript, WASM) cannot access local files directly. This is a browser security restriction. The browser can access files on behalf of your application using the <input type="file"> DOM element link.
In Python and JavaScript, you must provide the user with a file input selector. The user selects the file and your application can the retrieve the file data from the browser.
In JavaScript this functionality is implemented with the FileReader class. This class can be used in Python via the create_proxy() function to proxy event callbacks.
If you are just getting started with Pyscript, I have written a number of articles link.

See this issue report.
Code running in a webpage is sandboxed and can't freely access files on the computer hosting the browser.
(It would be a terrible security problem if just visiting a webpage would give the author of the page access to your files).
If you want to access a file on the user's computer, use <input type="file"> and have the user select it. I don't know if you can access it directly or if you would need to use a JavaScript FileReader and then pass the results from JS to PyScript … but one of those two approaches should be possible.

Related

Electron portable build not saving content locally

Use case
I'm building an app that has support for translations. The way I imagined it is that the app will come with English by default and will be sent around to different places. When it arrives in Germany (for example) a user will add German as a translation and then send it to France. The French user will be able to see both the original language (engligh) and German.
Background
The app is a React web app wrapped in Electron.
For the use case I implemented the localStorage API to store content changes and switch between different translations.
Problems
After building the app as a portable exe and testing it I started noticing that the content doesn't carry over to a new PC.
It seems like the data is stored on the system or something else is happening (like localStorage gets wiped when moving the portable exe to another machine).
In my view, because I'm using localStorage and Electron bundles the Chromium browser with the web app I was expecting localStorage to be saved within the portable app.
Would love to hear your opinion on this
Thanks a million.
The localStorage is not saved to your app's binary files but rather to a file on your account's part of your PC's file system.
On Linux, for example, Electron creates a directory for your app in ~/.config and stores your localStorage data there. I imagine that you might find a directory for your application in %APPDATA% (C:\Users\YourUsername\AppData\Roaming\ or similar) on Windows.
What you could try is to write your data to a file in your app's resources bundle which would be preserved while re-distributing, but that's another question and answer.

Generate new file using angularjs

I have certain html forms. On-submit it has to generate new text/json file (name to be saved based upon one of the field in form) in the local drive with the contents filled in the form. Is this possible in angularjs? As well as to retrieve the data based upon the file name.
Spontanously, I wouldn't want a javascript based web page to access my hard disk drive. That seems like a security risk for me.
Could local storage be an alternative for your use case?
Read more about local storage and angular js: http://www.amitavroy.com/justread/content/articles/html5-local-storage-angular-js
You can use filesystem API but it is chrome specific.
Check this Exploring the FileSystem APIs

Don't know how to convert an Angular Web App made with nodejs to windows exe

I'm a complete noob at programming with nodejs.
I'm developing an webapp with angularjs with this structure:
So, when I run the command "nw ." with the prompt command my application loads correctly.
All I want now is transform all this structure on an single Windows executable for distribute it for my customers.
I'm looking for any solutions like Bat to Exe, APPJS, and a lot of others. But I do not have any success either.
How can I do this?
You are probably going to need to looking at use a tool like ng-template to build everything into a single package or distributing a basic web server along with your site in order to make things work.
This is mainly due to the issue with the browser not allowing XHR requests from disk as AngularJS runs and request the various html files as your views change.
The best thing is to look using a tool like ng-template to compile all of the html partials into a single .js file that can be loaded from disk along with your AngularJS app. This way AngularJS will load all your partials into the $template cache and won't have to make any XHR calls to load them.
The other possibility is to look at something simple like the Cassini Web server or something similar that you can add to your directory and launch with a simple batch file. I say this because you are talking about distributing something to people who may not have Node.js installed, meaning you probably can't rely on them being able perform a bunch of installs just to get to your app.
How to package and distribute your apps solved my problem!

Read property file on both java and angularjs

I'm using a properties file in project. I want to read the properties file both java and angularjs. suggest me a best location to place the properties file which can accessible by java and also by angularjs.
By design it should be src/resources folder.
Well by opportunity, it can be placed in webapp too.
From what I know angularjs is meant to execute on browser. I do not recommend downloading properties into browser as web assets similar to css/js. So I would recommend keeping properties in src/main/resources/. If you need, host a small rest end point giving these properties as a json map in response. This can be used by angularjs on client side(browser)
Ideally properties which have confidential information shoild never be sent to browser.
Any one can get access to those information by debugging in tools like chrome,etc.
Sending such information should not create problems for your system. In those cases you shld hv 2 properties in src/ resources folder. One contains db passwords etc. Other with open information.

Using AngularJS in local HTML

What are the best practices for using AngularJS in local HTML? By that, I mean HTML files opened directly from the local file system, not from a web server.
I have had some luck in replacing partials that are in separate files with partials within script tags. And replacing Ajax calls with including JS files that contain the JSON data. However, I wondered if there are any guidelines for doing this, or even helper libraries?
BTW, my use case is a desktop application that generates HTML reports that are server using a local web server. In some cases users want to create a ZIP file that contains an HTML report, which they can email to someone else. Given the sensitivity of these reports, people may have objections to using a cloud service. Having HTML in a zip file gives them control of their data.

Resources