Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have a little homework webpage. I'm trying to automate the production of the webpage each week.
I know very little about this. Wednesday I asked in stackoverflow about importing Python functions that I made. I think I have grasped the concept. I've tested everything in Idle. All the modules work.
In my Python shell, (I use Idle in Ubuntu), everything does what I want.
I first append the paths:
Quote:
for i in range(0, len(pyPaths)):
sys.path.append(pyPaths[i])
then import the modules:
Quote:
from makeRBsInlineV1 import makeHTMLrbsNums
from makeCheckboxesInlineV1 import makeHTMLCBs
from makeDropdownboxesInlineV1 import makeDropdownboxes
from createhtmlTableInlineV1 import makeHTMLtable
from makeRBsInlineV2 import makeHTML_RBs
from readLinesTextboxesInlineV1 import readLinesmakeTBs
from makeThankyouPHPInlineV1 import makeThankyouPHP
All these modules return a text string which is a mixture of html tags + my text. It displays nicely in Firefox.
In Idle I just write, for example:
Quote:
myString = readLinesTextboxesInlineV3()
it asks me a few questions and off it goes. Afterwards, myString is ready to be put in the webpage text string.
However, in bash, I cannot write
Quote:
myString = readLinesTextboxesInlineV3()
in bash, I cannot write
Quote:
myString = input('Enter the name of the module you want. ')
and then enter,
Quote:
readLinesTextboxesInlineV3()
because then myString is just the input text, not the function.
Each week things are different, I may need to run 2 or more of the modules, add the result strings, then write them in the webpage text string in the correct insertion point.
I run this in a
Quote:
while True:
loop, so I can add strings from various modules.
For any given week, I don't know which module I want to use, so I make them all available.
You can run a Python module with the -m option, for example:
Code:
$ curl somewebsite.htm | python -m json.tool
If I understand you correctly, you are writing an interactive front-end to a bunch of Python-based web utilities that you developed yourself. My first question is: Why does the front-end have to be a bash script, and not a Python program?
If it really does have to be a Bash script, why do you need to get the module name using a Python program? Why don't you use shell commands:
I am not sure what you mean by "assign myString to any 1 of the modules". Is myString a variable in each module? You seem to be wanting to pass some data from a shell script into a Python module. I am sure there are several ways to do that, depending on what exactly you want to achieve.
I use Ubuntu and Idle to develop my little python programs. They save me a lot of time. I run the programs in bash. That's how bash comes into this. I have tried all this in Idle and it all works fine. Just when I try to run it in bash, I can't choose which function to run.
In python, it is necessary to import modules. That makes various functions available.
Simple example:
Quote:
import os
This alone does nothing, but makes what I think are called 'methods' available.
After importing os, I can, for example do this:
Quote:
photos = os.listdir(pathToPhotos)
Now photos is a list of all the photo names. I can iterate through the list and do things with the photos, in my case put them in an excel file next to the student with that name.
The crux is here: .listdir()
I can import my functions into makeWebpage.py but I don't know how to make methods available using .mymethod() notation.
makeCheckboxesInlineV1.py is a little routine, module, to take a text file and "htmlify" it.
I can call basicCB any number of times in a loop and replace X and WORD with a number and a word from a text file list, put it all together and the html is done and saved as a text file. 5 seconds!
Another example, from makeCheckboxesInlineV1.py I import the function makeHTMLCBs() (at import time the brackets are not needed):
Quote:
from makeCheckboxesInlineV1 import makeHTMLCBs
I run makeWebpage.py in bash. First it imports all my little html-makers. I want to choose which 1 to run and assign to myString, so I put them in a list of functions (no ' '):
but as soon as the Python interpreter comes across this, it immediately starts the function makeHTMLCBs() I cannot, like I can in Idle, assign myString = pyFiles[3]
This is just a list of strings and does nothing if I assign myString = pyFiles[3], well, it's just a string, not a function.
I want to choose which function from my functions I run. That seems difficult.
So, what I think I need is to import my html-maker files and make the individual methods available using .mymethod() notation like this:
myString = makeCheckboxesInlineV1.makeHTMLCBs()
But I don't know how to do this. I don't know how to give myself that choice in bash. I know many python modules do this.
At the moment I:
1. run 1 or more of my little html-makers, save all in a text file,
2. then run a simpler version of makeWebpage.py which calls the basic template and inserts the new html and various other details. Webpage done!
would be nice to give some more sample/simplified code you already written. Hard to understand based on the 4 lines you posted.
If I understand well you mixed python and bash code and you want to use the same variables in both languages which is more or less impossible (actually it is not really impossible, but this is the hard way).
I am an idiot, who has no training in Python and very little self-acquired knowledge. Also, sometimes you need to walk away from a problem to find a solution. I go to bed early and wake before dawn, which is a good time for thinking. Found the solution this morning!
I first import my modules (only learnt to do this 3 days ago!):
Code:
# import the html-makers
from makeCheckboxesInlineV1 import makeHTMLCBs
from makeDropdownboxesInlineV1 import makeDropdownboxes
from createhtmlTableInlineV1 import makeHTMLtable
from makeRBsInlineV2 import makeHTML_RBs
from readLinesTextboxesInlineV1 import readLinesmakeTBs
from makeThankyouPHPInlineV1 import makeThankyouPHP
then give myself options:
Code:
choices = ['checkboxes', 'dropdownboxes', 'htmltable', 'radiobuttons', 'textboxes', 'makethankyouphp']
pathToFile = '/home/pedro/myHWpageSummer2019/18BE/html/'
pathToParagraph = '/home/pedro/myHWpageSummer2019/textTohtml/paragraph/paragraph'
file = '18BEblanko.html'
while True:
print('This is to make the paragraph string, this weeks content html.')
carryon = input('Enter y to continue, enter nothing to stop. ')
if not carryon == 'y':
break
print('What do you want first? The choices are: ')
for choice in choices:
print(choice)
myChoice = input('What do you want? Enter your choice. ')
while myChoice not in choices:
for choice in choices:
print(choice)
myChoice = input('What do you want? Enter your choice. ')
if myChoice == 'checkboxes':
myString = makeHTMLCBs()
elif myChoice == 'dropdownboxes':
myString = makeDropdownboxes()
elif myChoice == 'htmltable':
myString = makeHTMLtable()
elif myChoice == 'radiobuttons':
myString = makeHTML_RBs()
elif myChoice == 'textboxes':
myString = readLinesmakeTBs()
elif myChoice == 'makethankyouphp':
myString = makeThankyouPHP()
The above makes any choice of module available repeatedly. In reality, I will only ever use 1 or 2 in any given week. The rest of my little webpage is all the same except the week number, switch-on and switch-off dates.
I tried this just now, I ran it in bash! It does what I want! (After I got rid of all the indentation errors!)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.