This page demonstrates how to build a report generation app. The app allows users to upload their data and to use Action Table to compile essential information to generate the report.
Before we start building, we are going to set up our Action Table. This is simple as clicking “New Action Table” in JamAIBase and then creating a “schema” inside the database.
Create a project
Click “New Action Table”.
Enter your project details.
Click “Create” to complete the Action Table setup.
Update the settings of the output column. Define the prompt message.
Add additional columns if necessary.
Building the app
Step 1: Set Up Your Environment
First, ensure you have Python installed on your system. Then, install Streamlit and other necessary libraries using pip:
pipinstallstreamlitpandasrequests
Step 2: Initialize Your Streamlit App
Create a new Python file for your app, for example, borehole_analysis.py.
Step 3: Import Required Libraries
Start by importing the necessary libraries in your Python script:
import pandas as pdimport streamlit as stimport requestsfrom requests.auth import HTTPBasicAuthimport json
Step 4: Design the Sidebar for File Upload
Create a sidebar for user inputs. Add a file uploader in the sidebar to allow users to upload their Excel data.
st.sidebar.title("Upload Borehole Data")uploaded_file = st.sidebar.file_uploader("Choose a file", type=['xlsx', 'xls'])
Step 5: Load and Display the Data
Once the file is uploaded, read and display the data using pandas.
if uploaded_file isnotNone: data = pd.read_excel(uploaded_file) st.write(data)
Step 6: Define Helper Functions for API Calls
Implement functions to interact with the database through API calls:
Add a button in Streamlit to trigger the API calls:
button_clicked = st.button("Run analysis")if button_clicked:add_row('YourTableID', st.session_state["input_text"]) st.session_state["lastest_row"]=get_latest_row('YourTableID')# Remaining code to process data goes here
Step 8: Prepare the Report Template
Display the static content of the report at appropriate sections:
st.markdown("### Drilling Sampling and On-site Testing")st.markdown(""" According to the on-site drilling work at this base, the approach is based on the site area, planning data, and analysis design requirements. """)...st.markdown(""" During the drilling process, a standard penetration test is conducted every 2.0 meters in the soil layer, which not only provides the N-value but also allows for the collection of split-spoon soil samples to supply the laboratory soil tests needed. """)
Display Action Table outputs dynamically to allow users to update the outputs:
defstream_data(input_text):for word in input_text.split(" "):yield"**"+ word +"** " time.sleep(0.1)defstream_text_sections(button_clicked,stream_text,placeholder_key): text_area_placeholder = st.empty()ifnot button_clicked: st.session_state[placeholder_key]= text_area_placeholder.text_area( placeholder_key, value="", height=100, )else:if st.session_state[placeholder_key]!="": st.session_state[placeholder_key]= text_area_placeholder.text_area( placeholder_key, value=st.session_state[ placeholder_key ], height=100, )return response =stream_data(stream_text) full_response =""for res in response: full_response += res text_area_placeholder.markdown(full_response, unsafe_allow_html=True) st.session_state[placeholder_key]= text_area_placeholder.text_area( placeholder_key, value=full_response.replace("**", ""), height=100, )# Add the functions at appropriate sections to display the outputs:stream_text_sections(st.session_state["button_clicked"], result, key)
Step 9: Add Interactivity
Powered by JamAI, users are capable to easily generate as many outputs as possible. Add interactive elements, such as buttons or sliders, to allow users to customize the analysis. For example, add a radio to select the most appropriate descriptiopn for the report:
keys = ["Result_A1","Result_A2","Result_A3"]results = [rA1, rA2, rA3]for key, result inzip(keys, results):stream_text_sections(st.session_state["button_clicked"], result, key)......options = keys + ["None"]selected_option = st.radio("Choose the appropriate description:", options, index=0)if selected_option =="None": user_text = st.text_area("User input", placeholder="Please provide your input." ) st.session_state["result_A"]= user_textelif selected_option in ["Result_A1","Result_A2","Result_A3"]: st.session_state["result_A"]= selected_option st.write(st.session_state.result_A)
Step 9: Add Button to download report
Add a button in Streamlit to download the report generated.