Passing values between two nav_items

Hi
I have on APP that have one nav_group with several nav_item. In one nav_item i upload one CSV file to use in another nav_item how can i pass the name of the file that was uploaded.

here is my code:
import os
from h2o_wave import Q, app, ui, main

sample_markdown = ‘’’
xxxxxx - Dataknowldge Application

Esta aplicação serve de apoio á análise dados’’’

example_dir = os.path.dirname(os.path.realpath(file))
_app_host = ‘127.0.0.1’
_app_port = ‘10102’

async def showpage(q: Q):
q.page[‘meta’] = ui.meta_card(box=’’, layouts=[
ui.layout(
breakpoint=‘m’,
zones=[
ui.zone(‘header’, size=‘80px’),
ui.zone(‘body’, direction=ui.ZoneDirection.ROW, zones=[
ui.zone(‘content’),
], size=‘700px’),
ui.zone(‘info’, size=‘30px’),
ui.zone(‘footer’, size=‘80px’),
]
)
])

q.page['header'] = ui.header_card(
	# Place card in the header zone, regardless of viewport size.
	box='header',
	title='UTopIA 2.0 - Dataknowledge',
	subtitle='Ferramenta de análise de dados',
	nav=[
		ui.nav_group('Menu', items=[
			ui.nav_item(name='#menu/Home', label='Página Inicial'),
			ui.nav_item(name='#menu/Importacao', label='Importação ficheiro'),
			ui.nav_item(name='#menu/Correlacao', label='Mapa correlação'),
			ui.nav_item(name='#menu/Vazios', label='Mapa valores vazios'),
		], collapsed=True),
		ui.nav_group('Help', items=[
			ui.nav_item(name='#about', label='About', icon='Info'),
			ui.nav_item(name='#support', label='Support', icon='Help'),
		], collapsed=True)
	],
)

q.page['Home'] = ui.markdown_card(
	box=ui.boxes('content', 'sidebar', 'sidebar'),
	title='',
	content=sample_markdown,
)

q.page['footer'] = ui.footer_card(box='footer', caption='(c) 2021 COFIDIS. All rights reserved.')

q.page['info'] = ui.markdown_card(box='info', title='', content='')

await q.page.save()

async def show_example(q: Q):
links = q.args.user_files
if links:
items = [ui.text_xl(‘Files uploaded!’)]
for link in links:
local_path = await q.site.download(link, ‘c:\temp\TESTE\’)
size = os.path.getsize(local_path)

		items.append(ui.link(label=f'{os.path.basename(link)} ({size} bytes)', download=True, path=link))
		#os.remove(local_path)

	items.append(ui.textbox(name='File', value=local_path))
	q.page['info'].items = items
	q.page['info'] = ui.markdown_card(box='info', title='', content=local_path)

if q.args.submit:
	del q.page['Home']

	q.page['Home'] = ui.markdown_card(
		box=ui.boxes('content'),
		title='',
		content=sample_markdown,
	)

hash = q.args['#']
if hash:
	if hash == 'menu/Home':
		del q.page['Home']

		q.page['Home'] = ui.markdown_card(
			box=ui.boxes('content'),
			title='',
			content=sample_markdown,
		)
	if hash == 'menu/Importacao':
		del q.page['Home']

		q.page['Home'] = ui.form_card(box=ui.box('content'),
									  items=[ui.text_xl(content='Importação de ficheiro'),
											 ui.file_upload(name='user_files', label='Upload', multiple=False,
															file_extensions=['csv'], visible=True),
									         ui.button(name='submit', label='Submit', primary=True),
											 ])
	if hash == 'menu/Correlacao':
		del q.page['Home']
		
		q.page['Home'] = ui.markdown_card(
			box=ui.boxes('content'),
			title='',
			content='teste-',
		)
await q.page.save()

@app(’/Dataknowledge’)
async def serve(q: Q):
if not q.client.initialized:
q.client.initialized = True
await showpage(q)

route = q.args['#']
if not route:
	route = 'Home'

await show_example(q)

print(’----------------------------------------’)
print(’ Welcome to DataKnowledge’)
print(’’)
print(’ Go to http://localhost:10101/Dataknowledge’)
print(’----------------------------------------’)

Hi @rmfjd78,

if I understood correctly, you need to store the file name when uploaded and use it elsewhere. This can be easily accomplished by using q.client. Citing documentation:

q.client is a dictionary-like object for storing and retrieving arbitrary information related to the active client (a browser tab or browser session). Anything you put into q.client while handling a event can be read back while handling a subsequent event, provided the subsequent event originates from the same client.

For your case this means replacing:
links = q.args.user_files
with
q.client.user_files = q.args.user_files

From this point on you can access the uploaded files anywhere in your app using q object (q.client.user_files).

Further reading for various state types: Tutorial: Bean Counter | H2O Wave

I’ve seen the tutorial of the bean count and i’ve changed like you sugested (using q.client), but what happens now is when i select another navbar (my code below and the navbar code is in bold) item the q.args.user_files is in blank

import os
from h2o_wave import Q, app, ui, main

sample_markdown = ‘’’
xxxxxx - Dataknowldge Application

Esta aplicação serve de apoio á análise dados’’’

example_dir = os.path.dirname(os.path.realpath(file))
_local_path = ‘c:\temp\teste\’
_app_host = ‘127.0.0.1’
_app_port = ‘10102’

async def showpage(q: Q):
q.page[‘meta’] = ui.meta_card(box=’’, layouts=[
ui.layout(
breakpoint=‘m’,
zones=[
ui.zone(‘header’, size=‘80px’),
ui.zone(‘body’, direction=ui.ZoneDirection.ROW, zones=[
ui.zone(‘content’),
], size=‘700px’),
ui.zone(‘info’, size=‘30px’),
ui.zone(‘footer’, size=‘80px’),
]
)
])

q.page['header'] = ui.header_card(
	# Place card in the header zone, regardless of viewport size.
	box='header',
	title='UTopIA 2.0 - Dataknowledge',
	subtitle='Ferramenta de análise de dados',
	nav=[
		ui.nav_group('Menu', items=[
			ui.nav_item(name='#menu/Home', label='Página Inicial'),
			ui.nav_item(name='#menu/Importacao', label='Importação ficheiro'),
			ui.nav_item(name='#menu/Correlacao', label='Mapa correlação'),
			ui.nav_item(name='#menu/Vazios', label='Mapa valores vazios'),
		], collapsed=True),
		ui.nav_group('Help', items=[
			ui.nav_item(name='#about', label='About', icon='Info'),
			ui.nav_item(name='#support', label='Support', icon='Help'),
		], collapsed=True)
	],
)

q.page['Home'] = ui.markdown_card(
	box=ui.boxes('content', 'sidebar', 'sidebar'),
	title='',
	content=sample_markdown,
)

q.page['footer'] = ui.footer_card(box='footer', caption='(c) 2021 COFIDIS. All rights reserved.')

q.page['info'] = ui.form_card(box='info',items=[ui.text_xl(name='filename', content='')])

await q.page.save()

async def show_example(q: Q):
q.client.user_files = q.args.user_files

if q.client.user_files:
	for link in q.client.user_files:
		local_path = await q.site.download(link, _local_path)
		q.client.filename = local_path
		size = os.path.getsize(local_path)

	q.page['info'] = ui.form_card(box='info', items=[ui.text_xl(name='filename', content=local_path)])

if q.args.submit:
	del q.page['Home']

	q.page['Home'] = ui.markdown_card(
		box=ui.boxes('content'),
		title='',
		content=sample_markdown,
	)

hash = q.args['#']
if hash:
	if hash == 'menu/Home':
		del q.page['Home']

		q.page['Home'] = ui.markdown_card(
			box=ui.boxes('content'),
			title='',
			content=sample_markdown,
		)
	if hash == 'menu/Importacao':
		del q.page['Home']

		q.page['Home'] = ui.form_card(box=ui.box('content'),
									  items=[ui.text_xl(content='Importação de ficheiro'),
											 ui.file_upload(name='user_files', label='Upload', multiple=False,
															file_extensions=['csv'], visible=True),
									         ui.button(name='submit', label='Submit', primary=True),
											 ])
	**if hash == 'menu/Correlacao':**

** del q.page[‘Home’]**
** filename = ‘’**
** if q.client.user_files:**
** filename = ‘filled’**
** else:**
** filename = ‘blank’**
** q.page[‘Home’] = ui.markdown_card(**
** box=ui.boxes(‘content’),**
** title=’’,**
** content=‘teste-’ + filename,**
** )**
await q.page.save()

@app(’/Dataknowledge’)
async def serve(q: Q):
if not q.client.initialized:
q.client.initialized = True
await showpage(q)

route = q.args['#']
if not route:
	route = 'Home'

await show_example(q)

print(’----------------------------------------’)
print(’ Welcome to DataKnowledge’)
print(’’)
print(’ Go to http://localhost:10101/Dataknowledge’)
print(’----------------------------------------’)

forget my last question i’ve discovered my problem

1 Like