texlive[63252] Master/texmf-dist: runcode (7may22)

commits+karl at tug.org commits+karl at tug.org
Sat May 7 22:39:54 CEST 2022


Revision: 63252
          http://tug.org/svn/texlive?view=revision&revision=63252
Author:   karl
Date:     2022-05-07 22:39:53 +0200 (Sat, 07 May 2022)
Log Message:
-----------
runcode (7may22)

Modified Paths:
--------------
    trunk/Master/texmf-dist/doc/latex/runcode/README
    trunk/Master/texmf-dist/doc/latex/runcode/runcode.pdf
    trunk/Master/texmf-dist/doc/latex/runcode/runcode.tex
    trunk/Master/texmf-dist/tex/latex/runcode/runcode.sty

Modified: trunk/Master/texmf-dist/doc/latex/runcode/README
===================================================================
--- trunk/Master/texmf-dist/doc/latex/runcode/README	2022-05-07 20:39:40 UTC (rev 63251)
+++ trunk/Master/texmf-dist/doc/latex/runcode/README	2022-05-07 20:39:53 UTC (rev 63252)
@@ -1,11 +1,11 @@
-LaTeX Package: runcode 2020/10/04 v1.0
+LaTeX Package: runcode 2022/05/03 v1.2
 ----------------------------------------
 The runcode package enables the execution of source code (e.g., R, 
-Julia, Matlab, shell, etc.) and embed the results in the pdf file
+Julia, Matlab, shell, Python, etc.) and embed the results in the pdf file
 when compiling the LaTeX file. To use this package the shell-escape 
 option must be enabled.
 
-Copyright (C) 2020
+Copyright (C) 2020-2022
 Haim Bar and HaiYing Wang https://github.com/Ossifragus/runcode
 
 Files:

Modified: trunk/Master/texmf-dist/doc/latex/runcode/runcode.pdf
===================================================================
(Binary files differ)

Modified: trunk/Master/texmf-dist/doc/latex/runcode/runcode.tex
===================================================================
--- trunk/Master/texmf-dist/doc/latex/runcode/runcode.tex	2022-05-07 20:39:40 UTC (rev 63251)
+++ trunk/Master/texmf-dist/doc/latex/runcode/runcode.tex	2022-05-07 20:39:53 UTC (rev 63252)
@@ -15,9 +15,9 @@
 \documentclass{ltxdoc}
 \usepackage[hyphens]{url}
 %\usepackage{runcode}
-\ProvidesPackage{runcode}[2021/04/17 runcode v1.1]
+\ProvidesPackage{runcode}[2022/05/03 runcode v1.1]
 \begin{document}
-    \title{The \textsf{runcode} package\thanks{This document  corresponds to \textsf{runcode}~v1.1, dated~2021/04/17.}}
+    \title{The \textsf{runcode} package\thanks{This document  corresponds to \textsf{runcode}~v1.2, dated~2022/05/03.}}
     \author{Haim Bar and HaiYing Wang \\ \texttt{haim.bar at uconn.edu}, \texttt{haiying.wang at uconn.edu}}
     \maketitle
 
@@ -141,8 +141,32 @@
 \end{itemize}
 
 
