marcenacp commited on
Commit
7f781f8
1 Parent(s): c8c8483

Deploy (see actual commits on https://github.com/mlcommons/croissant).

Browse files
Files changed (3) hide show
  1. app.py +5 -4
  2. core/query_params.py +42 -6
  3. requirements.txt +2 -1
app.py CHANGED
@@ -6,7 +6,9 @@ from components.flex import st_flex
6
  from core.constants import OAUTH_CLIENT_ID
7
  from core.constants import OAUTH_STATE
8
  from core.constants import REDIRECT_URI
 
9
  from core.query_params import get_project_timestamp
 
10
  from core.state import CurrentProject
11
  from core.state import get_user
12
  from core.state import User
@@ -21,8 +23,7 @@ init_state()
21
  user = get_user()
22
 
23
  if OAUTH_CLIENT_ID and not user:
24
- query_params = st.query_params
25
- state = query_params.get_all("state")
26
  if state and state[0] == OAUTH_STATE:
27
  code = query_params["code"]
28
  if not code:
@@ -34,7 +35,7 @@ if OAUTH_CLIENT_ID and not user:
34
  except:
35
  raise
36
  finally:
37
- st.query_params.clear()
38
  else:
39
  redirect_uri = urllib.parse.quote(REDIRECT_URI, safe="")
40
  client_id = urllib.parse.quote(OAUTH_CLIENT_ID, safe="")
@@ -48,7 +49,7 @@ if OAUTH_CLIENT_ID and not user:
48
 
49
  def _back_to_menu():
50
  """Sends the user back to the menu."""
51
- st.query_params.clear()
52
  init_state(force=True)
53
 
54
 
 
6
  from core.constants import OAUTH_CLIENT_ID
7
  from core.constants import OAUTH_STATE
8
  from core.constants import REDIRECT_URI
9
+ from core.query_params import clear_query_params
10
  from core.query_params import get_project_timestamp
11
+ from core.query_params import get_state
12
  from core.state import CurrentProject
13
  from core.state import get_user
14
  from core.state import User
 
23
  user = get_user()
24
 
25
  if OAUTH_CLIENT_ID and not user:
26
+ state = get_state("state")
 
27
  if state and state[0] == OAUTH_STATE:
28
  code = query_params["code"]
29
  if not code:
 
35
  except:
36
  raise
37
  finally:
38
+ clear_query_params()
39
  else:
40
  redirect_uri = urllib.parse.quote(REDIRECT_URI, safe="")
41
  client_id = urllib.parse.quote(OAUTH_CLIENT_ID, safe="")
 
49
 
50
  def _back_to_menu():
51
  """Sends the user back to the menu."""
52
+ clear_query_params()
53
  init_state(force=True)
54
 
55
 
core/query_params.py CHANGED
@@ -15,20 +15,44 @@ class QueryParams:
15
  OPEN_RECORD_SET = "recordSet"
16
 
17
 
 
 
 
 
 
 
 
18
  def _get_query_param(name: str) -> str | None:
19
  """Gets query param with the name `name`."""
20
- param = st.query_params.get_all(name)
 
 
 
 
 
 
 
21
  if isinstance(param, list) and len(param) > 0:
22
  return param[0]
23
  return None
24
 
25
 
26
  def _set_query_param(param: str, new_value: str) -> str | None:
27
- params = st.query_params
28
- if params.get_all(param) == [new_value]:
29
- # The value already exists in the query params.
30
- return
31
- params[param] = new_value
 
 
 
 
 
 
 
 
 
 
32
 
33
 
34
  def is_record_set_expanded(record_set: RecordSet) -> bool:
@@ -46,5 +70,17 @@ def get_project_timestamp() -> str | None:
46
  return _get_query_param(QueryParams.OPEN_PROJECT)
47
 
48
 
 
 
 
 
49
  def set_project(project: CurrentProject):
50
  _set_query_param(QueryParams.OPEN_PROJECT, project.path.name)
 
 
 
 
 
 
 
 
 
15
  OPEN_RECORD_SET = "recordSet"
16
 
17
 
18
+ def _has_query_params() -> bool:
19
+ """Returns whether `query_params` is available, which depends on the streamlit
20
+ version. If not, st.experimental_get_query_params() and
21
+ st.experimental_set_query_params() should be used."""
22
+ return hasattr(st, "query_params")
23
+
24
+
25
  def _get_query_param(name: str) -> str | None:
26
  """Gets query param with the name `name`."""
27
+ if _has_query_params():
28
+ param = st.query_params.get_all(name)
29
+ else:
30
+ params = st.experimental_get_query_params()
31
+ if not name in params:
32
+ return None
33
+ param = params[name]
34
+
35
  if isinstance(param, list) and len(param) > 0:
36
  return param[0]
37
  return None
38
 
39
 
40
  def _set_query_param(param: str, new_value: str) -> str | None:
41
+ """Sets query param with the name `name` to `new_value`."""
42
+ if _has_query_params():
43
+ params = st.query_params
44
+ if params.get_all(param) == [new_value]:
45
+ # The value already exists in the query params.
46
+ return
47
+ params[param] = new_value
48
+ else:
49
+ params = st.experimental_get_query_params()
50
+ if params.get(param) == [new_value]:
51
+ # The value already exists in the query params.
52
+ return
53
+ new_params = {k: v for k, v in params.items() if k != param}
54
+ new_params[param] = new_value
55
+ st.experimental_set_query_params(**new_params)
56
 
57
 
58
  def is_record_set_expanded(record_set: RecordSet) -> bool:
 
70
  return _get_query_param(QueryParams.OPEN_PROJECT)
71
 
72
 
73
+ def get_state() -> str | None:
74
+ return _get_query_param("state")
75
+
76
+
77
  def set_project(project: CurrentProject):
78
  _set_query_param(QueryParams.OPEN_PROJECT, project.path.name)
79
+
80
+
81
+ def clear_query_params():
82
+ """Clears query params."""
83
+ if _has_query_params():
84
+ st.query_params.clear()
85
+ else:
86
+ st.experimental_set_query_params()
requirements.txt CHANGED
@@ -6,5 +6,6 @@ pytest
6
  python-magic
7
  rdflib
8
  requests
9
- streamlit>=1.31.0
10
  streamlit-nested-layout
 
 
6
  python-magic
7
  rdflib
8
  requests
9
+ streamlit
10
  streamlit-nested-layout
11
+ twisted