[PYTHON] Create file update history and brief release notes with PySImple GUI

Introduction

There is no team development at work, and I often write code individually, so I have rarely written code update history or release notes. (I haven't written a release note because I haven't released it)

Suddenly, I thought it would be better to have good version control before the release.

You can also use GitHub locally, The OS used was different and it was a hassle to learn how to use it, so I came up with a simple version control tool.

Development environment

Windows10 PySImpleGUI 4.29.0 Python 3.7.6 pyinstaller (used for exe) 4.1

GUI The GUI looks like this: This time the theme is Light Brown 3.

Since the update history will be described in detail, more detailed updates will be included than the release. Therefore, it may be updated more than once with the same file name. Add the update time to the file name and copy the updated file to another folder. By doing so, the history of the text and the actual file are linked.

The "Title" of the release notes has somehow made it possible to outline the changes. makereleasenote_maindisp.png [update] Updates the time when saving the history. (* Automatic update will be added every 5 minutes to prevent forgetting to press)

[Choice] Select the file to be updated.

[Reflect] Write the update history to history.txt. ** Write so that the developer can see the specific changes. ** ** At the same time, the file to be updated with the update time added to the file name Copy it to the history folder.

[writing] Write the update contents to release.txt. ** Write in user-friendly language. ** **

code

Below is the code.

make_note_app.py



import PySimpleGUI as sg
import datetime
import os
import shutil

class MainDisp:
    def __init__(self,now):
        sg.theme("LightBrown3")  #Favorite theme setting

        self.layout = [
        [sg.Text(text="Update time"),
         sg.Input(default_text=now, size=(20,1), key=("-TIME-"),
                  use_readonly_for_disable=True),
         sg.Button(button_text="update", key=("-UPDATE-"))],

        [sg.Text(text="File"),
         sg.InputText(size=(50,1), key='-FILE-'),
         sg.FileBrowse("Choice", target=("-FILE-"))],

        [sg.Text(text="Change log")],

        [sg.Multiline(size=(200,5), key=("-HIST-"))],

        [sg.Button(button_text="Reflect", key=("-HANEI-"))],

        [sg.Text(text="Release notes")],

        [sg.Text(text="title"),
         sg.Input(size=(50,1), key=("-TITLE-"))],

        [sg.Multiline(size=(200,5), key=("-RELEASE-"))],

        [sg.Button(button_text="writing", key=("-WRITE-"))]
        ]

        self.window = sg.Window(title="MakeReleaseNote",
                                layout=self.layout,
                                size=(500,400))

    def writefile(self,type):
        time = self.window["-TIME-"].get()
        filename = self.window["-FILE-"].get()

        #Update history ver
        if type == "history":
            hist = self.window["-HIST-"].get()
            path = "../history.txt"
    
            if os.path.exists(path):
                '''
I want to write the updated information at the top of the file, so
Read the entire contents of the file once, then add new information to your head and write it again
                '''
                with open(path,"r") as f:
                    all = f.read()

                write = "Update time:{}\n Update file:{}\n Changes:\n{}\n".format(
                    time,os.path.basename(filename),hist) + all
            else:
                write = "Update time:{}\n Update file:{}\n Changes:\n{}\n".format(
                    time,os.path.basename(filename),hist)

            #Copy the updated file to the history folder
            #However, reshape once to reflect the time in the file name
            s_time = datetime.datetime.strptime(time,"%y/%m/%d %H:%M:%S")
            shutil.copy2(filename,"../history/"+
                s_time.strftime("%y%m%d_%H%M%S_")+os.path.basename(filename))

        #Release notes ver
        elif type == "release":
            hist = self.window["-RELEASE-"].get()
            title = self.window["-TITLE-"].get()
            path = "../release.txt"
           
            '''
I want to write the updated information at the top of the file, so
Read the entire contents of the file once, then add new information to your head and write it again
            '''
            if os.path.exists(path):
                with open(path,"r") as f:
                    all = f.read()

                write = "File:{}\nTitle:{}\nComment:\n{}\n".format(
                    os.path.basename(filename),title,hist) + all
            else:
                write = "File:{}\nTitle:{}\nComment:\n{}\n".format(
                    os.path.basename(filename),title,hist)

        with open(path,"w") as f:
            f.write(write)

    def main(self):
        while True:
            event, values = self.window.read()
            if event == None:  #close button
                break
            elif event == "-UPDATE-":  #Time update button
                self.window["-TIME-"].update(gettimenow())
            elif event == "-HANEI-":  #Update history reflection button
                self.writefile("history")
            elif event == "-WRITE-":  #Release notes write button
                self.writefile("release")
  
        self.window.close()


#A function that returns the current time as str
def gettimenow():
    time = datetime.datetime.now()
    return time.strftime("%y/%m/%d %H:%M:%S")

if __name__ == "__main__":
    maindisp = MainDisp(gettimenow())
    maindisp.main()

Output file

The update history text file has the latest information at the top like this. history_txt.png

The history folder is at the same time as the above history update time The updated file has been renamed and copied. history_folder.png

The text file of the release notes is as follows. The appearance is a little different because of the title. In the release note, it was originally planned to be created with the version added to the file name, so The update time is not listed. The file name is like the update time. (In the example below, the file names are the same, so it's difficult to understand ...) release.png

Bonus: exe conversion

Install pyinstaller.

pip install pyinstaller

When --noconsole is inserted, the console does not appear at startup.

pyinstaller [Path of the file you want to exe] --noconsole

When converted to exe, a folder with the same file name will be created in a folder called dist. Among them, there are a lot of necessary files, and exe also exists there.

** Because it is created in a directory deviated from the original position Please note that an error will occur if a relative path is used. ** **

If it must be a relative path, copy all the files in the same directory as the exe and You can do it by deploying it where you want it to be. ** You can't just move the exe. ** **

I wanted the app itself to be versatile so that it could be used anywhere, Instead of rewriting it to an absolute path, I moved the environment with the relative path.

in conclusion

It has nothing to do with people who manage versions such as GitHub, but What I want to say is that you can do this with PySImpleGUI right away. I really recommend the combination of Python and PySImpleGUI, which allows you to quickly realize what you have come up with.

Recommended Posts

Create file update history and brief release notes with PySImple GUI
Create a GUI executable file created with tkinter
Create a native GUI app with Py2app and Tkinter
How to do Bulk Update with PyMySQL and notes [Python]
Create xlsx file with XlsxWriter
Create a temporary file with django as a zip file and return it
Create and return a CP932 CSV file for Excel with Chalice
Create an Excel file with Python3
[GUI with Python] PyQt5-Drag and drop-
Draw a graph with PySimple GUI
Create a file uploader with Django
Read the file with python and delete the line breaks [Notes on reading the file]