-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreamlit_app.py
More file actions
418 lines (331 loc) ยท 14.1 KB
/
streamlit_app.py
File metadata and controls
418 lines (331 loc) ยท 14.1 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
import streamlit as st
import pandas as pd
from openflis_client import (
get_comprehensive_data,
build_item_overview,
parse_nsn_input,
lookup_by_part_number,
format_management_record,
format_moe_record,
format_standardization_record,
format_army_management_record,
get_api_key,
set_api_key
)
@st.cache_data(ttl=300, show_spinner=False)
def cached_get_comprehensive_data(niin):
return get_comprehensive_data(niin)
@st.cache_data(ttl=300, show_spinner=False)
def cached_lookup_by_part_number(part_number):
return lookup_by_part_number(part_number)
st.set_page_config(
page_title="OpenFLIS Data Lookup",
page_icon="๐",
layout="wide",
initial_sidebar_state="expanded"
)
st.markdown("""
<style>
.main-header {
font-size: 2.5rem;
font-weight: bold;
color: #1E3A5F;
margin-bottom: 0.5rem;
}
.sub-header {
font-size: 1rem;
color: #666;
margin-bottom: 2rem;
}
.metric-card {
background-color: #f8f9fa;
padding: 1rem;
border-radius: 0.5rem;
border-left: 4px solid #1E3A5F;
}
.section-header {
font-size: 1.3rem;
font-weight: bold;
color: #1E3A5F;
margin-top: 1rem;
margin-bottom: 0.5rem;
padding-bottom: 0.5rem;
border-bottom: 2px solid #e0e0e0;
}
.info-label {
font-weight: bold;
color: #444;
}
.info-value {
color: #1E3A5F;
}
.stDataFrame {
font-size: 0.9rem;
}
</style>
""", unsafe_allow_html=True)
def init_session_state():
if 'search_results' not in st.session_state:
st.session_state.search_results = None
if 'last_search' not in st.session_state:
st.session_state.last_search = ""
if 'search_type' not in st.session_state:
st.session_state.search_type = "NSN/NIIN"
if 'user_api_key' not in st.session_state:
st.session_state.user_api_key = ""
def display_item_overview(overview):
st.markdown('<p class="section-header">๐ Item Overview</p>', unsafe_allow_html=True)
col1, col2, col3 = st.columns(3)
with col1:
st.metric("NSN", overview.get('nsn', 'N/A') or 'N/A')
st.metric("FSC", overview.get('fsc', 'N/A') or 'N/A')
with col2:
st.metric("NIIN", overview.get('niin', 'N/A') or 'N/A')
st.metric("SOS", overview.get('sos', 'N/A') or 'N/A')
with col3:
st.metric("Managing Service", overview.get('managing_service', 'N/A') or 'N/A')
if overview.get('cancelled_niin'):
st.warning(f"โ ๏ธ Cancelled NIIN: {overview['cancelled_niin']}")
st.markdown("---")
col1, col2 = st.columns([2, 1])
with col1:
st.markdown(f"**Item Name:** {overview.get('item_name', 'N/A') or 'N/A'}")
if overview.get('end_item'):
st.markdown(f"**End Item Application:** {overview.get('end_item')}")
with col2:
if overview.get('user_services'):
st.markdown("**User Services:**")
for service in overview['user_services']:
st.markdown(f"โข {service}")
def display_management_data(management_data):
st.markdown('<p class="section-header">๐ฐ Management Data</p>', unsafe_allow_html=True)
records = management_data.get('records', [])
if not records:
st.info("No management data available for this item.")
return
formatted_records = [format_management_record(r) for r in records]
df = pd.DataFrame(formatted_records)
st.dataframe(df, use_container_width=True, hide_index=True)
if records:
first_record = records[0]
col1, col2, col3, col4 = st.columns(4)
with col1:
st.metric("Unit of Issue", first_record.get("UI", "N/A"))
with col2:
price = first_record.get("UNIT_PRICE", "")
try:
price_val = float(price) if price else 0
st.metric("Unit Price", f"${price_val:,.2f}")
except:
st.metric("Unit Price", "N/A")
with col3:
st.metric("AAC", first_record.get("AAC", "N/A"))
with col4:
st.metric("CIIC", first_record.get("CIIC", "N/A"))
def display_moe_rules(moe_data):
st.markdown('<p class="section-header">๐ MOE Rules (Service Ownership)</p>', unsafe_allow_html=True)
records = moe_data.get('records', [])
if not records:
st.info("No MOE rule data available for this item.")
return
formatted_records = [format_moe_record(r) for r in records]
df = pd.DataFrame(formatted_records)
st.dataframe(df, use_container_width=True, hide_index=True)
def display_standardization(std_data):
st.markdown('<p class="section-header">๐ Standardization Status</p>', unsafe_allow_html=True)
records = std_data.get('records', [])
if not records:
st.info("No standardization data available for this item.")
return
formatted_records = [format_standardization_record(r) for r in records]
df = pd.DataFrame(formatted_records)
st.dataframe(df, use_container_width=True, hide_index=True)
def display_army_management(army_data):
st.markdown('<p class="section-header">๐๏ธ Army Management Data</p>', unsafe_allow_html=True)
records = army_data.get('records', [])
if not records:
st.info("No Army-specific management data available for this item.")
return
formatted_records = [format_army_management_record(r) for r in records]
df = pd.DataFrame(formatted_records)
st.dataframe(df, use_container_width=True, hide_index=True)
def display_history(history_data):
st.markdown('<p class="section-header">๐ History</p>', unsafe_allow_html=True)
records = history_data.get('records', [])
if not records:
st.info("No history data available for this item.")
return
df = pd.DataFrame(records)
st.dataframe(df, use_container_width=True, hide_index=True)
def display_management_future(future_data):
st.markdown('<p class="section-header">๐ฎ Management Future</p>', unsafe_allow_html=True)
records = future_data.get('records', [])
if not records:
st.info("No future management data available for this item.")
return
df = pd.DataFrame(records)
st.dataframe(df, use_container_width=True, hide_index=True)
def display_management_history(mgmt_history_data):
st.markdown('<p class="section-header">๐ Management History</p>', unsafe_allow_html=True)
records = mgmt_history_data.get('records', [])
if not records:
st.info("No management history data available for this item.")
return
df = pd.DataFrame(records)
st.dataframe(df, use_container_width=True, hide_index=True)
def display_raw_data(comprehensive_data):
st.markdown('<p class="section-header">๐ง Raw API Data</p>', unsafe_allow_html=True)
for table_name, data in comprehensive_data.items():
with st.expander(f"{table_name.upper().replace('_', ' ')} ({len(data.get('records', []))} records)"):
if data.get('error'):
st.error(f"Error: {data['error']}")
elif data.get('records'):
df = pd.DataFrame(data['records'])
st.dataframe(df, use_container_width=True, hide_index=True)
else:
st.info("No records found.")
def main():
init_session_state()
set_api_key(st.session_state.user_api_key if st.session_state.user_api_key else None)
st.markdown('<p class="main-header">๐ OpenFLIS Data Lookup</p>', unsafe_allow_html=True)
st.markdown('<p class="sub-header">Federal Logistics Information System - NSN & NIIN Data Retrieval</p>', unsafe_allow_html=True)
with st.sidebar:
st.markdown("### Settings")
with st.expander("API Key Configuration", expanded=not st.session_state.user_api_key):
st.markdown("Enter your OpenFLIS API key below. You can get one from [openflis.com](https://app.openflis.com).")
api_key_input = st.text_input(
"API Key:",
value=st.session_state.user_api_key,
type="password",
placeholder="Enter your API key",
key="api_key_field"
)
if api_key_input != st.session_state.user_api_key:
st.session_state.user_api_key = api_key_input
set_api_key(api_key_input if api_key_input else None)
st.cache_data.clear()
st.rerun()
if st.session_state.user_api_key:
st.success("API key configured")
st.markdown("---")
st.markdown("### Search Options")
search_type = st.radio(
"Search by:",
["NSN/NIIN", "Part Number"],
key="search_type_radio"
)
st.markdown("---")
st.markdown("### About")
st.markdown("""
This application queries the OpenFLIS API to retrieve
comprehensive logistics data for military supply items.
**Data Available:**
- Item identification
- Management data & pricing
- Service ownership (MOE Rules)
- Standardization status
- Army-specific data
- Historical records
""")
st.markdown("---")
st.markdown("### Quick Tips")
st.markdown("""
- Enter full NSN (e.g., 1560-01-162-5517)
- Or just the NIIN (e.g., 011625517)
- Part numbers can include dashes
""")
col1, col2 = st.columns([3, 1])
with col1:
if search_type == "NSN/NIIN":
search_input = st.text_input(
"Enter NSN or NIIN:",
placeholder="e.g., 1560-01-162-5517 or 011625517",
key="nsn_input"
)
else:
search_input = st.text_input(
"Enter Part Number:",
placeholder="e.g., 39-9918-22",
key="part_input"
)
with col2:
st.markdown("<br>", unsafe_allow_html=True)
search_clicked = st.button("๐ Search", type="primary", use_container_width=True)
if not get_api_key():
st.warning("Please configure your API key in the sidebar settings to search.")
if search_clicked and search_input:
if not get_api_key():
st.error("API Key required. Please enter your API key in the sidebar settings.")
st.stop()
with st.spinner("Fetching data from OpenFLIS API..."):
if search_type == "NSN/NIIN":
niin, fsc = parse_nsn_input(search_input)
comprehensive_data = cached_get_comprehensive_data(niin)
overview = build_item_overview(niin, comprehensive_data)
if fsc and not overview.get('fsc'):
overview['fsc'] = fsc
overview['nsn'] = f"{fsc}-{niin}"
st.session_state.search_results = {
'type': 'nsn',
'niin': niin,
'overview': overview,
'comprehensive_data': comprehensive_data
}
else:
part_result = cached_lookup_by_part_number(search_input)
if part_result.get('Matched') and part_result.get('NIIN'):
niin = part_result['NIIN']
comprehensive_data = cached_get_comprehensive_data(niin)
overview = build_item_overview(niin, comprehensive_data)
st.session_state.search_results = {
'type': 'part',
'part_number': search_input,
'niin': niin,
'overview': overview,
'comprehensive_data': comprehensive_data
}
else:
st.session_state.search_results = {
'type': 'part',
'part_number': search_input,
'error': 'No matching item found for this part number.'
}
st.session_state.last_search = search_input
if st.session_state.search_results:
results = st.session_state.search_results
if results.get('error'):
st.error(results['error'])
else:
st.success(f"โ
Data retrieved for NIIN: {results['niin']}")
if results['type'] == 'part':
st.info(f"Part Number '{results['part_number']}' maps to NIIN: {results['niin']}")
overview = results['overview']
comprehensive_data = results['comprehensive_data']
display_item_overview(overview)
tab1, tab2, tab3, tab4, tab5, tab6, tab7 = st.tabs([
"๐ฐ Management",
"๐ MOE Rules",
"๐ Standardization",
"๐๏ธ Army Mgmt",
"๐ History",
"๐ฎ Future",
"๐ง Raw Data"
])
with tab1:
display_management_data(comprehensive_data.get('management', {}))
with tab2:
display_moe_rules(comprehensive_data.get('moe_rule', {}))
with tab3:
display_standardization(comprehensive_data.get('standardization', {}))
with tab4:
display_army_management(comprehensive_data.get('army_management', {}))
with tab5:
display_history(comprehensive_data.get('history', {}))
st.markdown("---")
display_management_history(comprehensive_data.get('management_history', {}))
with tab6:
display_management_future(comprehensive_data.get('management_future', {}))
with tab7:
display_raw_data(comprehensive_data)
if __name__ == "__main__":
main()