To create a basic search engine using Streamlit for filtering records from a CSV file with 5 columns, you can follow these steps:
1. Install Streamlit:
If you haven't already, install Streamlit by running:
```bash
pip install streamlit
```
2. Create a Python Script:
Create a Python script (e.g., `search_engine.py`) and add the following code:
```python
import streamlit as st
import pandas as pd
# Load the CSV file
@st.cache # Cache the data to improve performance
def load_data():
df = pd.read_csv('your_data.csv') # Replace 'your_data.csv' with your CSV file
return df
data = load_data()
# Create a Streamlit web app
st.title('Search Engine')
# Sidebar for user input
search_term = st.sidebar.text_input('Enter search term:', '')
# Filter the data based on the search term
filtered_data = data[data.apply(lambda row: any(search_term in str(cell) for cell in row), axis=1)]
# Display the filtered data
st.write(f"Displaying {len(filtered_data)} matching records:")
st.write(filtered_data)
```
In this code:
* We start by importing the necessary libraries: `streamlit` and `pandas`.
* The `load_data` function is defined to read the CSV file using `pd.read_csv()`. The `@st.cache` decorator is used to cache the data, which improves the app's performance by loading the data only once.
* The `data` variable stores the loaded CSV data.
* We create a Streamlit web app using `st.title('Search Engine')`, which sets the app's title.
* Next, we create a sidebar that allows the user to input a search term using `st.sidebar.text_input('Enter search term:', '')`.
* We filter the data based on the search term using a lambda function and the `apply` method. This line of code checks if the search term appears in any cell of each row.
* The filtered data is stored in the `filtered_data` variable.
* Finally, we display the filtered data using `st.write()`. We also show the count of matching records using `len(filtered_data)`.
3. Replace `'your_data.csv'`:
Replace `'your_data.csv'` with the actual path to your CSV file. Make sure the CSV file is in the same directory as your script, or provide the full path.
4. Run the Streamlit App:
Open your terminal and run the Streamlit app:
```aurdino
streamlit run search_engine.py
```
5. Use the Search Engine:
* Enter a search term in the sidebar's text input field.
* The app will filter and display records where the search term appears in any of the columns.
This basic Streamlit script creates a simple search engine for your CSV data. Users can input a search term, and the script will filter and display matching records.