summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex
diff options
context:
space:
mode:
authorKarl Berry <karl@freefriends.org>2022-05-07 20:39:53 +0000
committerKarl Berry <karl@freefriends.org>2022-05-07 20:39:53 +0000
commit66cf44c488603214d7931e3d90f74717a8a36d2c (patch)
tree92f4faae737f11ddf67c3a4af3e132eeda029210 /Master/texmf-dist/tex/latex
parent500ad93a0613967b960c8b732851c7135de2d440 (diff)
runcode (7may22)
git-svn-id: svn://tug.org/texlive/trunk@63252 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/texmf-dist/tex/latex')
-rw-r--r--Master/texmf-dist/tex/latex/runcode/runcode.sty75
1 files changed, 75 insertions, 0 deletions
diff --git a/Master/texmf-dist/tex/latex/runcode/runcode.sty b/Master/texmf-dist/tex/latex/runcode/runcode.sty
index 6737fd213f6..187bd77b0a2 100644
--- a/Master/texmf-dist/tex/latex/runcode/runcode.sty
+++ b/Master/texmf-dist/tex/latex/runcode/runcode.sty
@@ -79,12 +79,33 @@
\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.
\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