Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable global functions for multiple questions #34

Open
dragonxtek opened this issue Oct 29, 2020 · 10 comments
Open

enable global functions for multiple questions #34

dragonxtek opened this issue Oct 29, 2020 · 10 comments
Labels
enhancement New feature or request

Comments

@dragonxtek
Copy link

When I create questions in python, many times I must repeat functions or variables for every questions. Could you create global functions or variables to avoid repeating all the code?

@gpoore
Copy link
Owner

gpoore commented Oct 29, 2020

Allowing continuity between code blocks involves some complexity. I have an existing system that can manage this (https://github.com/gpoore/codebraid), but connecting that to text2qti will take some time. And I'm unfortunately short on time for the foreseeable future.

For now, you might consider putting all your functions and variables in a module (could just be a .py file in the quiz directory), and then import that in each code block.

@gpoore gpoore added the enhancement New feature or request label Oct 29, 2020
@gordonwatts
Copy link

gordonwatts commented Mar 6, 2021

@gpoore - I've taken a slightly different approach: I added the current working directory to PYTHONPATH. That seems to work in the sense you can do "from config import *" from a question code and it pulls everything in.

You can find proposed modifications here.

It has two problems I need to solve:

  • Proper handling of pythonpath when it isn't empty
  • How to get the path of the current markdown file that is being processed (rather than the cwd).

If you like this approach, I'm happy to fix the above issues and submit a PR so you can do a more careful review.

@gpoore
Copy link
Owner

gpoore commented Mar 6, 2021

@gordonwatts The current working directory for executed code should be the same as the directory of the quiz file. In cmdline.py, there's an os.chdir() to the quiz directory before any processing takes place. If you're seeing a different working directory, then there may be a bug or other issue. Or is this addressing a different problem besides working directory?

@gordonwatts
Copy link

No - that is perfect, I'd not realized there was a chdir() in there! In that case, I just need to solve the problem for Linux/mac/windows differences, but python has the infrastructure for that. So that is a StackOverflow search.

Let me know if you are interested in this modification and I'll submit a PR.

@gpoore
Copy link
Owner

gpoore commented Mar 6, 2021

@gordonwatts What differences are you seeing between operating systems? What isn't working that the modification to PYTHONPATH is fixing? I do know that the current system won't work with Python environments under Windows, but I just fixed that in another project and will add the same fix to text2qti soon.

@gordonwatts
Copy link

gordonwatts commented Mar 6, 2021

Hmmm... I'm using python environments under windows, and checked out the package and did pip install -e . and it seems to be working just fine.

The problem is the separator for PYTHONPATH. It is ":" on Linux/Mac, and ";" under Windows. I just need to fix that as the current code doesn't take care of that. For me it isn't currently mattering: PYTHONPATH isn't defined in my shell for this.

The modification to PYTHONPATH means that you can now do 'import config' in your question text and it will find config.py in the same directory as your exam questions (that is what drove this change - and it seemed to be inline with this issue's topic).

@gpoore
Copy link
Owner

gpoore commented Mar 6, 2021

@gordonwatts I should have been more specific about environments. You can run text2qti in an environment, but when text2qti runs Python code from a quiz, under Windows it currently uses the default Python rather than the current environment. If you have been having import issues, this may be the cause. If that's the case, modifying PYTHONPATH may work in some cases...the fix I will be adding makes Python code from the quiz run in the same environment that text2qti is in.

I'll try to get this fix on GitHub in the dev version in the next day or two, and if it doesn't fix everything for you, then we can try to figure out what else PYTHONPATH is contributing.

@gordonwatts
Copy link

gordonwatts commented Mar 7, 2021

Ah, I see. I wasn't trying to solve the problem of the environment. That is an interesting point. Instead, the only problem I'm trying to solve is the one in this bug report: I have a special file for this exam that has some common definitions in it. I want to do an "import config" at the top of my question to include it.

I'll continue using this hacked version and try yours when you've updated it. If it doesn't solve what I'm trying to do then we can look to see if my changes make sense or another approach is appropriate.

@gpoore
Copy link
Owner

gpoore commented Apr 18, 2021

@gordonwatts The dev version on GitHub should now work with Python environments under Windows. If that doesn't fix the issues you were having, let me know what issues still remain.

@gordonwatts
Copy link

Thanks for the updates!! I'm not teaching the quarter, but I'll try to do the installs in the next few days.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants