Sharing data tables between apps

WHAT I WANT
To have questions in a data table that are pulled over to the feedback form.
User can input their answer(s) and then can get a message sent back to them with percentage of inputted answers that are correct.

Right now I do have a Data Table of questions in a separate app, here is the Data Table with sample questions put in.

https://anvil.works/build#clone:UWQDFG44PFGD53QK=2VYC226XGJ254OB6H5LMF4QT

Is it possible to draw over the questions from the Data Tables and populate the feedback form? The feedback form is linked to below…

https://anvil.works/build#clone:AMYHUW7SEMVAB2P4=YS6WTTIIZBUTU63OP6A2TTQW

Since they are both Data Tables would I be able to combine them?

Any help would be welcome, please advise if you can.

Thanks!

-Taylor

(moved to its own topic since it is a separate question)

Hello,

If you are asking how to share data tables between apps, the docs I linked to in your previous post explains how to do this. Please see here for the docs.

If you want to combine two tables you will have to add rows from one to one the other, or build a new table with the rows you want from each. Or, if you have the questions in a CSV, you can write those rows into the DataTables too.

Something like this might work if you have both tables in the same app and the same columns exist in both (this is untested and from memory). Basically you need to loop through a table and add its rows to your “target” table, if that is the route you want to go. Getting familiar with the documentation will help a great deal.

for row in app_tables.table_1.search():
  row_dict=dict(row)
  app_tables.table_2.add_row(**row_dict)
1 Like

Hello once again,

Thanks for the response! Still working on this project, hoping to get it done eventually, even if it takes me lots of time.

I believe with the template you sent over before I should be able to modify it to make it work. Right now this is where I’m at, link to my code is below…

https://anvil.works/build#clone:AMYHUW7SEMVAB2P4=YS6WTTIIZBUTU63OP6A2TTQW

I have the Question and Answers tables in Data Tables section of the app, and both have columns named “Question”. I want to be able to pull a Question from the Questions Table over to the Answers table like you mentioned. This is what I’ve changed the code to…

for row1 in app_tables.answers.search():
row1[‘question’]=dict(row1[‘question’] in app_tables.questions)
app_tables.questions.add_row1(**row_dict)

This still does not populate the Question column on the Answers Table with the Question FROM the Question column on the Questions table, hopefully once I have one I can then move on to how to have multiple, and then how to get those questions and answers from the Python coding I’ve already done.

Any advice on if the syntax is not correct or what I could change it to to get what I want?

Thanks once again!

-Taylor

I believe your syntax is incorrect.

Let me make sure I understand what it is you are trying to do.

Are you trying to take each row of the Questions table and add it (the entire row) onto the Answers table?

If so, what I wrote above with some modification should work. Something like this:

for row in app_tables.questions.search():
  app_tables.answers.add_row(question=row['question'])

Also, to make your code look nice on the forum, you can format it by wrapping it in backticks like this:

```python

print('this will be syntax highlighted')

```

Thanks for the response once again! So blue sky(perfect world) this is what I want…

Right now I have the Feedback form where a user can input a question. I want to have them to receive a question automatically and then input an answer, and eventually be able to get 5 questions in a row. Then they can receive a percentage of questions that were correct.

Since I normally try and get one thing and then build on it was hoping to be able to pull ONE row from the Questions Table over to the Question box on the Feedback Form, which is actually called text_box_2 on the code section.

Believe I actually was not on the right track here and need to pivot. What I’m going to want is to get the answers that are inputted checked off of the Questions Table, which I can work on next.

What I need is a way to pull over a question from the question column to the Question input on the Feedback form, which is currently listed as “text_box_2”.

I believe this will be some type of code change on the “Code” tab on Form1. I believe it will be a change in this section…

 #set email to the text in the email box
    question = self.text_box_2.text

Maybe change to something like this…

 #set question to the question from the data table
    question = THEN REFER TO THE QUESTION COLUMN IN THE QUESTION DATA TABLE

Then, would be able to have a user input their answer, and that will be sent back to the Answers Table, at which point there will be another Question sent over to the text_box_2, for them to answer, and will continue until they get 5 questions and input 5 answers.

This will likely take another round of adding an anvil.server.call like the one here?

    anvil.server.call('add_feedback', name, question, answer)

I know I’ve changed my approach but as a lot of coding is an iterative process have to just acknowledge that’s par for the course and keep going!

I will look into specifics of how to get what I want as well, but as always any advice always welcome.

Thanks once again!

-Taylor

Yes, I suppose there are many ways to go about this depending on what you end up deciding on.

I still believe that understanding the feedback form tutorial, and I mean really studying it to see how it works, will teach you much of what you need to know.

As with building any app, try dividing the problems into smaller sub-problems, solving them one at a time until you get to the final product.

When you have a specific question that you cannot answer, please feel free to post a new question (or if you are still unsure how to move rows from one table to another, post here).

Good luck with your development!

3 Likes

Hello,

Stuck once again :/.

I have been putting a lot of time in, these last 2 days have been coding for about 10 hours in total, but can’t seem to get anywhere.

