Get the currently printing filename in python

Hi There,

Could you please help me out how can I get the currently printing filename in python?

I read through the documentation and found some information but I can not get it work.

The closest that I have found is in the /home/pi/oprint/lib/python2.7/site-packages/octoprint/server/api/

def _getCurrentFile():
    currentJob = printer.get_current_job()
    if (
        currentJob is not None
        and "file" in currentJob
        and "path" in currentJob["file"]
        and "origin" in currentJob["file"]
        return currentJob["file"]["origin"], currentJob["file"]["path"]
        return None, None

When I am trying to make the imports like
import octoprint.filemanager

I get the error:
ImportError: No module named octoprint.filemanager

I am working on a RPI with installed and working octoprint.

You can only access the internal modules from OctoPrint from within a plugin (or OctoPrint itself). Are you writing a plugin?

Otherwise, you must go through the API.

Thanks for the quick Answer! I did not want to go into detail but I am working on a Timelapse setup which uses the full resolution of the new HQ camera of the pi because octolapse plugin has a max FullHD resolution setup currently.
Would be nice to make a plugin out of my codes, but I am too beginner to make it.
So what I did just wrote some python scripts (settings, move, shoot etc.) and I run them with the help of the GCODE System Command plugin.
Currently I am simply checking the lastly updated file and analyze its GCODE for my calculations, but it would be much better to be able to get the actually printed filename so if it was already uploaded before and I would like to re-print it then my program would analyse its GCODE and not the lastly updated one.

Some videos about the first moves and first Benchy if you are interested :slight_smile:

Plugin would definitely be easier since you have access to OctoPrint, otherwise go through the HTTP API.

You can't import & use OctoPrint modules in a separate process without starting the core of the server, which you obviously don't want to do, since OctoPrint is already running.

Thanks for the direction, now I know that I was in the wrong path. I am checking those options and trying to find out which one is easier/quicker to make.

I just need the filename which is currently chosen to print. I hoped that it would be some simple code with some given namespace-s. I did not know that it is so complicated.

Are there maybe a .json file or something where this filename is temporarily stored and I could read from that out?

Use the API. No, it's not written to disk, that would be inefficient. It is not complicated:

Python code:

import requests

APIKEY = "abcdef"

response = requests.get('', headers={'X-Api-Key': APIKEY})

print("The current file is {}".format(response.json()['job']['file']['name']))

You can experiment in your browser. Open the API URLs, to find the responses.

1 Like

Thank you very much! It works!

1 Like

Here is the result :slight_smile:

Shiny :grinning_face_with_smiling_eyes: