Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up to make building MacOS Package work... #77

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
6 changes: 6 additions & 0 deletions build_instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,9 @@ file that can be run to install the software.
```sh
pyinstaller -p src --add-data="src;." -y -w --icon=src/assets/icon.ico --name=open-mcr src/main_gui.py; makensis installer.nsi
```

## MacOS
Use the following command:
``` sh
pyinstaller -p src --clean --onefile -y -w --icon=src/assets/icon.ico --name=open-mcr src/main_gui.py --add-data="src/assets/*.pdf:./assets"
```
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PyInstaller==3.5
typing==3.7.4.1
PyInstaller>=4.6
Pillow==9.2.0
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the change to PyInstaller>=4.6, but I'm confused by the change from typing (the type-checking tool) to Pillow (an image processing library). Was that an intentional change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pillow is the python library to use the PIL image library package. I ended up with it for something, maybe PyInstaller. I will look back at it. typing is not required for python 3.9+ as it is included. As the current python is 3.10, I am trying to update this to work with that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I will get to the rest of these this weekend.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typing is not required for python 3.9+ as it is included

Oh I didn't know that. 👍 to removing the dependency then, thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still have to figure out why I had to add Pillow. I think it is for icon building for MacOS.

  • Revert the scrolling frame changes

  • Revert the change from using / to join paths (if you do find this causes any issues, please open an issue and I can take another look at it)

  • Remove the test_ files -- I definitely agree with having more tests but we can open issues instead of adding TODO comments, that way all the work is tracked in one place

  • Revert the requirements change from typing==3.7.4.1 to Pillow==9.2.0; this looks like a mistake (please correct me if I'm wrong!)

numpy==1.22.0
opencv-python==4.5.4.60
flake8==3.7.8
Expand Down
48 changes: 48 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import setuptools

with open("readme.md", "r") as f:
long_description = f.read()

setuptools.setup(
name="open-mcr",
url = "https://github.com/gutow/open-mcr",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should make this the URL of the original repo:

Suggested change
url = "https://github.com/gutow/open-mcr",
url = "https://github.com/iansan5653/open-mcr",

version="1.3.0dev",
description="Graphical tool for optical mark recognition for test scoring.",
long_description=long_description,
long_description_content_type="text/markdown",
author="Ian Sanders",
author_email="[email protected]",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd just prefer to use my personal email for this (I might not work at GitHub forever 😄):

Suggested change
author_email="iansan5653@github.com",
author_email="iansan5653@gmail.com",

keywords="omr, optical-mark-recognition, open-educational-resources, " \
"exam-sheets, exam-scoring",
license="GPL-3.0+",
packages=setuptools.find_packages(exclude=("dist","build",)),
data_files=[
('src/assets',['src/assets/icon.ico','src/assets/icon.svg',
'src/assets/multiple_choice_sheet_75q.pdf',
'src/assets/multiple_choice_sheet_75q.svg',
'src/assets/multiple_choice_sheet_150q.pdf',
'src/assets/multiple_choice_sheet_150q.svg',
'src/assets/social.png', 'src/assets/wordmark.png',
'src/assets/wordmark.svg', 'src/assets/manual.md',
'src/assets/manual.pdf'])
],
include_package_data=True,
install_requires=[
'PyInstaller>=4.6',
'Pillow==9.2.0',
'numpy==1.22.0',
'opencv-python==4.5.4.60',
'flake8==3.7.8',
'yapf==0.28.0',
'pytest==6.2.5',
],
classifiers=[
'Development Status :: 5 - Production/Stable',
'Intended Audience :: Developers',
'Intended Audience :: Education',
'Intended Audience :: End Users/Desktop',
'License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)',
'Programming Language :: Python :: 3',
'Operating System :: OS Independent'
]
)
Binary file modified src/assets/multiple_choice_sheet_150q.pdf
Binary file not shown.
104 changes: 104 additions & 0 deletions src/tk_scrollable_frame.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import tkinter as tk
import platform


# ************************
# Scrollable Frame Class
# ************************
class ScrollFrame(tk.Frame):
"""
This is based on the code from this GIST: ttps://gist.github.com/mp035/9f2027c3ef9172264532fcd6262f3b01

This class provides a scrollable frame as a nearly drop-in replacement for a tkframe.

Add items to the ScrollFrame.viewPort, rather than to the ScrollFrame.
```
class Example(tk.Frame):
def __init__(self, root):
tk.Frame.__init__(self, root)
self.scrollFrame = ScrollFrame(self) # add a new scrollable frame.

# Now add some controls to the scrollframe.
# NOTE: the child controls are added to the view port (scrollFrame.viewPort, NOT scrollframe itself)
for row in range(100):
a = row
tk.Label(self.scrollFrame.viewPort, text="%s" % row, width=3, borderwidth="1",
relief="solid").grid(row=row, column=0)
t = "this is the second column for row %s" % row
tk.Button(self.scrollFrame.viewPort, text=t, command=lambda x=a: self.printMsg("Hello " + str(x))).grid(
row=row, column=1)

# when packing the scrollframe, we pack scrollFrame itself (NOT the viewPort)
self.scrollFrame.pack(side="top", fill="both", expand=True)

def printMsg(self, msg):
print(msg)


if __name__ == "__main__":
root = tk.Tk()
Example(root).pack(side="top", fill="both", expand=True)
root.mainloop()
```
"""
def __init__(self, parent):
super().__init__(parent) # create a frame (self)

self.canvas = tk.Canvas(self, borderwidth=0, background="#ffffff") # place canvas on self
self.viewPort = tk.Frame(self.canvas,
background="#ffffff") # place a frame on the canvas, this frame will hold the child widgets
self.vsb = tk.Scrollbar(self, orient="vertical", command=self.canvas.yview) # place a scrollbar on self
self.canvas.configure(yscrollcommand=self.vsb.set) # attach scrollbar action to scroll of canvas

self.vsb.pack(side="right", fill="y") # pack scrollbar to right of self
self.canvas.pack(side="left", fill="both", expand=True) # pack canvas to left of self and expand to fil
self.canvas_window = self.canvas.create_window((4, 4), window=self.viewPort, anchor="nw",
# add view port frame to canvas
tags="self.viewPort")

self.viewPort.bind("<Configure>",
self.onFrameConfigure) # bind an event whenever the size of the viewPort frame changes.
self.canvas.bind("<Configure>",
self.onCanvasConfigure) # bind an event whenever the size of the canvas frame changes.

self.viewPort.bind('<Enter>', self.onEnter) # bind wheel events when the cursor enters the control
self.viewPort.bind('<Leave>', self.onLeave) # unbind wheel events when the cursorl leaves the control

self.onFrameConfigure(
None) # perform an initial stretch on render, otherwise the scroll region has a tiny border until the first resize

def onFrameConfigure(self, event):
'''Reset the scroll region to encompass the inner frame'''
self.canvas.configure(scrollregion=self.canvas.bbox(
"all")) # whenever the size of the frame changes, alter the scroll region respectively.

def onCanvasConfigure(self, event):
'''Reset the canvas window to encompass inner frame when required'''
canvas_width = event.width
self.canvas.itemconfig(self.canvas_window,
width=canvas_width) # whenever the size of the canvas changes alter the window region respectively.

def onMouseWheel(self, event): # cross platform scroll wheel event
if platform.system() == 'Windows':
self.canvas.yview_scroll(int(-1 * (event.delta / 120)), "units")
elif platform.system() == 'Darwin':
self.canvas.yview_scroll(int(-1 * event.delta), "units")
else:
if event.num == 4:
self.canvas.yview_scroll(-1, "units")
elif event.num == 5:
self.canvas.yview_scroll(1, "units")

def onEnter(self, event): # bind wheel events when the cursor enters the control
if platform.system() == 'Linux':
self.canvas.bind_all("<Button-4>", self.onMouseWheel)
self.canvas.bind_all("<Button-5>", self.onMouseWheel)
else:
self.canvas.bind_all("<MouseWheel>", self.onMouseWheel)

def onLeave(self, event): # unbind wheel events when the cursorl leaves the control
if platform.system() == 'Linux':
self.canvas.unbind_all("<Button-4>")
self.canvas.unbind_all("<Button-5>")
else:
self.canvas.unbind_all("<MouseWheel>")
49 changes: 33 additions & 16 deletions src/user_interface.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import abc
import enum
import os.path
from pathlib import Path
import subprocess
import sys
import tkinter as tk
from tkinter import filedialog, ttk
from tk_scrollable_frame import ScrollFrame
import typing as tp
import platform

Expand Down Expand Up @@ -442,7 +444,7 @@ def show_exit_button_and_wait(self):
sys.exit(0)


class MainWindow:
class MainWindow(tk.Frame):
root: tk.Tk
input_folder: Path
output_folder: Path
Expand All @@ -469,21 +471,25 @@ def __init__(self):
app.iconbitmap(iconpath)

app.protocol("WM_DELETE_WINDOW", self.__on_close)
app.minsize(500,600)

# Scrollable Inner Frame
self.scroll = ScrollFrame(app)

self.__input_folder_picker = InputFolderPickerWidget(
app, self.__on_update)
self.__answer_key_picker = AnswerKeyPickerWidget(app, self.__on_update)
self.scroll.viewPort, self.__on_update)
self.__answer_key_picker = AnswerKeyPickerWidget(self.scroll.viewPort, self.__on_update)
self.__arrangement_map_picker = ArrangementMapPickerWidget(
app, self.__on_update)
self.scroll.viewPort, self.__on_update)
self.__output_folder_picker = OutputFolderPickerWidget(
app, self.__on_update)
self.scroll.viewPort, self.__on_update)

