paulbricman commited on
Commit
7d19e9a
β€’
1 Parent(s): 64f0260

fix: issues with two cookies: one user cookie now

Browse files

frontend/components/__pycache__/microverses.cpython-38.pyc CHANGED
Binary files a/frontend/components/__pycache__/microverses.cpython-38.pyc and b/frontend/components/__pycache__/microverses.cpython-38.pyc differ
frontend/components/microverses.py CHANGED
@@ -10,28 +10,24 @@ import random
10
 
11
 
12
  def paint():
13
- cookie_manager = get_manager()
 
14
 
15
- microverses = get_microverses()
16
- st.session_state['microverses'] = microverses
 
 
 
17
 
18
- layout = get_layout(True)
19
- if not layout:
20
- sleep(0.5)
21
- layout = get_layout(False)
22
- if not layout:
23
- layout = {
24
- 'viewportCols': 3,
25
- 'leftColumn': ['navigator', 'ranker'],
26
- 'rightColumn': ['inspector']
27
- }
28
-
29
- st.session_state['layout'] = layout
30
 
31
  with st.sidebar:
32
  with st.expander('πŸ—” layout', expanded=True):
33
- layout['viewportCols'] = int(st.number_input(
34
- 'viewport cols', 1, 5, layout.get('viewportCols', 3), 1))
35
 
36
  faux_components = ['header', 'knowledge',
37
  'microverses', 'viewport']
@@ -42,45 +38,42 @@ def paint():
42
 
43
  components = [e.split('.')[0] for e in os.listdir(components_path) if e.endswith(
44
  '.py') and e.split('.')[0] not in faux_components]
45
- layout['leftColumn'] = st.multiselect(
46
- 'left column', components, layout.get('leftColumn', ['navigator', 'ranker']))
47
- layout['rightColumn'] = st.multiselect(
48
- 'right column', components, layout.get('rightColumn', ['inspector']))
49
- st.session_state['layout'] = layout
50
- cookie_manager.set('layout', layout, expires_at=datetime.datetime.now(
51
  ) + datetime.timedelta(days=30))
52
 
53
- if len(microverses) > 0:
54
  with st.expander('πŸ”Œ connected microverses', expanded=True):
55
- if len(microverses) > 0:
56
- for e_idx, e in enumerate(microverses):
57
- if e['auth']['custodian']:
58
- display_text = 'πŸ—οΈ ' + e['url']
59
- else:
60
- display_text = e['url']
61
- st.code(display_text)
62
-
63
- if e['auth']['custodian']:
64
- if st.button('create archive'):
65
- archive = requests.get(e['url'] + '/dump',
66
- headers={'Authorization': f"Bearer {e['token']}"}).content
67
- st.download_button(
68
- 'download archive', data=archive, file_name='knowledge.zip')
69
-
70
- if st.button('remove', key=(e, e_idx), help='Remove this source of thoughts.'):
71
- microverses.remove(e)
72
- cookie_manager.delete('microverses')
73
- cookie_manager.set(
74
- 'microverses', microverses, expires_at=datetime.datetime.now() + datetime.timedelta(days=30), key='remove')
75
- st.session_state['microverses'] = microverses
76
- sleep(0.5)
77
- st.experimental_rerun()
78
 
79
  with st.expander('πŸ†• connect to new microverse', expanded=True):
80
  url = st.text_input('conceptarium url',
81
- key=microverses, help='Specify the base URL of the conceptarium you wish to access thoughts from. If you\'re trying to connect to your local instance, enter `localhost`.')
82
  token = st.text_input(
83
- 'access token', key=microverses, help='Specify the token to be used in authorizing access to this conceptarium. If you\'re the custodian of this conceptarium, enter your custodian token. If this is someone else\'s instance, please use the microverse token they provided you with.', type='password')
84
 
85
  if st.button('add', help='Add this conceptarium as a source of thoughts to be explored.'):
86
  if '://' not in url:
@@ -91,21 +84,19 @@ def paint():
91
  custodian_check = json.loads(
92
  requests.get(url + '/custodian/check',
93
  headers={'Authorization': f"Bearer {token}"}).content)
94
- if len([e for e in microverses if e['url'] == url]) == 0:
95
- microverses += [{
96
  'url': url,
97
  'token': token,
98
  'auth': custodian_check
99
  }]
100
  cookie_manager.set(
101
- 'microverses', microverses, expires_at=datetime.datetime.now() + datetime.timedelta(days=30), key='add')
102
- st.session_state['microverses'] = microverses
103
  sleep(0.5)
104
-
105
- st.experimental_rerun()
106
 
107
  custodian_microverse = [
108
- e for e in microverses if e['auth']['custodian'] == True]
109
  if len(custodian_microverse) > 0:
110
  shared_microverses = json.loads(requests.get(custodian_microverse[0]['url'] + '/microverse/list',
111
  headers={'Authorization': f"Bearer {custodian_microverse[0]['token']}"}).content)
@@ -126,24 +117,14 @@ def paint():
126
  st.experimental_rerun()
127
 
128
 
129
- @st.cache(allow_output_mutation=True)
130
- def get_manager():
131
- return stx.CookieManager()
132
-
133
-
134
- def get_microverses():
135
- cookie_manager = get_manager()
136
- microverses = cookie_manager.get_all(
137
- 'microverses_cookie').get('microverses')
138
- if not microverses:
139
- microverses = []
140
-
141
- return microverses
142
-
143
 
144
- def get_layout(initial):
145
- cookie_manager = get_manager()
146
- layout = cookie_manager.get_all(
147
- 'layout_cookie' + str(initial)).get('layout')
148
 
149
- return layout
 
 
10
 
11
 
12
  def paint():
13
+ cookie_manager = get_cookie_manager()
14
+ user_state = cookie_manager.get('user_state')
15
 
16
+ if not user_state:
17
+ sleep(1.)
18
+ user_state = cookie_manager.get('user_state')
19
+ if not user_state:
20
+ user_state = {}
21
 
22
+ user_state['layout'] = user_state.get('layout', default_layout())
23
+ user_state['microverses'] = user_state.get('microverses', [])
24
+ st.session_state['microverses'] = user_state['microverses']
25
+ st.session_state['layout'] = user_state['layout']
 
 
 
 
 
 
 
 
26
 
27
  with st.sidebar:
28
  with st.expander('πŸ—” layout', expanded=True):
29
+ user_state['layout']['viewportCols'] = int(st.number_input(
30
+ 'viewport cols', 1, 5, user_state['layout'].get('viewportCols', 3), 1))
31
 
32
  faux_components = ['header', 'knowledge',
33
  'microverses', 'viewport']
38
 
39
  components = [e.split('.')[0] for e in os.listdir(components_path) if e.endswith(
40
  '.py') and e.split('.')[0] not in faux_components]
41
+ user_state['layout']['leftColumn'] = st.multiselect(
42
+ 'left column', components, user_state['layout'].get('leftColumn', ['navigator', 'ranker']))
43
+ user_state['layout']['rightColumn'] = st.multiselect(
44
+ 'right column', components, user_state['layout'].get('rightColumn', ['inspector']))
45
+ st.session_state['layout'] = user_state['layout']
46
+ cookie_manager.set('user_state', user_state, expires_at=datetime.datetime.now(
47
  ) + datetime.timedelta(days=30))
48
 
49
+ if len(user_state['microverses']) > 0:
50
  with st.expander('πŸ”Œ connected microverses', expanded=True):
