Skip to content
Snippets Groups Projects
Commit ecd4fb74 authored by Gagaro's avatar Gagaro
Browse files

#59: get token from query params

parent e5caff1c
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id: tags:
# Connectivities analysis
This notebook is prepared to help you in the analysis of your connectivities.
%% Cell type:code id: tags:
``` python
from nbconnectivity import utils
```
%% Cell type:markdown id: tags:
## Visualize graph
%% Cell type:code id: tags:
``` python
importer = utils.ImporterUI()
importer.UI
%%javascript
var params = new URLSearchParams(window.location.search)
IPython.notebook.kernel.execute("ACCESS_TOKEN = '" + params.get("access_token") + "'");
IPython.notebook.kernel.execute("REFRESH_TOKEN = '" + params.get("refresh_token") + "'");
```
%% Output
%% Cell type:code id: tags:
``` python
importer = utils.ImporterUI(access_token=ACCESS_TOKEN, refresh_token=REFRESH_TOKEN)
importer.UI
```
%% Cell type:markdown id: tags:
## Use raw data
You can directly use the connectivity matrix and the dataframe containing your locations. They are accessible in the attributes `connectivty_matrix` and `locations` on the `importerUI` object.
%% Cell type:code id: tags:
``` python
importer.connectivity_matrix
```
%% Cell type:code id: tags:
``` python
importer.locations
```
......
......@@ -10,39 +10,28 @@ from .graph import *
class ImporterUI(widgets.Box):
def __init__(self):
def __init__(self, access_token, refresh_token):
"""
Constructor
"""
super(ImporterUI, self).__init__()
self.access_token = None
self.refresh_token = None
self.access_token = access_token
self.refresh_token = refresh_token
# Initialize UI
self.init_ui()
def _login(self):
try:
response = requests.post(TROPOLINK_API_TOKEN, data={
'username': self.userarea.value,
'password': self.userpassword.value,
})
response.raise_for_status()
data = response.json()
self.access_token = data['access_token']
self.refresh_token = data['refresh_token']
except requests.exceptions.RequestException as e:
self.studies_errormsg.value = str(e)
self.load_studies()
def _refresh(self):
try:
response = requests.post(TROPOLINK_API_REFRESH_TOKEN, data={
'refresh_token': self.access_token,
'refresh': self.access_token,
})
response.raise_for_status()
data = response.json()
self.access_token = data['access_token']
self.refresh_token = data['refresh_token']
except requests.exceptions.RequestException as e:
self.studies_errormsg.value = str(e)
......@@ -55,14 +44,9 @@ class ImporterUI(widgets.Box):
def get(self, url):
# Log in if necessary
if self.access_token is None:
self._login()
elif self._is_access_token_expired(self.access_token):
if self._is_access_token_expired(self.access_token):
self.access_token = None
self._refresh()
# Could not log in
if self.access_token is None:
return
try:
r = requests.get(url, headers={'Authorization': f'Bearer {self.access_token}'})
r.raise_for_status()
......@@ -86,16 +70,12 @@ class ImporterUI(widgets.Box):
self.widgetsDict = json.load(json_data)
# Initialize widgets
self.init_username(self.widgetsDict["username"])
self.init_studies(self.widgetsDict["study_name"])
self.init_graph(self.widgetsDict["graph_plot"])
center = widgets.GridspecLayout(2, 3)
center[0, 0] = self.userarea
center[0, 1] = self.userpassword
center[0, 2] = self.userbutton
center[1, 0] = self.studies_dropdown
center[1, 1:] = self.studies_errormsg
center = widgets.GridspecLayout(1, 2)
center[0, 0] = self.studies_dropdown
center[0, 1] = self.studies_errormsg
self.UI = widgets.AppLayout(
header=None,
......@@ -106,31 +86,6 @@ class ImporterUI(widgets.Box):
pane_heights=[1, '100px', '170px'],
)
def init_username(self, widgets_dict):
"""
Initializes a text area to enter the username
:param widgetsDict: a dictionary with widgets strings
"""
self.userbutton = widgets.Button(
description=widgets_dict["button"]["label"],
tooltip=widgets_dict["button"]["description"]
)
self.userarea = widgets.Text(
placeholder=widgets_dict["userarea"]["placeholder"],
description=widgets_dict["userarea"]["label"],
description_tooltip=widgets_dict["userarea"]["description"]
)
self.userpassword = widgets.Password(
placeholder=widgets_dict["password"]["placeholder"],
description=widgets_dict["password"]["label"],
description_tooltip=widgets_dict["password"]["description"]
)
self.userbutton.on_click(self.load_studies)
self.userarea.observe(self.reinit_username, "value")
def init_studies(self, widgets_dict):
self.studies_dropdown = widgets.Dropdown(
......@@ -202,17 +157,7 @@ class ImporterUI(widgets.Box):
children=[self.plotbutton, plotoptions],
)
def reinit_username(self, change):
"""
Reinit UI when the username is changed
:param change: a username is provided
"""
self.userbutton.button_style = ''
self.userbutton.icon = ''
self.studies_errormsg.value = ''
self.studies_dropdown.options = []
def load_studies(self, change):
def load_studies(self):
"""
Load the list of previous studies names
"""
......@@ -231,13 +176,9 @@ class ImporterUI(widgets.Box):
if len(studies_list) == 0:
self.studies_errormsg.value = "No connectivities calculation was found."
self.userbutton.icon = ''
self.userbutton.button_style = 'warning'
else:
studies_list.sort()
self.studies_dropdown.options = studies_list
self.userbutton.icon = 'check'
self.userbutton.button_style = 'success'
def get_connectivity_matrix(self, connectivity_name):
"""
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment