whackthejacker/DataHubHub
1
1import streamlit as st2import pandas as pd3import json4 5def render_dataset_preview(dataset, dataset_type):6 """7 Renders a preview of the dataset with pagination options.8 9 Args:10 dataset: The dataset to preview (pandas DataFrame)11 dataset_type: The type of dataset (csv, json, etc.)12 """13 if dataset is None:14 st.warning("No dataset to preview.")15 return16 17 st.markdown(f"<h3>Dataset Preview: {st.session_state.dataset_name}</h3>", unsafe_allow_html=True)18 19 # Show basic info20 col1, col2, col3 = st.columns(3)21 with col1:22 st.metric("Rows", f"{dataset.shape[0]:,}")23 with col2:24 st.metric("Columns", f"{dataset.shape[1]:,}")25 with col3:26 st.metric("Type", dataset_type.upper())27 28 # Preview options29 col1, col2 = st.columns([1, 3])30 with col1:31 num_rows = st.number_input("Rows to display", min_value=5, max_value=100, value=10, step=5)32 with col2:33 preview_mode = st.radio("Preview mode", ["Head", "Tail", "Sample"], horizontal=True)34 35 # Display dataset preview36 st.markdown("<div class='dataset-preview'>", unsafe_allow_html=True)37 38 if preview_mode == "Head":39 st.dataframe(dataset.head(num_rows), use_container_width=True)40 elif preview_mode == "Tail":41 st.dataframe(dataset.tail(num_rows), use_container_width=True)42 else: # Sample43 st.dataframe(dataset.sample(min(num_rows, len(dataset))), use_container_width=True)44 45 st.markdown("</div>", unsafe_allow_html=True)46 47 # Show dataset schema48 with st.expander("Dataset Schema"):49 col1, col2 = st.columns(2)50 51 with col1:52 st.markdown("**Column Types**")53 type_df = pd.DataFrame({54 'Column': dataset.dtypes.index,55 'Type': dataset.dtypes.values.astype(str)56 })57 st.dataframe(type_df, use_container_width=True)58 59 with col2:60 st.markdown("**Missing Values**")61 missing_df = pd.DataFrame({62 'Column': dataset.columns,63 'Missing': dataset.isna().sum().values,64 'Percentage': dataset.isna().sum().values / len(dataset) * 10065 })66 st.dataframe(missing_df.style.format({67 'Percentage': '{:.2f}%'68 }), use_container_width=True)69 70 # Raw data71 with st.expander("Raw Data (First 5 records)"):72 if dataset_type == 'csv':73 st.code(dataset.head(5).to_csv(index=False), language="text")74 else: # json or jsonl75 st.code(dataset.head(5).to_json(orient='records', indent=2), language="json")76 