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

__file__ global variable in script execution #212

Merged
merged 1 commit into from May 14, 2017

Conversation

Projects
None yet
4 participants
@Titan-C
Member

Titan-C commented Mar 14, 2017

Fix #211
@amueller does this work for you?

@amueller

This comment has been minimized.

Contributor

amueller commented Mar 14, 2017

yeah that's the fix I did locally, thanks :)

@lesteve

This comment has been minimized.

Contributor

lesteve commented Mar 15, 2017

There was already a PR like this and it was reverted because __file__ does not exist in a Jupyter notebook, see #166.

@amueller

This comment has been minimized.

Contributor

amueller commented Mar 15, 2017

I'm ok with not supporting it as long as there is an alternative for my use-case ;)
I want to load a file that's stored with the example. How do I get the path?

@GaelVaroquaux

This comment has been minimized.

Contributor

GaelVaroquaux commented Mar 15, 2017

@amueller

This comment has been minimized.

Contributor

amueller commented Mar 15, 2017

but that doesn't work when just running the example script if you execute it from another folder, right? [Actually I'm not sure I understand your suggestion]

@GaelVaroquaux

This comment has been minimized.

Contributor

GaelVaroquaux commented Mar 15, 2017

@amueller

This comment has been minimized.

Contributor

amueller commented Mar 15, 2017

It seems the best solution would be for me to check if __file__ exists, and if so, use it, and otherwise assume we cwd'ed?

@amueller

This comment has been minimized.

Contributor

amueller commented Mar 15, 2017

though if I rely on a local file, how is the notebook download gonna be helpful? Neither of the downloads will work by themselves, right?

@amueller

This comment has been minimized.

Contributor

amueller commented Mar 15, 2017

But I'm generally not sold on the downloads, you can just copy and paste the script, which is usually much easier than opening a downloaded notebook. You can't open a notebook from the browser, you need to navigate to the download folder and start a server there....

@GaelVaroquaux

This comment has been minimized.

Contributor

GaelVaroquaux commented Mar 15, 2017

@GaelVaroquaux

This comment has been minimized.

Contributor

GaelVaroquaux commented Mar 15, 2017

@amueller

This comment has been minimized.

Contributor

amueller commented Mar 15, 2017

I can assume that if I gain something from it ;) And I mind it because it will mean many more open issues on my issue tracker.

As I said above, the downloads actually don't include the file that I'm trying to load, so if I'm using __file__ the notebook will be invariably broken - and the .py download too. For my use-case I probably should just remove the download buttons as there is no way to download a working example.

So there are two possible solutions: You can make sphinx-gallery cwd and I can add code to check if __file__ exists and otherwise assume it's in the right directory.
Or you can add __file__ to the globals.
Both solutions result in running the script locally to work, but the downloads not working. Only adding __file__ makes the examples slightly easier.
I'm ok with either solution but I don't see a strong argument with the notebook perspective.

@GaelVaroquaux

This comment has been minimized.

Contributor

GaelVaroquaux commented Mar 15, 2017

@amueller

This comment has been minimized.

Contributor

amueller commented Mar 15, 2017

Alright. But the notebooks they create will probably be still as broken, as mine will be, even without relying on __file__.

@GaelVaroquaux

This comment has been minimized.

Contributor

GaelVaroquaux commented Mar 15, 2017

@amueller

This comment has been minimized.

Contributor

amueller commented Mar 15, 2017

yeah

@Titan-C

This comment has been minimized.

Member

Titan-C commented Mar 15, 2017

@Titan-C Titan-C force-pushed the Titan-C:file_var branch from 34159cf to 79453b0 Apr 1, 2017

@Titan-C

This comment has been minimized.

Member

Titan-C commented Apr 1, 2017

I'm fixing this issue with documentation that os.getcwd should be used

@Titan-C Titan-C force-pushed the Titan-C:file_var branch from 79453b0 to a2bd592 Apr 1, 2017

@amueller

This comment has been minimized.

Contributor

amueller commented Apr 2, 2017

Ok so if I want my example to be able to run from the command line (from another folder) I need to do

path = os.path.basename(__file__)  if "__file__" in locals() else os.getcwd()

I don't like it that I have to change my very simple example code for sphinx-gallery to work, but I guess that's how it's gonna go.

@Titan-C Titan-C force-pushed the Titan-C:file_var branch from a2bd592 to 4718997 Apr 16, 2017

@Titan-C Titan-C force-pushed the Titan-C:file_var branch from 4718997 to e636cd1 May 14, 2017

@Titan-C Titan-C merged commit 8a6e6ee into sphinx-gallery:master May 14, 2017

3 checks passed

ci/circleci Your tests passed on CircleCI!
Details
continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details

@Titan-C Titan-C deleted the Titan-C:file_var branch May 14, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment