S-Dreamer commited on
Commit
7c38ea9
·
verified ·
1 Parent(s): 4b7dcef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -51
app.py CHANGED
@@ -1,74 +1,61 @@
1
-
2
- # Import the necessary libraries:
3
- # - `gradio` is a library for creating interactive web interfaces
4
- # - `typing` provides type annotations for Python
5
- # - `data_loader` is a custom module that contains functions to read different types of data
6
-
7
  import gradio as gr
8
  from typing import Dict, Any
9
  from data_loader import read_dask_data, read_polars_data, read_another_dask_data
10
 
11
- # Define a function called `load_and_process_data` that takes a dataset choice and the number of rows to display
12
- # This function is responsible for loading and processing the data based on the user's input
13
  def load_and_process_data(dataset_choice: str, num_rows: int) -> Dict[str, Any]:
14
- try:
15
- # Create a mapping of dataset choices to their corresponding data loading functions
16
- dataset_mapping = {
17
- "Dask Data": read_dask_data,
18
- "Polars Data": read_polars_data,
19
- "Another Dask Data": read_another_dask_data
20
- }
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
- # Fetch the appropriate data loading function based on the user's dataset choice
23
- data_loader = dataset_mapping.get(dataset_choice)
24
- if not data_loader:
25
- # If the dataset choice is invalid, return an error message
26
- return {"error": "Invalid dataset choice."}
27
-
28
- # Load the data using the selected data loading function
29
  data = data_loader()
30
-
31
- # Process the data to show the specified number of rows
32
  processed_data = data.head(num_rows)
33
-
34
- # Convert the processed data to a dictionary for JSON serialization
35
- return {
36
- "processed_data": processed_data.to_dict()
37
- }
38
  except Exception as e:
39
- # If an exception occurs during data processing, log the error
40
- # and return an error message
41
- print(f"Error processing data: {str(e)}")
42
- return {"error": "Unable to process data. Please check the logs for details."}
43
 
44
- # Define a function called `create_interface` that creates a Gradio interface
45
- # The interface allows the user to select a dataset and the number of rows to display
46
  def create_interface():
47
- # Define the input components for the interface
48
- dataset_choice = gr.components.Dropdown(
49
- choices=["Dask Data", "Polars Data", "Another Dask Data"],
50
- label="Select Dataset"
51
- )
52
- num_rows = gr.components.Slider(
53
- minimum=1, maximum=100, value=5, label="Number of Rows to Display"
54
- )
55
-
56
- # Define the layout of the Gradio interface
57
  with gr.Blocks() as demo:
58
  gr.Markdown("# Enhanced Dataset Loader Demo")
59
  gr.Markdown("Interact with various datasets and select the amount of data to display.")
 
60
  with gr.Row():
61
- dataset_choice.render()
62
- num_rows.render()
 
 
 
 
 
 
63
  processed_data_output = gr.JSON(label="Processed Data")
 
 
 
64
  processed_data_output.render()
65
 
66
- # Add the input components and the data processing function to the interface
67
- demo.add(dataset_choice, num_rows, processed_data_output, load_and_process_data)
68
 
69
- # Launch the Gradio interface
70
  demo.launch()
71
 
72
- # Execute the `create_interface` function when the script is run
73
  if __name__ == "__main__":
74
  create_interface()
 
 
 
 
 
 
 
1
  import gradio as gr
2
  from typing import Dict, Any
3
  from data_loader import read_dask_data, read_polars_data, read_another_dask_data
4
 
 
 
5
  def load_and_process_data(dataset_choice: str, num_rows: int) -> Dict[str, Any]:
6
+ """
7
+ Load and process data based on the user's choice and specified number of rows.
8
+
9
+ Args:
10
+ dataset_choice (str): The dataset to load.
11
+ num_rows (int): The number of rows to display.
12
+
13
+ Returns:
14
+ Dict[str, Any]: A dictionary containing the processed data or error message.
15
+ """
16
+ dataset_mapping = {
17
+ "Dask Data": read_dask_data,
18
+ "Polars Data": read_polars_data,
19
+ "Another Dask Data": read_another_dask_data
20
+ }
21
+
22
+ data_loader = dataset_mapping.get(dataset_choice)
23
+ if not data_loader:
24
+ return {"error": "Invalid dataset choice."}
25
 
26
+ try:
 
 
 
 
 
 
27
  data = data_loader()
 
 
28
  processed_data = data.head(num_rows)
29
+ return {"processed_data": processed_data.to_dict()}
 
 
 
 
30
  except Exception as e:
31
+ return {"error": f"Data processing failed: {str(e)}"}
 
 
 
32
 
 
 
33
  def create_interface():
34
+ """
35
+ Create and launch the Gradio interface for data selection and display.
36
+ """
 
 
 
 
 
 
 
37
  with gr.Blocks() as demo:
38
  gr.Markdown("# Enhanced Dataset Loader Demo")
39
  gr.Markdown("Interact with various datasets and select the amount of data to display.")
40
+
41
  with gr.Row():
42
+ dataset_choice = gr.Dropdown(
43
+ choices=["Dask Data", "Polars Data", "Another Dask Data"],
44
+ label="Select Dataset"
45
+ )
46
+ num_rows = gr.Slider(
47
+ minimum=1, maximum=100, value=5, label="Number of Rows to Display"
48
+ )
49
+
50
  processed_data_output = gr.JSON(label="Processed Data")
51
+
52
+ dataset_choice.render()
53
+ num_rows.render()
54
  processed_data_output.render()
55
 
56
+ demo.add(dataset_choice, num_rows, processed_data_output, load_and_process_data)
 
57
 
 
58
  demo.launch()
59
 
 
60
  if __name__ == "__main__":
61
  create_interface()