Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I'm trying to run a python log file summary and classification tool using python as a base with windows Forms C# GUI. MY code is :
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Collections;


namespace Sepa

{
    public partial class Form1 : Form
    {
        string LogFileLocation;
        string DataPreprocessScript;
        readonly string SummaryScript;
        readonly string ClassificationScript;
        readonly string PythonFilePath;

        public string DataPreprocessScript1 { get => DataPreprocessScript; set => DataPreprocessScript = value; }

        public Form1()
        {
            PythonFilePath = LocatePython();
            DataPreprocessScript1 = Directory.GetCurrentDirectory() + "\\Scripts\\data_process.py";
            SummaryScript = Directory.GetCurrentDirectory() + "\\Scripts\\Summary_code.py";
            ClassificationScript = Directory.GetCurrentDirectory() + "\\Scripts\\Classify.py";
            InitializeComponent();
        }


        private void BrowseButton_Click(object sender, EventArgs e)

        {
            OpenFileDialog ofd = new OpenFileDialog
            {
                //Only allows user to acess CSV files
                Filter = "CSV Files |* .csv",
                Title = "Open CSV"
            };

            // Check if they opened the file instead of closing the window
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                //temporary outputs filename in a message box
                FilePath.AppendText(ofd.FileName);
                //save csv file location
                LogFileLocation = ofd.FileName;
            }
        }


        private void SubmitButton_Click(object sender, EventArgs e)
        {
            var psi = new ProcessStartInfo
            {
                FileName = PythonFilePath
            };


            //Python Errors and outputs
            var errors = "";


            if (string.IsNullOrEmpty(psi.FileName))
            {
                errors += "Python Not detected on PC";
            }
            //Switch gate for specific python files
            else if(Summary.Checked == false && Classification.Checked == false)
            {
                errors += "Please Pick an Output";
            }
            else if (LogFileLocation == null)
            {
                errors += "No CSV File Submitted";
            }
            else 
            {
                //Python File path
                string script;
                if (Summary.Checked)
                {
                     script = SummaryScript;
                }
                else
                {
                    script = ClassificationScript;
                }
                //Processor Configuration
                psi.Arguments = $"{script} {LogFileLocation} "; 
                psi.UseShellExecute = false;
                psi.CreateNoWindow = true;
                psi.RedirectStandardOutput = true;
                psi.RedirectStandardError = true;

                    //Running Python Script
                    RunPython(psi, errors);

                
            }


            

        }



        private void Form1_Load(object sender, EventArgs e)
        {

        }


        private void Clear_Click(object sender, EventArgs e)

        {
            Output.Text = "";
        }

        private void RunPython(ProcessStartInfo psi, string errors)
        {
            var results = "";
            using (var process = Process.Start(psi))
            {

                errors += process.StandardError.ReadToEnd();
                results += process.StandardOutput.ReadToEnd();

            }
            //Output To Terminal
            if (!string.IsNullOrEmpty(results))
            {
                Output.Text += "Results: " + results + System.Environment.NewLine;
            }

            if (!string.IsNullOrEmpty(errors))
            {
                Output.Text += "Errors: " + errors + System.Environment.NewLine;
            }
        }

        private string LocatePython()
        {
            //Outpuits entire value of envirmoental variables into 1 string
            string Path = Environment.GetEnvironmentVariable("Path");
            //splits the enviromental variables into individuals paths
            string[] paths = Path.Split(';');
            //Cycle through enviromental variables till it finds python
            for (int i = 0; i < paths.Length; i++)
            {
                string p = paths[i];
                if (p.IndexOf("Python") > -1)
                {
                    return p;
                }
                continue;
            }
            return "";

        }
    }
    
}



And the Python code is:
Python
#!/usr/bin/env python
# coding: utf-8

# In[59]:


#Import Python Libraries
import pandas as pd
import numpy as np
import re
import os
import wikipedia as wk
import wikipediaapi
import sys

import requests
from bs4 import BeautifulSoup


# In[63]:


