Last active
March 11, 2023 07:22
-
-
Save mzechmeister/8bfec277c385c6d72fbc9c8dcedbb2fd to your computer and use it in GitHub Desktop.
Sent files from command line to csv_plotter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python3 | |
# Javascript only allows to fetch files from the local system by drag&drop. | |
# Here a local server is setup to sent a file from command line to csv_plotter. | |
# Furthermore, we also need to allow CORS... | |
# The server closes finally; thus the browser cannot reload the url/file. | |
# Example | |
# ./csv_sent <filename> | |
from http.server import HTTPServer, SimpleHTTPRequestHandler, os, sys | |
from _thread import start_new_thread | |
from urllib.parse import quote_plus | |
class CORSRequestHandler(SimpleHTTPRequestHandler): | |
def end_headers(self): | |
self.send_header('Access-Control-Allow-Origin', '*') | |
SimpleHTTPRequestHandler.end_headers(self) | |
browser = 'xdg-open' # or e.g. firefox | |
filename = 'http://localhost:8000/' + quote_plus(sys.argv[1]) | |
url = 'https://raw.githack.com/mzechmeister/csvplotter/master/csv_plotter.htm' | |
args = sys.argv[2] if sys.argv[2:] else 'cx=$1&cy=$2' | |
if __name__ == '__main__': | |
start_new_thread(HTTPServer(('', 8000), CORSRequestHandler).serve_forever, (1,)) | |
os.system(f"{browser} '{url}?url={filename}&{args}' && sleep 6") # waits a bit to sent the file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is an addon for https://github.com/mzechmeister/csvplotter.
Installation