Skip to content

Instantly share code, notes, and snippets.

@eastlondoner
Last active May 5, 2019 16:51
Show Gist options
  • Save eastlondoner/3c0c833d0290c3f196bf9020c3e8aecb to your computer and use it in GitHub Desktop.
Save eastlondoner/3c0c833d0290c3f196bf9020c3e8aecb to your computer and use it in GitHub Desktop.
LOAD CSV using local CSV file on any remote Neo4j Server with internet access
# PYTHON 3.6+ is required
from http.server import HTTPServer, SimpleHTTPRequestHandler
from functools import partial
import os
import time
import kthread
from pyngrok import ngrok
PORT = 8000
def host(directory_to_host: str):
web_dir = os.path.abspath(directory_to_host)
handler_class = partial(SimpleHTTPRequestHandler, directory=web_dir)
httpd = HTTPServer(("", PORT), handler_class)
print("serving at port", PORT)
httpd.serve_forever()
def host_in_thread(dir_to_host: str):
t = kthread.KThread(target=host, args=(dir_to_host,), name="KillableThread1")
t.start()
time.sleep(5)
print("Thread is Alive?", t.isAlive())
public_url = ngrok.connect(port=PORT)
print('Published to', public_url)
return t, public_url
def example():
# Example Usage:
dir_to_host = os.path.join(os.path.dirname(__file__), 'web')
http_server_thread, ngrok_url = host_in_thread(dir_to_host)
try:
with GraphDatabase.driver(bolt_url, auth=bolt_auth) as driver:
with driver.session() as session:
my_file_url = f'{ngrok_url}/my_file_name.csv'
session.run(f"""
USING PERIODIC COMMIT 10000
LOAD CSV WITH HEADERS FROM "{my_file_url}" AS row
...
""").consume()
finally:
http_server_thread.terminate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment