I have a web-app on which user will be writing a Python snippet.
I want to add a custom message whenever user imports a library. For example, if user's code is something as below
import pandas
import os
from subprocess import Popen, PIPE
Then the output should have:
You have imported pandas
You have imported os
You have imported Popen
You have imported PIPE
The user's code can be dynamic, and can actually import any library.
The only way I can think of right now is to use try except. You can take user input (the library name) as string and then:
import importlib
lib_to_import = 'math'
try:
importlib.import_module(lib_to_import)
print("You have imported {}.".format(lib_to_import))
except:
print("Something went wrong when importing {}.".format(lib_to_import))
You can wrap the above into a function and call it every time when the user imports a library.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.