#Read csv file - change it to add code for insertion of input "C:/Users/harsh/OneDrive/Desktop/CMD_Sample.csv"
try:
    filename = sys.argv[1]
    df = pd.read_csv(filename)
except IOError:
    print("Error: File cannot be found/ Data cannot be read")
    sys.exit()
else:
    print("Successful upload of file.")

# getting access to file path for output purposes
savefile = filename.rsplit('.')
savefilepath = savefile[0]


# In[86]:


#deleting columns that are not required
try: #incase there is no time column
        df = df[['Time of Day','Process Name','PID','Command Line']]
except KeyError:
        df = df[['Process Name','PID','Command Line']]

#data preprocessing - in case of missing values
df.fillna('N/A', inplace = True)
df.fillna(0, inplace = True)

#separate cmd line columns from the other columns
try: #incase of absence of time column
    df1 = df[['Time of Day','Process Name','PID','Command Line']]
except KeyError:
    df1 = df[['Process Name','PID','Command Line']]

#separating command line column for translation work
df2 = df[['Command Line']]

#removing space in column names
df1 = df1.rename({'Command Line': 'Command_Line'}, axis='columns')
df2 = df2.rename({'Command Line': 'Command_Line'}, axis='columns')
print("Data preprocessing is complete.")


# In[87]:


#separating cmd line column into two - Command Line and Command Path
df2[['Command_Path','Command_Line']] = df2.Command_Line.str.split(" ",n=1,expand=True)


# In[88]:


#creating dictionary for special characters
charreplace = {
  "\\\\": " ",
    "\"": " ",
    "\'": " "
}
#replacing special characters in command line args
df_fixed = df2.replace({'Command_Line':charreplace, 'Command_Path':charreplace}, regex=True)

df2[['Command_Path']] = df_fixed[['Command_Path']]
df2[['Command_Line']] = df_fixed[['Command_Line']]

#remove extra dataframes
del df_fixed


# In[89]:


#separating main file for translation
df2[['path','file']] = df2.Command_Path.str.rsplit(" ",n=1,expand=True)
# defining list for translation result
translation = []
#use df2["file"] as the resulting column
cleanr = re.compile('<.*?>')

executable = list(df2["file"])
for index in executable:
	if index is not None:
		a = re.sub('.exe' ,'.html', index.lower())
		#url = "https://ss64.com/nt/eventcreate.html"
		url = "https://ss64.com/nt/" + a
		response = requests.get(url)
		if url == "https://ss64.com/nt/":
			title = "[Page not found]"
		else:
			soup = BeautifulSoup(response.text, "html.parser")
			title = re.sub(cleanr, '', str(soup.findAll('title')))

		if title != "[Page not found]":
			htmlcontent = soup.findAll('p')
			y = False
			for x in htmlcontent:
				if y is False:
					print(re.sub(cleanr,'',str(x)))
					y = True
		else:
			print("Manual Not Found")
# In[ ]:


I'm using the An A-Z Index of Windows CMD commands - SS64.com[^] manual page to get my information via web scraping but the GUI cant seem to handle dynamically finding creating the proper URL by adding a file .exe to the back and will just indefinitely freeze. The program currently prints it onto a terminal but it will eventually be saved into a excel document. When running this code via python normally, it out puts everything perfectly fine, but will just freeze from my GUI. I'm not even sure where to start looking for the issues.

What I have tried:

Ive tried manually setting the URL to specific pages and get information and that information properly outputs, but will freeze when the GUI tries to create the URL via the log file even if the created URL should be the same as the manually inputted one.
Posted
Updated 8-Sep-20 20:14pm
v2

1 solution

You may perform your time-consuming task (RunPython) in a BackgroundWorker thread. See BackgroundWorker Class (System.ComponentModel) | Microsoft Docs[^].
 
Share this answer
 
Comments
TheRealSteveJudge 9-Sep-20 2:16am    
5*
brandon Thai 9-Sep-20 11:55am    
After testing it out, all it really does it make it so i can still interact with the GUI while the program is working, but it still doesn't output what it is supposed to, which is still no better then being frozen

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900