Skip to content

Conversation

@monchier
Copy link
Collaborator

@monchier monchier commented Oct 21, 2019

Issue: #301

Description: In Windows, we cannot rely on NamedTemporaryFile removing the temporary file that we use for the temporary python script on running from url. This PR adds a custom TemporaryDirectory context manager to use with remote files.

The TemporaryDirectory context manager remove the content of the temporary directory when exits.


Contribution License Agreement

By submiting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is getting quite big now... can you pull it into a utility function?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I am going to change this.

@monchier monchier added the WIP label Oct 22, 2019
@monchier
Copy link
Collaborator Author

I change the solution quite a bit. Will change title and description for this PR.

@monchier monchier changed the title Use NamedTemporaryFile with delete=False in cli.py Use custom context manager for temporary file in cli.py Oct 23, 2019
@monchier monchier removed the WIP label Oct 24, 2019
from streamlit.temporary_directory import TemporaryDirectory

with TemporaryDirectory() as temp_dir:
script_path = os.path.join(temp_dir, os.path.basename(file_or_url))
Copy link
Contributor

@tvst tvst Oct 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use basename with URLs. It's meant to be used with filesystem paths, so on Windows it will use backslashes. Also, it doesn't work when the URL ends in a slash.

Use something like urllib.parse.urlparse() instead.

Copy link
Contributor

@tvst tvst Oct 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, to make sure this doesn't break in the future, can you add a test for main_run?

If tests don't work with click, you can always pull the entire body of main_run into another function called _main_run_real and test that:

@stuff
@stuff
def main_run(...):
  _main_run_real(...)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have tests for this. I definitely had to update them :-)

with TemporaryDirectory() as temp_dir:
from urllib.parse import urlparse
path = urlparse(file_or_url).path
script_path = os.path.join(temp_dir, path.rsplit('/', 1)[-1])
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure there is a better way of doing this. This way seems "StackOverflow" accepted: https://stackoverflow.com/questions/7253803/how-to-get-everything-after-last-slash-in-a-url

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should do path.strip('/') first to remove the final slash, if any.

@monchier
Copy link
Collaborator Author

Updated after @tvst 's comments.

with TemporaryDirectory() as temp_dir:
from urllib.parse import urlparse
path = urlparse(file_or_url).path
script_path = os.path.join(temp_dir, path.rsplit('/', 1)[-1])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should do path.strip('/') first to remove the final slash, if any.

@monchier monchier merged commit b807219 into streamlit:develop Oct 28, 2019
tconkling added a commit to tconkling/streamlit that referenced this pull request Oct 29, 2019
* develop:
  Use custom context manager for temporary file in cli.py (streamlit#489)
  Update pull_request_template.md
  Update pull_request_template.md
  Handle lack of trailing slash (streamlit#528)
  Set auto_envvar_prefix to STREAMLIT (streamlit#477)
  Strip slashes in path components (streamlit#523)
  Create doc_improvement.md
  ESlint: allow @ts-ignore (streamlit#499)
  Update Pipfile (streamlit#505)
  Release 0.49.0 (streamlit#509)
  Removing package json (streamlit#501)
  Feature/input number (streamlit#416)
  ESLint warnings are fatal in Circle (streamlit#492)
  Doc updates (streamlit#286)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants