Create local files from a Django web - python

I have written a Django web run under Linux server, the web is used to read a
file path and generate some folders and files on the local machine which browse
the web, not sure how to do that, currently it can only generate the files on
the server runs the web locally.
Does it need to use the python package paramiko?
Thanks,
Le

Django runs on server and every web server has no access to local (user) computer - it can't create folder on local computer (and it can't steal your files). It can only generate file and user can download it.

Related

How can I work on a python project hosted on a remote server using Pycharm?

I have a server I connect to using SFTP, and I've followed these steps to edit individual files hosted on the server.
But I have an entire python project structure on the server, and I'd like PyCharm's suggestions/auto-complete to be aware of the other files/directories in said project.
Is there anyway to achieve this using PyCharm? I feel like file mappings are the answer, but I don't see any way to download a remote directory onto a local drive and then setup a synchronization.
Also there are some resources I don't want to download onto my local environment, so if there was a way to do this remote only, so much the better.

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

Run local python script from Google Apps Script

As the title suggests, I'm looking for a way to run local python scripts from the Google Apps Scripts environment. Right now my method is to write a json file from app scripts and save it to a Google Drive directory. The local machine polls the directory via Google Drive File Stream, and runs the necessary code when the file appears in the directory. Not pretty, but it gets the job done. The main concern with this is that File Stream can be fairly latent - could be up to 15 minutes after a file is put in the directory before File Stream catches it and syncs the local machine.
Is anybody aware of other options? I could try re-writing the python code in Apps Script, though the python code relies on a few third party libraries that aren't available in JS. Another idea is setting up a flask server, but that seems like a good bit of work for the return.

How To Keep My File Paths Congruent for My Django/Python Application

I am developing a Django app on my Personal Computer. I have my files uploaded to a private repository on GitHub and I am using that to pull them down to my production server.
My question is this
I have file paths set inside of my application on my PC to file locations that are on my PC. But I also have those same files being uploaded and pulled onto my Production Server, but then needing to change the file path on my Production Server side application files in order to match the actual location on my Production Server
I have searched far and wide and cannot seem to find a proper solution to this. Is there some type of way I can write out the paths so that when I use an sys.path.append I don't have to give out the complete path, but rather a local one, so that it matches on both servers?
Possible Findings
Is there an issue if I just use sys.path.append('./<directory>')? This is, of course, assuming that the Python file is running within that current directory.

Which folder on AWS EC2 for my private Python scripts?

I have an AWS EC2 (Amazon Linux) instance configured with a LAMP web server on it as well. On top of having a small website hosted there I'd like it to run in the background a few Python scripts with cron.
Those scripts are private and separate from the website, so I don't want them in the /var/www/html folder.
On my personal computer those would stay in my User's Documents folder, but being new at having a server (and linux) I don't know where to put them.
By common practice, is there a specific folder I should use ?

Categories