ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 25 Aug 2020 03:58:23 +0200export sage file as pythonhttps://ask.sagemath.org/question/53148/export-sage-file-as-python/I have a CoCalc notebook running a SageMath program. What I have runs under SageMath 9.1. Someone wants to include my stuff in a Python work environment. So the first step is to convert it to Python. I tried 'preparse', but that doesn't seem to work from the terminal window.
Is there a working automatic translation to help with this conversion?Sun, 23 Aug 2020 23:30:15 +0200https://ask.sagemath.org/question/53148/export-sage-file-as-python/Comment by slelievre for <p>I have a CoCalc notebook running a SageMath program. What I have runs under SageMath 9.1. Someone wants to include my stuff in a Python work environment. So the first step is to convert it to Python. I tried 'preparse', but that doesn't seem to work from the terminal window. </p>
<p>Is there a working automatic translation to help with this conversion?</p>
https://ask.sagemath.org/question/53148/export-sage-file-as-python/?comment=53153#post-id-53153In CoCalc, are you using a `.sagews` document or a `.ipynb` document?
Is it a document you can share (eg via CoCalc's share functionality)?
Otherwise can you share a simplified document that illustrates the question?Mon, 24 Aug 2020 05:38:55 +0200https://ask.sagemath.org/question/53148/export-sage-file-as-python/?comment=53153#post-id-53153Comment by Larry Moss for <p>I have a CoCalc notebook running a SageMath program. What I have runs under SageMath 9.1. Someone wants to include my stuff in a Python work environment. So the first step is to convert it to Python. I tried 'preparse', but that doesn't seem to work from the terminal window. </p>
<p>Is there a working automatic translation to help with this conversion?</p>
https://ask.sagemath.org/question/53148/export-sage-file-as-python/?comment=53160#post-id-53160Thanks for engaging with this. It's a .py document, but I am not sure why it's listed that way: I wrote it in Sage several years ago, and I think it originally was a .sagews file.
I did share the file with you just now.
Among the issues that I'm having:
(1) Declarations like
var('skunks', 'rabbits', 'quadrupeds', 'deer', 'pests',
'beautiful_creatures', 'ugly_creatures', 'birds', 'bats', 'horses',
'ducks', 'pos', 'neg')
(2) the use of 'exists' and 'forall' in dealing with lists
(3) the use of graph operations like making graphs with DiGraph, and also matrix opearations that are native here that might require a package in Python.Tue, 25 Aug 2020 00:20:00 +0200https://ask.sagemath.org/question/53148/export-sage-file-as-python/?comment=53160#post-id-53160Answer by slelievre for <p>I have a CoCalc notebook running a SageMath program. What I have runs under SageMath 9.1. Someone wants to include my stuff in a Python work environment. So the first step is to convert it to Python. I tried 'preparse', but that doesn't seem to work from the terminal window. </p>
<p>Is there a working automatic translation to help with this conversion?</p>
https://ask.sagemath.org/question/53148/export-sage-file-as-python/?answer=53162#post-id-53162To sum up the question: we have a file `myfile.sage` and the question is to turn it into a `myfile.py` file.
## Automatic
The automatic way is to run in the terminal:
$ sage --preparse myfile.sage
which will produce a file `myfile.sage.py` that is a preparsed version of the original file.
In terms of imports, it adds the following import:
from sage.all_cmdline import * # import sage library
## Manual
Import, load, run your code and fix each problem as it comes.
Regarding missing imports, use the `import_statements` function.
sage: import_statements(var)
from sage.calculus.var import var
sage: import_statements(exists)
from sage.misc.misc import exists
sage: import_statements(forall)
from sage.misc.misc import forall
sage: import_statements(DiGraph)
from sage.graphs.digraph import DiGraph
This will give minimal imports that make the code run in Python (with access to the Sage library).
If you want to remove access to the Sage library, it's different.
Look into `all` and `any` to replace use of `forall` and `exists`.
Regarding `var`, SymPy certainly has similar functionality,
and would be lighter than Sage.
Regarding graphs, see what graph libraries have the functionality you need.Tue, 25 Aug 2020 02:17:18 +0200https://ask.sagemath.org/question/53148/export-sage-file-as-python/?answer=53162#post-id-53162Comment by Larry Moss for <p>To sum up the question: we have a file <code>myfile.sage</code> and the question is to turn it into a <code>myfile.py</code> file.</p>
<h2>Automatic</h2>
<p>The automatic way is to run in the terminal:</p>
<pre><code>$ sage --preparse myfile.sage
</code></pre>
<p>which will produce a file <code>myfile.sage.py</code> that is a preparsed version of the original file.</p>
<p>In terms of imports, it adds the following import:</p>
<pre><code>from sage.all_cmdline import * # import sage library
</code></pre>
<h2>Manual</h2>
<p>Import, load, run your code and fix each problem as it comes.</p>
<p>Regarding missing imports, use the <code>import_statements</code> function.</p>
<pre><code>sage: import_statements(var)
from sage.calculus.var import var
sage: import_statements(exists)
from sage.misc.misc import exists
sage: import_statements(forall)
from sage.misc.misc import forall
sage: import_statements(DiGraph)
from sage.graphs.digraph import DiGraph
</code></pre>
<p>This will give minimal imports that make the code run in Python (with access to the Sage library).</p>
<p>If you want to remove access to the Sage library, it's different.</p>
<p>Look into <code>all</code> and <code>any</code> to replace use of <code>forall</code> and <code>exists</code>.</p>
<p>Regarding <code>var</code>, SymPy certainly has similar functionality,
and would be lighter than Sage.</p>
<p>Regarding graphs, see what graph libraries have the functionality you need.</p>
https://ask.sagemath.org/question/53148/export-sage-file-as-python/?comment=53163#post-id-53163Thanks very much for all of this! This should go a long way towards doing what I need to do here.Tue, 25 Aug 2020 03:58:23 +0200https://ask.sagemath.org/question/53148/export-sage-file-as-python/?comment=53163#post-id-53163