I watched the entire Feedback Form Tutorial, and that allowed me to get what I have thus far with a working Feedback Form that takes in input of name, question, and answer from a user and then moves that over to the Answers Data Table.

I still am trying to get a defined Question from the Questions Data Table pulled over to the Feedback Form. Hopefully after I have that I can turn that into 5 questions, and then find a way to import all my Python questions I have into it.

But for the first problem, still trying to populate the Question text_box with a Question from the Questions Data Table. My idea was to set a variable equal to the Object for the Question in the Data Table that I want, and then set THAT to the text box I have on my Feedback Form. I will share my code again, and paste it as well.

https://anvil.works/build#clone:AMYHUW7SEMVAB2P4=YS6WTTIIZBUTU63OP6A2TTQW

 # Any code you write here will run when the form opens.
    
    **q1 = app_tables.questions.get(q.any_of(question = 'Massachusetts'))**
    

  def submit_button_click(self, **event_args):
    """This method is called when the button is clicked"""
    #set 'name' to the text in the 'name box'
    name = self.name_box.text
    #set email to the text in the email box
    question = self.text_box_2.text
    self.text_box_2.text = q1
    #set feedback to the text in the feedback box
    answer = self.text_area_1.text

When I run this the Question still does not populate to the text box on the Feedback Form.

I tried using the syntax that essentially said “contains part of” (q.any of), but what would be better would be an exact row reference. AKA something like app.tables.questions.question[1], or some way to index into each of the specific objects in the Data Table.

I couldn’t find any Tutorial that described how to index into an object of a Data Table. Also I’m not entirely sure that I can set a variable where I do in my code, can it write it where I do or would it need to be at a more specific place?

Help always welcome and appreciated, and if there is a more efficient way to pull over one Question from the Questions Data Table (with the end goal of pulling 5 Questions over) feel free to share that as well.

Thanks!

-Taylor

q1 is a row from the table, not a text field. You need to pull one text field from the row to put into the text box. Something like:

self.text_box_2.text = q1['Question']

Assuming that you have a text field in the row named Question.

1 Like

Hello,

So still working on getting a question populated to the feedback form. I wrote this based of off your advice…

    self.text_box_2.text = app_tables.questions.search(answer="Boston")

This would seemingly be how to refer to the text field in the Questions table that has Boston in the answer column, according to the documentation I found.

It still does not work, any idea of why it is not working?

Also it may be where it is located? It is written where the submit_button_click function is written, but may need to make that a multi step process if a user is inputting their name, then getting a question on the form, and then submitting an answer?

Code is below, any advice or tips welcome.

https://anvil.works/build#clone:AMYHUW7SEMVAB2P4=YS6WTTIIZBUTU63OP6A2TTQW

Thanks!

-Taylor

search() will return a list-like object, not a string. This is what you would use if you wanted to return multiple rows. For example, if there were multiple rows with answer='Boston'. This is often used to set the repeated elements in a repeating panel’s items.

If you want to set the text of a label to a string/text from a datatable, and you are targeting a single row, use app_tables.questions.get(). For example:

self.text_box_2.text = app_tables.questions.get(answer="Boston")['question']
1 Like

Thanks Allan! Really appreciate your help, it’s a lot trying to learn Anvil and get my Jeopardy! quiz published, but going to keep going until I get it.

I inputted the code you wrote before and still not working :/.

Although I was using the “Answer” column as somewhat of a trial because I thought it would be more basic what I really want is a value from the question column.

Experimenting with formatting I believe that would be something more like this…

    self.text_box_2.text = app_tables.questions.get(q.any_of(question="Massachusetts")['question']

That is assuming the “q.any_of” part is a search for containing like function which I believe it is.

I would rather target a single row numerically, as when done would like to have Q1 populated from the first row, then Q2 from the second row, etc…

Is there any way you know how to do this? I tried reading through the docs and the Links Between Tables tutorial but couldn’t find anything that would seemingly solve my problem.

Also, assuming syntax is correct is it in the right place right now (written under where the “submit button click” function is defined)? Believe that if the syntax is correct it will populate the Question on the feedback form with a question from the Data Table, but if not then I need to figure out where the right place would be.

Code is shared below, any advice always welcome.

https://anvil.works/build#clone:AMYHUW7SEMVAB2P4=YS6WTTIIZBUTU63OP6A2TTQW

Thanks!

-Taylor

Yes the code works. I tested it with your previous clone. That is, it sets the text box’s text to the text in the column “question”. That is what you were asking I believe.

Your question appears to be changing underneath my feet though as there are a few concepts you are asking about.

Unfortunately, I do not have time to write an app that demonstrates all of these concepts at the moment, but I would suggest carefully going through the introductory tutorials and documentation on repeating panels and datatables. There you will learn many concepts that will likely relate to what you are describing.

For example, a repeating panel of groups of radio buttons would be a simple setup for a multiple choice quiz. Then, for each row in your datatable, you could have a column for the right answer, a column for the question, and a simple object column containing the options. When the user submits their quiz, the necessary data is sent back to the server so that you could compare their answers to the correct answers that have been stored. I don’t even think you would need two tables. One would likely suffice.

I hope this helps somewhat. Good luck with your development and feel free to open a new topic when you have a new question.

1 Like