Running python script on web browser - python

I am new to python. I have created a Script which asks the user to add date and file name and then generates csv file. I want to run that Script on our network. So that everyone on the network can put the dates and generate their report. Can anybody please suggest me which module should i use and how.Although my script is generating two files , i only want everyone to download Revenue report not the missing id's.
here is the snippet from my program which is calling all of my functions,i made.
Thanks in advance.
print "Enter state date(eg:-2015-01-01):",
start_date = raw_input()
print "Enter the last date(eg:-2015-01-01):",
end_date=raw_input()
print "Please give a filename for this report(eg:-January_rev_report): ",
file_name=raw_input()
in_file = open(""+file_name+".csv", "w")
in_file2=open("missiong_ids.csv","w")
in_file2.write("Missing_ids\n")
in_file.write("Partner_id|Partner_name|Price_of_lead|Date|Osdial_Lead_id|Bob_lead_id|list_id|Phone_number|State|Postal_code|Status\n")
data_=getPidsForThisMonth(start_date,end_date)
for j in data_:
if getReport(j,start_date,end_date) is None:
missing_ids=""
missing_ids+=j
#print missing_ids + " is missing id, the whole list of missing id's will be added to missing_ids.csv file "
in_file2.write(missing_ids)
else:
data=""
details = getPartnerDetails(j)
pid = str(details[0])
name = str(details[1])
price = str(details[2])
report_data=getReport(j,start_date,end_date)
date=str(report_data[0])
lead_id=str(report_data[1])
bob_id=str(report_data[2])
list_id=str(report_data[3])
phone=str(report_data[4])
state=str(report_data[5])
postal_code=str(report_data[6])
status=str(report_data[7])
data+=pid+"|"+name+"|"+price+"|"+date +"|"+lead_id+"|"+bob_id+"|"+list_id+"|"+phone+"|"+state+"|"+postal_code+"|"+status
data+="\n"
in_file.write(data)

Flask would be suited to turn this into a small web-app: http://flask.pocoo.org/
I would have one controller that takes two parameters, the start- and end-date. Or better have a small page where dates can be selected and pass this using POST to a controller. This would run the script and return the file. If you set the response correctly the csv file will start as a download.
You won't need to write the file, just store the lines in a list and at the end generate the full content using '\n'.join(lines).

Related

Python - file does not write all of the data

I have JSON data which I am pulling in via API.
here's my code
# list of each api url to use
link =[]
#for every id in the accounts , create a new url link into the link list
for id in accounts:
link.append('https://example.ie:0000/v123/accounts/'+id+'/users')
accountReq = []
for i in link:
accountReq.append(requests.get(i, headers=headers).json())
with open('masterSheet.txt', 'x') as f:
for each in accountReq:
account = each['data']
for data in account:
list=(data['username']+" "+" ",data['first_name'],data['last_name'])
f.write(str(list)+"\n")
This pulls in data no problem .
If I do
print(data['username']+" "+" ",data['first_name'],data['last_name'])
I get all of the data back , around 500lines.
However my problem I am having is when I try to write to my file , it writes about 8lines of data and then stops running with no errors.
I'm assuming its due to the data size. How can I fix my issue of not printing all of the data to the .txt file??
Are you trying to write each data point to the file? Your write function is outside the nested for loop, so you are actually only writing the last list variable that you create to the file.
You should move the f.write() under the for loop if you intend to write every single data point into the file.
for data in account:
list=(data['username']+" "+" ",data['first_name'],data['last_name'])
f.write(str(list)+"\n")

Trying to create a script that crosschecks if excel file has been updated and if it has been updated, sends out an email

basically i run a shop where we once a day update an excel file that gets external data and then we send the updated files via email to a group of people. We do this with quite a lot of reports, so i want to write a script that does this automatically.
The external data comes once a day and sometimes it comes at 1 in the morning, sometimes early in the morning, sometimes later - but mainly it comes during the night/early morning, so when i get to work the external dataset should be updated.
My question is:
Getting the excel file loaded and send via email seems pretty straight forward but i dont want the email to be send if lets say the external data set has not been updated.
How do i compare the day before dataset with todays dataset, without saving yesterdays dataset as a seperate file on my computer, as this would build up to quite alot of files.
Based on this link Generating an MD5 checksum of a file
Code could look something like this:
import hashlib
from os.path import exists
def get_oldsum():
global last_checksum
if exists("lastsum"):
with open("lastsum","r") as file:
last_checksum = file.readline()
else:
last_checksum = ""
def save_newsum(newsum):
with open("lastsum", "w") as file:
file.write(newsum)
def make_new_hash(file):
with open(file,"rb") as file:
file_hash = hashlib.md5()
while chunk := file.read(8192):
file_hash.update(chunk)
return file_hash.hexdigest()
if __name__ == "__main__":
get_oldsum()
new_hash = make_new_hash("shared_reports.xlsx")
print("This MD5 Hash:",new_hash)
if new_hash != last_checksum:
print("New Hash! We should trigger our mail-function asap...")
save_newsum(new_hash)

Can't append a PyCharm file

I'm a newbie programmer , I'm coding this account tool for a game (Just for myself, can only run it in the console for now)
To do this I needed a database but what I did was creating a Python file with my class, and another one that is interactive and it has to append some things you type to the other Python file , the one with the class, so it automatically saves on both files.
At first I did this
action = input('Select an action')
if action == 'addw':
champion = input('\n'+'Select a champion \n')
f = open('/Users/tommaso/PycharmProjects/Random/Champs.py', 'a')
f.write('\n' + str(champion.title()) + r'.addw()')
print('\nA Win was added to' + ' '+ str(champion))
And it works. However, I at the end of my program (not just the code up there) it would just stop, and since I needed it to restart or at least give me the option to restart/quit, I had to use a while loop, so at first I tried if it worked with a while loop, and I did this (just a scratch):
while True:
try:
x = open('/Users/tommaso/PycharmProjects/Random/Champs.py', 'a')
x.write('hello')
except: break
And this , too, worked.
So I made a big while loop with everything from my first console file, using the exact same method to append the action, but it doesn't work.
Doesn't give me any error, but it doesn't append anything, here's the code:
while True:
try:
action = input('Welcome to your own account tool.\n' + 'Select an action:\n' + 'addw,addl\n' + 'getstats,getallstats,lanes\n\n')
if action == 'addw':
champion = input('\n' + 'Select a champion \n')
f = open('/Users/Tommaso/PycharmProjects/Random/Champs.py', 'a')
f.write('\n' + str(champion.title()) + r'.addw()')
print('\nA Win was added to' + ' ' + str(champion))
except: Exceptaction = input('Exit / Restart')
if Exceptaction== 'Exit':
break
else:
pass
But it doesn't work, it just doesn't append what I tell him to my file.
Anyone knows why and how can I fix this?
Thanks in advance!!
I would recommend writing to a text file rather than writing to the actual python file.
A python file (Python Program /Python Script) is used to carry out the tasks you desire (the actual program).
Where as a text file is commonly used to store and read the data for the python program.
Working with .txt or text files in python is really easy.
You could also use mySQL to create a Data Base to store and retrieve data but I would suggest starting with the text files for now and once you understand that method progress to mySQL, that way you will learn two common methods of storing and fetching data.
PS: Working with text files is quick and simple it will save a headache. Also look up some free PDF books via a google advanced search, you will have so much tutorials, you will not know where to start !!.
Hope this helps. Happy Coding :).

Keep input we get from user in text file after we execute our python code again to get that input to show in the screen when we need it from text file

" Keep input we get from user in text file after we execute our python code again to get that input to show in the screen when we need it from text file "
I am new to Python and I spend lot's of time to learn it well. We learned " File Access in Python " in the university and I have some questions.
We are getting input from user to write in the text file ( which I managed to do it nicely along with some other features that I wrote with more methods in my file access class, you can see it from my below code ) but what I want is this:
After execute my python code again ( re-open the py file again to execute the same code) I want to call back the things I typed and saved in the text file before. Right now, when I re-open my python file and execute my code , the things I wrote before in the text file are all gone :( Do you know how to fix it ? and how can I print out the 3rd-4th etc lines until this letter on the python shell too ?
The reason I ask this because I am working on my gui tkinter which is done mostly but I want to keep the things I typed on my entry labels and save them on my text file and call them from text file again to show on my gui again when I press a button with the help of this " File Access Python Topic ". Some people said me to do it with MySQL but I want to do and learn with this way first. I will be very happy if someone would help me and teach me how do we do these kind of things, thank you very much from now on. This is my code:
class fileAccess:
#Create a short program to open two files (one for reading, one for writing)
#then print the files three basic attributes to screen (.name .closed .mode)
#close the file then print them again
def writeFile(self):
myfile=open("newfile.txt","w")
myfile.write("My computer fell out of his tree")
myfile.close()
myfile=open("newfile.txt","r")
fistfilecontent = myfile.read()
myfile.close()
print(fistfilecontent)
def readFile(self):
mysecondfile=open("text.txt","r")
secondfilecontent = mysecondfile.read()
mysecondfile.close()
print(secondfilecontent)
#Create a short program that reads user input from the keyboard and writes it to a file called #whatyouwrote.txt
def userWrites(self):
user=str(input("Type whatever you want to write for your text file: "))
mythirdfile=open("whatyouwrote.txt","w")
mythirdfile.write(user)
mythirdfile.close()
mythirdfile=open("whatyouwrote.txt","r")
thirdfilecontent = mythirdfile.read()
mythirdfile.close()
print(thirdfilecontent)
#Modify the example from lecture so that
#instead of going to the screen interaction with the program sends data ( like writing ) to a file called #logfile.txt
def sendData(self):
#Open a file
fo = open("foo.txt","r+")
value = fo.read(10)
print("Read the 1st String is: ", value)
myfifthfile = open("logfile.txt","r+")
myfifthfile.write(value)
myfifthfile.close()
#Close opened file
fo.close()
def sendData_2(self):
#Check current position
fo = open("foo.txt","r+")
#0 is the first position of your line in the text then we count 18 after that point
fo.seek(18,0)
#read the 11 chr after that 18 chr from seek
secondvalue = "\n"+fo.read(11)
print("Read the 2nd String is: ", secondvalue)
mysixthfile = open("logfile.txt","a+")
mysixthfile.write(secondvalue)
mysixthfile.close()
#Close opened file
fo.close()
### MAIN
aykut=fileAccess()
#aykut.sendData()
#aykut.sendData_2()
#aykut.writeFile()
#aykut.readFile()
aykut.userWrites()
When you
open("sometext.txt","w")
you actually overwrite what was written there before.
You can keep writing on the same file if you use the option "a"
open("sometext.txt","a")

Save Outfile with Python Loop in SPSS

Ok so I've been playing with python and spss to achieve almost what I want. I am able to open the file and make the changes, however I am having trouble saving the files (and those changes). What I have (using only one school in the schoollist):
begin program.
import spss, spssaux
import os
schoollist = ['brow']
for x in schoollist:
school = 'brow'
school2 = school + '06.sav'
filename = os.path.join("Y:\...\Data", school2) #In this instance, Y:\...\Data\brow06.sav
spssaux.OpenDataFile(filename)
#--This block are the changes and not particularly relevant to the question--#
cur=spss.Cursor(accessType='w')
cur.SetVarNameAndType(['name'],[8])
cur.CommitDictionary()
for i in range(cur.GetCaseCount()):
cur.fetchone()
cur.SetValueChar('name', school)
cur.CommitCase()
cur.close()
#-- What am I doing wrong here? --#
spss.Submit("save outfile = filename".)
end program.
Any suggestions on how to get the save outfile to work with the loop? Thanks. Cheers
In your save call, you are not resolving filename to its actual value. It should be something like this:
spss.Submit("""save outfile="%s".""" % filename)
I'm unfamiliar with spssaux.OpenDataFile and can't find any documentation on it (besides references to working with SPSS data files in unicode mode). But what I am going to guess is the problem is that it grabs the SPSS data file for use in the Python program block, but it isn't actually opened to further submit commands.
Here I make a test case that instead of using spssaux.OpenDataFile to grab the file, does it all with SPSS commands and just inserts the necessary parts via python. So first lets create some fake data to work with.
*Prepping the example data files.
FILE HANDLE save /NAME = 'C:\Users\andrew.wheeler\Desktop\TestPython'.
DATA LIST FREE / A .
BEGIN DATA
1
2
3
END DATA.
SAVE OUTFILE = "save\Test1.sav".
SAVE OUTFILE = "save\Test2.sav".
SAVE OUTFILE = "save\Test3.sav".
DATASET CLOSE ALL.
Now here is a paired down version of what your code is doing. I have the LIST ALL. command inserted in so you can check the output that it is adding the variable of interest to the file.
*Sequential opening the data files and appending data name.
BEGIN PROGRAM.
import spss
import os
schoollist = ['1','2','3']
for x in schoollist:
school2 = 'Test' + x + '.sav'
filename = os.path.join("C:\\Users\\andrew.wheeler\\Desktop\\TestPython", school2)
#opens the SPSS file and makes a new variable for the school name
spss.Submit("""
GET FILE = "%s".
STRING Name (A20).
COMPUTE Name = "%s".
LIST ALL.
SAVE OUTFILE = "%s".
""" %(filename, x,filename))
END PROGRAM.

Categories