Skip to content
Snippets Groups Projects
Commit 564dace1 authored by Vanessa Stehr's avatar Vanessa Stehr
Browse files

Add connection to server

Includes error handling and user output

Co-authored-by: default avatarBeSeeTek <BeSeeTek@users.noreply.github.com>
parent a8a86263
No related branches found
No related tags found
No related merge requests found
import base64
import json
import warnings
from bokeh.plotting import curdoc, figure
from bokeh.layouts import column
from bokeh.models import FileInput, Div, CustomJS, Button, TabPanel, Tabs
from pyDsiVectorsAndTables.pyDsiVectorsAndTables import dsiVector, dsiMultiVector, getNDTablesFromDCC, getFromDict
from pyDsiVectorsAndTables.dsiMultiVectorPlot import dsiMultiVectorPlot
import numpy as np
import requests
from urllib3.exceptions import NameResolutionError, MaxRetryError
VERSION = "0.1.0"
......@@ -25,7 +28,7 @@ class page:
curdoc().add_root(self.dataView.layout)
def readFile(self, attr, old, new):
self.fileData = base64.b64decode(self.fileUploader.fileInput.value)
self.fileData = str(base64.b64decode(self.fileUploader.fileInput.value),encoding="utf-8")
# Start all the fun things here
# Make this the new download file
......@@ -38,7 +41,7 @@ class page:
self.tabs = self.dcc.dataToTabs()
self.fileUploader.setMessage(
"File processed successfully!", css_classes=["msg-positive"]
self.dcc.serverUserMsg[0], self.dcc.serverUserMsg[1]
)
self.dataView.update_tabs(self.tabs)
......@@ -60,9 +63,38 @@ class dcc:
Returns:
dict: representation of the file contents
"""
# TODO: Replace this placeholder with connection to the server
f = open("../sample-data/sample_dcc_server.json")
return json.load(f)
dccRestServer = "https://s18361.bs.ptb.de/dcc_rest_server/"
proxies = {"http": "", "https": "", "ftp": ""}
response = False
data = json.dumps(
{"xml": self.xmlFileData}
) # for FastAPI build a jsonized dict, payload is in the dict
try:
response = requests.post(dccRestServer + "dcc2json/", data=data, proxies=proxies)
except requests.exceptions.ConnectionError as e:
print(e)
if response and response.ok: # check if request succeeded
jso = response.text # extract the requested json from response object
dic = json.loads(jso) # convert json string to dict
print(json.dumps(dic, indent=4)) # pretty-print the dict
self.serverUserMsg = ("File processed successfully!", ["msg-positive"])
return dic
else:
if response:
# if it failed give some information, why
print("invalid response: %s" % response.reason)
print("invalid response: %s" % response.text)
userMsg = "Server reported the following problem:\n" + response.reason + "\n" + response.text
else:
print("Could not connect to DCC rest server!")
userMsg = "Could not connect to DCC rest server!"
warnings.warn(RuntimeWarning("USING TEST DATA !!!!!!"))
userMsg += "\n USING TEST SAMPLE DATA INSTEAD OF UPLOADED FILE!"
f = open("../sample-data/sample_dcc_server.json")
self.serverUserMsg = (userMsg, ["msg-negative"])
return json.load(f)
def dataToTabs(self):
# TODO, this is a placeholder
......@@ -94,8 +126,8 @@ class dcc:
# with open("../../py-dsi-vectors-and-tables/tests/test_multiVectorJSONDumpingAndLoadingFromFile.json",encoding='utf-8') as JSONFile:
# mv=dsiMultiVector.fromJson(JSONFile.read())
for plotName, plot in self.multiVectorPlots.items():
for figure in plot.figures:
tabs.append(TabPanel(child = figure, title = plotName))
tabs.append(TabPanel(child = plot.widget, title = plotName))
# print(self.createdMultiVectors)
# p1 = figure(width=300, height=300)
# p1.circle([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], size=20, color="navy", alpha=0.5)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment