Unable to config pywebview caching directory - python

I need to create a desktop app that access a webpage and also need to be able to control it's caching location (such as images of this webpage) for offline usage.
Just like any browser already does?
Yes, but I cannot use any browser like Firefox, Chrome, Chromium... Unless it's in the form of a DLL or something similar (the browser itself cannot be installed)
I already found how to delete cache and how to disable caching, just need to alter the caching directory.
take this code as example:
import webview
webview.create_window('Hello world', 'https://pywebview.flowrl.com/hello')
webview.start()
Let's suppose it downloads a image and stores it in cache.
I need to be able to open multiple instances of the application, each with it's separate caching directory.
Anyone knows how to change the caching directory? something like:
webview.cache.setDir("c://User/user-2")
I'm alright with the usage of other libs such ad flask or bottle (or any other really)
Also, the solution needs to work for both Widows and Ubuntu
Thanks in advance.

As you mentioned, There are third-party libraries you can use. Flask for instance works very well with pywebview. the webview.create_window method accepts an HTTP server(In this case flask). With flask you can then use the flask cache module to specify a directory to store the files
You can check the docs on flask caching to see other possibilities
https://flask-caching.readthedocs.io/en/latest/

Related

Python deployment on webserver with CGI

I have a python script that I want to make accessible through a website with an userinterface.
I was experimenting with Flask, but I'm not sure this is the right tool for what I want to do.
My script takes userdata (.doc/.txt files), does something with it and returns it to the user. I don't want to save anything and I don't think that I need a database for this (is this right?). The file will be temporarily saved on the server and everything will be deleted once the user downloaded the modified file.
My webhosting provider supports Python and only accepts CGI. I read that WSGI is the preferred method to use with Python and that CGI has scaling issues and can only process one request at a time. I'm not sure if I understand this correctly. If several users would upload files at the same time, the server would only accept one request or overwrite previous requests? Or it can do one request per unique IP address/user?
Would CGI be ok for the simple get/process/return task of my python script or should I look into a hosting service that uses WSGI?
I had a look at Heroku and Render to deploy a flask app, but I think I could do that through my webhosting provider I guess.
For anyone interested in this topic,
I decided to deploy my app on render.com, which supports gunicorn (WSGI).

How to start python Eel in any available browser of user system?

I am trying to create a windows based application using eel. I want it to start in any available browser in the system of the user. How can I do it ? ( consider that user have not installed chrome in his system )
You can pass in mode='default' to the eel.start function, and it will try to open the system's default browser. Something like this:
eel.start('index.html', mode='default')
In this case, behind-the-scenes Eel will be proxying the open request to Python's webbrowser.open function, so it's best to check there if you want to know the logic being used. Or you can look at the Python source file for webbrowser.py on your machine... on Windows it would be located somewhere like: C:\Python39\Lib\webbrowser.py.
Maybe it's too late for you, I had the same problem. So to resolve it you have open your folder with code source of eel and open init.py and change browser option.

Downloading a file in downloads directory - flask, python

I have a flask app in which it has a button to download an HTML report. The download button when hit creates an xlsx file. It worked fine until that app was running on my local server because the python code found my Downloads directory no matter what os it is using python's os module.
Now the app is deployed on a remote server, in this case, how do you let the system know the client's download directory path where this xlsx file can then be created? Or any other pointers for a better solution if I'm missing something?
To serve a user a file to download, the webserver needs to deliver it in a special way. You can't use the os module to write to the user's computer. It worked locally, because your server was the same computer as your user environment in the browser.
The correct way to serve a user a file for downloading is to use flask's send_file method.
To show you the process, I've created an example below. In this example, I'm assuming that you have a function called build_report(...) which accepts a report_id and returns an io.BytesIO object containing the content of the xlsx file.
from flask import send_file
#app.route("/download-report/<report_id>")
def download_report(report_id):
file_obj = build_report(report_id)
return send_file(
file_obj,
mimetype="application/vnd.ms-excel",
attachment_filename=f"report-{report_id}.xlsx",
)
If i understand correctly - you want to specify a directory to which the file should be downloaded on users computer when he/she hits download button.
This is not possible, and is handled fully by the browser.
Browser processes the request/stream of special type and then creates the output file in the location specified by the user in browser settings. The os library which you mentioned relates to your server machine not client, so any os opertaions that you provide in your code will be executed on your server (like creating a file). So that's why it worked on your local machine - which was server and client at once.
Why it's dissalowed?
Imagine a "virus file" being uploaded to your C:\Windows\System32. Web applications could be granted control over your machine with a simple button download. The huge security issue doesnt allow for client's machine access from web application

Overwriting variables in system libraries (smtplib.py)

So I'm using Flask for a website and I'm using the extension Flask Mail to send emails for me. Unfortunately, my email server doesn't support CRAM_MD5 (for a valid reason) so when smtplib.py (Python system library file) reaches the line that is:
preferred_auths = [AUTH_CRAM_MD5, AUTH_PLAIN, AUTH_LOGIN]
it uses CRAM, and fails because of an authentication error. On my development server, I just edited the file and switch the order of PLAIN and CRAM and faced no problems. My system admin doesn't want to make this change every time he does a server build if he can help it so I was wondering if there was a way I could overwrite that system library variable from my Flask application? My Flask application is a little large so if there is a way to do this, I can post a structure of my application. I mostly want to know if it is possible to overwrite variables in system libraries and if so, the standard procedure for doing so. Thanks!

What is the possibility of using a Python app, deployed online, that has access to a users local disk?

I currently have a local python application that scans a users drive, maps it into a tree and displays this information with javascript. I would really like to try to develop something with a Drop-Box like system to manage drive trees.
I have searched and read that App Engine specifically doesn't allow access to a user's local disk. Is there a way to use webpy or something else to access a user's local drive to create a tree directory out of it?
You'd have to create a "client" and "server" type of interface to do this. So it wouldn't be a solely JavaScript with Python on the server program. They'd have to have something running on their end as well, communicating in the background.
HTML5 allows some local storage, but not what you're looking for.
You could create a signed java applet that will run along side the javascript and allow access to local files. You may be able to find an applet already developed that you can call from javascript. You have to be careful with this though because once the user trusts the applet it's installed and any site can call the applet unless the applet code is restricted to a specific site.

Categories