self.__status_text = tk.StringVar()
status = tk.Label(app, textvariable=self.__status_text)
status = tk.Label(self.scroll.viewPort, textvariable=self.__status_text)
status.pack(fill=tk.X, expand=1, pady=(YPADDING * 2, 0))
self.__on_update()

buttons_frame = tk.Frame(app)
buttons_frame = tk.Frame(self.scroll.viewPort)

# "Open Help" Button
pack(ttk.Button(buttons_frame,
Expand All @@ -509,6 +515,9 @@ def __init__(self):
side=tk.RIGHT)
pack(buttons_frame, fill=tk.X, expand=1)

# when packing the scrollframe, we pack scrollFrame itself (NOT the viewPort)
self.scroll.pack(side="top", fill="both", expand=True)

self.__ready_to_continue = tk.IntVar(name="Ready to Continue")
app.wait_variable("Ready to Continue")

Expand Down Expand Up @@ -609,20 +618,28 @@ def __confirm(self):
self.__ready_to_continue.set(1)

def __show_help(self):
helpfile = str(Path(__file__).parent / "assets" / "manual.pdf")
subprocess.Popen([helpfile], shell=True)
helpfile = helpfile = os.path.join(Path(__file__).parent, "assets",
"manual.pdf")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure os.path.join is necessary here? Notice that in the original code we are not combining strings but instead using the slash (/) operator, which should join paths similarly to os.path.join. I think that the / way is more readable if it does work on all systems.

For reference, here's the documentation on the slash operator.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have not investigated the newer path module. However, I am guessing it rides on top of os.path. In interpreted code I generally try to avoid abstractions as much as possible. I know that os.path.join works across all platforms. It does look like your usage of path works across all platforms as you use it other places as well, but I am not confident of how robust it is. I also am wary of overloading fundamental operators such as divide /.

Copy link
Owner

@iansan5653 iansan5653 Sep 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep with the current style please, since it's more readable and consistent across all the code. The new path module should work in all situations, and I think it's the more modern/actively updated library.

if platform.system() in ('Darwin','Linux'):
subprocess.Popen(['open',helpfile])
else:
subprocess.Popen([helpfile], shell=True)

def __show_sheet(self):
if (self.form_variant == FormVariantSelection.VARIANT_75_Q):
helpfile = str(
Path(__file__).parent / "assets" /
helpfile = os.path.join(Path(__file__).parent, "assets",
"multiple_choice_sheet_75q.pdf")
subprocess.Popen([helpfile], shell=True)
if platform.system() in ('Darwin','Linux'):
subprocess.Popen(['open', helpfile])
else:
subprocess.Popen([helpfile], shell=True)
elif (self.form_variant == FormVariantSelection.VARIANT_150_Q):
helpfile = str(
Path(__file__).parent / "assets" /
"multiple_choice_sheet_150q.pdf")
subprocess.Popen([helpfile], shell=True)
helpfile = os.path.join(Path(__file__).parent, "assets",
"multiple_choice_sheet_150q.pdf")
if platform.system() in ('Darwin','Linux'):
subprocess.Popen(['open', helpfile])
else:
subprocess.Popen([helpfile], shell=True)

def __on_close(self):
self.__app.destroy()
Expand Down
2 changes: 1 addition & 1 deletion test/end-to-end/150q-core/output/results.csv
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Last Name,First Name,Middle Name,Test Form Code,Student ID,Course ID,Source File,Q1,Q2,Q3,Q4,Q5
,,,,12345678,,a.jpg,A,B,B,D,E
,,,,12334235,,b.jpg,A,,C,D,E
,,,,12345678,,a.jpg,A,B,B,D,E
2 changes: 1 addition & 1 deletion test/end-to-end/150q-core/output/scores.csv
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Last Name,First Name,Middle Name,Test Form Code,Student ID,Course ID,Source File,Total Score (%),Total Points,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8,Q9,Q10,Q11,Q12,Q13,Q14,Q15,Q16,Q17,Q18,Q19,Q20,Q21,Q22,Q23,Q24,Q25,Q26,Q27,Q28,Q29,Q30,Q31,Q32,Q33,Q34,Q35,Q36,Q37,Q38,Q39,Q40,Q41,Q42,Q43,Q44,Q45,Q46,Q47,Q48,Q49,Q50,Q51,Q52,Q53,Q54,Q55,Q56,Q57,Q58,Q59,Q60,Q61,Q62,Q63,Q64,Q65,Q66,Q67,Q68,Q69,Q70,Q71,Q72,Q73,Q74,Q75,Q76,Q77,Q78,Q79,Q80,Q81,Q82,Q83,Q84,Q85,Q86,Q87,Q88,Q89,Q90,Q91,Q92,Q93,Q94,Q95,Q96,Q97,Q98,Q99,Q100,Q101,Q102,Q103,Q104,Q105,Q106,Q107,Q108,Q109,Q110,Q111,Q112,Q113,Q114,Q115,Q116,Q117,Q118,Q119,Q120,Q121,Q122,Q123,Q124,Q125,Q126,Q127,Q128,Q129,Q130,Q131,Q132,Q133,Q134,Q135,Q136,Q137,Q138,Q139,Q140,Q141,Q142,Q143,Q144,Q145,Q146,Q147,Q148,Q149,Q150
,,,,12345678,,a.jpg,80.0,4,1,1,0,1,1
,,,,12334235,,b.jpg,80.0,4,1,0,1,1,1
,,,,12345678,,a.jpg,80.0,4,1,1,0,1,1
2 changes: 1 addition & 1 deletion test/end-to-end/rotation/output/results.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Last Name,First Name,Middle Name,Test Form Code,Student ID,Course ID,Source File,Q1,Q2,Q3,Q4,Q5
,,,,12334235,,90deg.jpg,A,,C,D,E
,,,,12334235,,180deg.jpg,A,,C,D,E
,,,,12334235,,270deg.jpg,A,,C,D,E
,,,,12334235,,90deg.jpg,A,,C,D,E
11 changes: 11 additions & 0 deletions test/test_tk_scrollable_frame.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import tkinter as tk
# This standard testing will not work because open-mcr is
# is not built as a python package.

# def test_init():
# root = tk.Tk()
# tstframe = ScrollFrame(root)
# assert (isinstance(tstframe,ScrollFrame))
# assert (isinstance(tstframe,tk.Frame))

# TODO more tests
17 changes: 17 additions & 0 deletions test/test_user_interface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# TODO the following need tests
# pack
# create_and_pack_label
# PickerWidget
# FolderPickerWidget
# FilePickerWidget
# CheckboxWidget
# SelectWidget
# FormVariantSelection
# InputFolderPickerWidget
# OutputFolderPickerWidget
# AnswerKeyPickerWidget
# ArrangementMapPickerWidget
# ProgressTrackerWidget
# create_and_pack_progress

# MainWindow