Add Files to your Python Trinkets

We are very excited to announce a new feature that allows you to add files to your Python trinkets. Many users have additional modules they’ve written to import, data files they would like to read in and manipulate, and other types of files that make really powerful programs. Before today you would need to copy and paste the contents of those files into your trinket and use them directly. Today that’s all changed with a new tab interface that lets you create new files and separate out your main program from other content.

Let’s take a look at a simple example that creates a module for calculating the Fibonacci series and imports it.

You’ll see two tabs: main.py and fibo.py. Click on the fibo.py tab to see the implementation of the methods used for doing the calculation. By putting methods into modules like this your students can focus on using the methods you provide yet they can still see how they were written. This also teaches best practices by keeping separate concerns in their own files. Remember: to make the methods from your additional files available to your main program you’ll use the import filename (without the .py).

You can also use the built-in open method to read in contents from any tab. Here’s another example that reads in a simple JSON file, parses the content and uses Pygal, a charting library, to display the results.

To add another file simply click the plus icon to the right of your main.py file and you’ll be prompted to name the file. If you’re writing a module to import you’ll need to follow Python conventions for naming your file.

Screenshot 2015-01-09 at 9.21.21 AM

You’ll be able to rename files or delete them if they’re no longer needed by selecting a tab and clicking on the gear icon.

Screenshot 2015-01-09 at 9.25.16 AM

We’re looking forward to seeing how this improvement changes your use of trinkets and can’t wait to see some of the cool programs you and your students create. Share your new programs with us on Twitter at @trinketapp or email us at hello@trinket.io. Of course we’d love your feedback on this update and the next thing you need to create ever more powerful trinkets.

4 thoughts on “Add Files to your Python Trinkets

  1. Hi, I was wondering if it was possible to include programmatically create the two separate tabs inside the app? Consider the scenario where I have written a wrapper for a builtin module that is not implemented in the trinkets app. It would be great if I could separate out the 30 or so lines of the wrapper code from the main function. Thanks

    1. Hello! Yes, I believe what you’re describing is possible. In the case where the wrapper code was in its own module you’s need to import the wrapper module directly from main.py. Does that help?

Comments are closed.