51
+ for e_idx, e in enumerate(user_state['microverses']):
52
+ if e['auth']['custodian']:
53
+ display_text = 'πŸ—οΈ ' + e['url']
54
+ else:
55
+ display_text = e['url']
56
+ st.code(display_text)
57
+
58
+ if e['auth']['custodian']:
59
+ if st.button('create archive'):
60
+ archive = requests.get(e['url'] + '/dump',
61
+ headers={'Authorization': f"Bearer {e['token']}"}).content
62
+ st.download_button(
63
+ 'download archive', data=archive, file_name='knowledge.zip')
64
+
65
+ if st.button('remove', key=(e, e_idx), help='Remove this source of thoughts.'):
66
+ user_state['microverses'].remove(e)
67
+ cookie_manager.delete('user_state')
68
+ cookie_manager.set(
69
+ 'user_state', user_state, expires_at=datetime.datetime.now() + datetime.timedelta(days=30), key='remove')
70
+ sleep(0.5)
 
 
 
71
 
72
  with st.expander('πŸ†• connect to new microverse', expanded=True):
73
  url = st.text_input('conceptarium url',
74
+ key=user_state['microverses'], help='Specify the base URL of the conceptarium you wish to access thoughts from. If you\'re trying to connect to your local instance, enter `localhost`.')
75
  token = st.text_input(
76
+ 'access token', key=user_state['microverses'], help='Specify the token to be used in authorizing access to this conceptarium. If you\'re the custodian of this conceptarium, enter your custodian token. If this is someone else\'s instance, please use the microverse token they provided you with.', type='password')
77
 
78
  if st.button('add', help='Add this conceptarium as a source of thoughts to be explored.'):
79
  if '://' not in url:
84
  custodian_check = json.loads(
85
  requests.get(url + '/custodian/check',
86
  headers={'Authorization': f"Bearer {token}"}).content)
87
+ if len([e for e in user_state['microverses'] if e['url'] == url]) == 0:
88
+ user_state['microverses'] += [{
89
  'url': url,
90
  'token': token,
91
  'auth': custodian_check
92
  }]
93
  cookie_manager.set(
94
+ 'user_state', user_state, expires_at=datetime.datetime.now() + datetime.timedelta(days=30), key='add')
 
95
  sleep(0.5)
96
+ st.session_state['microverses'] = user_state['microverses']
 
97
 
98
  custodian_microverse = [
99
+ e for e in user_state['microverses'] if e['auth']['custodian'] == True]
100
  if len(custodian_microverse) > 0:
101
  shared_microverses = json.loads(requests.get(custodian_microverse[0]['url'] + '/microverse/list',
102
  headers={'Authorization': f"Bearer {custodian_microverse[0]['token']}"}).content)
117
  st.experimental_rerun()
118
 
119
 
120
+ def default_layout():
121
+ return {
122
+ 'viewportCols': 3,
123
+ 'leftColumn': ['navigator', 'ranker'],
124
+ 'rightColumn': ['inspector']
125
+ }
 
 
 
 
 
 
 
 
126
 
 
 
 
 
127
 
128
+ @st.cache(allow_output_mutation=True)
129
+ def get_cookie_manager():
130
+ return stx.CookieManager()
frontend/requirements.txt CHANGED
@@ -6,4 +6,5 @@ requests==2.22.0
6
  numpy==1.17.4
7
  streamlit==1.4.0
8
  Pillow==9.0.0
9
- extra-streamlit-components==0.1.53
 
6
  numpy==1.17.4
7
  streamlit==1.4.0
8
  Pillow==9.0.0
9
+ extra-streamlit-components==0.1.53
10
+ arxiv2bib==1.0.8
requirements.txt CHANGED
@@ -4,4 +4,5 @@ requests==2.22.0
4
  numpy==1.17.4
5
  streamlit==1.4.0
6
  Pillow==9.0.0
7
- extra-streamlit-components==0.1.53
 
4
  numpy==1.17.4
5
  streamlit==1.4.0
6
  Pillow==9.0.0
7
+ extra-streamlit-components==0.1.53
8
+ arxiv2bib==1.0.8