7

I'm trying to access a VTK file where the solution to the heat equation is saved, but I've no idea where it's saved in Colab.

from fenics import *
import time
T = 2.0            # final time
num_steps = 50     # number of time steps
dt = T / num_steps # time step size
# Create mesh and define function space
nx = ny = 30
mesh = RectangleMesh(Point(-2, -2), Point(2, 2), nx, ny)
V = FunctionSpace(mesh, 'P', 1)
# Define boundary condition
def boundary(x, on_boundary):
    return on_boundary
bc = DirichletBC(V, Constant(0), boundary)
# Define initial value
u_0 = Expression('exp(-a*pow(x[0], 2) - a*pow(x[1], 2))',
                 degree=2, a=5)
u_n = interpolate(u_0, V)
# Define variational problem
u = TrialFunction(V)
v = TestFunction(V)
f = Constant(0)
F = u*v*dx + dt*dot(grad(u), grad(v))*dx - (u_n + dt*f)*v*dx
a, L = lhs(F), rhs(F)
# Create VTK file for saving solution
vtkfile = File('heat_gaussian/solution.pvd')
# Time-stepping
u = Function(V)
t=0
for n in range(num_steps):
    # Update current time
    t += dt
    # Compute solution
    solve(a == L, u, bc)
    # Save to file and plot solution
    vtkfile << (u, t)
    plot(u)
    # Update previous solution
    u_n.assign(u)
# Hold plot
#interactive()

I've tried;

from google.colab import files
plt.savefig("vtkfile")
files.download("vtkfile")

And

from google.colab import files files.upload()
from google.colab import drive drive.mount('vtkfile')

But still getting errors. Where are files created in the notebook stored?

Mo Houshmand
  • 115
  • 1
  • 2
  • 12

7 Answers7

13

On the left side of colab interface, there is a "Files" tab. You can find all the files you saved there.

Jules Cui
  • 141
  • 1
  • 5
8

Many answers here are focusing on where you can see the files visually in the Colab UI.

Physically the files are stored in the Colab Hosted VM. When you start an instance of your notebook, Google spins up a dedicated and temporary VM, in which your Jupyter notebook runs. This is where your notebook is stored and executed, and thus where any files in your code are read from and written to. When this notebook eventually times out (you will get a "Runtime Disconnected" message), the VM is destroyed, along with any files you created. As the other answers indicate, as long as the VM is running, you can see the files residing on this VM using the UI:

Colab Files

Any external files that your code needs to read must first be uploaded to the VM. Similarly, if you want to keep any files created by your code, you have to download them locally from the VM before they are destroyed. You can use the Files area to manually upload and download files to and from the VM.

You can do this in code using google.colab.files:

from google.colab import files

# Upload a file from local PC to your Colab VM
files.upload('mylocalfile.txt')

# Download a file from your Colab VM to local PC
files.download('mylocalfile.txt')

You can also have the VM access files on your Google Drive directly by mounting the drive to the Colab VM, using google.colab.drive:

from google.colab import drive
drive.mount('/content/drive') # Mount your Google Drive to the local /content/drive directory
with open('/content/drive/My Drive/foo.txt', 'w') as f:
  # read / write like any VM-local file
  f.write('Hello Google Drive!')

See the official Colab Documentation on the Local file system for more details and examples.

Btw a Hosted VM is not the only option:

Colab VM options

You can also connect to a local runtime, in which case the files would be local to your machine as with a local Jupyter instance, or a GCE (Google Compute Engine) VM, which is available for rent separately and allows you to use a substantially more powerful VM that does not timeout and is under your full control.

Steven
  • 1,733
  • 2
  • 16
  • 30
3

Complementing @jules-cui answer, on the left side of the Colab interface you will see a few icons. Click on the folder icon, which opens all the files in your runtime. You can click on any of the files' extended menu to the right, and click Download.

glicerico
  • 690
  • 4
  • 20
3

If you accidentally clicked on a folder and it opened and you have no idea where the old folders went and there doesn't seem to be a way to go back, check the content folder - it may have what you're looking for.

Pro Q
  • 4,391
  • 4
  • 43
  • 92
2

If you mounted GDrive then files should be stored in the folder named Colab Notebooks

You could also check your current folder with one of the commands below.

%cd

or

!pwd 
BI Dude
  • 1,842
  • 5
  • 37
  • 67
1

Initially when you click on the files symbol on left side you can find all the files that are uploaded by you, if you unknowingly came out of that directory you can find the files in the content folder within the files tab. All files that are uploaded and even those that are saved by you will be in the content folder from their you can even download the files into the machine by clicking on the three dots that appear when you hover over the file name.

0

To complement previous answers, if you want to save your code as a .py file and then download it: %%writefile your_file.py

For example:

%%writefile test.py

print('Hello World!')

If you want to see if it works: !python test.py

Output: Hello World!

To download the file, go to the temp folder (left side), there you fill find it.

biodotpe
  • 1
  • 2