CURENT / agvis

Geo-visualization for energy system
https://ltb.curent.org
GNU General Public License v3.0
7 stars 5 forks source link

Interactive use through code #31

Closed jinningwang closed 1 year ago

jinningwang commented 1 year ago

Enable interactive starting AGVis using the code as shown below:

import agvis
agvis.config_logger(10)
web = agvis.AGVisWeb(host='localhost', port=8810):
web.run(open_browser=True)
web.stop()

@nparsly, I am wondering is it possible to do some configuration file uploading or even simulation file uploading through the class WebHTTPRequestHandler? I got this reply from ChatGPT:

To pass a file into the web application using the WebHTTPRequestHandler class, you can add custom request handling logic to the do_GET method of the handler.

Here's an example implementation of the do_GET method that accepts file uploads from clients:

class WebHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
    def __init__(self, *args, **kwargs):
        # Get the path of the requested file
        path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "static")
        super().__init__(*args, directory=path, **kwargs)

    def do_GET(self):
        # Add custom request handling logic here
        if self.path == '/upload':
            # If the client requests the '/upload' URL, display the file upload form
            self.send_response(200)
            self.send_header('Content-type', 'text/html')
            self.end_headers()
            # Display a form for the client to upload a file
            self.wfile.write(b'<html><head><title>File upload form</title></head><body><h1>Upload a file</h1><form action="/upload" method="post" enctype="multipart/form-data"><input type="file" name="file"><input type="submit" value="Upload"></form></body></html>')
        else:
            # For all other requests, use the default behavior
            super().do_GET()

    def do_POST(self):
        # Add custom POST request handling logic here
        if self.path == '/upload':
            # If the client uploads a file, save the file to disk
            content_length = int(self.headers['Content-Length'])
            post_data = self.rfile.read(content_length)
            # Parse the file upload data
            form = cgi.FieldStorage(
                fp=io.BytesIO(post_data),
                headers=self.headers,
                environ={'REQUEST_METHOD': 'POST'}
            )
            # Save the file to disk
            file_item = form['file']
            with open(os.path.join(os.path.dirname(os.path.realpath(__file__)), "static", file_item.filename), 'wb') as f:
                f.write(file_item.file.read())
            # Redirect the client to the file download URL
            self.send_response(302)
            self.send_header('Location', '/files/' + file_item.filename)
            self.end_headers()
        else:
            # For all other POST requests, use the default behavior
            super().do_POST()

In this example implementation, if the client requests the /upload URL, we display a file upload form to the client. When the client uploads a file, we save the file to the server's disk and redirect the client to the download URL for the uploaded file.

You can modify this example implementation to accept file uploads and implement your custom logic to handle the uploaded files.

Regards, Jinning