0

I have written a script that imports existing data from a .csv file, modifies it, plots it, and also asks the user for an input (input2) that is used as the graph title and filename for the data set and graph. I would like to have another script (import.py) that executes the original script (new_file.py) and is able to determine what the user's input was so that I can access the newly-created files. How can I pass the user input from one script to the other?

The script that takes the user input is new_file.py:

def create_graph(): import pandas as pd import numpy as np import matplotlib.pyplot as plt input1 = input("Enter the file you want to import: ") data_file = pd.read_excel(input1 + ".xlsx") ws = np.array(data_file) a = ws[:, 0] b = ws[:, 1] c = ws[:, 2] bc = b + c my_data1 = np.vstack((a, b, c, bc)) my_data1 = my_data1.T input2 = input("Enter the name for new graph: ") np.savetxt(input2 + ".csv", my_data1, delimiter=',') plt.plot(a, b, 'ro') plt.plot(a, c, 'go') plt.plot(a, bc, 'bo') plt.ylabel("y-axis") plt.xlabel("x-axis") plt.legend(['Column 1 data', 'Column 2 data', 'Column 3 data'], loc='best') plt.title(input2) plt.savefig(input2) plt.show() 

The second script (import.py) that I am trying to use to run this is currently:

import new_file as nf nf.create_graph() 

I'm not sure how to pass input2 from new_file.py to import.py. Can anyone help me please? Thank you

2
  • Have you tried running this? I'm not sure what the problem is. It should work fine since you define input2 within your function that is being called. Commented Jun 8, 2020 at 22:37
  • Just return the value(s) from the function. learnpython.org/en/Functions Commented Jun 8, 2020 at 22:38

2 Answers 2

1

Simply return the value.

def create_graph(): ... return input2 

then in your other script:

import new_file as nf input2 = nf.create_graph() 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! This worked. Very new to this. I appreciate everyone's time and responses
1

It looks like what you're wanting to do it return information from your function.

def create_graph(): # ... all of your code other code ... return input2 

Then inside of your import.py you can receive your returned value like this:

import new_file as nf input2 = nf.create_graph() # use input2 however you want 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.