Skip to content

Commit

Permalink
updated jupyter-widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh H committed Sep 12, 2018
1 parent 13e450c commit 0aad280
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 29 deletions.
69 changes: 62 additions & 7 deletions client.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,76 @@
import requests
from ipywidgets import widgets
from IPython.display import display
from os.path import expanduser
import os
import json


def connect_to_queue(button_type='help', disp_input_fields=False, base_url="http://jhell3.pythonanywhere.com"):
home = expanduser("~")
config_file = os.path.join(home, ".pyqueue")
contents = None
if os.path.isfile(config_file):
with open(config_file, 'r') as file:
contents = file.read()

def connect_to_queue(first_name, last_name, kerberos, base_url="http://127.0.0.1:5000/"):
help_button = widgets.Button(description="Request Help")
checkoff_button = widgets.Button(description="Request Checkoff")
status_text = widgets.Label(value="")
if contents:
res = contents.split(',')
name, kerberos = res[0].strip(), res[1].strip()
name_field = widgets.Text(description='Name', placeholder='Ben Bitdiddle', value=name)
kerberos_field = widgets.Text(description='Kerberos', placeholder='benbit', value=kerberos)
else:
name_field = widgets.Text(description='Name', placeholder='Ben Bitdiddle')
kerberos_field = widgets.Text(description='Kerberos', placeholder='benbit')

queue_button = help_button if button_type == 'help' else checkoff_button

if disp_input_fields:
widget_list = [name_field, kerberos_field, queue_button, status_text]
else:
widget_list = [queue_button, status_text]

queue_box = widgets.HBox(widget_list)
display(queue_box)

def get_field_values():
try:
res = name_field.value.split(' ')
first_name, last_name = res[0].strip(), res[1].strip()
kerberos = kerberos_field.value.strip()

display(help_button)
display(checkoff_button)
if len(first_name) > 0 and len(last_name) > 0 and len(kerberos) > 0:
return first_name, last_name, kerberos
else:
raise ValueError
except:
print("Please enter a first and last name, and a kerberos")
return None

params = {"first_name": first_name, "last_name": last_name, "kerberos": kerberos}
def save_config(first_name, last_name, kerberos):
with open(config_file, 'w') as file:
file.write(first_name+' '+last_name+','+kerberos)

def make_help_request(a):
r = requests.post(base_url+'queue/help', data=params)
fields = get_field_values()
if fields:
params = {"first_name": fields[0], "last_name": fields[1], "kerberos": fields[2]}
r = requests.post(base_url+'/queue/help', data=params)
save_config(fields[0], fields[1], fields[2])
response = json.loads(r.text)
status_text.value = response['message']

def make_checkoff_request(a):
r = requests.post(base_url+'queue/checkoff', data=params)
fields = get_field_values()
if fields:
params = {"first_name": fields[0], "last_name": fields[1], "kerberos": fields[2]}
r = requests.post(base_url+'/queue/checkoff', data=params)
save_config(fields[0], fields[1], fields[2])
response = json.loads(r.text)
status_text.value = response['message']

help_button.on_click(make_help_request)
checkoff_button.on_click(make_checkoff_request)
checkoff_button.on_click(make_checkoff_request)
55 changes: 33 additions & 22 deletions demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,29 @@
"cells": [
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 304,
"metadata": {},
"outputs": [],
"source": [
"from client import connect_to_queue"
]
},
{
"cell_type": "code",
"execution_count": 305,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "25a6eb3ee00c4e8a8de70a7d09f59833",
"model_id": "5967c874eb8a43e5aab6ca8fde191047",
"version_major": 2,
"version_minor": 0
},
"text/html": [
"<p>Failed to display Jupyter Widget of type <code>Button</code>.</p>\n",
"<p>Failed to display Jupyter Widget of type <code>HBox</code>.</p>\n",
"<p>\n",
" If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean\n",
" that the widgets JavaScript is still loading. If this message persists, it\n",
Expand All @@ -28,21 +39,33 @@
"</p>\n"
],
"text/plain": [
"Button(description='Request Help', style=ButtonStyle())"
"HBox(children=(Text(value='Josh Hellerstein', description='Name', placeholder='Ben Bitdiddle'), Text(value='joshh', description='Kerberos', placeholder='benbit'), Button(description='Request Help', style=ButtonStyle()), Label(value='')))"
]
},
"metadata": {},
"output_type": "display_data"
},
}
],
"source": [
"# connect to the help queue by specifying the button type as 'help' or 'checkoff'\n",
"# the latest input field values are cached\n",
"connect_to_queue('help', disp_input_fields=True)"
]
},
{
"cell_type": "code",
"execution_count": 306,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "a3bb621a17414d6b901c38df40b4b83f",
"model_id": "61b1b613685d4dd4bbe2c3f0f62f8335",
"version_major": 2,
"version_minor": 0
},
"text/html": [
"<p>Failed to display Jupyter Widget of type <code>Button</code>.</p>\n",
"<p>Failed to display Jupyter Widget of type <code>HBox</code>.</p>\n",
"<p>\n",
" If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean\n",
" that the widgets JavaScript is still loading. If this message persists, it\n",
Expand All @@ -57,29 +80,17 @@
"</p>\n"
],
"text/plain": [
"Button(description='Request Checkoff', style=ButtonStyle())"
"HBox(children=(Button(description='Request Checkoff', style=ButtonStyle()), Label(value='')))"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from client import connect_to_queue\n",
"\n",
"first_name = 'HELO'\n",
"last_name = 'TEST'\n",
"kerberos = 'bitdid'\n",
"\n",
"connect_to_queue(first_name, last_name, kerberos)"
"# you can choose to not display the input fields as well\n",
"connect_to_queue('checkoff')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down
4 changes: 4 additions & 0 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ def help_queue_manager():
elif entry not in help_queue:
help_queue.append(entry)
msg = 'added to queue'
else:
msg = 'already added to queue'
return jsonify({'message': msg})
else:
msg = 'missing queue data'
Expand All @@ -61,6 +63,8 @@ def checkoff_queue_manager():
elif entry not in checkoff_queue:
checkoff_queue.append(entry)
msg = 'added to queue'
else:
msg = 'already added to queue'
return jsonify({'message': msg})
else:
msg = 'missing queue data'
Expand Down

0 comments on commit 0aad280

Please sign in to comment.