pytexutils.gui.table_editor

  1from tkinter import simpledialog
  2import tkinter as tk
  3import numpy as np
  4
  5import sys
  6sys.path += ['.', './tables/']
  7from pytexutils.tables.table import table
  8
  9
 10def table_editor():
 11    '''
 12        Opens a GUI with table editor that produces LaTeX code to display a table.
 13
 14        Parameters:  
 15        -----------
 16        - Nothing
 17
 18        Returns:  
 19        --------  
 20        - latex_table : str  
 21            LaTeX code to display a table  
 22
 23        Usage:
 24        ------
 25
 26        ```python
 27            latex_table = table_editor()
 28        ```
 29
 30        Output:
 31        -------
 32
 33        ```latex
 34        \\documentclass[11pt]{article}
 35        \\usepackage{booktabs}
 36        \\usepackage{graphicx}
 37
 38        \\begin{document}
 39
 40        \\begin{table}[!ht]
 41                \\centering
 42                \\caption{My table 1}\\label{tab:tab1}
 43                \\resizebox{\\columnwidth}{!}{
 44                \\begin{tabular}{ccc}
 45                        \\toprule
 46                            A     &     B     &     C     \\\\
 47                        \\midrule
 48                            0.1     &     0.2     &     0.3     \\\\
 49                            0.4     &     0.5     &     0.6     \\\\
 50                            0.7     &     0.8     &     0.9     \\\\
 51                            1.1     &     1.2     &     1.3     \\\\
 52                        \\bottomrule
 53                \\end{tabular}}
 54        \\end{table}
 55
 56        \\end{document}
 57        ```
 58    '''
 59
 60
 61    def get_value():
 62        data = []
 63        for i in range(rows):
 64            ddd = []
 65            for j in range(cols):
 66                try:
 67                    d = float(entries[i,j].get())
 68                except Exception:
 69                    d = str(entries[i,j].get())
 70                ddd.append(d)
 71            data.append(ddd)
 72        
 73        data = np.array(data)
 74        columns = data[0,:]
 75
 76        global latex_table
 77        latex_table = table(columns_name=columns, data=data[1:,:])
 78    
 79    window = tk.Tk()
 80    window.title('Table Editor')
 81    window.attributes("-zoomed", True)
 82
 83    rows = simpledialog.askinteger(title='Number of Rows',    prompt='Number of Rows:', parent=window)
 84    cols = simpledialog.askinteger(title='Number of Columns', prompt='Number of Columns:', parent=window)
 85
 86    entries = []
 87    for i in range(rows):
 88        eee = []
 89        for j in range(cols):
 90            e = tk.Entry(window, width=10)
 91            eee.append(e)
 92            e.grid(row=i, column=j)
 93            e.insert(tk.END, "")
 94        entries.append(eee)
 95    entries = np.array(entries)
 96
 97    b = tk.Button(window, text='Save', command=get_value)
 98    b.grid(row=i+1, column=0)
 99
100    b2 = tk.Button(window, text='Exit', command=window.destroy)
101    b2.grid(row=i+1, column=1)
102
103    window.mainloop()
104
105    return latex_table
def table_editor():
 12def table_editor():
 13    '''
 14        Opens a GUI with table editor that produces LaTeX code to display a table.
 15
 16        Parameters:  
 17        -----------
 18        - Nothing
 19
 20        Returns:  
 21        --------  
 22        - latex_table : str  
 23            LaTeX code to display a table  
 24
 25        Usage:
 26        ------
 27
 28        ```python
 29            latex_table = table_editor()
 30        ```
 31
 32        Output:
 33        -------
 34
 35        ```latex
 36        \\documentclass[11pt]{article}
 37        \\usepackage{booktabs}
 38        \\usepackage{graphicx}
 39
 40        \\begin{document}
 41
 42        \\begin{table}[!ht]
 43                \\centering
 44                \\caption{My table 1}\\label{tab:tab1}
 45                \\resizebox{\\columnwidth}{!}{
 46                \\begin{tabular}{ccc}
 47                        \\toprule
 48                            A     &     B     &     C     \\\\
 49                        \\midrule
 50                            0.1     &     0.2     &     0.3     \\\\
 51                            0.4     &     0.5     &     0.6     \\\\
 52                            0.7     &     0.8     &     0.9     \\\\
 53                            1.1     &     1.2     &     1.3     \\\\
 54                        \\bottomrule
 55                \\end{tabular}}
 56        \\end{table}
 57
 58        \\end{document}
 59        ```
 60    '''
 61
 62
 63    def get_value():
 64        data = []
 65        for i in range(rows):
 66            ddd = []
 67            for j in range(cols):
 68                try:
 69                    d = float(entries[i,j].get())
 70                except Exception:
 71                    d = str(entries[i,j].get())
 72                ddd.append(d)
 73            data.append(ddd)
 74        
 75        data = np.array(data)
 76        columns = data[0,:]
 77
 78        global latex_table
 79        latex_table = table(columns_name=columns, data=data[1:,:])
 80    
 81    window = tk.Tk()
 82    window.title('Table Editor')
 83    window.attributes("-zoomed", True)
 84
 85    rows = simpledialog.askinteger(title='Number of Rows',    prompt='Number of Rows:', parent=window)
 86    cols = simpledialog.askinteger(title='Number of Columns', prompt='Number of Columns:', parent=window)
 87
 88    entries = []
 89    for i in range(rows):
 90        eee = []
 91        for j in range(cols):
 92            e = tk.Entry(window, width=10)
 93            eee.append(e)
 94            e.grid(row=i, column=j)
 95            e.insert(tk.END, "")
 96        entries.append(eee)
 97    entries = np.array(entries)
 98
 99    b = tk.Button(window, text='Save', command=get_value)
100    b.grid(row=i+1, column=0)
101
102    b2 = tk.Button(window, text='Exit', command=window.destroy)
103    b2.grid(row=i+1, column=1)
104
105    window.mainloop()
106
107    return latex_table

Opens a GUI with table editor that produces LaTeX code to display a table.

Parameters:

  • Nothing

Returns:

  • latex_table : str
    LaTeX code to display a table

Usage:

    latex_table = table_editor()

Output:

\documentclass[11pt]{article}
\usepackage{booktabs}
\usepackage{graphicx}

\begin{document}

\begin{table}[!ht]
        \centering
        \caption{My table 1}\label{tab:tab1}
        \resizebox{\columnwidth}{!}{
        \begin{tabular}{ccc}
                \toprule
                    A     &     B     &     C     \\
                \midrule
                    0.1     &     0.2     &     0.3     \\
                    0.4     &     0.5     &     0.6     \\
                    0.7     &     0.8     &     0.9     \\
                    1.1     &     1.2     &     1.3     \\
                \bottomrule
        \end{tabular}}
\end{table}

\end{document}