Scratchpad cannot find support file

Scratchpad cannot find support file

by Jose Osorio Azevedo -
Number of replies: 3

I have a Python question that reads a support file and uses the Ace interface. I converted it to the Scratchpad interface. The Verify button works OK, but the Run button in the Scratchpad box gives me a FileNotFoundError when I try to open the support file. Moodle and Jobe Server were updated recently.

How can I open a support file in the Scratchpad?


In reply to Jose Osorio Azevedo

Re: Scratchpad cannot find support file

by Richard Lobb -
Sorry, but the short answer is probably "You can't".

Support files are files that the question author loads into the Jobe server workspace in order to test the student's code. They don't exist in the student's environment (their browser). Conceptually at least, the Scratchpad is just a short circuit from the student's browser to the Jobe server, so the support files, which are provided by Moodle when running the job, are simply not available.

However, you can customise the Scratchpad UI to provide just about any runtime environment you like. See the CodeRunner UI documentation and the demo site. Whether it's worth the effort depends on what sort of support files you're talking about and how much effort you're prepared to go to. 

Here's a simple example. Suppose you ask the student a question like "Write code to open the file 'test.txt' and print its contents" and you want it to work in the scratchpad. Then you could proceed as follows:
  1. Create a new Python3 question.
  2. Click customise.
  3. Choose Scratchpad instead of Ace as the UI.
  4. Set the UI parameters to use globalextra as the wrapper source:
    Screenshot of UI parameters
  5. Set globalextra to consist of a bit of code that writes a file text1.txt before running the student's answer code (and/or the scratchpad contents). For example:
    screenshot of globalextra
  6. Create a file test1.txt containing the same as the file_contents from the above, and add that as a support file. This ensures that both the Check button and the Run button will have the file available.
  7. Configure the rest of the question fields, as usual. The expected output for the first test should be the file contents. And the Sample answer field might be
    Screenshot of sample answer

The Run button should now work and it should pass when Check is clicked, too.



A downside of the above - apart from the hassle of essentially defining the file twice - is that any errors in the student code will have confusing line numbers in the error message. You can avoid that by changing the wrapper to run the student code in a subprocess after setting up the file(s).

If the above doesn't fit with your use case, feel free to post back, explaining what sort of question you're trying to ask.





In reply to Richard Lobb

Re: Scratchpad cannot find support file

by Jose Osorio Azevedo -
I'm not familiar with customization or the CR architecture, so I'll take some time to go through the docs and your suggestions.
My course is an introduction to Data Science. I teach Pandas, and we use a lot of data files, none of them small. So I cannot create the files with code only. Is there a folder in the Jobe Server to which I could FTP the files?
Talking about architecture, the whole point of the Scratchpad is to encourage the student to create and perform his/her own tests, without losing tentatives. So, the data flow started by the Run button should be the same of the Verify button, and not a direct shortcut from the browser to the Jobe Server. While we don't fix this, I guess we will keep dealing with workarounds.
Congratulations for your system. My courses improved a lot with it. Thanks a lot for your support,
Jose Osorio, from Brazil
In reply to Jose Osorio Azevedo

Re: Scratchpad cannot find support file

by Richard Lobb -
The scratchpad UI was intended to provide a very simple mini-ide-like environment for beginner students before they move onto using real IDEs. I can see this isn't what you're after.

The Jobe server is a standard Linux server and you can put data files anywhere you like, giving students the path to them. But ... it should be firewalled to prevent access from anywhere other than the Moodle server, so you won't be able to FTP files onto it yourself. You'll have to get a sysadmin to upload files to it.

If you want to have specific files preloaded into the workspace, it sounds like you need a zero-penalty question. I attach a zip file of a question prototype we call python3_sandbox and a question that uses it. If you import both those questions (they're in Moodle XML format) and preview the example question you should see something like the following, which is a "handy python workspace" we make available to our students. You can preload any additional files you like into this question.



And when Check is clicked the student would see:



Is that more what you're after? Note, though, that this approach does not give students the ability to use the input() function in an interactive manner - any standard input has to be put into a separate file in advance and uploaded.