-
-
Notifications
You must be signed in to change notification settings - Fork 487
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
Add sage.misc.latex.pdf to save the image of objects to pdf #38339
Conversation
Documentation preview for this PR (built with commit 841298f; changes) is ready! 🎉 |
|
The report at https://groups.google.com/g/sage-support/c/YfyaZxc8Z6k has nothing to do with the viewer opening too fast. LaTeX failed to build the PDF in the first place. |
No. The PDF was built. This code # Return immediately but only clean up the temporary file after
# the viewer has closed. This function is synchronous and waits
# for the process to complete...
def run_viewer():
run([viewer, output_file], capture_output=True)
tmp.cleanup() does not work as the comment promised. This is what happens: The viewer "opens" (that is, the operating system successfully launched the viewer). Then We have to give the viewer enough time to really open the PDF file after it launched. There is no way to detect when the viewer has opened the file. 5 seconds seems enough. |
The error |
I don't know the reporter's system. He says "Latex does work though". So I guess that the message is from the viewer. |
By the way, I also experience this "no pdf file" symptom on my system (mac). Why I prepared this PR :-) |
He tells you his system. Like I said, you can run pdflatex yourself and see exactly where the error comes from. You're trying to fix something that isn't broken. There may be a problem on mac, but I can't test. You should probably figure out why the current approach doesn't work. Or just add a special case for mac if you don't feel like it. But don't make the situation worse for everyone else because nothing is broken on linux. |
I see what you mean. I cannot be sure on the reporter's case.
For me, the symptom is sporadic. If the viewer opens up the file quickly enough, no problem. Sometimes not. Maybe dependent on the pdf file size...
From the reporter's case, I guessed the symptom can also happen on linux, perhaps depending on the latex system or pdf viewer.
OK. No problem. I can wait for input from other people. |
how can this happen at all? |
Do you by any chance have an antivirus or some other spyware^H^H^^ thing like this installed on your mac? Often that's the case with employer-issued machines... |
No. I explained how it happens above: #38339 (comment) There is nothing strange why it happens. |
Do you mean it's a bug in CPython on macOS? Could be, of course, in how threads are handled, but this is not something that should be fixed by adding dodgy extra waiting times, unconditionally on the platform. Or, perhaps how run_viewer is launched: from threading import Thread
t = Thread(target=run_viewer)
t.daemon = True
t.start() does not work correctly on macOS? |
reading |
The daemon flag wasn't for synchronization per se. Without it, sage would refuse to exit while the viewer was still open; sage was waiting for the view/cleanup thread to finish. That page says,
Adding it does allow sage to quit while the viewer is open. |
it seems to be the default behaviour of
This way, |
This is the way e.g. |
Adding On the other hand, If I end sage session without closing |
Now I am using Dima's solution |
Anything else? The problem of not cleaning up the temporary file if sage session ends while the viewer is open persists, before and after the PR. This PR fixes only the problem on mac that the viewer sporadically crashes because the temporary file is cleaned up too soon. |
and of course, we get a new |
see #38339 (comment) |
I did. So? I followed the uncontroversial approach of adding |
Would it make sense to split the |
Why? In addition to that, this PR enables
There is no negative review about this. |
(a) It seems to often be a good idea for a PR to do one thing, not several independent things. Easier for reviewing, easier for merging. (b) In particular, the |
In practice, that argument is reasonable only when the PR is big, and developers often combine small things to the main thing in their PRs.
The "bug" has been in sage for years. I have lived with it. There is nothing urgent for such an old bug. I am not in hurry. The bug fix is a small thing of this PR. If anyone is in hurry and wants to merge it in quickly, please open a separate PR. It is just one-liner. I am not willing to do it at this moment.
Though DIma made me confused in #38339 (comment), I think there is no disagreement about that. The fix is not perfect on mac (because of how Preview works), but the situation gets better. The main thing of this PR is also small. Please review the main thing. |
in |
Right. Done. |
Let me elaborate on this. On mac, this happens when |
I experimented more. The temporary file is removed when sage is closed, even if the Preview app is still running. Perfect! |
I'd happy to give it a positive review. |
Thanks! |
…s to pdf <!-- ^ Please provide a concise and informative title. --> <!-- ^ Don't put issue numbers in the title, do this in the PR description below. --> <!-- ^ For example, instead of "Fixes sagemath#12345" use "Introduce new method to calculate 1 + 2". --> <!-- v Describe your changes below in detail. --> <!-- v Why is this change required? What problem does it solve? --> <!-- v If this PR resolves an open issue, please link to it here. For example, "Fixes sagemath#12345". --> Hence ```sage sage: from sage.misc.latex import pdf sage: pdf(pi, 'test.pdf') ``` works. This is a companion to the existing `sage.misc.latex.png`, which saves png image of objects. Of course, pdf image is scalable and hence more suitable for inclusion into latex documents. Also we fix it that `view()` sporadically fails on mac because the temporary file is removed before the viewer opens the file. For example, ```sage sage: view(pi) # show pi ``` Thus after this PR, the temporary file is removed when the viewer (`Preview` app on mac, not the window showing the image) closes. This behavior is consistent with that on linux. ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. - [x] I have linked a relevant issue or discussion. - [ ] I have created tests covering the changes. - [ ] I have updated the documentation and checked the documentation preview. ### ⌛ Dependencies <!-- List all open PRs that this PR logically depends on. For example, --> <!-- - sagemath#12345: short description why this is a dependency --> <!-- - sagemath#34567: ... --> URL: sagemath#38339 Reported by: Kwankyu Lee Reviewer(s): Dima Pasechnik
<!-- ^ Please provide a concise and informative title. --> <!-- ^ Don't put issue numbers in the title, do this in the PR description below. --> <!-- ^ For example, instead of "Fixes sagemath#12345" use "Introduce new method to calculate 1 + 2". --> <!-- v Describe your changes below in detail. --> <!-- v Why is this change required? What problem does it solve? --> <!-- v If this PR resolves an open issue, please link to it here. For example, "Fixes sagemath#12345". --> Fixes sagemath#38745 ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. - [x] I have linked a relevant issue or discussion. - [ ] I have created tests covering the changes. - [ ] I have updated the documentation and checked the documentation preview. ### ⌛ Dependencies <!-- List all open PRs that this PR logically depends on. For example, --> <!-- - sagemath#12345: short description why this is a dependency --> <!-- - sagemath#34567: ... --> sagemath#38339 URL: sagemath#38764 Reported by: Kwankyu Lee Reviewer(s): Travis Scrimshaw
<!-- ^ Please provide a concise and informative title. --> <!-- ^ Don't put issue numbers in the title, do this in the PR description below. --> <!-- ^ For example, instead of "Fixes sagemath#12345" use "Introduce new method to calculate 1 + 2". --> <!-- v Describe your changes below in detail. --> <!-- v Why is this change required? What problem does it solve? --> <!-- v If this PR resolves an open issue, please link to it here. For example, "Fixes sagemath#12345". --> Fixes sagemath#38745 ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. - [x] I have linked a relevant issue or discussion. - [ ] I have created tests covering the changes. - [ ] I have updated the documentation and checked the documentation preview. ### ⌛ Dependencies <!-- List all open PRs that this PR logically depends on. For example, --> <!-- - sagemath#12345: short description why this is a dependency --> <!-- - sagemath#34567: ... --> sagemath#38339 URL: sagemath#38764 Reported by: Kwankyu Lee Reviewer(s): Travis Scrimshaw
Hence
works. This is a companion to the existing
sage.misc.latex.png
, which saves png image of objects. Of course, pdf image is scalable and hence more suitable for inclusion into latex documents.Also we fix it that
view()
sporadically fails on mac because the temporary file is removed before the viewer opens the file. For example,Thus after this PR, the temporary file is removed when the viewer (
Preview
app on mac, not the window showing the image) closes. This behavior is consistent with that on linux.📝 Checklist
⌛ Dependencies