Skip to content

Instantly share code, notes, and snippets.

@mzechmeister
Last active March 11, 2023 07:22
Show Gist options
  • Save mzechmeister/8bfec277c385c6d72fbc9c8dcedbb2fd to your computer and use it in GitHub Desktop.
Save mzechmeister/8bfec277c385c6d72fbc9c8dcedbb2fd to your computer and use it in GitHub Desktop.
Sent files from command line to csv_plotter
#! /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
@mzechmeister
Copy link
Author

This is an addon for https://github.com/mzechmeister/csvplotter.

Installation

git clone https://gist.github.com/8bfec277c385c6d72fbc9c8dcedbb2fd.git csv_sent
cd csv_sent
chmod a+x csv_sent.py
ln -s $PWD/csv_sent.py ~/bin/csv_sent

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment