-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathurl.py
More file actions
54 lines (42 loc) · 2.08 KB
/
url.py
File metadata and controls
54 lines (42 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import openpolicedata as opd
import utils
def get_opd_explorer_dataset_url(state=None, source=None, table_type=None, url_type=None):
'''Return URL that will go to default page and/or filters of OPD Explorer (https://openpolicedata.streamlit.app)
If set, inputs state, source, and/or table_type will be appended to the URL to go to a filtered view of the OPD Explorer dataset finder page.
If no parameters inputs, returned URL will be to the OPD Explorer dataset finder page
Parameters
----------
state : str
Name of state. Set to None for a URL for all states, by default None
source : str
Name of source. This is generally the name of the municipality (i.e. Chicago, not Chicago Police Department)
Set to None for a URL for all sources in the selected state(s) , by default None
table_type : str
Name of table type (such as USE OF FORCE or STOPS). Valid values can be found in the table type dropdown with no other filters set here:
https://openpolicedata.streamlit.app/?page=2_Find_Datasets.
Set to None for a URL for all table types for the selected state(s) and source(s), by default None
url_type : str
If set to 'local', the URL will be for running Streamlit locally: http://localhost:8501/, by default None
Returns
-------
str | None
URL if requested filter will find datasets and None if not
'''
url = 'https://openpolicedata.streamlit.app' if url_type!='local' else 'http://localhost:8501'
url += '/Find_Datasets/'
df = opd.datasets.query()
pre = '?'
if state:
df = df[df['State']==state]
url+=pre+'state'+'='+state
pre = '&'
if source:
df = df[df['SourceName']==source]
url+=pre+'source'+'='+source
pre = '&'
if table_type:
df['TableTypeGeneral'],_,_ = utils.split_tables(df['TableType'].tolist())
df = df[df['TableTypeGeneral']==table_type]
url+=pre+'table'+'='+table_type
pre = '&'
return url if len(df)>0 else None