+\bigskip
+\noindent \verb|\runPython[Arg1]{Arg2}{Arg3}[Arg4]| runs an external Python code file.
+\begin{itemize}
+\item \texttt{Arg1} is optional and uses \textit{talk2stat]}'s Python server by default.
+\item \texttt{Arg2}, \texttt{Arg3}, and \texttt{Arg4} have the same effects as those of the basic command \verb|\runExtCode|. 
+\end{itemize}
+
+\noindent \verb|\inlnPython[Arg1]{Arg2}[Arg3]| runs Python source code (\texttt{Arg2}) and displays the output in line.
+\begin{itemize}
+\item \texttt{Arg1} is optional and uses the Python server by default.
+\item \texttt{Arg2} is the R source code to run. If the Python  source code is wrapped between \verb|```| on both sides (as in the markdown grammar), then it will be implemented directly; otherwise the code will be written to a file on the disk and then be called.
+\item \texttt{Arg3} has the same effect as that of the basic command  \verb|\inln|.
+\end{itemize}
+
+\noindent \verb|\runPythonBatch[Arg1][Arg2]| runs an external Python source code (\texttt{Arg1}) in batch mode (without a server running). Python (at least currently), unlike the other languages we use, does not have an option to save and restore a session, which means that once a Python session ends, the working environment (variable, functions) is deleted. In order to allow a batch-mode in Python, we implemented such capability. It requires the dill module (https://pypi.org/project/dill/) module, which has to be installed via  `pip3 install dill`.
+\begin{itemize}
+\item \texttt{Arg1} is the Python source code to run.
+\item \texttt{Arg2} is the output file name.
+\end{itemize}
+
+ 
+
+
 \section{Revisions}
 \begin{itemize}
+\item v1.2, May 3, 2022: Added python options (server and batch).
 \item v1.1, April 17, 2021: Added a nohup option; improved error handling (missing code files, zero bytes in output files.)
 \end{itemize}
 

Modified: trunk/Master/texmf-dist/tex/latex/runcode/runcode.sty
===================================================================
--- trunk/Master/texmf-dist/tex/latex/runcode/runcode.sty	2022-05-07 20:39:40 UTC (rev 63251)
+++ trunk/Master/texmf-dist/tex/latex/runcode/runcode.sty	2022-05-07 20:39:53 UTC (rev 63252)
@@ -79,6 +79,26 @@
   \fi  
 }
 
+\DeclareOption{python}{
+  % create a configuration file for Python server if it does not exist.
+  \IfFileExists{python.config}{}{
+    \newwrite\tempfile
+    \immediate\openout\tempfile=python.config
+    \immediate\write\tempfile{[SERVERCONFIG]}
+    \immediate\write\tempfile{PORT = 65433}
+    \immediate\write\tempfile{DEBUGFILE = pythondebug.txt}
+    \immediate\write\tempfile{PIPETIMEOUT = 300}
+    \immediate\closeout\tempfile
+  }
+  % Start the server. Need to run just once. Can comment out after the first
+  % compilation, but remember to terminate the server
+  \ifnotnohup
+  {\immediate\write18{python3 -c 'from talk2stat.talk2stat import server,client; server("./","python") if not client("./","python","``` ```") else print("server is already running")'}}
+  \else
+  {\immediate\write18{nohup python3 -c 'from talk2stat.talk2stat import server,client; server("./","python") if not client("./","python","``` ```") else print("server is already running")' &}}
+  \fi  
+}
+
 \DeclareOption{stopserver}{
   \AtEndDocument{
   %% stop the server when the pdf compilation is done.
@@ -85,6 +105,7 @@
     \immediate\write18{python3 -c 'from talk2stat.talk2stat import client; client("./","R","QUIT")'}
     \immediate\write18{python3 -c 'from talk2stat.talk2stat import client; client("./","julia","QUIT")'}
     \immediate\write18{python3 -c 'from talk2stat.talk2stat import client; client("./","matlab","QUIT")'}
+    \immediate\write18{python3 -c 'from talk2stat.talk2stat import client; client("./","python","QUIT")'}
 }}
 
 \ProcessOptions*
@@ -294,4 +315,58 @@
 
 \NewDocumentCommand{\inlnMatlab}{O{python3 -c 'from talk2stat.talk2stat import client; client("./","matlab","\tmpname.txt")'} m O{inline}}{\IfBeginWith{#2}{```}{\inln{python3 -c 'from talk2stat.talk2stat import client;client("./","matlab","#2")'}{#2}[#3]}{\inln{#1}{#2}[#3]}}
 
+
+\NewDocumentCommand{\runPython}{O{python3 -c 'from talk2stat.talk2stat import client; client("./","python","#2")'} m m O{}}
+{ 
+  \def\runcmd{python3 -c 'from talk2stat.talk2stat import client; client("./","python","#2")'}
+  \ifstrequal{#1}{\runcmd}
+  {\runExtCode{#1}{}{#3}[#4]}
+  {\runExtCode{#1}{#2}{#3}[#4]}
+}
+
+\NewDocumentCommand{\inlnPython}{O{python3 -c 'from talk2stat.talk2stat import client; client("./","python","\tmpname.txt")'} m O{inline}}{\IfBeginWith{#2}{```}{\inln{python3 -c 'from talk2stat.talk2stat import client;client("./","python","#2")'}{#2}[#3]}{\inln{#1}{#2}[#3]}}
+
+
+%%%%%%%%
+% For Python batch mode, implement saving and restoring session
+% Arg #1 is the source file name,
+% Arg #2 is the output file name
+
+\NewDocumentCommand{\runPythonBatch}{m m}{%\-\\
+   \stepcounter{codeOutput}
+   \ifthenelse{\isempty{#2}} % set the output file name
+        { \setvalue{\tmpname}{tmp/\jobname_tmp\thecodeOutput.tex} }
+        { \setvalue{\tmpname}{tmp/#2.tex} }
+
+   \IfFileExists{#1}{
+     \ifruncode
+          \newwrite\tempfile
+          \immediate\openout\tempfile=tmp/\jobname_#1
+          \immediate\write\tempfile{import dill}
+          \immediate\write\tempfile{from os.path import exists}
+          \immediate\write\tempfile{if exists('tmp/session'):}
+          \immediate\write\tempfile{    dill.load_session('tmp/session')}
+          \immediate\write\tempfile{}
+          \newread\infile
+          \openin\infile=#1
+          \begingroup\endlinechar=-1
+          \loop\unless\ifeof\infile
+             \read\infile to\fileline  % Read one line and store it into \fileline
+             \immediate\write\tempfile{\unexpanded\expandafter{\fileline}}  % print the content to copy.txt
+          \repeat
+          \endgroup
+          \closein\infile
+          \immediate\write\tempfile{dill.dump_session('tmp/session')}
+          \immediate\write\tempfile{}
+          \immediate\closeout\tempfile
+          \immediate\write18{python3  tmp/\jobname_#1 > \tmpname}
+       \fi
+     }
+     {
+        \immediate\write18{cat /dev/null > \tmpname}
+        \textcolor{red}{\textbf{runPythonBatch: File #1 does not exist!}}
+     }
+}
+
+
 \endinput



More information about the tex-live-commits mailing list.