Welcome to the Pythonista Community Forums!

Pythonista is a Python programming environment for iOS. To learn more, head over to the Pythonista Website.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Twitter
Using the Dropbox Module
  • Version 1.2 of Pythonista includes the official Dropbox Python SDK as a built-in module. Because of the way OAuth works, using it can be a little daunting at first, so I've created a minimal example that uses the webbrowser module for logging in and the keychain module for storing the credentials.

    The script then just shows some information about the logged-in account, but once you're there, downloading and uploading files is relatively easy.

    If you make sure that the script's name contains no spaces, you can also import it from other scripts and use the get_client function from there, so that you don't have to duplicate all that login boilerplate code everywhere you want to use the dropbox module.

    https://gist.github.com/4034526

    Please note that you need to create a Dropbox app in order to get this to work. Go to
    http://dropbox.com/developers/apps
    to create one.
  • So I used your login code as the basis for a file syncronization script:
    https://gist.github.com/4049625

    Your script 'dropboxlogin' has to be installed to get the authentication. It's intended to work with an app folder as the first step it takes if if doesn't know anything is to download everything! And then upload everything. After the first run it stores the local state in a hidden file named '.dropbox_state' and should only download/upload/delete when something changes

    If something breaks the best thing to do is start from scratch by removing the persistent state file:
    > os.remove('.dropbox_state')
  • Sweet! I was hoping somebody would build this. :)
  • Awesome, now I can be way more productive testing code. Thanks wrenoud.
  • wrenoud, any chance you could add an ignore list for files or directories? For example, hidden source control directories.
  • @jjlawren specifically to prevent uploading to dropbox?
  • I think the more likely use case is to prevent downloading from Dropbox.

    For example: you could have a Pythonista folder in your Dropbox, managed on your desktop using git, and have it (almost) automatically synced to your devices via Dropbox and this script. But there's no need to download the hidden .git files/folders, as they'd serve no purpose within Pythonista... so it would be nice to be able to set parameters in the script telling it to ignore them and not download them.
  • What is an app_key ( for to set in the source) ?
  • @PKHG I believe it is the API key you get from this link: http://dropbox.com/developers/apps
  • Thanks, will have a look ;-)
  • @wrenoud Any chance you will update your script to also support the entire folder? I can't figure out what lines to change to not download my entire Dropbox first.
  • @wrenoud I second Zettt's question!

    Any chance of an update to your sync script to include an optional subfolder variable, so the sync doesn't happen with the root of your Dropbox but with a specified path?

    I've been trying to work out how to alter it myself, but I'm struggling! :-S
  • Update: Er, for anyone else wondering the same thing, I thought I'd come back here to say that it's actually very easy to get this script syncing with a single folder. Register your app as a specific folder rather than the full access, and it's automatic! (The sync folder will end up inside the 'Apps' folder...)

    It's also pretty straightforward to have two Dropbox apps setup, one for syncing everything in Pythonista with a specific folder, and one with full dropbox access (which I wanted for other purposes).
  • I've added editor.reload_files() to the script because when it creates files in pythonista my list qas not being updated unless I've ordered again or closed and opened the app.

    https://gist.github.com/fernando82/5225002
  • I'm trying to run the script dropboxlogin - after creating the Dropbox app, getting the key and secret, setting these in the script and the access type to 'dropbox' - but when I run the script I get the error
    "Invalid key (.......). Check your app's configuration to make sure everything. Is correct".
    In this message the key string is correct. I am sure I am missing something but help would be appreciated.
  • @johnbenallen Make sure that the app you've configured on the Dropbox developer page is also set to "full access" (and not app folder), that's the only thing I can think of right now.
  • On the App Info page it says that access type is "Full Dropbox'", so should be ok.

    Another thought: when I created the app I took the option "core API". Should I have selected "sync API"? This choice is not visible in the App Info page.
  • No, core API is right, not sure what else could have gone wrong, if you're certain that you copied the key and secret strings correctly.
  • Thanks for the script, it works fine. The only annoying thing was that it syncs everything, including .git/ folders and the like.

    This is my solution to exclude a folder from syncing:
    1. move the .git folder out of the Dropbox sync folder
    2. create an empty .git folder in the old location
    3. uncheck this empty folder in the selective sync list in the Dropbox preferences. It will be deleted locally (that's why we moved it in step 1)
    4. move back the folder to it's previous location. Dropbox will now ignore it.
  • This worked perfectly. Thanks!!!