codys12 commited on
Commit
460480a
·
verified ·
1 Parent(s): fe90c9a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -18
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import gradio as gr
2
  import pandas as pd
 
3
  from io import BytesIO
4
 
5
  def process_woocommerce_data_in_memory(netcom_file):
@@ -14,7 +15,7 @@ def process_woocommerce_data_in_memory(netcom_file):
14
  "Microsoft": "https://devthe.tech/wp-content/uploads/2025/01/Microsoft-e1737494120985-1.png"
15
  }
16
 
17
- # 1. Read the uploaded CSV into a DataFrame (Gradio provides a tempfile-like object)
18
  netcom_df = pd.read_csv(netcom_file.name, encoding='latin1')
19
  netcom_df.columns = netcom_df.columns.str.strip() # standardize column names
20
 
@@ -38,14 +39,14 @@ def process_woocommerce_data_in_memory(netcom_file):
38
  .reset_index(name='Aggregated_Times')
39
  )
40
 
41
- # 3. Extract unique parent products from the NetCom data
42
  parent_products = netcom_df.drop_duplicates(subset=['Course ID'])
43
 
44
- # 4. Merge aggregated dates and times into the parent product DataFrame
45
  parent_products = parent_products.merge(date_agg, on='Course ID', how='left')
46
  parent_products = parent_products.merge(time_agg, on='Course ID', how='left')
47
 
48
- # 5. Create the parent (variable) product DataFrame
49
  woo_parent_df = pd.DataFrame({
50
  'Type': 'variable',
51
  'SKU': parent_products['Course ID'],
@@ -84,7 +85,7 @@ def process_woocommerce_data_in_memory(netcom_file):
84
  'Meta: agenda': parent_products['Outline'] # Agenda now copies the outline
85
  })
86
 
87
- # 6. Create the child (variation) product DataFrame
88
  woo_child_df = pd.DataFrame({
89
  'Type': 'variation, virtual',
90
  'SKU': netcom_df['Course SID'],
@@ -125,10 +126,10 @@ def process_woocommerce_data_in_memory(netcom_file):
125
  'Meta: agenda': netcom_df['Outline'] # Agenda now copies the outline
126
  })
127
 
128
- # 7. Combine parent and child data
129
  woo_final_df = pd.concat([woo_parent_df, woo_child_df], ignore_index=True)
130
 
131
- # 8. Define the desired column order (matching WooCommerce import format)
132
  column_order = [
133
  'Type', 'SKU', 'Name', 'Published', 'Visibility in catalog',
134
  'Short description', 'Description', 'Tax status', 'In stock?',
@@ -141,7 +142,7 @@ def process_woocommerce_data_in_memory(netcom_file):
141
  ]
142
  woo_final_df = woo_final_df[column_order]
143
 
144
- # 9. Convert the final DataFrame to CSV in memory
145
  output_buffer = BytesIO()
146
  woo_final_df.to_csv(output_buffer, index=False, encoding='utf-8-sig')
147
  output_buffer.seek(0)
@@ -150,22 +151,19 @@ def process_woocommerce_data_in_memory(netcom_file):
150
 
151
  def process_file_and_return_csv(uploaded_file):
152
  """
153
- Gradio wrapper function that:
154
  - Takes the uploaded file,
155
  - Processes it,
156
- - Returns a dictionary that Gradio recognizes as a downloadable file.
 
157
  """
158
  processed_csv_io = process_woocommerce_data_in_memory(uploaded_file)
159
 
160
- # Return a dict with the keys Gradio expects for a File output
161
- return {
162
- "name": "WooCommerce_Mapped_Data.csv",
163
- "data": processed_csv_io.getvalue()
164
- }
165
 
166
- #########################
167
- # Gradio App #
168
- #########################
169
 
170
  app = gr.Interface(
171
  fn=process_file_and_return_csv,
 
1
  import gradio as gr
2
  import pandas as pd
3
+ import tempfile
4
  from io import BytesIO
5
 
6
  def process_woocommerce_data_in_memory(netcom_file):
 
15
  "Microsoft": "https://devthe.tech/wp-content/uploads/2025/01/Microsoft-e1737494120985-1.png"
16
  }
17
 
18
+ # 1. Read the uploaded CSV into a DataFrame
19
  netcom_df = pd.read_csv(netcom_file.name, encoding='latin1')
20
  netcom_df.columns = netcom_df.columns.str.strip() # standardize column names
21
 
 
39
  .reset_index(name='Aggregated_Times')
40
  )
41
 
42
+ # 3. Extract unique parent products
43
  parent_products = netcom_df.drop_duplicates(subset=['Course ID'])
44
 
45
+ # 4. Merge aggregated dates and times
46
  parent_products = parent_products.merge(date_agg, on='Course ID', how='left')
47
  parent_products = parent_products.merge(time_agg, on='Course ID', how='left')
48
 
49
+ # 5. Create parent (variable) products
50
  woo_parent_df = pd.DataFrame({
51
  'Type': 'variable',
52
  'SKU': parent_products['Course ID'],
 
85
  'Meta: agenda': parent_products['Outline'] # Agenda now copies the outline
86
  })
87
 
88
+ # 6. Create child (variation) products
89
  woo_child_df = pd.DataFrame({
90
  'Type': 'variation, virtual',
91
  'SKU': netcom_df['Course SID'],
 
126
  'Meta: agenda': netcom_df['Outline'] # Agenda now copies the outline
127
  })
128
 
129
+ # 7. Combine parent + child
130
  woo_final_df = pd.concat([woo_parent_df, woo_child_df], ignore_index=True)
131
 
132
+ # 8. Desired column order
133
  column_order = [
134
  'Type', 'SKU', 'Name', 'Published', 'Visibility in catalog',
135
  'Short description', 'Description', 'Tax status', 'In stock?',
 
142
  ]
143
  woo_final_df = woo_final_df[column_order]
144
 
145
+ # 9. Convert to CSV (in memory)
146
  output_buffer = BytesIO()
147
  woo_final_df.to_csv(output_buffer, index=False, encoding='utf-8-sig')
148
  output_buffer.seek(0)
 
151
 
152
  def process_file_and_return_csv(uploaded_file):
153
  """
 
154
  - Takes the uploaded file,
155
  - Processes it,
156
+ - Writes the CSV to a temp file,
157
+ - Returns that path for Gradio to provide as a downloadable file.
158
  """
159
  processed_csv_io = process_woocommerce_data_in_memory(uploaded_file)
160
 
161
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".csv") as tmp:
162
+ tmp.write(processed_csv_io.getvalue())
163
+ tmp.flush() # ensure data is written to disk
164
+ temp_path = tmp.name
 
165
 
166
+ return temp_path
 
 
167
 
168
  app = gr.Interface(
169
  fn=process_file_and_return_csv,